|
@ -21,16 +21,16 @@ |
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
|
# SOFTWARE. |
|
|
# SOFTWARE. |
|
|
|
|
|
|
|
|
import os |
|
|
import sys |
|
|
import logging |
|
|
import logging |
|
|
import struct |
|
|
import struct |
|
|
import time |
|
|
import time |
|
|
from shadowsocks import shell, eventloop, tcprelay, udprelay, asyncdns, common |
|
|
from shadowsocks import shell, eventloop, tcprelay, udprelay, asyncdns, common |
|
|
import threading |
|
|
import threading |
|
|
import sys |
|
|
|
|
|
import traceback |
|
|
import traceback |
|
|
from socket import * |
|
|
from socket import * |
|
|
from configloader import load_config, get_config |
|
|
from configloader import get_config |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MainThread(threading.Thread): |
|
|
class MainThread(threading.Thread): |
|
|
def __init__(self, params): |
|
|
def __init__(self, params): |
|
@ -40,8 +40,8 @@ class MainThread(threading.Thread): |
|
|
def run(self): |
|
|
def run(self): |
|
|
ServerPool._loop(*self.params) |
|
|
ServerPool._loop(*self.params) |
|
|
|
|
|
|
|
|
class ServerPool(object): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ServerPool(object): |
|
|
instance = None |
|
|
instance = None |
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
def __init__(self): |
|
@ -79,10 +79,12 @@ class ServerPool(object): |
|
|
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) |
|
|
traceback.print_exc() |
|
|
traceback.print_exc() |
|
|
os.exit(0) |
|
|
sys.exit(0) |
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.error(e) |
|
|
logging.error(e) |
|
|
traceback.print_exc() |
|
|
traceback.print_exc() |
|
@ -117,12 +119,16 @@ class ServerPool(object): |
|
|
else: |
|
|
else: |
|
|
a_config = self.config.copy() |
|
|
a_config = self.config.copy() |
|
|
a_config.update(user_config) |
|
|
a_config.update(user_config) |
|
|
if len(a_config['server_ipv6']) > 2 and a_config['server_ipv6'][0] == "[" and a_config['server_ipv6'][-1] == "]": |
|
|
if len(a_config['server_ipv6']) > 2 \ |
|
|
|
|
|
and a_config['server_ipv6'][0] == "[" \ |
|
|
|
|
|
and a_config['server_ipv6'][-1] == "]": |
|
|
a_config['server_ipv6'] = a_config['server_ipv6'][1:-1] |
|
|
a_config['server_ipv6'] = a_config['server_ipv6'][1:-1] |
|
|
|
|
|
|
|
|
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['max_connect'] = 128 |
|
|
a_config['max_connect'] = 128 |
|
|
a_config['method'] = common.to_str(a_config['method']) |
|
|
a_config['method'] = common.to_str(a_config['method']) |
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
logging.info("starting server at [%s]:%d" % (common.to_str(a_config['server']), port)) |
|
|
logging.info("starting server at [%s]:%d" % (common.to_str(a_config['server']), port)) |
|
|
|
|
|
|
|
@ -137,7 +143,7 @@ class ServerPool(object): |
|
|
if common.to_str(a_config['server_ipv6']) == "::": |
|
|
if common.to_str(a_config['server_ipv6']) == "::": |
|
|
ipv6_ok = True |
|
|
ipv6_ok = True |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.warn("IPV6 %s " % (e,)) |
|
|
logging.warning("IPV6 %s " % (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: |
|
@ -162,7 +168,7 @@ class ServerPool(object): |
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
if not ipv6_ok: |
|
|
if not ipv6_ok: |
|
|
logging.warn("IPV4 %s " % (e,)) |
|
|
logging.warning("IPV4 %s " % (e,)) |
|
|
|
|
|
|
|
|
return True |
|
|
return True |
|
|
|
|
|
|
|
@ -171,10 +177,11 @@ class ServerPool(object): |
|
|
logging.info("del server at %d" % port) |
|
|
logging.info("del server at %d" % port) |
|
|
try: |
|
|
try: |
|
|
udpsock = socket(AF_INET, SOCK_DGRAM) |
|
|
udpsock = socket(AF_INET, SOCK_DGRAM) |
|
|
udpsock.sendto('%s:%s:0:0' % (get_config().MANAGE_PASS, port), (get_config().MANAGE_BIND_IP, get_config().MANAGE_PORT)) |
|
|
udpsock.sendto('%s:%s:0:0' % (get_config().MANAGE_PASS, port), |
|
|
|
|
|
(get_config().MANAGE_BIND_IP, get_config().MANAGE_PORT)) |
|
|
udpsock.close() |
|
|
udpsock.close() |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.warn(e) |
|
|
logging.warning(e) |
|
|
return True |
|
|
return True |
|
|
|
|
|
|
|
|
def cb_del_server(self, port): |
|
|
def cb_del_server(self, port): |
|
@ -188,12 +195,12 @@ class ServerPool(object): |
|
|
self.tcp_servers_pool[port].close(True) |
|
|
self.tcp_servers_pool[port].close(True) |
|
|
del self.tcp_servers_pool[port] |
|
|
del self.tcp_servers_pool[port] |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.warn(e) |
|
|
logging.warning(e) |
|
|
try: |
|
|
try: |
|
|
self.udp_servers_pool[port].close(True) |
|
|
self.udp_servers_pool[port].close(True) |
|
|
del self.udp_servers_pool[port] |
|
|
del self.udp_servers_pool[port] |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.warn(e) |
|
|
logging.warning(e) |
|
|
|
|
|
|
|
|
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: |
|
@ -204,12 +211,12 @@ class ServerPool(object): |
|
|
self.tcp_ipv6_servers_pool[port].close(True) |
|
|
self.tcp_ipv6_servers_pool[port].close(True) |
|
|
del self.tcp_ipv6_servers_pool[port] |
|
|
del self.tcp_ipv6_servers_pool[port] |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.warn(e) |
|
|
logging.warning(e) |
|
|
try: |
|
|
try: |
|
|
self.udp_ipv6_servers_pool[port].close(True) |
|
|
self.udp_ipv6_servers_pool[port].close(True) |
|
|
del self.udp_ipv6_servers_pool[port] |
|
|
del self.udp_ipv6_servers_pool[port] |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.warn(e) |
|
|
logging.warning(e) |
|
|
|
|
|
|
|
|
return True |
|
|
return True |
|
|
|
|
|
|
|
@ -219,20 +226,20 @@ class ServerPool(object): |
|
|
try: |
|
|
try: |
|
|
self.tcp_servers_pool[port].update_users(users) |
|
|
self.tcp_servers_pool[port].update_users(users) |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.warn(e) |
|
|
logging.warning(e) |
|
|
try: |
|
|
try: |
|
|
self.udp_servers_pool[port].update_users(users) |
|
|
self.udp_servers_pool[port].update_users(users) |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.warn(e) |
|
|
logging.warning(e) |
|
|
if port in self.tcp_ipv6_servers_pool: |
|
|
if port in self.tcp_ipv6_servers_pool: |
|
|
try: |
|
|
try: |
|
|
self.tcp_ipv6_servers_pool[port].update_users(users) |
|
|
self.tcp_ipv6_servers_pool[port].update_users(users) |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.warn(e) |
|
|
logging.warning(e) |
|
|
try: |
|
|
try: |
|
|
self.udp_ipv6_servers_pool[port].update_users(users) |
|
|
self.udp_ipv6_servers_pool[port].update_users(users) |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logging.warn(e) |
|
|
logging.warning(e) |
|
|
|
|
|
|
|
|
def get_server_transfer(self, port): |
|
|
def get_server_transfer(self, port): |
|
|
port = int(port) |
|
|
port = int(port) |
|
@ -290,4 +297,3 @@ class ServerPool(object): |
|
|
u, d = self.get_server_mu_transfer(self.udp_ipv6_servers_pool[port]) |
|
|
u, d = self.get_server_mu_transfer(self.udp_ipv6_servers_pool[port]) |
|
|
self.update_mu_transfer(ret, u, d) |
|
|
self.update_mu_transfer(ret, u, d) |
|
|
return ret |
|
|
return ret |
|
|
|
|
|
|
|
|