Skip to content

Commit

Permalink
Send vpncscript events to Daemon
Browse files Browse the repository at this point in the history
In addition to "connect" and "disconnect" events, also send the
"pre-init", "attempt-reconnect" and "reconnect" events from vpncscript
to the Daemon.

Signed-off-by: hwipl <[email protected]>
  • Loading branch information
hwipl committed May 23, 2024
1 parent e560eea commit dc028d9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 24 deletions.
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)
}
}
}

0 comments on commit dc028d9

Please sign in to comment.