|
@ -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 |
|
@ -38,174 +38,172 @@ from socket import * |
|
|
|
|
|
|
|
|
class ServerPool(object): |
|
|
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 ===================================== |
|
|
|
|
|
|
|
|
self.tcp_servers_pool = {} |
|
|
self.tcp_servers_pool = {} |
|
|
self.tcp_ipv6_servers_pool = {} |
|
|
self.tcp_ipv6_servers_pool = {} |
|
|
self.udp_servers_pool = {} |
|
|
self.udp_servers_pool = {} |
|
|
self.udp_ipv6_servers_pool = {} |
|
|
self.udp_ipv6_servers_pool = {} |
|
|
|
|
|
|
|
|
self.loop = eventloop.EventLoop() |
|
|
self.loop = eventloop.EventLoop() |
|
|
thread.start_new_thread(ServerPool._loop, (self.loop, self.dns_resolver, self.mgr)) |
|
|
thread.start_new_thread(ServerPool._loop, (self.loop, self.dns_resolver, self.mgr)) |
|
|
|
|
|
|
|
|
@staticmethod |
|
|
@staticmethod |
|
|
def get_instance(): |
|
|
def get_instance(): |
|
|
if ServerPool.instance is None: |
|
|
if ServerPool.instance is None: |
|
|
ServerPool.instance = ServerPool() |
|
|
ServerPool.instance = ServerPool() |
|
|
return ServerPool.instance |
|
|
return ServerPool.instance |
|
|
|
|
|
|
|
|
@staticmethod |
|
|
@staticmethod |
|
|
def _loop(loop, dns_resolver, mgr): |
|
|
def _loop(loop, dns_resolver, mgr): |
|
|
try: |
|
|
try: |
|
|
mgr.add_to_loop(loop) |
|
|
mgr.add_to_loop(loop) |
|
|
dns_resolver.add_to_loop(loop) |
|
|
dns_resolver.add_to_loop(loop) |
|
|
loop.run() |
|
|
loop.run() |
|
|
except (KeyboardInterrupt, IOError, OSError) as e: |
|
|
except (KeyboardInterrupt, IOError, OSError) as e: |
|
|
logging.error(e) |
|
|
logging.error(e) |
|
|
import traceback |
|
|
import traceback |
|
|
traceback.print_exc() |
|
|
traceback.print_exc() |
|
|
os.exit(0) |
|
|
os.exit(0) |
|
|
|
|
|
|
|
|
def server_is_run(self, port): |
|
|
def server_is_run(self, port): |
|
|
port = int(port) |
|
|
port = int(port) |
|
|
ret = 0 |
|
|
ret = 0 |
|
|
if port in self.tcp_servers_pool: |
|
|
if port in self.tcp_servers_pool: |
|
|
ret = 1 |
|
|
ret = 1 |
|
|
if port in self.tcp_ipv6_servers_pool: |
|
|
if port in self.tcp_ipv6_servers_pool: |
|
|
ret |= 2 |
|
|
ret |= 2 |
|
|
return ret |
|
|
return ret |
|
|
|
|
|
|
|
|
def server_run_status(self, port): |
|
|
def server_run_status(self, port): |
|
|
if 'server' in self.config: |
|
|
if 'server' in self.config: |
|
|
if port not in self.tcp_servers_pool: |
|
|
if port not in self.tcp_servers_pool: |
|
|
return False |
|
|
return False |
|
|
if 'server_ipv6' in self.config: |
|
|
if 'server_ipv6' in self.config: |
|
|
if port not in self.tcp_ipv6_servers_pool: |
|
|
if port not in self.tcp_ipv6_servers_pool: |
|
|
return False |
|
|
return False |
|
|
return True |
|
|
return True |
|
|
|
|
|
|
|
|
def new_server(self, port, password): |
|
|
def new_server(self, port, password): |
|
|
ret = True |
|
|
ret = True |
|
|
port = int(port) |
|
|
port = int(port) |
|
|
|
|
|
|
|
|
if 'server_ipv6' in self.config: |
|
|
if 'server_ipv6' in self.config: |
|
|
if port in self.tcp_ipv6_servers_pool: |
|
|
if port in self.tcp_ipv6_servers_pool: |
|
|
logging.info("server already at %s:%d" % (self.config['server_ipv6'], port)) |
|
|
logging.info("server already at %s:%d" % (self.config['server_ipv6'], port)) |
|
|
return 'this port server is already running' |
|
|
return 'this port server is already running' |
|
|
else: |
|
|
else: |
|
|
a_config = self.config.copy() |
|
|
a_config = self.config.copy() |
|
|
a_config['server'] = a_config['server_ipv6'] |
|
|
a_config['server'] = a_config['server_ipv6'] |
|
|
a_config['server_port'] = port |
|
|
a_config['server_port'] = port |
|
|
a_config['password'] = password |
|
|
a_config['password'] = password |
|
|
try: |
|
|
try: |
|
|
logging.info("starting server at %s:%d" % (a_config['server'], port)) |
|
|
logging.info("starting server at %s:%d" % (a_config['server'], port)) |
|
|
tcp_server = tcprelay.TCPRelay(a_config, self.dns_resolver, False) |
|
|
tcp_server = tcprelay.TCPRelay(a_config, self.dns_resolver, False) |
|
|
tcp_server.add_to_loop(self.loop) |
|
|
tcp_server.add_to_loop(self.loop) |
|
|
self.tcp_ipv6_servers_pool.update({port: tcp_server}) |
|
|
self.tcp_ipv6_servers_pool.update({port: tcp_server}) |
|
|
if self.udp_on: |
|
|
if self.udp_on: |
|
|
udp_server = udprelay.UDPRelay(a_config, self.dns_resolver, False) |
|
|
udp_server = udprelay.UDPRelay(a_config, self.dns_resolver, False) |
|
|
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: |
|
|
logging.info("server already at %s:%d" % (self.config['server'], port)) |
|
|
logging.info("server already at %s:%d" % (self.config['server'], port)) |
|
|
return 'this port server is already running' |
|
|
return 'this port server is already running' |
|
|
else: |
|
|
else: |
|
|
a_config = self.config.copy() |
|
|
a_config = self.config.copy() |
|
|
a_config['server_port'] = port |
|
|
a_config['server_port'] = port |
|
|
a_config['password'] = password |
|
|
a_config['password'] = password |
|
|
try: |
|
|
try: |
|
|
logging.info("starting server at %s:%d" % (a_config['server'], port)) |
|
|
logging.info("starting server at %s:%d" % (a_config['server'], port)) |
|
|
tcp_server = tcprelay.TCPRelay(a_config, self.dns_resolver, False) |
|
|
tcp_server = tcprelay.TCPRelay(a_config, self.dns_resolver, False) |
|
|
tcp_server.add_to_loop(self.loop) |
|
|
tcp_server.add_to_loop(self.loop) |
|
|
self.tcp_servers_pool.update({port: tcp_server}) |
|
|
self.tcp_servers_pool.update({port: tcp_server}) |
|
|
if self.udp_on: |
|
|
if self.udp_on: |
|
|
udp_server = udprelay.UDPRelay(a_config, self.dns_resolver, False) |
|
|
udp_server = udprelay.UDPRelay(a_config, self.dns_resolver, False) |
|
|
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 %s " % (e,)) |
|
|
logging.warn("IPV4 exception") |
|
|
|
|
|
logging.warn(e) |
|
|
return True |
|
|
|
|
|
|
|
|
return True |
|
|
def del_server(self, port): |
|
|
|
|
|
port = int(port) |
|
|
def del_server(self, port): |
|
|
logging.info("del server at %d" % port) |
|
|
port = int(port) |
|
|
try: |
|
|
logging.info("del server at %d" % port) |
|
|
udpsock = socket(AF_INET, SOCK_DGRAM) |
|
|
try: |
|
|
udpsock.sendto('%s:%s:0:0' % (Config.MANAGE_PASS, port), (Config.MANAGE_BIND_IP, Config.MANAGE_PORT)) |
|
|
udpsock = socket(AF_INET, SOCK_DGRAM) |
|
|
udpsock.close() |
|
|
udpsock.sendto('%s:%s:0:0' % (Config.MANAGE_PASS, port), (Config.MANAGE_BIND_IP, Config.MANAGE_PORT)) |
|
|
except Exception, e: |
|
|
udpsock.close() |
|
|
logging.warn(e) |
|
|
except Exception, e: |
|
|
return True |
|
|
logging.warn(e) |
|
|
|
|
|
return True |
|
|
def cb_del_server(self, port): |
|
|
|
|
|
port = int(port) |
|
|
def cb_del_server(self, port): |
|
|
|
|
|
port = int(port) |
|
|
if port not in self.tcp_servers_pool: |
|
|
|
|
|
logging.info("stopped server at %s:%d already stop" % (self.config['server'], port)) |
|
|
if port not in self.tcp_servers_pool: |
|
|
else: |
|
|
logging.info("stopped server at %s:%d already stop" % (self.config['server'], port)) |
|
|
logging.info("stopped server at %s:%d" % (self.config['server'], port)) |
|
|
else: |
|
|
try: |
|
|
logging.info("stopped server at %s:%d" % (self.config['server'], port)) |
|
|
self.tcp_servers_pool[port].destroy() |
|
|
try: |
|
|
del self.tcp_servers_pool[port] |
|
|
self.tcp_servers_pool[port].destroy() |
|
|
except Exception, e: |
|
|
del self.tcp_servers_pool[port] |
|
|
logging.warn(e) |
|
|
except Exception, e: |
|
|
if self.udp_on: |
|
|
logging.warn(e) |
|
|
try: |
|
|
if self.udp_on: |
|
|
self.udp_servers_pool[port].destroy() |
|
|
try: |
|
|
del self.udp_servers_pool[port] |
|
|
self.udp_servers_pool[port].destroy() |
|
|
except Exception, e: |
|
|
del self.udp_servers_pool[port] |
|
|
logging.warn(e) |
|
|
except Exception, e: |
|
|
|
|
|
logging.warn(e) |
|
|
if 'server_ipv6' in self.config: |
|
|
|
|
|
if port not in self.tcp_ipv6_servers_pool: |
|
|
if 'server_ipv6' in self.config: |
|
|
logging.info("stopped server at %s:%d already stop" % (self.config['server_ipv6'], port)) |
|
|
if port not in self.tcp_ipv6_servers_pool: |
|
|
else: |
|
|
logging.info("stopped server at %s:%d already stop" % (self.config['server_ipv6'], port)) |
|
|
logging.info("stopped server at %s:%d" % (self.config['server_ipv6'], port)) |
|
|
else: |
|
|
try: |
|
|
logging.info("stopped server at %s:%d" % (self.config['server_ipv6'], port)) |
|
|
self.tcp_ipv6_servers_pool[port].destroy() |
|
|
try: |
|
|
del self.tcp_ipv6_servers_pool[port] |
|
|
self.tcp_ipv6_servers_pool[port].destroy() |
|
|
except Exception, e: |
|
|
del self.tcp_ipv6_servers_pool[port] |
|
|
logging.warn(e) |
|
|
except Exception, e: |
|
|
if self.udp_on: |
|
|
logging.warn(e) |
|
|
try: |
|
|
if self.udp_on: |
|
|
self.udp_ipv6_servers_pool[port].destroy() |
|
|
try: |
|
|
del self.udp_ipv6_servers_pool[port] |
|
|
self.udp_ipv6_servers_pool[port].destroy() |
|
|
except Exception, e: |
|
|
del self.udp_ipv6_servers_pool[port] |
|
|
logging.warn(e) |
|
|
except Exception, e: |
|
|
|
|
|
logging.warn(e) |
|
|
return True |
|
|
|
|
|
|
|
|
return True |
|
|
def get_server_transfer(self, port): |
|
|
|
|
|
port = int(port) |
|
|
def get_server_transfer(self, port): |
|
|
ret = [0, 0] |
|
|
port = int(port) |
|
|
if port in self.tcp_servers_pool: |
|
|
ret = [0, 0] |
|
|
ret[0] = self.tcp_servers_pool[port].server_transfer_ul |
|
|
if port in self.tcp_servers_pool: |
|
|
ret[1] = self.tcp_servers_pool[port].server_transfer_dl |
|
|
ret[0] = self.tcp_servers_pool[port].server_transfer_ul |
|
|
if port in self.tcp_ipv6_servers_pool: |
|
|
ret[1] = self.tcp_servers_pool[port].server_transfer_dl |
|
|
ret[0] += self.tcp_ipv6_servers_pool[port].server_transfer_ul |
|
|
if port in self.tcp_ipv6_servers_pool: |
|
|
ret[1] += self.tcp_ipv6_servers_pool[port].server_transfer_dl |
|
|
ret[0] += self.tcp_ipv6_servers_pool[port].server_transfer_ul |
|
|
return ret |
|
|
ret[1] += self.tcp_ipv6_servers_pool[port].server_transfer_dl |
|
|
|
|
|
return ret |
|
|
def get_servers_transfer(self): |
|
|
|
|
|
servers = self.tcp_servers_pool.copy() |
|
|
def get_servers_transfer(self): |
|
|
servers.update(self.tcp_ipv6_servers_pool) |
|
|
servers = self.tcp_servers_pool.copy() |
|
|
ret = {} |
|
|
servers.update(self.tcp_ipv6_servers_pool) |
|
|
for port in servers.keys(): |
|
|
ret = {} |
|
|
ret[port] = self.get_server_transfer(port) |
|
|
for port in servers.keys(): |
|
|
return ret |
|
|
ret[port] = self.get_server_transfer(port) |
|
|
|
|
|
return ret |
|
|
|
|
|
|
|
|
|
|
|