-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
86 lines (73 loc) · 2.27 KB
/
run.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
from __future__ import unicode_literals
import click
import traceback, sys, json
if sys.version[0] == '3':
raw_input = input
from tgbot import TgBot
from tkbot import TkBot
from utils.user import User
from utils.tunnel import Tunnel
def loadConfig(path):
with open(path) as f:
config = json.loads(f.read())
return config
def loadAdmin(config):
configList = config['admin']
admins = []
for each in configList:
admins.append(User.fromSave(each))
return admins
def loadBind(config):
configList = config['bindTgTk']
tunnels = []
for each in configList:
tunnels.append(Tunnel.fromSave(each))
return tunnels
def writeAdmin(config, admins):
config['admin'] = []
for each in admins:
config['admin'].append(each.toSave())
def writeBind(config, tunnels):
config['bindTgTk'] = []
for each in tunnels:
config['bindTgTk'].append(each.toSave())
def writeConfig(config, admins, tunnels, path):
writeAdmin(config, admins)
writeBind(config, tunnels)
data = json.dumps(config, indent=4)
with open(path, 'w'): pass # Clear file content
with open(path, 'w') as f:
f.write(data)
@click.command()
@click.option('--start', default=False, type=bool)
@click.option('--path', default='./config.json', type=str)
@click.option('--debug', default=False, type=bool)
def run(start, path, debug):
config = loadConfig(path)
admins = loadAdmin(config)
tunnels = loadBind(config)
tgBot = TgBot(config=config['telegramBot'], admins=admins, tunnels=tunnels, debug=debug)
tkBot = TkBot(admins=admins, tunnels=tunnels, debug=debug)
if start:
tgBot.start()
tkBot.start()
while True:
try:
userInput = raw_input('> ')
if 'start' in userInput:
tgBot.start()
tkBot.start()
elif 'stop' in userInput:
tgBot.stop()
tkBot.stop()
writeConfig(config=config, admins=admins, tunnels=tunnels, path=path)
sys.exit(0)
except KeyboardInterrupt:
tgBot.stop()
tkBot.stop()
sys.exit(0)
except Exception as e:
if debug:
traceback.print_exc()
if __name__ == "__main__":
run()