1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
from . import lib, VPNStateChange, encode_args, decode_res
from typing import Optional, Tuple
import threading
from .event import StateType, EventHandler
import json
eduvpn_objects = {}
def add_as_global_object(eduvpn) -> bool:
global eduvpn_objects
if eduvpn.name not in eduvpn_objects:
eduvpn_objects[eduvpn.name] = eduvpn
return True
return False
def remove_as_global_object(eduvpn):
global eduvpn_objects
eduvpn_objects.pop(eduvpn.name, None)
@VPNStateChange
def state_callback(name, old_state, new_state, data):
name = name.decode()
if name not in eduvpn_objects:
return
eduvpn_objects[name].callback(old_state.decode(), new_state.decode(), data.decode())
class EduVPN(object):
def __init__(self, name: str, config_directory: str):
self.event_handler = EventHandler()
self.name = name
self.config_directory = config_directory
# Callbacks that need to wait for specific events
# The ask profile callback needs to wait for the UI thread to select a profile
# This is stored in the profile_event
self.profile_event: Optional[threading.Event] = None
self.location_event: Optional[threading.Event] = None
@self.event.on("Ask_Profile", StateType.Wait)
def wait_profile_event(old_state: str, profiles: str):
if self.profile_event:
self.profile_event.wait()
@self.event.on("Ask_Location", StateType.Wait)
def wait_location_event(old_state: str, locations: str):
if self.location_event:
self.location_event.wait()
def go_function(self, func, *args):
# The functions all have at least one arg type which is the name of the client
args_gen = encode_args(list(args), func.argtypes[1:])
res = func(self.name.encode("utf-8"), *(args_gen))
return decode_res(func.restype)(res)
def cancel_oauth(self) -> None:
cancel_oauth_err = self.go_function(lib.CancelOAuth)
if cancel_oauth_err:
raise Exception(cancel_oauth_err)
def deregister(self) -> None:
deregister_err = self.go_function(lib.Deregister)
remove_as_global_object(self)
if deregister_err:
raise Exception(deregister_err)
def register(self, debug: bool = False) -> None:
if not add_as_global_object(self):
raise Exception("Already registered")
register_err = self.go_function(
lib.Register, self.config_directory, state_callback, debug
)
if register_err:
raise Exception(register_err)
def get_disco_servers(self) -> str:
servers, servers_err = self.go_function(lib.GetDiscoServers)
if servers_err:
raise Exception(servers_err)
return servers
def get_disco_organizations(self) -> str:
organizations, organizations_err = self.go_function(lib.GetDiscoOrganizations)
if organizations_err:
raise Exception(organizations_err)
return organizations
def get_config(self, url: str, func: callable, force_tcp: bool = False):
# Because it could be the case that a profile callback is started, store a threading event
# In the constructor, we have defined a wait event for Ask_Profile, this waits for this event to be set
# The event is set in self.set_profile
self.profile_event = threading.Event()
config_json, config_err = self.go_function(func, url, force_tcp)
self.profile_event = None
self.location_event = None
if config_err:
raise Exception(config_err)
config_json_dict = json.loads(config_json)
config = config_json_dict["config"]
config_type = config_json_dict["config_type"]
return config, config_type
def get_config_custom_server(
self, url: str, force_tcp: bool = False
) -> Tuple[str, str]:
return self.get_config(url, lib.GetConfigCustomServer, force_tcp)
def get_config_institute_access(
self, url: str, force_tcp: bool = False
) -> Tuple[str, str]:
return self.get_config(url, lib.GetConfigInstituteAccess, force_tcp)
def get_config_secure_internet(
self, url: str, force_tcp: bool = False
) -> Tuple[str, str]:
self.location_event = threading.Event()
return self.get_config(url, lib.GetConfigSecureInternet, force_tcp)
def set_connected(self) -> None:
connect_err = self.go_function(lib.SetConnected)
if connect_err:
raise Exception(connect_err)
def set_disconnected(self) -> None:
disconnect_err = self.go_function(lib.SetDisconnected)
if disconnect_err:
raise Exception(disconnect_err)
def get_identifier(self) -> str:
identifier, identifier_err = self.go_function(lib.GetIdentifier)
if identifier_err:
raise Exception(identifier_err)
return identifier
def set_identifier(self, identifier: str) -> None:
identifier_err = self.go_function(lib.SetIdentifier, identifier)
if identifier_err:
raise Exception(identifier_err)
def set_search_server(self) -> None:
search_err = self.go_function(lib.SetSearchServer)
if search_err:
raise Exception(search_err)
def remove_class_callbacks(self, cls) -> None:
self.event_handler.change_class_callbacks(cls, add=False)
def register_class_callbacks(self, cls) -> None:
self.event_handler.change_class_callbacks(cls)
@property
def event(self) -> EventHandler:
return self.event_handler
def callback(self, old_state: str, new_state: str, data: str) -> None:
self.event.run(old_state, new_state, data)
def set_profile(self, profile_id: str) -> None:
# Set the profile id
profile_err = self.go_function(lib.SetProfileID, profile_id)
# If there is a profile event, set it so that the wait callback finishes
# And so that the Go code can move to the next state
if self.profile_event:
self.profile_event.set()
if profile_err:
raise Exception(profile_err)
def set_secure_location(self, country_code: str) -> None:
# Set the location by country code
location_err = self.go_function(lib.SetSecureLocation, country_code)
# If there is a location event, set it so that the wait callback finishes
# And so that the Go code can move to the next state
if self.location_event:
self.location_event.set()
if location_err:
raise Exception(location_err)
|