Browse Source

use db key

dev
breakwa11 10 years ago
parent
commit
7c2fe9fd56
  1. 53
      db_transfer.py
  2. 4
      server.py
  3. 14
      server_pool.py
  4. 3
      switchrule.py

53
db_transfer.py

@ -7,6 +7,7 @@ import time
import sys import sys
from server_pool import ServerPool from server_pool import ServerPool
import Config import Config
import traceback
class DbTransfer(object): class DbTransfer(object):
@ -75,13 +76,17 @@ class DbTransfer(object):
@staticmethod @staticmethod
def pull_db_all_user(): def pull_db_all_user():
#数据库所有用户信息 #数据库所有用户信息
keys = ['port', 'u', 'd', 'transfer_enable', 'passwd', 'switch', 'enable', 'plan' ]
conn = cymysql.connect(host=Config.MYSQL_HOST, port=Config.MYSQL_PORT, user=Config.MYSQL_USER, conn = cymysql.connect(host=Config.MYSQL_HOST, port=Config.MYSQL_PORT, user=Config.MYSQL_USER,
passwd=Config.MYSQL_PASS, db=Config.MYSQL_DB, charset='utf8') passwd=Config.MYSQL_PASS, db=Config.MYSQL_DB, charset='utf8')
cur = conn.cursor() cur = conn.cursor()
cur.execute("SELECT port, u, d, transfer_enable, passwd, switch, enable, plan FROM user") cur.execute("SELECT " + ','.join(keys) + " FROM user")
rows = [] rows = []
for r in cur.fetchall(): for r in cur.fetchall():
rows.append(list(r)) d = {}
for column in xrange(len(keys)):
d[keys[column]] = r[column]
rows.append(d)
cur.close() cur.close()
conn.close() conn.close()
return rows return rows
@ -95,33 +100,34 @@ class DbTransfer(object):
for row in rows: for row in rows:
try: try:
import switchrule import switchrule
allow = switchrule.isTurnOn(row[7], row[5]) and row[6] == 1 and row[1] + row[2] < row[3] allow = switchrule.isTurnOn(row) and row['enable'] == 1 and row['u'] + row['d'] < row['transfer_enable']
except Exception, e: except Exception, e:
allow = False allow = False
cur_servers[row[0]] = row[4] port = row['port']
passwd = row['passwd']
cur_servers[port] = passwd
if ServerPool.get_instance().server_is_run(row[0]) > 0: if ServerPool.get_instance().server_is_run(port) > 0:
if not allow: if not allow:
logging.info('db stop server at port [%s]' % (row[0])) logging.info('db stop server at port [%s]' % (port,))
ServerPool.get_instance().del_server(row[0]) ServerPool.get_instance().del_server(port)
elif (row[0] in ServerPool.get_instance().tcp_servers_pool and ServerPool.get_instance().tcp_servers_pool[row[0]]._config['password'] != row[4]) \ elif (port in ServerPool.get_instance().tcp_servers_pool and ServerPool.get_instance().tcp_servers_pool[port]._config['password'] != passwd) \
or (row[0] in ServerPool.get_instance().tcp_ipv6_servers_pool and ServerPool.get_instance().tcp_ipv6_servers_pool[row[0]]._config['password'] != row[4]): or (port in ServerPool.get_instance().tcp_ipv6_servers_pool and ServerPool.get_instance().tcp_ipv6_servers_pool[port]._config['password'] != passwd):
#password changed #password changed
logging.info('db stop server at port [%s] reason: password changed' % (row[0])) logging.info('db stop server at port [%s] reason: password changed' % (port,))
ServerPool.get_instance().del_server(row[0]) ServerPool.get_instance().del_server(port)
elif ServerPool.get_instance().server_run_status(row[0]) is False:
if allow: if allow and ServerPool.get_instance().server_is_run(port) == 0:
logging.info('db start server at port [%s] pass [%s]' % (row[0], row[4])) logging.info('db start server at port [%s] pass [%s]' % (port, passwd))
ServerPool.get_instance().new_server(row[0], row[4]) ServerPool.get_instance().new_server(port, passwd)
for row in last_rows: for row in last_rows:
if row[0] in cur_servers: if row['port'] in cur_servers:
if row[4] == cur_servers[row[0]]:
pass pass
else: else:
logging.info('db stop server at port [%s] reason: port not exist' % (row[0])) logging.info('db stop server at port [%s] reason: port not exist' % (row['port']))
ServerPool.get_instance().del_server(row[0]) ServerPool.get_instance().del_server(row['port'])
@staticmethod @staticmethod
def thread_db(): def thread_db():
@ -131,18 +137,15 @@ class DbTransfer(object):
socket.setdefaulttimeout(timeout) socket.setdefaulttimeout(timeout)
last_rows = [] last_rows = []
while True: while True:
#logging.warn('db loop')
try: try:
DbTransfer.get_instance().push_db_all_user() DbTransfer.get_instance().push_db_all_user()
rows = DbTransfer.get_instance().pull_db_all_user() rows = DbTransfer.get_instance().pull_db_all_user()
DbTransfer.del_server_out_of_bound_safe(last_rows, rows) DbTransfer.del_server_out_of_bound_safe(last_rows, rows)
last_rows = rows last_rows = rows
except Exception as e: except Exception as e:
logging.warn('db thread except:%s' % e) trace = traceback.format_exc()
logging.error(trace)
#logging.warn('db thread except:%s' % e)
finally: finally:
time.sleep(15) time.sleep(15)
#SQLData.pull_db_all_user()
#print DbTransfer.get_instance().test()

