-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
93 lines (81 loc) · 2.65 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import time
import threading
import logging
import os
import argparse
from eventlet.hubs import epolls, kqueue, selects
from dns import dnssec, e164, hash, namedict, tsigkeyring, update, version, zone
from backend.tor import Tor
from backend.api import MainApi
logging.basicConfig(
format='%(asctime)s [%(levelname)-8s] (%(filename)-10s:%(lineno)3d) (%(name)s) %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.DEBUG
)
logging.getLogger("engineio.server").setLevel(logging.WARNING)
def main(**kwargs):
main_folder = os.path.expanduser(kwargs['data_folder'])
if not os.path.exists(main_folder):
os.makedirs(main_folder)
logging.info(f"Data directory created at {main_folder}")
logging.info(f"args: {args.__dict__}")
logging.info("Starting services...")
tor_service = Tor()
route = tor_service.start_service(**kwargs)
logging.info("Onion service started")
flask_api = MainApi(route, **kwargs)
# Start API service in new thread
api_thread = threading.Thread(target=flask_api.start, args=(kwargs['port'],))
api_thread.daemon = True
api_thread.start()
logging.info("API started")
while True:
try:
time.sleep(24*60*60) # Wait 24h
except KeyboardInterrupt:
logging.info("Stopping services...")
flask_api.stop()
logging.debug("API stopped")
tor_service.stop_service()
logging.debug("Onion service stopped")
logging.info("Services stopped, bye")
break
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Chat app using onion hidden services.')
parser.add_argument(
'--port',
type=int,
default=5000,
dest='port',
help='local port to bind flask app (default: 5000)'
)
parser.add_argument(
'--onion_port',
type=int,
default=80,
dest='onion_port',
help='port to bind onion hidden service (default: 80)'
)
parser.add_argument(
'--onion_control_port',
type=int,
default=9051,
dest='onion_control_port',
help='port to bind onion hidden service (default: 9051)'
)
parser.add_argument(
'--onion_socks_port',
type=int,
default=9050,
dest='onion_socks_port',
help='port to bind onion hidden service (default: 9050)'
)
parser.add_argument(
'--folder',
type=str,
default='~/calsotchat',
dest='data_folder',
help='Directory to save data between executions (default: ~/calsotchat)'
)
args = parser.parse_args()
main(**args.__dict__)