summaryrefslogtreecommitdiff
path: root/wrappers/python/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'wrappers/python/main.py')
-rw-r--r--wrappers/python/main.py92
1 files changed, 75 insertions, 17 deletions
diff --git a/wrappers/python/main.py b/wrappers/python/main.py
index 1c1afd7..c887fed 100644
--- a/wrappers/python/main.py
+++ b/wrappers/python/main.py
@@ -1,32 +1,90 @@
import eduvpncommon.main as eduvpn
import webbrowser
+import json
+# Asks the user for a profile index
+# It loops up until a valid input is given
+def ask_profile_input(total: int) -> int:
+ profile_index = None
-_eduvpn = eduvpn.EduVPN("org.eduvpn.app.linux", "configs")
+ while profile_index is None:
+ try:
+ profile_index = int(
+ input("Please select a profile by inputting a number (e.g. 1): ")
+ )
+ if (profile_index > total) or (profile_index < 1):
+ print("Invalid profile range")
+ profile_index = None
+ except ValueError:
+ print("Please enter a valid input")
+ # The profile is one based, move to zero based input
+ return profile_index - 1
-@_eduvpn.event.on("OAuth_Started", eduvpn.StateType.Enter)
-def oauth_initialized(url):
- print(f"Got OAUTH url {url}")
- webbrowser.open(url)
+# Sets up the callbacks using the provided class
+def setup_callbacks(_eduvpn: eduvpn.EduVPN) -> None:
+ # The callback that starst OAuth
+ # It needs to open the URL in the web browser
+ @_eduvpn.event.on("OAuth_Started", eduvpn.StateType.Enter)
+ def oauth_initialized(old_state: str, url: str) -> None:
+ print(f"Got OAuth URL {url}, old state: {old_state}")
+ webbrowser.open(url)
+ # The callback which asks the user for a profile
+ @_eduvpn.event.on("Ask_Profile", eduvpn.StateType.Enter)
+ def ask_profile(old_state: str, profiles: str):
+ print("Multiple profiles found, you need to select a profile, old state: {old_state}")
-@_eduvpn.event.on("Ask_Profile", eduvpn.StateType.Enter)
-def ask_profile(profiles):
- print("ASK PROFILE CB", profiles)
- _eduvpn.set_profile("prefer-openvpn")
+ # Parse the profiles as JSON
+ data = json.loads(profiles)
+ # Get a lits of profiles
+ profile_strings = [x["profile_id"] for x in data["info"]["profile_list"]]
+ total_profiles = len(profile_strings)
-success = _eduvpn.register(debug=True)
+ # Create a list of the strings to standard output
+ for idx, profile in enumerate(profile_strings):
+ print(f"{idx+1}. {profile}")
-if not success:
- print("failed to register")
+ # Get the profile index from the user
+ profile_index = ask_profile_input(total_profiles)
-print(_eduvpn.get_disco())
+ # Set the profile with the index
+ _eduvpn.set_profile(profile_strings[profile_index])
-config, error = _eduvpn.get_config_institute_access("https://eduvpn.jwijenbergh.com")
-if error:
- print("Got connect error", error)
+# The main entry point
+if __name__ == "__main__":
+ _eduvpn = eduvpn.EduVPN("org.eduvpn.app.linux", "configs")
+ setup_callbacks(_eduvpn)
-print(config)
+ # Register with the eduVPN-common library
+ try:
+ _eduvpn.register(debug=True)
+ except Exception as e:
+ print("Failed registering:", e)
+
+ server = input(
+ "Which Institute Access server do you want to connect to? (e.g. https://eduvpn.example.com): "
+ )
+
+ # Ensure we have a valid http prefix
+ if not server.startswith("http"):
+ # https by default
+ server = "https://" + server
+
+ # Get a Wireguard/OpenVPN config
+ try:
+ config, config_type = _eduvpn.get_config_institute_access(server)
+ except Exception as e:
+ print("Failed to connect:", e)
+ print(f"Got a config with type: {config_type} and contents:\n{config}")
+
+ # Set the internal FSM state to connected
+ try:
+ _eduvpn.set_connected()
+ except Exception as e:
+ print("Failed to set connected:", e)
+
+ # Save and exit
+ _eduvpn.deregister()