Skip to content

Commit

Permalink
All: Initial client cert notification support using systemd
Browse files Browse the repository at this point in the history
  • Loading branch information
jwijenbergh committed Jun 24, 2024
1 parent 30669d9 commit cd9bbea
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
/.envrc
/geteduroam-cli
/geteduroam-gui
/geteduroam-notifcheck
/shell.nix
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ help: ## Print this help message

.DEFAULT_GOAL := help

.build-notifcheck:
go build -o geteduroam-notifcheck ./cmd/geteduroam-notifcheck

.build-cli:
go build -o geteduroam-cli ./cmd/geteduroam-cli

Expand All @@ -18,12 +21,18 @@ lint: ## Lint the codebase using Golangci-lint
fmt: ## Format the codebase using Gofumpt
gofumpt -w .

build-notifcheck: .build-notifcheck ## Build notification checker
@echo "Done building, run 'make run-notifcheck' to run the notification checker"

build-cli: .build-cli ## Build CLI version
@echo "Done building, run 'make run-cli' to run the CLI"

build-gui: .build-gui ## Build GUI version
@echo "Done building, run 'make run-gui' to run the GUI"

run-notifcheck: .build-notifcheck ## Run notification checker
./geteduroam-notifcheck

run-cli: .build-cli ## Run CLI version
./geteduroam-cli

Expand All @@ -32,6 +41,7 @@ run-gui: .build-gui ## Run GUI version

clean: ## Clean the project
go clean
rm -rf geteduroam-notifcheck
rm -rf geteduroam-cli
rm -rf geteduroam-gui

