summaryrefslogtreecommitdiff
path: root/wrappers/python/eduvpncommon/main.py
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-03-23 12:10:47 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-03-23 12:10:47 +0100
commitb9b2659908d5fe8afcc74f2769a8da7bab243018 (patch)
treec8d524cb99cd98d326d78b78ce988395e4fb3e26 /wrappers/python/eduvpncommon/main.py
parented6073f2c2c6600063f2e5062937b7a2a1162eb2 (diff)
Add wrapping functionality for getting a wireguard config
Diffstat (limited to 'wrappers/python/eduvpncommon/main.py')
-rw-r--r--wrappers/python/eduvpncommon/main.py49
1 files changed, 36 insertions, 13 deletions
diff --git a/wrappers/python/eduvpncommon/main.py b/wrappers/python/eduvpncommon/main.py
index 9a4931f..9e1f25e 100644
--- a/wrappers/python/eduvpncommon/main.py
+++ b/wrappers/python/eduvpncommon/main.py
@@ -1,12 +1,13 @@
from . import lib, VPNStateChange, GetDataError, GetPtrString
from ctypes import *
from enum import Enum
-import functools
+
class StateType(Enum):
Enter = 1
Leave = 2
+
# Registers the python app with the Go code
# name: The name of the app to be registered
# url: The url of the server to connect to, FIXME: To be removed
@@ -19,19 +20,46 @@ def Register(name, config_directory, state_callback):
return err_string
+def GetDiscoServers():
+ servers, serversErr = GetDataError(lib.GetServersList())
+ organizations, organizationsErr = GetDataError(lib.GetOrganizationsList())
+ return servers, serversErr, organizations, organizationsErr
+
+
+def Connect(url):
+ url_bytes = url.encode("utf-8")
+ data_error = lib.Connect(url_bytes)
+ return GetDataError(data_error)
+
+
+# This has to be global as otherwise the callback is not alive
+callback_function = None
+
+
+def register_callback(eduvpn):
+ global callback_function
+ callback_function = VPNStateChange(
+ lambda old_state, new_state, data: eduvpn.callback(
+ old_state.decode(), new_state.decode(), data.decode()
+ )
+ )
+
+
class EduVPN(object):
def __init__(self, name, config_directory):
self.event_handler = EventHandler()
self.name = name
self.config_directory = config_directory
+ register_callback(self)
def register(self) -> bool:
- closure = VPNStateChange(
- lambda old_state, new_state, data: self.callback(
- old_state.decode(), new_state.decode(), data.decode()
- )
- )
- return Register(self.name, self.config_directory, closure) == ""
+ return Register(self.name, self.config_directory, callback_function) == ""
+
+ def get_disco(self):
+ return GetDiscoServers()
+
+ def connect(self, url):
+ return Connect(url)
@property
def event(self):
@@ -51,6 +79,7 @@ class EventHandler(object):
self.handlers[(state, state_type)] = []
self.handlers[(state, state_type)].append(func)
return func
+
return wrapped_f
def run_state(self, state, state_type, data):
@@ -64,9 +93,3 @@ class EventHandler(object):
return
self.run_state(old_state, StateType.Leave, data)
self.run_state(new_state, StateType.Enter, data)
-
-
-def GetDiscoServers():
- servers, serversErr = GetDataError(lib.GetServersList())
- organizations, organizationsErr = GetDataError(lib.GetOrganizationsList())
- return servers, serversErr, organizations, organizationsErr