Add new api calls and onterminate

Former-commit-id: 96143e4b38
This commit is contained in:
Owen
2025-11-24 17:36:44 -05:00
parent fff234bdd5
commit 2718d15825
3 changed files with 107 additions and 79 deletions

View File

@@ -415,3 +415,14 @@ func (s *API) handleDisconnect(w http.ResponseWriter, r *http.Request) {
"status": "disconnect initiated",
})
}
func (s *API) GetStatus() StatusResponse {
return StatusResponse{
Connected: s.isConnected,
Registered: s.isRegistered,
Version: s.version,
OrgID: s.orgID,
PeerStatuses: s.peerStatuses,
NetworkSettings: network.GetSettings(),
}
}

View File

@@ -25,52 +25,6 @@ import (
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
type GlobalConfig struct {
// Logging
LogLevel string
// HTTP server
EnableAPI bool
HTTPAddr string
SocketPath string
Version string
// Callbacks
OnRegistered func()
OnConnected func()
// Source tracking (not in JSON)
sources map[string]string
}
type TunnelConfig struct {
// Connection settings
Endpoint string
ID string
Secret string
UserToken string
// Network settings
MTU int
DNS string
UpstreamDNS []string
InterfaceName string
// Advanced
Holepunch bool
TlsClientCert string
// Parsed values (not in JSON)
PingIntervalDuration time.Duration
PingTimeoutDuration time.Duration
OrgID string
// DoNotCreateNewClient bool
FileDescriptorTun uint32
FileDescriptorUAPI uint32
}
var (
privateKey wgtypes.Key
connected bool
@@ -184,41 +138,13 @@ func Init(ctx context.Context, config GlobalConfig) {
},
// onSwitchOrg
func(req api.SwitchOrgRequest) error {
logger.Info("Processing org switch request to orgId: %s", req.OrgID)
// Ensure we have an active olmClient
if olmClient == nil {
return fmt.Errorf("no active connection to switch organizations")
}
// Update the orgID in the API server
apiServer.SetOrgID(req.OrgID)
// Mark as not connected to trigger re-registration
connected = false
Close()
// Clear peer statuses in API
apiServer.SetRegistered(false)
// Trigger re-registration with new orgId
logger.Info("Re-registering with new orgId: %s", req.OrgID)
publicKey := privateKey.PublicKey()
stopRegister = olmClient.SendMessageInterval("olm/wg/register", map[string]interface{}{
"publicKey": publicKey.String(),
"relay": true, // Default to relay mode for org switch
"olmVersion": globalConfig.Version,
"orgId": req.OrgID,
}, 1*time.Second)
return nil
logger.Info("Received switch organization request via HTTP: orgID=%s", req.OrgID)
return SwitchOrg(req.OrgID)
},
// onDisconnect
func() error {
logger.Info("Processing disconnect request via API")
StopTunnel()
return nil
return StopTunnel()
},
// onExit
func() error {
@@ -1020,7 +946,11 @@ func StartTunnel(config TunnelConfig) {
olm.RegisterHandler("olm/terminate", func(msg websocket.WSMessage) {
logger.Info("Received terminate message")
olm.Close()
Close()
if globalConfig.OnTerminated != nil {
go globalConfig.OnTerminated()
}
})
olm.OnConnect(func() error {
@@ -1155,7 +1085,7 @@ func Close() {
// StopTunnel stops just the tunnel process and websocket connection
// without shutting down the entire application
func StopTunnel() {
func StopTunnel() error {
logger.Info("Stopping tunnel process")
// Cancel the tunnel context if it exists
@@ -1189,6 +1119,8 @@ func StopTunnel() {
network.ClearNetworkSettings()
logger.Info("Tunnel process stopped")
return nil
}
func StopApi() error {
@@ -1210,3 +1142,39 @@ func StartApi() error {
}
return nil
}
func GetStatus() api.StatusResponse {
return apiServer.GetStatus()
}
func SwitchOrg(orgID string) error {
logger.Info("Processing org switch request to orgId: %s", orgID)
// Ensure we have an active olmClient
if olmClient == nil {
return fmt.Errorf("no active connection to switch organizations")
}
// Update the orgID in the API server
apiServer.SetOrgID(orgID)
// Mark as not connected to trigger re-registration
connected = false
Close()
// Clear peer statuses in API
apiServer.SetRegistered(false)
// Trigger re-registration with new orgId
logger.Info("Re-registering with new orgId: %s", orgID)
publicKey := privateKey.PublicKey()
stopRegister = olmClient.SendMessageInterval("olm/wg/register", map[string]interface{}{
"publicKey": publicKey.String(),
"relay": true, // Default to relay mode for org switch
"olmVersion": globalConfig.Version,
"orgId": orgID,
}, 1*time.Second)
return nil
}

View File

@@ -1,5 +1,7 @@
package olm
import "time"
type WgData struct {
Sites []SiteConfig `json:"sites"`
TunnelIP string `json:"tunnelIP"`
@@ -75,3 +77,50 @@ type UpdateRemoteSubnetsData struct {
OldRemoteSubnets []string `json:"oldRemoteSubnets"` // old list of remote subnets
NewRemoteSubnets []string `json:"newRemoteSubnets"` // new list of remote subnets
}
type GlobalConfig struct {
// Logging
LogLevel string
// HTTP server
EnableAPI bool
HTTPAddr string
SocketPath string
Version string
// Callbacks
OnRegistered func()
OnConnected func()
OnTerminated func()
// Source tracking (not in JSON)
sources map[string]string
}
type TunnelConfig struct {
// Connection settings
Endpoint string
ID string
Secret string
UserToken string
// Network settings
MTU int
DNS string
UpstreamDNS []string
InterfaceName string
// Advanced
Holepunch bool
TlsClientCert string
// Parsed values (not in JSON)
PingIntervalDuration time.Duration
PingTimeoutDuration time.Duration
OrgID string
// DoNotCreateNewClient bool
FileDescriptorTun uint32
FileDescriptorUAPI uint32
}