Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send vpncscript events to Daemon #94

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,16 @@ func (d *Daemon) updateVPNConfig(request *api.Request) {
return
}

// handle config update for vpn (dis)connect
if configUpdate.Reason == "disconnect" {
// handle config update for vpn pre-init, connect, disconnect,
// attempt-reconnect, reconnect
log.WithField("reason", configUpdate.Reason).
Info("Daemon got OpenConnect event from VPNCScript")
switch configUpdate.Reason {
case "connect":
d.updateVPNConfigUp(configUpdate.Config)
case "disconnect":
d.updateVPNConfigDown()
return
}
d.updateVPNConfigUp(configUpdate.Config)
}

// handleClientRequest handles a client request.
Expand Down
2 changes: 1 addition & 1 deletion internal/daemon/vpnconfigupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type VPNConfigUpdate struct {
// Valid returns whether the config update is valid.
func (c *VPNConfigUpdate) Valid() bool {
switch c.Reason {
case "disconnect":
case "pre-init", "disconnect", "attempt-reconnect", "reconnect":
// config must be nil
if c.Config != nil {
return false
Expand Down
8 changes: 1 addition & 7 deletions internal/vpncscript/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,13 @@ func run(args []string) error {

// handle reason environment variable
switch e.reason {
case "pre-init":
return nil
case "connect", "disconnect":
case "pre-init", "connect", "disconnect", "attempt-reconnect", "reconnect":
c, err := createConfigUpdate(e)
if err != nil {
return fmt.Errorf("VPNCScript could not create config update: %w", err)
}
log.WithField("update", c).Debug("VPNCScript created config update")
return runClient(socketFile, c)
case "attempt-reconnect":
return nil
case "reconnect":
return nil
default:
return errors.New("VPNCScript called with unknown reason")
}
Expand Down
15 changes: 3 additions & 12 deletions internal/vpncscript/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,16 @@ func TestRun(t *testing.T) {

// test with errors
for _, v := range []string{
"pre-init",
"connect",
"disconnect",
"attempt-reconnect",
"reconnect",
"invalid",
} {
t.Setenv("reason", v)
if err := run([]string{"test"}); err == nil {
t.Errorf("%s: should return error", v)
}
}

// test without errors
for _, v := range []string{
"pre-init",
"attempt-reconnect",
"reconnect",
} {
t.Setenv("reason", v)
if err := run([]string{"test"}); err != nil {
t.Errorf("%s: should not return error, got: %v", v, err)
}
}
}
Loading