Skip to content

Commit

Permalink
use events in VPN library
Browse files Browse the repository at this point in the history
  • Loading branch information
jyyi1 committed Jan 23, 2025
1 parent 0b76894 commit ef92aaa
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions client/go/outline/vpn/vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ package vpn

import (
"context"
"encoding/json"
"io"
"log/slog"
"sync"

"github.com/Jigsaw-Code/outline-apps/client/go/outline/event"
"github.com/Jigsaw-Code/outline-sdk/transport"
)

Expand Down Expand Up @@ -47,9 +49,23 @@ type platformVPNConn interface {
Close() error
}

// ConnectionStatus represents the status of a [VPNConnection].
type ConnectionStatus string

const (
ConnectionConnected ConnectionStatus = "Connected"
ConnectionDisconnected ConnectionStatus = "Disconnected"
ConnectionConnecting ConnectionStatus = "Connecting"
ConnectionDisconnecting ConnectionStatus = "Disconnecting"
)

// ConnectionStatusChanged event will be raised when the status of a [VPNConnection] changes.
const ConnectionStatusChanged event.EventName = "VPNConnStatusChanged"

// VPNConnection represents a system-wide VPN connection.
type VPNConnection struct {
ID string
ID string `json:"id"`
Status ConnectionStatus `json:"status"`

cancelEst context.CancelFunc
wgEst, wgCopy sync.WaitGroup
Expand All @@ -63,6 +79,16 @@ type VPNConnection struct {
var mu sync.Mutex
var conn *VPNConnection

// SetStatus sets the [VPNConnection] Status and raises the [ConnectionStatusChanged] event.
func (c *VPNConnection) SetStatus(status ConnectionStatus) {
c.Status = status
if connJson, err := json.Marshal(c); err == nil {
event.Fire(ConnectionStatusChanged, string(connJson))
} else {
slog.Warn("failed to marshal VPN connection", "err", err)
}
}

// EstablishVPN establishes a new active [VPNConnection] connecting to a [ProxyDevice]
// with the given VPN [Config].
// It first closes any active [VPNConnection] using [CloseVPN], and then marks the
Expand All @@ -81,7 +107,7 @@ func EstablishVPN(
panic("a PacketListener must be provided")
}

c := &VPNConnection{ID: conf.ID}
c := &VPNConnection{ID: conf.ID, Status: ConnectionDisconnected}
ctx, c.cancelEst = context.WithCancel(ctx)

if c.platform, err = newPlatformVPNConn(conf); err != nil {
Expand All @@ -97,6 +123,14 @@ func EstablishVPN(
}

slog.Debug("establishing vpn connection ...", "id", c.ID)
c.SetStatus(ConnectionConnecting)
defer func() {
if err == nil {
c.SetStatus(ConnectionConnected)
} else {
c.SetStatus(ConnectionDisconnected)
}
}()

if c.proxy, err = ConnectRemoteDevice(ctx, sd, pl); err != nil {
slog.Error("failed to connect to the remote device", "err", err)
Expand Down Expand Up @@ -154,15 +188,16 @@ func closeVPNNoLock() (err error) {
return nil
}

slog.Debug("terminating the global vpn connection...", "id", conn.ID)
conn.SetStatus(ConnectionDisconnecting)
defer func() {
if err == nil {
slog.Info("vpn connection terminated", "id", conn.ID)
conn.SetStatus(ConnectionDisconnected)
conn = nil
}
}()

slog.Debug("terminating the global vpn connection...", "id", conn.ID)

// Cancel the Establish process and wait
conn.cancelEst()
conn.wgEst.Wait()
Expand Down

0 comments on commit ef92aaa

Please sign in to comment.