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
from server_pool import ServerPool
import Config
import traceback
class DbTransfer(object):
@ -75,13 +76,17 @@ class DbTransfer(object):
@staticmethod
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,
passwd=Config.MYSQL_PASS, db=Config.MYSQL_DB, charset='utf8')
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 = []
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()
conn.close()
return rows
@ -95,33 +100,34 @@ class DbTransfer(object):
for row in rows:
try:
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:
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:
logging.info('db stop server at port [%s]' % (row[0]))
ServerPool.get_instance().del_server(row[0])
elif (row[0] in ServerPool.get_instance().tcp_servers_pool and ServerPool.get_instance().tcp_servers_pool[row[0]]._config['password'] != row[4]) \
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]):
logging.info('db stop server at port [%s]' % (port,))
ServerPool.get_instance().del_server(port)
elif (port in ServerPool.get_instance().tcp_servers_pool and ServerPool.get_instance().tcp_servers_pool[port]._config['password'] != passwd) \
or (port in ServerPool.get_instance().tcp_ipv6_servers_pool and ServerPool.get_instance().tcp_ipv6_servers_pool[port]._config['password'] != passwd):
#password changed
logging.info('db stop server at port [%s] reason: password changed' % (row[0]))
ServerPool.get_instance().del_server(row[0])
elif ServerPool.get_instance().server_run_status(row[0]) is False:
if allow:
logging.info('db start server at port [%s] pass [%s]' % (row[0], row[4]))
ServerPool.get_instance().new_server(row[0], row[4])
logging.info('db stop server at port [%s] reason: password changed' % (port,))
ServerPool.get_instance().del_server(port)
if allow and ServerPool.get_instance().server_is_run(port) == 0:
logging.info('db start server at port [%s] pass [%s]' % (port, passwd))
ServerPool.get_instance().new_server(port, passwd)
for row in last_rows:
if row[0] in cur_servers:
if row[4] == cur_servers[row[0]]:
if row['port'] in cur_servers:
pass
else:
logging.info('db stop server at port [%s] reason: port not exist' % (row[0]))
ServerPool.get_instance().del_server(row[0])
logging.info('db stop server at port [%s] reason: port not exist' % (row['port']))
ServerPool.get_instance().del_server(row['port'])
@staticmethod
def thread_db():
@ -131,18 +137,15 @@ class DbTransfer(object):
socket.setdefaulttimeout(timeout)
last_rows = []
while True:
#logging.warn('db loop')
try:
DbTransfer.get_instance().push_db_all_user()
rows = DbTransfer.get_instance().pull_db_all_user()
DbTransfer.del_server_out_of_bound_safe(last_rows, rows)
last_rows = rows
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:
time.sleep(15)
#SQLData.pull_db_all_user()
#print DbTransfer.get_instance().test()

4
server.py

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

14
server_pool.py

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

3
switchrule.py

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

Loading…
Cancel
Save