#!/usr/bin/python
#coding=utf-8
import os
import json
import datetime
import string
from random import choice
from pprint import pprint
from optparse import OptionParser
“””
操作前备份 /etc/shadowsocks/config.json
新加用户操用
- 修改 /etc/shadowsocks/config.json
- 重启 /etc/init.d/shadowsocks restart
- 加防火墙规则
删除用户操用
- 修改 /etc/shadowsocks/config.json
- 重启 /etc/init.d/shadowsocks restart
- 删防火墙规则
“””
shadowsocks_cnf = “/etc/shadowsocks/config.json”
passwd_len = 8
def create_new_passwd(length=8, chars=string.ascii_letters + string.digits):
return ''.join([choice(chars) for i in range(length)])
def store(data):
with open(shadowsocks_cnf, ‘w’) as json_file:
json_file.write(json.dumps(data))
def load():
with open(shadowsocks_cnf) as json_file:
data = json.load(json_file)
return data
def store_new_user(port,passwd):
data = load()
data['port_password'][port]=passwd
store(data)
def remove_old_user(port):
try:
data = load()
old_passwd = data['port_password'].pop(port)
store(data)
return {"port":port,"passwd":old_passwd}
except:
return False
def get_next_port():
data = load()
ports = data['port_password'].keys()
return int(max(ports))+1
def user_list():
data = load()
users = data[‘port_password’]
return users
def add_user():
next_port = get_next_port()
next_passwd = create_new_passwd(length=passwd_len)
store_new_user(next_port, next_passwd)
return {"port":next_port,"passwd":next_passwd}
def del_user(port):
return remove_old_user(port)
def reload_ss():
cmd = "/etc/init.d/shadowsocks restart"
os.popen(cmd)
def iptables_add_new_port(port):
cmd = "iptables -A INPUT -p tcp --dport %s -j ACCEPT && iptables -A OUTPUT -p tcp --sport %s -j ACCEPT && service iptables save"%(port,port)
os.popen(cmd)
def iptables_del_old_port(port):
cmd = "iptables -D INPUT -p tcp --dport %s -j ACCEPT && iptables -D OUTPUT -p tcp --sport %s -j ACCEPT && service iptables save"%(port,port)
os.popen(cmd)
def bak_cnf():
bak_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
cmd = "cp /etc/shadowsocks/config.json /etc/shadowsocks/config.json.bak_%s" % bak_time
os.popen(cmd)
if name == “main“:
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-p", "--userlist", action="store_true",
dest="userlist",
default=False,
help="python ss_manage.py --userlist")
parser.add_option("-z", "--adduser", action="store_true",
dest="adduser",
default=False,
help="python ss_manage.py --adduser")
parser.add_option("-d", "--deluser", action="store",
dest="deluser",
default=False,
help="python ss_manage.py --deluser port")
(options, args) = parser.parse_args()
if options.userlist == True:
users = user_list()
print "用户列表 :" ,len(users)
print "-"*30
for k,v in users.items():
print "port:",k,"passwd:",v
elif options.adduser == True:
bak_cnf()
new_user = add_user()
reload_ss()
iptables_add_new_port(new_user["port"])
print "添加用户:"
print "-" * 30
print "port:", new_user["port"], "passwd:", new_user["passwd"]
elif options.deluser:
bak_cnf()
old_user = del_user(options.deluser)
reload_ss()
if old_user == False:
print "删除用户:"
print "-" * 30
print("端口%s不在列表中"%options.deluser)
else:
print "删除用户:"
print "-" * 30
print "port:", old_user["port"], "passwd:", old_user["passwd"]
iptables_del_old_port(old_user["port"])
else:
parser.print_help()