summaryrefslogtreecommitdiff
path: root/wrappers/python
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
parented6073f2c2c6600063f2e5062937b7a2a1162eb2 (diff)
Add wrapping functionality for getting a wireguard config
Diffstat (limited to 'wrappers/python')
-rw-r--r--wrappers/python/eduvpncommon/__init__.py1
-rw-r--r--wrappers/python/eduvpncommon/main.py49
-rw-r--r--wrappers/python/main.py21
3 files changed, 55 insertions, 16 deletions
diff --git a/wrappers/python/eduvpncommon/__init__.py b/wrappers/python/eduvpncommon/__init__.py
index 073af39..faa311d 100644
--- a/wrappers/python/eduvpncommon/__init__.py
+++ b/wrappers/python/eduvpncommon/__init__.py
@@ -33,6 +33,7 @@ class DataError(Structure):
VPNStateChange = CFUNCTYPE(None, c_char_p, c_char_p, c_char_p)
# Exposed functions
+lib.Connect.argtypes, lib.Connect.restype = [c_char_p], DataError
lib.Register.argtypes, lib.Register.restype = [c_char_p, c_char_p, VPNStateChange], None
lib.GetOrganizationsList.argtypes, lib.GetOrganizationsList.restype = [], DataError
lib.GetServersList.argtypes, lib.GetServersList.restype = [], DataError
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
diff --git a/wrappers/python/main.py b/wrappers/python/main.py
index a248b71..6bd7d86 100644
--- a/wrappers/python/main.py
+++ b/wrappers/python/main.py
@@ -1,12 +1,27 @@
import eduvpncommon.main as eduvpn
+import webbrowser
_eduvpn = eduvpn.EduVPN("org.eduvpn.app.linux", "configs")
-@_eduvpn.event.on("REGISTERED", eduvpn.StateType.Enter)
-def registered(data):
- print(f"REGISTERED PYTHON WITH DATA {data}")
+@_eduvpn.event.on("OAuthInitialized", eduvpn.StateType.Enter)
+def oauth_initialized(url):
+ print(f"Got OAUTH url {url}")
+ webbrowser.open(url)
+
+
+@_eduvpn.event.on("OAuthFinished", eduvpn.StateType.Enter)
+def oauth_finished(data):
+ print(f"Oauth finished {data}")
_eduvpn.register()
+print(_eduvpn.get_disco())
+
+config, error = _eduvpn.connect("https://eduvpn.jwijenbergh.com")
+#
+if error != "":
+ print("Got connect error", error)
+
+print(config)