diff --git a/internal/client/client.go b/internal/client/client.go index 521bbd6..34702f6 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -166,6 +166,7 @@ func printStatus(status *vpnstatus.Status) error { fmt.Printf("IP: %s\n", status.IP) fmt.Printf("Device: %s\n", status.Device) fmt.Printf("Current Server: %s\n", status.Server) + fmt.Printf("Server IP: %s\n", status.ServerIP) if status.ConnectedAt <= 0 { fmt.Printf("Connected At:\n") diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 43572c7..a949eb7 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -144,6 +144,19 @@ func (d *Daemon) setStatusServer(server string) { d.dbus.SetProperty(dbusapi.PropertyServer, server) } +// setStatusServerIP sets the current server IP in status. +func (d *Daemon) setStatusServerIP(serverIP string) { + if d.status.ServerIP == serverIP { + // connected server IP not changed + return + } + + // connected server IP changed + log.WithField("ServerIP", serverIP).Info("Daemon changed Server IP status") + d.status.ServerIP = serverIP + d.dbus.SetProperty(dbusapi.PropertyServerIP, serverIP) +} + // setStatusConnectedAt sets the connection time in status. func (d *Daemon) setStatusConnectedAt(connectedAt int64) { if d.status.ConnectedAt == connectedAt { @@ -227,13 +240,16 @@ func (d *Daemon) connectVPN(login *logininfo.LoginInfo) { return } + // set server address + d.serverIP = net.ParseIP(strings.Trim(login.Host, "[]")) + // update status d.setStatusOCRunning(true) d.setStatusServer(login.Server) + d.setStatusServerIP(d.serverIP.String()) d.setStatusConnectionState(vpnstatus.ConnectionStateConnecting) - // set server address and add it to allowed addrs in trafpol - d.serverIP = net.ParseIP(strings.Trim(login.Host, "[]")) + // add server address to allowed addrs in trafpol if d.trafpol != nil && d.serverIP != nil { d.serverIPAllowed = d.trafpol.AddAllowedAddr(d.serverIP) } @@ -338,6 +354,7 @@ func (d *Daemon) updateVPNConfigDown() { d.setStatusVPNConfig(nil) d.setStatusConnectionState(vpnstatus.ConnectionStateDisconnected) d.setStatusServer("") + d.setStatusServerIP("") d.setStatusConnectedAt(0) d.setStatusIP("") d.setStatusDevice("") @@ -447,6 +464,7 @@ func (d *Daemon) handleRunnerDisconnect() { d.setStatusOCRunning(false) d.setStatusConnectionState(vpnstatus.ConnectionStateDisconnected) d.setStatusServer("") + d.setStatusServerIP("") d.setStatusConnectedAt(0) // make sure the vpn config is not active any more diff --git a/internal/dbusapi/service.go b/internal/dbusapi/service.go index 7ca7baa..608eb21 100644 --- a/internal/dbusapi/service.go +++ b/internal/dbusapi/service.go @@ -27,6 +27,7 @@ const ( PropertyIP = "IP" PropertyDevice = "Device" PropertyServer = "Server" + PropertyServerIP = "ServerIP" PropertyConnectedAt = "ConnectedAt" PropertyServers = "Servers" PropertyOCRunning = "OCRunning" @@ -64,6 +65,11 @@ const ( ServerInvalid = "" ) +// Property "ServerIP" values. +const ( + ServerIPInvalid = "" +) + // Property "Connected At" values. const ( ConnectedAtInvalid int64 = -1 @@ -224,6 +230,7 @@ func (s *Service) start() { s.props.SetMust(Interface, PropertyIP, IPInvalid) s.props.SetMust(Interface, PropertyDevice, DeviceInvalid) s.props.SetMust(Interface, PropertyServer, ServerInvalid) + s.props.SetMust(Interface, PropertyServerIP, ServerIPInvalid) s.props.SetMust(Interface, PropertyConnectedAt, ConnectedAtInvalid) s.props.SetMust(Interface, PropertyServers, ServersInvalid) s.props.SetMust(Interface, PropertyOCRunning, OCRunningUnknown) @@ -313,6 +320,12 @@ func (s *Service) Start() error { Emit: prop.EmitTrue, Callback: nil, }, + PropertyServerIP: { + Value: ServerIPInvalid, + Writable: false, + Emit: prop.EmitTrue, + Callback: nil, + }, PropertyConnectedAt: { Value: ConnectedAtInvalid, Writable: false, diff --git a/pkg/client/client.go b/pkg/client/client.go index 2dbba55..705f545 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -146,6 +146,8 @@ func updateStatusFromProperties(status *vpnstatus.Status, props map[string]dbus. err = v.Store(&dest.Device) case dbusapi.PropertyServer: err = v.Store(&dest.Server) + case dbusapi.PropertyServerIP: + err = v.Store(&dest.ServerIP) case dbusapi.PropertyConnectedAt: err = v.Store(&dest.ConnectedAt) case dbusapi.PropertyServers: @@ -260,6 +262,8 @@ func handlePropertiesChanged(s *dbus.Signal, status *vpnstatus.Status) *vpnstatu status.Device = dbusapi.DeviceInvalid case dbusapi.PropertyServer: status.Server = dbusapi.ServerInvalid + case dbusapi.PropertyServerIP: + status.ServerIP = dbusapi.ServerIPInvalid case dbusapi.PropertyConnectedAt: status.ConnectedAt = dbusapi.ConnectedAtInvalid case dbusapi.PropertyServers: diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index ae921ac..afd68be 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -95,6 +95,7 @@ func TestDBusClientQuery(t *testing.T) { dbusapi.PropertyIP: dbus.MakeVariant(dbusapi.IPInvalid), dbusapi.PropertyDevice: dbus.MakeVariant(dbusapi.DeviceInvalid), dbusapi.PropertyServer: dbus.MakeVariant(dbusapi.ServerInvalid), + dbusapi.PropertyServerIP: dbus.MakeVariant(dbusapi.ServerIPInvalid), dbusapi.PropertyConnectedAt: dbus.MakeVariant(dbusapi.ConnectedAtInvalid), dbusapi.PropertyServers: dbus.MakeVariant(dbusapi.ServersInvalid), dbusapi.PropertyOCRunning: dbus.MakeVariant(dbusapi.OCRunningUnknown), @@ -205,6 +206,7 @@ func TestDBusClientSubscribe(t *testing.T) { dbusapi.PropertyIP, dbusapi.PropertyDevice, dbusapi.PropertyServer, + dbusapi.PropertyServerIP, dbusapi.PropertyConnectedAt, dbusapi.PropertyServers, dbusapi.PropertyOCRunning, diff --git a/pkg/vpnstatus/status.go b/pkg/vpnstatus/status.go index 6a9d93f..64ad654 100644 --- a/pkg/vpnstatus/status.go +++ b/pkg/vpnstatus/status.go @@ -104,6 +104,7 @@ type Status struct { IP string Device string Server string + ServerIP string ConnectedAt int64 Servers []string OCRunning OCRunning @@ -121,6 +122,7 @@ func (s *Status) Copy() *Status { IP: s.IP, Device: s.Device, Server: s.Server, + ServerIP: s.ServerIP, ConnectedAt: s.ConnectedAt, Servers: append(s.Servers[:0:0], s.Servers...), OCRunning: s.OCRunning, diff --git a/pkg/vpnstatus/status_test.go b/pkg/vpnstatus/status_test.go index 8476a25..4f5b11b 100644 --- a/pkg/vpnstatus/status_test.go +++ b/pkg/vpnstatus/status_test.go @@ -131,6 +131,7 @@ func TestStatusCopy(t *testing.T) { ConnectionState: ConnectionStateConnected, IP: "192.168.1.1", Server: "test server 1", + ServerIP: "10.0.0.1", ConnectedAt: 1700000000, Servers: []string{"test server 1", "test server 2"}, OCRunning: OCRunningRunning, diff --git a/tools/dbusclient/main.go b/tools/dbusclient/main.go index 12fd8e0..9a2af26 100644 --- a/tools/dbusclient/main.go +++ b/tools/dbusclient/main.go @@ -35,6 +35,7 @@ func main() { ip := dbusapi.IPInvalid device := dbusapi.DeviceInvalid server := dbusapi.ServerInvalid + serverIP := dbusapi.ServerInvalid connectedAt := dbusapi.ConnectedAtInvalid servers := dbusapi.ServersInvalid ocRunning := dbusapi.OCRunningUnknown @@ -52,6 +53,7 @@ func main() { getProperty(dbusapi.PropertyIP, &ip) getProperty(dbusapi.PropertyDevice, &device) getProperty(dbusapi.PropertyServer, &server) + getProperty(dbusapi.PropertyServerIP, &serverIP) getProperty(dbusapi.PropertyConnectedAt, &connectedAt) getProperty(dbusapi.PropertyServers, &servers) getProperty(dbusapi.PropertyOCRunning, &ocRunning) @@ -62,6 +64,7 @@ func main() { log.Println("IP:", ip) log.Println("Device:", device) log.Println("Server:", server) + log.Println("ServerIP:", serverIP) log.Println("ConnectedAt:", connectedAt) log.Println("Servers:", servers) log.Println("OCRunning:", ocRunning) @@ -118,6 +121,11 @@ func main() { log.Fatal(err) } fmt.Println(server) + case dbusapi.PropertyServerIP: + if err := value.Store(&serverIP); err != nil { + log.Fatal(err) + } + fmt.Println(serverIP) case dbusapi.PropertyConnectedAt: if err := value.Store(&connectedAt); err != nil { log.Fatal(err) @@ -160,6 +168,8 @@ func main() { device = dbusapi.DeviceInvalid case dbusapi.PropertyServer: device = dbusapi.ServerInvalid + case dbusapi.PropertyServerIP: + device = dbusapi.ServerIPInvalid case dbusapi.PropertyConnectedAt: connectedAt = dbusapi.ConnectedAtInvalid case dbusapi.PropertyServers: