summaryrefslogtreecommitdiff
path: root/wrappers/python
diff options
context:
space:
mode:
Diffstat (limited to 'wrappers/python')
-rw-r--r--wrappers/python/eduvpn_common/loader.py23
-rw-r--r--wrappers/python/eduvpn_common/main.py40
-rw-r--r--wrappers/python/eduvpn_common/types.py29
3 files changed, 73 insertions, 19 deletions
diff --git a/wrappers/python/eduvpn_common/loader.py b/wrappers/python/eduvpn_common/loader.py
index e163148..8ac6372 100644
--- a/wrappers/python/eduvpn_common/loader.py
+++ b/wrappers/python/eduvpn_common/loader.py
@@ -5,7 +5,7 @@ from eduvpn_common import __version__
from eduvpn_common.types import (
BoolError,
DataError,
- ProxyReady,
+ HandlerError,
ProxySetup,
ReadRxBytes,
RefreshList,
@@ -134,14 +134,27 @@ def initialize_functions(lib: CDLL) -> None:
],
BoolError,
)
- lib.StartProxyguard.argtypes, lib.StartProxyguard.restype = (
+ lib.NewProxyguard.argtypes, lib.NewProxyguard.restype = (
[
c_int,
- c_char_p,
+ c_int,
c_int,
c_char_p,
ProxySetup,
- ProxyReady,
],
- c_void_p,
+ HandlerError,
+ )
+ lib.ProxyguardTunnel.argtypes, lib.ProxyguardTunnel.restype = (
+ [
+ c_int,
+ c_int,
+ c_int,
+ ],
+ c_char_p,
+ )
+ lib.ProxyguardPeerIPs.argtypes, lib.ProxyguardPeerIPs.restype = (
+ [
+ c_int,
+ ],
+ DataError,
)
diff --git a/wrappers/python/eduvpn_common/main.py b/wrappers/python/eduvpn_common/main.py
index ce52024..04a2302 100644
--- a/wrappers/python/eduvpn_common/main.py
+++ b/wrappers/python/eduvpn_common/main.py
@@ -1,13 +1,12 @@
import ctypes
import json
from enum import IntEnum
-from typing import Any, Callable, Iterator, Optional
+from typing import Any, Callable, Iterator, List, Optional
from eduvpn_common.event import EventHandler
from eduvpn_common.loader import initialize_functions, load_lib
from eduvpn_common.state import State
from eduvpn_common.types import (
- ProxyReady,
ProxySetup,
ReadRxBytes,
RefreshList,
@@ -21,6 +20,24 @@ from eduvpn_common.types import (
global_object = None
+class Proxyguard(object):
+ def __init__(self, parent, handler):
+ self.parent = parent
+ self.handler = handler
+
+ def tunnel(self, wglisten: int):
+ tunnel_err = self.parent.go_cookie_function(self.parent.lib.ProxyguardTunnel, self.handler, wglisten)
+ if tunnel_err:
+ forwardError(tunnel_err)
+
+ @property
+ def peer_ips(self) -> List[str]:
+ peer_ips, peer_ips_err = self.parent.go_function(self.parent.lib.ProxyguardPeerIPs, self.handler)
+ if peer_ips_err:
+ forwardError(peer_ips_err)
+ return json.loads(peer_ips)
+
+
class WrappedError(Exception):
def __init__(self, translations, language, misc):
self.translations = translations
@@ -359,24 +376,23 @@ class EduVPN(object):
forwardError(dropped_err)
return dropped
- def start_proxyguard(
+ def new_proxyguard(
self,
- listen: str,
- source_port: int,
+ listen_port: int,
+ tcp_source_port: int,
peer: str,
setup: ProxySetup,
- ready: ProxyReady,
- ):
- proxy_err = self.go_cookie_function(
- self.lib.StartProxyguard,
- listen,
- source_port,
+ ) -> Proxyguard:
+ proxy, proxy_err = self.go_cookie_function(
+ self.lib.NewProxyguard,
+ listen_port,
+ tcp_source_port,
peer,
setup,
- ready,
)
if proxy_err:
forwardError(proxy_err)
+ return Proxyguard(self, proxy)
def cancel(self):
self.jar.cancel()
diff --git a/wrappers/python/eduvpn_common/types.py b/wrappers/python/eduvpn_common/types.py
index 716556e..5e23d61 100644
--- a/wrappers/python/eduvpn_common/types.py
+++ b/wrappers/python/eduvpn_common/types.py
@@ -25,6 +25,15 @@ class DataError(Structure):
_fields_ = [("data", c_void_p), ("error", c_void_p)]
+class HandlerError(Structure):
+ """The C type that represents a tuple of a CGO handler and error (string) as returned by the Go library
+
+ :meta private:
+ """
+
+ _fields_ = [("handler", c_int), ("error", c_void_p)]
+
+
class BoolError(Structure):
"""The C type that represents a tuple of boolean and error as returned by the Go library
@@ -36,8 +45,7 @@ class BoolError(Structure):
# The type for a Go state change callback
VPNStateChange = CFUNCTYPE(c_int, c_int, c_int, c_char_p)
-ProxySetup = CFUNCTYPE(c_void_p, c_int, c_char_p)
-ProxyReady = CFUNCTYPE(c_void_p)
+ProxySetup = CFUNCTYPE(c_void_p, c_int)
ReadRxBytes = CFUNCTYPE(c_ulonglong)
RefreshList = CFUNCTYPE(c_void_p)
TokenGetter = CFUNCTYPE(c_void_p, c_char_p, c_int, POINTER(c_char), c_size_t)
@@ -84,6 +92,7 @@ def decode_res(res: Any) -> Any:
"""
decode_map = {
c_void_p: get_ptr_string,
+ HandlerError: get_handler_error,
DataError: get_data_error,
BoolError: get_bool_error,
}
@@ -126,6 +135,22 @@ def get_data_error(lib: CDLL, data_error: DataError) -> Tuple[str, str]:
return data, error
+def get_handler_error(lib: CDLL, handler_error: HandlerError) -> Tuple[int, str]:
+ """Convert a C handler+error structure to a Python usable handler+error structure
+
+ :param lib: CDLL: The Go shared library
+ :param handler_error: HandlerError: The handler error C structure
+
+ :meta private:
+
+ :return: The handler and error
+ :rtype: Tuple[int, str]
+ """
+ handler = int(handler_error.handler)
+ error = get_ptr_string(lib, handler_error.error)
+ return handler, error
+
+
def get_bool_error(lib: CDLL, bool_error: BoolError) -> Tuple[bool, str]:
"""Convert a C boolean (c int)+error structure to a Python usable boolean+error structure