From b320b13b5d019c26928d2f00d8cba0febacb104b Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Fri, 13 Jan 2023 13:56:26 +0100 Subject: Client + Exports: Separate cleanup from disconnect --- client/fsm.go | 15 +-------------- client/server.go | 24 ++++++++++++++++++++++++ exports/exports.go | 17 ++++++++++++++--- wrappers/python/eduvpn_common/loader.py | 4 ++-- wrappers/python/eduvpn_common/main.py | 16 ++++++++++++++-- 5 files changed, 55 insertions(+), 21 deletions(-) diff --git a/client/fsm.go b/client/fsm.go index 88f2cf9..f9c7976 100644 --- a/client/fsm.go +++ b/client/fsm.go @@ -2,7 +2,6 @@ package client import ( "github.com/eduvpn/eduvpn-common/internal/fsm" - "github.com/eduvpn/eduvpn-common/internal/oauth" "github.com/eduvpn/eduvpn-common/internal/server" "github.com/go-errors/errors" ) @@ -295,7 +294,7 @@ func (c *Client) SetDisconnecting() error { // This indicates that the VPN is currently disconnected from the server. // This also sends the /disconnect API call to the server. // Returns an error if this state transition is not possible. -func (c *Client) SetDisconnected(cleanup bool, ct oauth.Token) error { +func (c *Client) SetDisconnected() error { if c.InFSMState(StateDisconnected) { // already disconnected, show no error c.Logger.Warningf("Already disconnected") @@ -312,18 +311,6 @@ func (c *Client) SetDisconnected(cleanup bool, ct oauth.Token) error { return err } - if cleanup { - // If we need to relogin, update tokens - if server.NeedsRelogin(srv) { - server.UpdateTokens(srv, ct) - } - // Do the /disconnect API call and go to disconnected after... - err := server.Disconnect(srv) - if err != nil { - c.Logger.Warningf("Error disconnecting %v", err) - } - } - c.FSM.GoTransitionWithData(StateDisconnected, srv) return nil diff --git a/client/server.go b/client/server.go index bc6b1fe..4070705 100644 --- a/client/server.go +++ b/client/server.go @@ -95,6 +95,30 @@ func (c *Client) getConfig(srv server.Server, preferTCP bool, t oauth.Token) (*C return cfg, nil } +// Cleanup cleans up the VPN connection by sending a /disconnect to the server +func (c *Client) Cleanup(ct oauth.Token) error { + srv, err := c.Servers.GetCurrentServer() + if err != nil { + c.logError(err) + return err + } + + // If we need to relogin, update tokens + if server.NeedsRelogin(srv) { + server.UpdateTokens(srv, ct) + } + // Do the /disconnect API call + err = server.Disconnect(srv) + if err != nil { + // We log nothing here because this can happen regularly + // Maybe we should not log errors that we return directly anyways? + return err + } + // TODO: Tokens might be refreshed, return updated tokens + // Not implemented yet, because ideally we want this implemented with an interface + return nil +} + // SetSecureLocation sets the location for the current secure location server. countryCode is the secure location to be chosen. // This function returns an error e.g. if the server cannot be found or the location is wrong. func (c *Client) SetSecureLocation(countryCode string) error { diff --git a/exports/exports.go b/exports/exports.go index 89d18dc..5a17da0 100644 --- a/exports/exports.go +++ b/exports/exports.go @@ -414,8 +414,8 @@ func SetSearchServer(name *C.char) *C.error { return getError(setSearchErr) } -//export SetDisconnected -func SetDisconnected(name *C.char, cleanup C.int, prevTokens C.token) *C.error { +//export Cleanup +func Cleanup(name *C.char, prevTokens C.token) *C.error { nameStr := C.GoString(name) state, stateErr := GetVPNState(nameStr) if stateErr != nil { @@ -426,7 +426,18 @@ func SetDisconnected(name *C.char, cleanup C.int, prevTokens C.token) *C.error { Refresh: C.GoString(prevTokens.refresh), ExpiredTimestamp: time.Unix(int64(prevTokens.expired), 0), } - setDisconnectedErr := state.SetDisconnected(int(cleanup) == 1, t) + err := state.Cleanup(t) + return getError(err) +} + +//export SetDisconnected +func SetDisconnected(name *C.char) *C.error { + nameStr := C.GoString(name) + state, stateErr := GetVPNState(nameStr) + if stateErr != nil { + return getError(stateErr) + } + setDisconnectedErr := state.SetDisconnected() return getError(setDisconnectedErr) } diff --git a/wrappers/python/eduvpn_common/loader.py b/wrappers/python/eduvpn_common/loader.py index 988d827..202e2da 100644 --- a/wrappers/python/eduvpn_common/loader.py +++ b/wrappers/python/eduvpn_common/loader.py @@ -140,11 +140,11 @@ def initialize_functions(lib: CDLL) -> None: lib.RenewSession.argtypes, lib.RenewSession.restype = [c_char_p], c_void_p lib.SetConnected.argtypes, lib.SetConnected.restype = [c_char_p], c_void_p lib.SetConnecting.argtypes, lib.SetConnecting.restype = [c_char_p], c_void_p - lib.SetDisconnected.argtypes, lib.SetDisconnected.restype = [ + lib.Cleanup.argtypes, lib.Cleanup.restype = [ c_char_p, - c_int, cToken, ], c_void_p + lib.SetDisconnected.argtypes, lib.SetDisconnected.restype = [c_char_p], c_void_p lib.SetDisconnecting.argtypes, lib.SetDisconnecting.restype = [c_char_p], c_void_p lib.SetProfileID.argtypes, lib.SetProfileID.restype = [c_char_p, c_char_p], c_void_p lib.SetSearchServer.argtypes, lib.SetSearchServer.restype = [c_char_p], c_void_p diff --git a/wrappers/python/eduvpn_common/main.py b/wrappers/python/eduvpn_common/main.py index 4204239..7e96d6a 100644 --- a/wrappers/python/eduvpn_common/main.py +++ b/wrappers/python/eduvpn_common/main.py @@ -332,7 +332,19 @@ class EduVPN(object): if connecting_err: raise connecting_err - def set_disconnected(self, cleanup: bool = True, tokens: Optional[Token] = None) -> None: + def cleanup(self, tokens: Optional[Token] = None) -> None: + """Cleanup the vpn connection + + :param tokens: Optional[Token] (Default value = None): The OAuth tokens if available + + :raises WrappedError: An error by the Go library + """ + cleanup_err = self.go_function(self.lib.Cleanup, encode_tokens(tokens)) + + if cleanup_err: + raise cleanup_err + + def set_disconnected(self, ) -> None: """Set the FSM to disconnected :param cleanup: bool: (Default value = True): Whether or not to call /disconnect to the server. This invalidates the OpenVPN/WireGuard configuration @@ -340,7 +352,7 @@ class EduVPN(object): :raises WrappedError: An error by the Go library """ - disconnect_err = self.go_function(self.lib.SetDisconnected, cleanup, encode_tokens(tokens)) + disconnect_err = self.go_function(self.lib.SetDisconnected) if disconnect_err: raise disconnect_err -- cgit v1.2.3