supper local json database for mu
This commit is contained in:
parent
8dac9faa28
commit
9e288c9ac0
5 changed files with 87 additions and 34 deletions
|
@ -1,10 +1,10 @@
|
||||||
# Config
|
# Config
|
||||||
TRANSFER_MUL = 1.0
|
TRANSFER_MUL = 1.0
|
||||||
NODE_ID = 1
|
NODE_ID = 1
|
||||||
API_INTERFACE = 'sspanelv2' #sspanelv2, sspanelv3, sspanelv3ssr, muapiv2(not support)
|
API_INTERFACE = 'sspanelv2' #mudbjson, sspanelv2, sspanelv3, sspanelv3ssr, muapiv2(not support)
|
||||||
|
|
||||||
# Mysql
|
# Mysql
|
||||||
MYSQL_HOST = 'mdss.mengsky.net'
|
MYSQL_HOST = '127.0.0.1'
|
||||||
MYSQL_PORT = 3306
|
MYSQL_PORT = 3306
|
||||||
MYSQL_USER = 'ss'
|
MYSQL_USER = 'ss'
|
||||||
MYSQL_PASS = 'ss'
|
MYSQL_PASS = 'ss'
|
||||||
|
@ -12,7 +12,7 @@ MYSQL_DB = 'shadowsocks'
|
||||||
MYSQL_UPDATE_TIME = 60
|
MYSQL_UPDATE_TIME = 60
|
||||||
|
|
||||||
# API
|
# API
|
||||||
API_HOST = 'breakwa11.moe'
|
API_HOST = '127.0.0.1'
|
||||||
API_PORT = 80
|
API_PORT = 80
|
||||||
API_PATH = '/mu/v2/'
|
API_PATH = '/mu/v2/'
|
||||||
API_TOKEN = 'abcdef'
|
API_TOKEN = 'abcdef'
|
||||||
|
|
|
@ -6,24 +6,17 @@ import time
|
||||||
import sys
|
import sys
|
||||||
from server_pool import ServerPool
|
from server_pool import ServerPool
|
||||||
import traceback
|
import traceback
|
||||||
from shadowsocks import common
|
from shadowsocks import common, shell
|
||||||
from configloader import load_config, get_config
|
from configloader import load_config, get_config
|
||||||
|
|
||||||
|
db_instance = None
|
||||||
|
|
||||||
class DbTransfer(object):
|
class DbTransfer(object):
|
||||||
|
|
||||||
instance = None
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
import threading
|
import threading
|
||||||
self.last_get_transfer = {}
|
self.last_get_transfer = {}
|
||||||
self.event = threading.Event()
|
self.event = threading.Event()
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_instance():
|
|
||||||
if DbTransfer.instance is None:
|
|
||||||
DbTransfer.instance = DbTransfer()
|
|
||||||
return DbTransfer.instance
|
|
||||||
|
|
||||||
def update_all_user(self, dt_transfer):
|
def update_all_user(self, dt_transfer):
|
||||||
import cymysql
|
import cymysql
|
||||||
query_head = 'UPDATE user'
|
query_head = 'UPDATE user'
|
||||||
|
@ -83,8 +76,7 @@ class DbTransfer(object):
|
||||||
self.update_all_user(dt_transfer)
|
self.update_all_user(dt_transfer)
|
||||||
self.last_get_transfer = curr_transfer
|
self.last_get_transfer = curr_transfer
|
||||||
|
|
||||||
@staticmethod
|
def pull_db_all_user(self):
|
||||||
def pull_db_all_user():
|
|
||||||
import cymysql
|
import cymysql
|
||||||
#数据库所有用户信息
|
#数据库所有用户信息
|
||||||
try:
|
try:
|
||||||
|
@ -107,8 +99,7 @@ class DbTransfer(object):
|
||||||
conn.close()
|
conn.close()
|
||||||
return rows
|
return rows
|
||||||
|
|
||||||
@staticmethod
|
def del_server_out_of_bound_safe(self, last_rows, rows):
|
||||||
def del_server_out_of_bound_safe(last_rows, rows):
|
|
||||||
#停止超流量的服务
|
#停止超流量的服务
|
||||||
#启动没超流量的服务
|
#启动没超流量的服务
|
||||||
#需要动态载入switchrule,以便实时修改规则
|
#需要动态载入switchrule,以便实时修改规则
|
||||||
|
@ -180,7 +171,7 @@ class DbTransfer(object):
|
||||||
|
|
||||||
if len(new_servers) > 0:
|
if len(new_servers) > 0:
|
||||||
from shadowsocks import eventloop
|
from shadowsocks import eventloop
|
||||||
DbTransfer.get_instance().event.wait(eventloop.TIMEOUT_PRECISION)
|
self.event.wait(eventloop.TIMEOUT_PRECISION)
|
||||||
for port in new_servers.keys():
|
for port in new_servers.keys():
|
||||||
passwd, cfg = new_servers[port]
|
passwd, cfg = new_servers[port]
|
||||||
logging.info('db start server at port [%s] pass [%s]' % (port, passwd))
|
logging.info('db start server at port [%s] pass [%s]' % (port, passwd))
|
||||||
|
@ -196,32 +187,68 @@ class DbTransfer(object):
|
||||||
ServerPool.get_instance().cb_del_server(port)
|
ServerPool.get_instance().cb_del_server(port)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def thread_db():
|
def thread_db(obj):
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
global db_instance
|
||||||
timeout = 60
|
timeout = 60
|
||||||
socket.setdefaulttimeout(timeout)
|
socket.setdefaulttimeout(timeout)
|
||||||
last_rows = []
|
last_rows = []
|
||||||
|
db_instance = obj()
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
load_config()
|
load_config()
|
||||||
try:
|
try:
|
||||||
DbTransfer.get_instance().push_db_all_user()
|
db_instance.push_db_all_user()
|
||||||
rows = DbTransfer.get_instance().pull_db_all_user()
|
rows = db_instance.pull_db_all_user()
|
||||||
DbTransfer.del_server_out_of_bound_safe(last_rows, rows)
|
db_instance.del_server_out_of_bound_safe(last_rows, rows)
|
||||||
last_rows = rows
|
last_rows = rows
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
trace = traceback.format_exc()
|
trace = traceback.format_exc()
|
||||||
logging.error(trace)
|
logging.error(trace)
|
||||||
#logging.warn('db thread except:%s' % e)
|
#logging.warn('db thread except:%s' % e)
|
||||||
if DbTransfer.get_instance().event.wait(get_config().MYSQL_UPDATE_TIME) or not ServerPool.get_instance().thread.is_alive():
|
if db_instance.event.wait(get_config().MYSQL_UPDATE_TIME) or not ServerPool.get_instance().thread.is_alive():
|
||||||
break
|
break
|
||||||
except KeyboardInterrupt as e:
|
except KeyboardInterrupt as e:
|
||||||
pass
|
pass
|
||||||
DbTransfer.del_servers()
|
db_instance.del_servers()
|
||||||
ServerPool.get_instance().stop()
|
ServerPool.get_instance().stop()
|
||||||
|
db_instance = None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def thread_db_stop():
|
def thread_db_stop():
|
||||||
DbTransfer.get_instance().event.set()
|
global db_instance
|
||||||
|
db_instance.event.set()
|
||||||
|
|
||||||
|
class MuJsonTransfer(DbTransfer):
|
||||||
|
def __init__(self):
|
||||||
|
super(MuJsonTransfer, self).__init__()
|
||||||
|
|
||||||
|
def update_all_user(self, dt_transfer):
|
||||||
|
import json
|
||||||
|
rows = None
|
||||||
|
|
||||||
|
config_path = "mudb.json"
|
||||||
|
with open(config_path, 'r+') as f:
|
||||||
|
rows = shell.parse_json_in_str(f.read().decode('utf8'))
|
||||||
|
for row in rows:
|
||||||
|
if "port" in row:
|
||||||
|
port = row["port"]
|
||||||
|
if port in dt_transfer:
|
||||||
|
row["u"] += dt_transfer[port][0]
|
||||||
|
row["d"] += dt_transfer[port][1]
|
||||||
|
|
||||||
|
if rows:
|
||||||
|
output = json.dumps(rows, sort_keys=True, indent=4, separators=(',', ': '))
|
||||||
|
with open(config_path, 'w') as f:
|
||||||
|
f.write(output)
|
||||||
|
|
||||||
|
def pull_db_all_user(self):
|
||||||
|
rows = None
|
||||||
|
|
||||||
|
config_path = "mudb.json"
|
||||||
|
with open(config_path, 'r+') as f:
|
||||||
|
rows = shell.parse_json_in_str(f.read().decode('utf8'))
|
||||||
|
|
||||||
|
return rows
|
||||||
|
|
||||||
|
|
26
mudb.json
Normal file
26
mudb.json
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"user": "admin",
|
||||||
|
"port": 443,
|
||||||
|
"u": 0,
|
||||||
|
"d": 0,
|
||||||
|
"transfer_enable": 1125899906842624,
|
||||||
|
"passwd": "admin",
|
||||||
|
"method": "aes-128-cfb",
|
||||||
|
"protocol": "auth_sha1_v2_compatible",
|
||||||
|
"obfs": "tls1.2_ticket_auth_compatible",
|
||||||
|
"enable": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"user": "user001",
|
||||||
|
"port": 10001,
|
||||||
|
"u": 0,
|
||||||
|
"d": 0,
|
||||||
|
"transfer_enable": 1099511627776,
|
||||||
|
"passwd": "abc123",
|
||||||
|
"method": "rc4-md5",
|
||||||
|
"protocol": "auth_sha1_v2_compatible",
|
||||||
|
"obfs": "http_post_compatible",
|
||||||
|
"enable": true
|
||||||
|
}
|
||||||
|
]
|
17
server.py
17
server.py
|
@ -27,27 +27,28 @@ if __name__ == '__main__':
|
||||||
import server_pool
|
import server_pool
|
||||||
import db_transfer
|
import db_transfer
|
||||||
from shadowsocks import shell
|
from shadowsocks import shell
|
||||||
|
from configloader import load_config, get_config
|
||||||
#def test():
|
|
||||||
# thread.start_new_thread(DbTransfer.thread_db, ())
|
|
||||||
# Api.web_server()
|
|
||||||
|
|
||||||
class MainThread(threading.Thread):
|
class MainThread(threading.Thread):
|
||||||
def __init__(self):
|
def __init__(self, obj):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
|
self.obj = obj
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
db_transfer.DbTransfer.thread_db()
|
self.obj.thread_db(self.obj)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
db_transfer.DbTransfer.thread_db_stop()
|
self.obj.thread_db_stop()
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
shell.check_python()
|
shell.check_python()
|
||||||
if False:
|
if False:
|
||||||
db_transfer.DbTransfer.thread_db()
|
db_transfer.DbTransfer.thread_db()
|
||||||
else:
|
else:
|
||||||
thread = MainThread()
|
if get_config().API_INTERFACE == 'mudbjson':
|
||||||
|
thread = MainThread(db_transfer.MuJsonTransfer)
|
||||||
|
else:
|
||||||
|
thread = MainThread(db_transfer.DbTransfer)
|
||||||
thread.start()
|
thread.start()
|
||||||
try:
|
try:
|
||||||
while thread.is_alive():
|
while thread.is_alive():
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
from configloader import load_config, get_config
|
from configloader import load_config, get_config
|
||||||
|
|
||||||
def getKeys():
|
def getKeys():
|
||||||
load_config()
|
|
||||||
key_list = ['port', 'u', 'd', 'transfer_enable', 'passwd', 'enable' ]
|
key_list = ['port', 'u', 'd', 'transfer_enable', 'passwd', 'enable' ]
|
||||||
if get_config().API_INTERFACE == 'sspanelv3':
|
if get_config().API_INTERFACE == 'sspanelv3':
|
||||||
key_list += ['method']
|
key_list += ['method']
|
||||||
|
|
Loading…
Add table
Reference in a new issue