forked from laobubu/ssland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
executable file
·106 lines (97 loc) · 3.22 KB
/
cli.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
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# CLI
#
import config, user
import traffic
import ssmgr
from utils import *
import sys, os, getpass
def print_help():
print(
'''
Usage:
user list
user add [username] [password] [sskey]
user del username [username2] [...]
user suspend username [username2] [...]
user unsuspend username [username2] [...]
user passwd [username] [password]
user sskey [username] [sskey]
sys update
(tx = traffic)
tx query [username]
tx update
''')
sys.exit(0)
def run(scope, action, argv):
if scope == 'user':
if action == 'list':
print("id\tusername\tsuspended\tport\tsskey")
for u in user.get_all():
print('\t'.join(( str(item) for item in
(u.id, u.username, 'True' if u.suspended else 'False', u.get_port(), u.sskey)
)))
elif action == 'add':
username = argv[0] if len(argv) > 0 else raw_input('Username: ')
password = argv[1] if len(argv) > 1 else getpass.getpass()
sskey = argv[2] if len(argv) > 2 else getpass.getpass('Shadowsocks Key: ')
u = user.User()
u.username = username
u.set_password(password)
u.sskey = sskey
u.create()
u.write()
elif action == 'del':
user.delete_users(*argv)
elif action in ['suspend', 'unsuspend']:
user.batch_update(*argv, suspended=(1 if action == 'suspend' else 0))
elif action == 'passwd':
username = argv[0] if len(argv) > 0 else raw_input('Username: ')
password = argv[1] if len(argv) > 1 else getpass.getpass()
u = user.get_by_username(username)
u.set_password(password)
u.write()
elif action == 'sskey':
username = argv[0] if len(argv) > 0 else raw_input('Username: ')
sskey = argv[1] if len(argv) > 1 else raw_input('Shadowsocks Key: ')
u = user.get_by_username(username)
u.sskey = sskey
u.write()
else:
print_help()
elif scope == 'sys':
if action == 'update':
ssmgr.update_and_restart()
get_stdout(["./web.py", "-d", "restart"])
elif action == 'init':
# RUN THIS WHEN SYSTEM BOOTS ONLY !
traffic.update_iptables()
get_stdout(["./web.py", "-d", "restart"])
ssmgr.update_and_restart()
else:
print_help()
elif scope == 'tx' or scope == 'traffic':
if action == 'query':
un = {}
uid = -1
for u in user.get_all():
un[u.id] = u.username
if len(argv) > 0 and u.username == argv[0] :
uid = u.id
for r in traffic.query(uid=uid,sum=traffic.QS_DAY):
print("%s\t%s\t%s"%(un[r[0]], r[3], sizeof_fmt(r[2])))
elif action == 'update':
traffic.stat()
else:
print_help()
else:
print_help()
if __name__ == '__main__':
if len(sys.argv) < 3:
print_help()
scope = sys.argv[1]
action = sys.argv[2]
argv = sys.argv[3:]
run(scope, action, argv)