-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
64 lines (52 loc) · 1.43 KB
/
functions.py
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
import subprocess
from time import time
import ipaddress
import orjson
from loguru import logger
def fprint(message):
print(message, flush=True)
def is_valid_ip(ipaddr):
try:
ipaddress.ip_address(ipaddr)
return True
except ValueError:
return False
def get_tailscale_data() -> dict:
try:
output = subprocess.check_output(
["tailscale", "status", "--json"],
stderr=subprocess.STDOUT,
universal_newlines=True,
)
return orjson.loads(output.strip().encode("utf8"))
except subprocess.CalledProcessError as exc:
logger.error(exc)
return {}
def get_tailscale_ip4() -> str:
try:
output = subprocess.check_output(
["tailscale", "ip", "-4"],
stderr=subprocess.STDOUT,
universal_newlines=True,
)
ip = output.strip()
if is_valid_ip(ip):
return ip
except subprocess.CalledProcessError as e:
logger.error(e)
return ""
def get_tailscale_ip6() -> str:
try:
output = subprocess.check_output(
["tailscale", "ip", "-6"],
stderr=subprocess.STDOUT,
universal_newlines=True,
)
ip = output.strip()
if is_valid_ip(ip):
return ip
except subprocess.CalledProcessError as e:
logger.error(e)
return ""
def a_record(ip):
return "A" if ":" not in ip else "AAAA"