|
1 | 1 | # pylint: disable=wrong-import-order |
2 | 2 |
|
3 | | -import requests, json, os, logging |
| 3 | +import requests, json, os, logging, yaml |
4 | 4 | from cryptography.fernet import Fernet |
5 | 5 | from datetime import timedelta, date |
6 | 6 | from dateutil import parser |
|
19 | 19 | ################################################################## |
20 | 20 | # Functions related to HEADSCALE and API KEYS |
21 | 21 | ################################################################## |
22 | | - |
23 | | -def get_url(): return os.environ['HS_SERVER'] |
| 22 | +def get_url(inpage=False): |
| 23 | + if not inpage: |
| 24 | + return os.environ['HS_SERVER'] |
| 25 | + config_file = "" |
| 26 | + try: |
| 27 | + config_file = open("/etc/headscale/config.yml", "r") |
| 28 | + app.logger.info("Opening /etc/headscale/config.yml") |
| 29 | + except: |
| 30 | + config_file = open("/etc/headscale/config.yaml", "r") |
| 31 | + app.logger.info("Opening /etc/headscale/config.yaml") |
| 32 | + config_yaml = yaml.safe_load(config_file) |
| 33 | + if "server_url" in config_yaml: |
| 34 | + return str(config_yaml["server_url"]) |
| 35 | + app.logge.warning("Failed to find server_url in the config. Falling back to ENV variable") |
| 36 | + return os.environ['HS_SERVER'] |
24 | 37 |
|
25 | 38 | def set_api_key(api_key): |
26 | 39 | # User-set encryption key |
@@ -194,9 +207,8 @@ def move_user(url, api_key, machine_id, new_user): |
194 | 207 | return response.json() |
195 | 208 |
|
196 | 209 | def update_route(url, api_key, route_id, current_state): |
197 | | - action = "" |
198 | | - if current_state == "True": action = "disable" |
199 | | - if current_state == "False": action = "enable" |
| 210 | + action = "disable" if current_state == "True" else "enable" |
| 211 | + |
200 | 212 | app.logger.info("Updating Route %s: Action: %s", str(route_id), str(action)) |
201 | 213 |
|
202 | 214 | # Debug |
@@ -299,9 +311,8 @@ def get_routes(url, api_key): |
299 | 311 | } |
300 | 312 | ) |
301 | 313 | return response.json() |
302 | | - |
303 | 314 | ################################################################## |
304 | | -# Functions related to NAMESPACES |
| 315 | +# Functions related to USERS |
305 | 316 | ################################################################## |
306 | 317 |
|
307 | 318 | # Get all users in use |
@@ -370,7 +381,7 @@ def add_user(url, api_key, data): |
370 | 381 | return {"status": status, "body": response.json()} |
371 | 382 |
|
372 | 383 | ################################################################## |
373 | | -# Functions related to PREAUTH KEYS in NAMESPACES |
| 384 | +# Functions related to PREAUTH KEYS in USERS |
374 | 385 | ################################################################## |
375 | 386 |
|
376 | 387 | # Get all PreAuth keys associated with a user "user_name" |
|
0 commit comments