-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
79 lines (55 loc) · 1.81 KB
/
config.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import asyncio
import json
import os
import time
import socket
from tornado.httpclient import AsyncHTTPClient
from ops.data_ops import get_home
from hashing import create_nonce
def config_found(file=f"{get_home()}/private/config.dat"):
if os.path.isfile(file):
return True
else:
return False
def get_timestamp():
return float(time.time())
def get_timestamp_seconds():
return int(time.time_ns() / 1000000000)
def get_protcol():
return 2
def get_port():
return 9173
def test_self_port(ip, port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(0.3)
result = sock.connect_ex((ip, port))
if not result:
return "Yes! Hooray!"
else:
return "No, ports are blocked"
async def get_public_ip():
http_client = AsyncHTTPClient()
url = "https://api.ipify.org"
ip = await http_client.fetch(url, request_timeout=5)
return ip.body.decode()
def get_config(config_path: str = f"{get_home()}/private/config.dat"):
with open(config_path) as infile:
return json.loads(infile.read())
def update_config(new_config: dict, config_path: str = f"{get_home()}/private/config.dat"):
config = get_config()
for key, value in new_config.items():
config[key] = value
with open(config_path, "w") as outfile:
json.dump(config, outfile)
def create_config(config_path: str = f"{get_home()}/private/config.dat"):
config_contents = {
"port": get_port(),
"ip": asyncio.run(get_public_ip()),
"protocol": get_protcol(),
"server_key": create_nonce(length=64),
}
if not os.path.exists(config_path):
with open(config_path, "w") as outfile:
json.dump(config_contents, outfile)
if __name__ == "__main__":
create_config()