def update_conf(): ''' Update Shadowsocks Config file. ''' try: j = json.load(open(config.SS_CONF, 'r')) except: # Cannot read config file. Use default print( "Cannot read Shadowsocks config file. File not exist or SSLand has no permission.", file=sys.stderr) j = { "server": "0.0.0.0", "port_password": { "6789": "aba" }, "timeout": 300, "method": "aes-256-cfb" } pp = {} for u in user.get_all(): if u.suspended: continue port = str(config.user_port(u.id)) key = u.sskey if len(key) > 0: pp[port] = key j['port_password'] = pp json.dump(j, open(config.SS_CONF, 'w')) return j
def stat(): ''' Get the stat data and storage into database. Add IPTABLES rules if necessary. ''' cs, ec = get_stdout(IPTABLES + ('-nxvL', 'SSLAND')) # get current stat if ec == 3: return False # No privilege if ec == 1: # chain not found # create the chain get_stdout(IPTABLES + ('-N','SSLAND')) get_stdout(IPTABLES + ('-I','OUTPUT','1','-j','SSLAND')) get_stdout(IPTABLES + ('-Z','SSLAND')) # empty IPTABLES stat t = {} # dict PORT=>(pkgs, bytes) for i in re.findall(r"^\s*(\d+)\s+(\d+).+dpt:(\d+)", cs, re.M): # i = Pkgs Traffic(byte) port t[int(i[2])] = ( int(i[0]) , int(i[1]) ) query = [] users = user.get_all() for u in users: if u.suspended: continue port = config.user_port(u.id) if port in t: ti = t[port] query.append((u.id, ti[0], ti[1])) else: get_stdout(IPTABLES + ('-A','SSLAND','-p','tcp','--dport',str(port))) cursor = database.conn.cursor() cursor.executemany('INSERT INTO traffic(user,packages,traffic) VALUES (?, ?, ?)', query) cursor.close() database.conn.commit()
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', config.user_port(u.id), u.sskey) ))) elif action == 'add': username = argv[0] if len(argv) > 0 else raw_input('Username: '******'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: '******'sskey': username = argv[0] if len(argv) > 0 else raw_input('Username: '******'Shadowsocks Key: ') u = user.get_by_username(username) u.sskey = sskey u.write() else: print_help() elif scope == 'sys': if action == 'update': import ssmgr ssmgr.update_and_restart() try: import os with open(config.TMP_ROOT + "/ssland.web.pid", 'r') as f: pid = int(f.read()) os.kill(pid, 0) from utils import get_stdout get_stdout(["./web.py", "-d", "restart"]) except: pass else: print_help() else: print_help()
def update_conf(): ''' Update Shadowsocks Config file. ''' try: j = json.load(open(config.SS_CONF, 'r')) except: # Cannot read config file. Use default print("Cannot read Shadowsocks config file. File not exist or SSLand has no permission.", file=sys.stderr) j = {"server": "0.0.0.0", "port_password": {"6789": "aba"}, "timeout": 300, "method": "aes-256-cfb"} pp = {} for u in user.get_all(): if u.suspended: continue port = str(config.user_port(u.id)) key = u.sskey if len(key) > 0: pp[port] = key j['port_password'] = pp json.dump(j, open(config.SS_CONF, 'w')) return j
def get_port(self): default_port = config.user_port(self.id) try: return int(self.get_meta("port")) or default_port except: return default_port