4
server.py

@ -1,6 +1,4 @@
#!/usr/bin/env python #!/usr/bin/python
# -*- coding: utf-8 -*-
import time import time
import sys import sys
import thread import thread

14
server_pool.py

@ -24,7 +24,7 @@
import os import os
import logging import logging
import time import time
from shadowsocks import utils from shadowsocks import shell
from shadowsocks import eventloop from shadowsocks import eventloop
from shadowsocks import tcprelay from shadowsocks import tcprelay
from shadowsocks import udprelay from shadowsocks import udprelay
@ -41,9 +41,9 @@ class ServerPool(object):
instance = None instance = None
def __init__(self): def __init__(self):
utils.check_python() shell.check_python()
self.config = utils.get_config(False) self.config = shell.get_config(False)
utils.print_shadowsocks() shell.print_shadowsocks()
self.dns_resolver = asyncdns.DNSResolver() self.dns_resolver = asyncdns.DNSResolver()
self.mgr = asyncmgr.ServerMgr() self.mgr = asyncmgr.ServerMgr()
self.udp_on = True ### UDP switch ===================================== self.udp_on = True ### UDP switch =====================================
@ -115,8 +115,7 @@ class ServerPool(object):
udp_server.add_to_loop(self.loop) udp_server.add_to_loop(self.loop)
self.udp_ipv6_servers_pool.update({port: udp_server}) self.udp_ipv6_servers_pool.update({port: udp_server})
except Exception, e: except Exception, e:
logging.warn("IPV6 exception") logging.warn("IPV6 %s " % (e,))
logging.warn(e)
if 'server' in self.config: if 'server' in self.config:
if port in self.tcp_servers_pool: if port in self.tcp_servers_pool:
@ -136,8 +135,7 @@ class ServerPool(object):
udp_server.add_to_loop(self.loop) udp_server.add_to_loop(self.loop)
self.udp_servers_pool.update({port: udp_server}) self.udp_servers_pool.update({port: udp_server})
except Exception, e: except Exception, e:
logging.warn("IPV4 exception") logging.warn("IPV4 %s " % (e,))
logging.warn(e)
return True return True

3
switchrule.py

@ -1,2 +1,3 @@
def isTurnOn(plan, switch): def isTurnOn(row):
return True return True

Loading…
Cancel
Save