Expand Down
32 changes: 27 additions & 5 deletions cmd/geteduroam-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/geteduroam/linux-app/internal/instance"
"github.com/geteduroam/linux-app/internal/log"
"github.com/geteduroam/linux-app/internal/network"
"github.com/geteduroam/linux-app/internal/notification"
"github.com/geteduroam/linux-app/internal/utils"
"github.com/geteduroam/linux-app/internal/version"
)
Expand Down Expand Up @@ -60,7 +61,11 @@ func ask(prompt string, validator func(input string) bool) string {
for {
var x string
fmt.Print(prompt)
fmt.Scanln(&x)
_, err := fmt.Scanln(&x)
if err != nil {
slog.Debug("failed to get input", "err", err)
// x will be empty
}

if validator(x) {
return x
Expand Down Expand Up @@ -409,6 +414,8 @@ const usage = `Usage of %s:
-d, --debug Debug
-l <file>, --local=<file> The path to a local EAP metadata file
This CLI binary is used to add an eduroam connection profile with integration using NetworkManager.
Log file location: %s
`

Expand Down Expand Up @@ -448,7 +455,10 @@ func main() {
if !IsTerminal() {
msg := "Not starting the CLI as it is not run in a terminal. You might want to install the GUI: https://github.com/geteduroam/linux-app/releases"
slog.Error(msg)
_ = exec.Command("notify-send", "geteduroam-cli", msg).Start()
err := notification.Send(msg)
if err != nil {
slog.Error("failed to send a notification for CLI clicked in file manager", "err", err)
}
os.Exit(1)
}
var v *time.Time
Expand All @@ -457,8 +467,20 @@ func main() {
} else {
v = doDiscovery()
}
fmt.Println("\nYour eduroam connection has been added to NetworkManager with the name: \"eduroam (from geteduroam)\"")
if v != nil {
fmt.Printf("Your connection is valid for: %d days\n", utils.ValidityDays(*v))
fmt.Println("\nAn eduroam profile has been added to NetworkManager with the name: \"eduroam (from geteduroam)\"")
if v == nil {
return
}
fmt.Printf("Your profile is valid for: %d days\n", utils.ValidityDays(*v))
if !notification.HasDaemonSupport() {
return
}
in := ask("Do you want to enable notifications that warn for expiry of the profile (requires systemd and notify-send) (y/n)?: ", func(msg string) bool {
if msg != "y" && msg != "n" {
fmt.Fprintln(os.Stderr, "Please enter y/n")
return false
}
return true
})
notification.ConfigureDaemon(in == "y")
}
14 changes: 11 additions & 3 deletions cmd/geteduroam-gui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (m *mainState) rowActivated(sel instance.Instance) {
}
fmt.Println("Browser has been opened with URL:", url)
}
s := NewSuccessState(m.builder, m.stack, valid, isredirect)
s := NewSuccessState(m.builder, m.app.GetActiveWindow(), m.stack, valid, isredirect)
uiThread(func() {
s.Initialize()
})
Expand Down Expand Up @@ -266,7 +266,7 @@ func (m *mainState) localMetadata() {
})
return
}
s := NewSuccessState(m.builder, m.stack, v, false)
s := NewSuccessState(m.builder, m.app.GetActiveWindow(), m.stack, v, false)
s.Initialize()
}()
})
Expand Down Expand Up @@ -403,20 +403,28 @@ func main() {
--version Prints version information
-d, --debug Debug
--gtk-args Arguments to pass to gtk as a string, e.g. "--help". These flags are split on spaces
This GUI binary is used to add an eduroam connection profile with integration using NetworkManager and Gtk.
Log file location: %s
`

var help bool
var versionf bool
var debug bool
var gtkarg string
program := "geteduroam-gui"
lpath, err := log.Location(program)
if err != nil {
lpath = "N/A"
}
flag.BoolVar(&help, "help", false, "Show help")
flag.BoolVar(&help, "h", false, "Show help")
flag.BoolVar(&versionf, "version", false, "Show version")
flag.BoolVar(&debug, "d", false, "Debug")
flag.BoolVar(&debug, "debug", false, "Debug")
flag.StringVar(&gtkarg, "gtk-args", "", "Gtk arguments")
flag.Usage = func() { fmt.Printf(usage, "geteduroam-gui") }
flag.Usage = func() { fmt.Printf(usage, program, lpath) }
flag.Parse()
if help {
flag.Usage()
Expand Down
28 changes: 27 additions & 1 deletion cmd/geteduroam-gui/success.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ import (
"fmt"
"time"

"github.com/geteduroam/linux-app/internal/notification"
"github.com/geteduroam/linux-app/internal/utils"
"github.com/jwijenbergh/puregotk/v4/adw"
"github.com/jwijenbergh/puregotk/v4/glib"
"github.com/jwijenbergh/puregotk/v4/gtk"
)

type SuccessState struct {
builder *gtk.Builder
parent *gtk.Window
stack *adw.ViewStack
expiry *time.Time
isredirect bool
}

func NewSuccessState(builder *gtk.Builder, stack *adw.ViewStack, expiry *time.Time, isredirect bool) *SuccessState {
func NewSuccessState(builder *gtk.Builder, parent *gtk.Window, stack *adw.ViewStack, expiry *time.Time, isredirect bool) *SuccessState {
return &SuccessState{
builder: builder,
parent: parent,
stack: stack,
expiry: expiry,
isredirect: isredirect,
Expand Down Expand Up @@ -70,4 +74,26 @@ func (s *SuccessState) Initialize() {
}
// set the page as current
setPage(s.stack, &page)

if s.expiry == nil {
return
}
if !notification.HasDaemonSupport() {
return
}

dialog := adw.NewMessageDialog(s.parent, "Enable notifications?", fmt.Sprintf("This connection will expire in %d days.\n\nDo you want to enable notifications that warn for imminent expiry using systemd?", utils.ValidityDays(*s.expiry)))
dialog.AddResponse("disable", "Disable")
dialog.AddResponse("enable", "Enable")
dialog.SetResponseAppearance("enable", adw.ResponseSuggestedValue)
dialog.SetDefaultResponse("disable")
dialog.SetCloseResponse("disale")

var dialogcb func(adw.MessageDialog, string)
dialogcb = func(_ adw.MessageDialog, response string) {
defer glib.UnrefCallback(&dialogcb) //nolint:errcheck
notification.ConfigureDaemon(response == "enable")
}
dialog.ConnectResponse(&dialogcb)
dialog.Present()
}
79 changes: 79 additions & 0 deletions cmd/geteduroam-notifcheck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"flag"
"fmt"
"time"

"golang.org/x/exp/slog"

"github.com/geteduroam/linux-app/internal/config"
"github.com/geteduroam/linux-app/internal/log"
"github.com/geteduroam/linux-app/internal/nm"
"github.com/geteduroam/linux-app/internal/notification"
)

const usage = `Usage of %s:
-h, --help Prints this help information
This CLI binary is needed for periodically checking for validity and giving notifications when the eduroam connection added by geteduroam is about to expire.
It gives a warning 10 days before expiry, and then every day. You can schedule to start this binary daily yourself or rely on the built-in systemd user timer.
You also need notify-send installed to send the actual notifications.
Log file location: %s
`

func main() {
program := "geteduroam-notifcheck"
lpath, err := log.Location(program)
if err != nil {
lpath = "N/A"
}
flag.Usage = func() { fmt.Printf(usage, program, lpath) }
flag.Parse()
log.Initialize("geteduroam-notifcheck", false)
cfg, err := config.Load()
if err != nil {
slog.Error("no previous state", "error", err)
return
}
con, err := nm.PreviousCon(cfg.UUID)
if err != nil {
slog.Error("no connection with uuid", "uuid", cfg.UUID, "error", err)
return
}
if con == nil {
slog.Error("connection is nil")
return
}

if cfg.Validity == nil {
slog.Info("validity is nil")
return
}
valid := *cfg.Validity
now := time.Now()
diff := valid.Sub(now)
days := int(diff.Hours() / 24)

var text string
if days > 10 {
slog.Info("the connection is still valid for more than 10 days", "days", days)
return
}
if days < 0 {
text = "profile is expired"
}
if days == 0 {
text = "profile expires today"
}
if days > 0 {
text = fmt.Sprintf("profile expires in %d days", days)
}
msg := fmt.Sprintf("Your eduroam %s. Re-run geteduroam to renew the profile", text)
err = notification.Send(msg)
if err != nil {
slog.Error("failed to send notification", "error", err)
return
}
}
20 changes: 18 additions & 2 deletions goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ builds:
goarch:
- amd64
binary: geteduroam-gui
- main: ./cmd/geteduroam-notifcheck
id: geteduroam-notifcheck
goos:
- linux
goarch:
- amd64
binary: geteduroam-notifcheck

nfpms:
- file_name_template: '{{ .Binary }}_{{ .Os }}_{{ .Arch }}'
- file_name_template: '{{ .PackageName }}_{{ .Os }}_{{ .Arch }}'
id: geteduroam-cli
package_name: geteduroam-cli
maintainer: Jeroen Wijenbergh
homepage: https://geteduroam.org/
builds:
- geteduroam-cli
- geteduroam-notifcheck
formats:
- deb
- rpm
Expand All @@ -32,6 +40,10 @@ nfpms:
release: 1
description: |-
Geteduroam CLI client for Linux distributions.
contents:
- src: systemd/user/
dst: /etc/systemd/user/
type: tree

overrides:
deb:
Expand All @@ -41,13 +53,14 @@ nfpms:
dependencies:
- NetworkManager

- file_name_template: '{{ .Binary }}_{{ .Os }}_{{ .Arch }}'
- file_name_template: '{{ .PackageName }}_{{ .Os }}_{{ .Arch }}'
id: geteduroam-gui
package_name: geteduroam-gui
maintainer: Jeroen Wijenbergh
homepage: https://geteduroam.org/
builds:
- geteduroam-gui
- geteduroam-notifcheck
formats:
- deb
- rpm
Expand All @@ -60,6 +73,9 @@ nfpms:
- src: cmd/geteduroam-gui/resources/share/
dst: /usr/share
type: tree
- src: systemd/user/
dst: /etc/systemd/user/
type: tree

overrides:
deb:
Expand Down
Loading

0 comments on commit cd9bbea

Please sign in to comment.