def run_server(): def child_handler(signum, _): logging.warn('received SIGQUIT, doing graceful shutting down..') list( map(lambda s: s.close(next_tick=True), tcp_servers + udp_servers)) signal.signal(getattr(signal, 'SIGQUIT', signal.SIGTERM), child_handler) def int_handler(signum, _): sys.exit(1) signal.signal(signal.SIGINT, int_handler) try: loop = eventloop.EventLoop() dns_resolver.add_to_loop(loop) list(map(lambda s: s.add_to_loop(loop), tcp_servers + udp_servers)) daemon.set_user(config.get('user', None)) loop.run() except Exception as e: shell.print_exception(e) sys.exit(1)
def operate_on_unix(ssr_dict, **kwargs): """在Unix操作系统平台开启shadowsocksr代理 :param kwargs: local_address: 本地监听地址 local_port: 本地监听端口 timeout: 启动延迟 workers: 加密级别 pid_file: shadowsocksr进程号文件 log_file: shadowsocksr日志文件 daemon: 标识操作,启动或者停止 :type ssr_dict: shadowsocksr节点信息字典 """ ssr_dict['daemon'] = kwargs['daemon'] ssr_dict['local_address'] = kwargs['local_address'] ssr_dict['local_port'] = kwargs['local_port'] ssr_dict['timeout'] = kwargs['timeout'] ssr_dict['workers'] = kwargs['workers'] ssr_dict['pid-file'] = kwargs['pid_file'] ssr_dict['log-file'] = kwargs['log_file'] if not ssr_dict.get('dns_ipv6', False): asyncdns.IPV6_CONNECTION_SUPPORT = False try: daemon.daemon_exec(ssr_dict) dns_resolver = asyncdns.DNSResolver() tcp_server = tcprelay.TCPRelay(ssr_dict, dns_resolver, True) udp_server = udprelay.UDPRelay(ssr_dict, dns_resolver, True) loop = eventloop.EventLoop() dns_resolver.add_to_loop(loop) tcp_server.add_to_loop(loop) udp_server.add_to_loop(loop) def handler(signum, _): logger.info('received SIGQUIT, doing graceful shutting down..') tcp_server.close(next_tick=True) udp_server.close(next_tick=True) signal.signal(getattr(signal, 'SIGQUIT', signal.SIGTERM), handler) def int_handler(signum, _): logger.info("Shadowsocksr is stop") sys.exit(1) signal.signal(signal.SIGINT, int_handler) daemon.set_user(ssr_dict.get('user', None)) logger.info('Shadowsocksr is start on {0}:{1}'.format( kwargs['local_address'], kwargs['local_port'])) loop.run() except Exception as e: from shadowsocksr_cli.functions import UpdateConfigurations UpdateConfigurations.clear_ssr_nodes(ssr_dict['id']) import traceback logger.error(traceback.format_exc()) sys.exit(1)
def main(): shell.check_python() # fix py2exe if hasattr(sys, "frozen") and sys.frozen in \ ("windows_exe", "console_exe"): p = os.path.dirname(os.path.abspath(sys.executable)) os.chdir(p) config = shell.get_config(True) print('*' * 20) print(config) print('*' * 20) if not config.get('dns_ipv6', False): asyncdns.IPV6_CONNECTION_SUPPORT = False daemon.daemon_exec(config) print('*' * 20) print(config) print('*' * 20) logging.info("local start with protocol[%s] password [%s] method [%s] obfs [%s] obfs_param [%s]" % (config['protocol'], config['password'], config['method'], config['obfs'], config['obfs_param'])) try: logging.info("starting local at %s:%d" % (config['local_address'], config['local_port'])) dns_resolver = asyncdns.DNSResolver() tcp_server = tcprelay.TCPRelay(config, dns_resolver, True) udp_server = udprelay.UDPRelay(config, dns_resolver, True) loop = eventloop.EventLoop() dns_resolver.add_to_loop(loop) tcp_server.add_to_loop(loop) udp_server.add_to_loop(loop) def handler(signum, _): logging.warn('received SIGQUIT, doing graceful shutting down..') tcp_server.close(next_tick=True) udp_server.close(next_tick=True) signal.signal(getattr(signal, 'SIGQUIT', signal.SIGTERM), handler) def int_handler(signum, _): sys.exit(1) signal.signal(signal.SIGINT, int_handler) daemon.set_user(config.get('user', None)) loop.run() except Exception as e: shell.print_exception(e) sys.exit(1)
def start_on_windows(ssr_dict, **kwargs): """在Windows操作系统平台开启shadowsocksr代理 :param kwargs: local_address: 本地监听地址 local_port: 本地监听端口 timeout: 启动延迟 workers: 加密级别 :type ssr_dict: shadowsocksr节点信息字典 """ ssr_dict['local_address'] = kwargs['local_address'] ssr_dict['local_port'] = kwargs['local_port'] ssr_dict['timeout'] = kwargs['timeout'] ssr_dict['workers'] = kwargs['workers'] if not ssr_dict.get('dns_ipv6', False): asyncdns.IPV6_CONNECTION_SUPPORT = False try: daemon.daemon_exec(ssr_dict) dns_resolver = asyncdns.DNSResolver() tcp_server = tcprelay.TCPRelay(ssr_dict, dns_resolver, True) udp_server = udprelay.UDPRelay(ssr_dict, dns_resolver, True) loop = eventloop.EventLoop() dns_resolver.add_to_loop(loop) tcp_server.add_to_loop(loop) udp_server.add_to_loop(loop) def handler(signum, _): logger.info('received SIGQUIT, doing graceful shutting down..') tcp_server.close(next_tick=True) udp_server.close(next_tick=True) signal.signal(getattr(signal, 'SIGQUIT', signal.SIGTERM), handler) def int_handler(signum, _): logger.info("Shadowsocksr is stop") sys.exit(1) signal.signal(signal.SIGINT, int_handler) daemon.set_user(ssr_dict.get('user', None)) logger.info('Shadowsocksr is start on {0}:{1}'.format( kwargs['local_address'], kwargs['local_port'])) logger.info('Press Ctrl+C to stop shadowsocksr') loop.run() except Exception as e: logger.error(e) sys.exit(1)
def __init__(self, config): self._config = config self._relays = {} # (tcprelay, udprelay) self._loop = eventloop.EventLoop() self._dns_resolver = asyncdns.DNSResolver() self._dns_resolver.add_to_loop(self._loop) self._statistics = collections.defaultdict(int) self._control_client_addr = None try: manager_address = common.to_str(config['manager_address']) if ':' in manager_address: addr = manager_address.rsplit(':', 1) addr = addr[0], int(addr[1]) addrs = socket.getaddrinfo(addr[0], addr[1]) if addrs: family = addrs[0][0] else: logging.error('invalid address: %s', manager_address) exit(1) else: addr = manager_address family = socket.AF_UNIX self._control_socket = socket.socket(family, socket.SOCK_DGRAM) self._control_socket.bind(addr) self._control_socket.setblocking(False) except (OSError, IOError) as e: logging.error(e) logging.error('can not bind to manager address') exit(1) self._loop.add(self._control_socket, eventloop.POLL_IN, self) self._loop.add_periodic(self.handle_periodic) port_password = config['port_password'] del config['port_password'] for port, password in port_password.items(): a_config = config.copy() a_config['server_port'] = int(port) a_config['password'] = password self.add_port(a_config)
def test(): black_hostname_list = [ 'baidu.com', 'yahoo.com', ] dns_resolver = DNSResolver(black_hostname_list=black_hostname_list) loop = eventloop.EventLoop() dns_resolver.add_to_loop(loop) global counter counter = 0 def make_callback(): global counter def callback(result, error): global counter # TODO: what can we assert? print(result, error) counter += 1 if counter == 12: dns_resolver.close() loop.stop() a_callback = callback return a_callback assert (make_callback() != make_callback()) dns_resolver.resolve(b'google.com', make_callback()) dns_resolver.resolve('google.com', make_callback()) dns_resolver.resolve('baidu.com', make_callback()) dns_resolver.resolve('map.baidu.com', make_callback()) dns_resolver.resolve('yahoo.com', make_callback()) dns_resolver.resolve('example.com', make_callback()) dns_resolver.resolve('ipv6.google.com', make_callback()) dns_resolver.resolve('www.facebook.com', make_callback()) dns_resolver.resolve('ns2.google.com', make_callback()) dns_resolver.resolve('invalid.@!#$%^&[email protected]', make_callback()) dns_resolver.resolve( 'toooooooooooooooooooooooooooooooooooooooooooooooooo' 'ooooooooooooooooooooooooooooooooooooooooooooooooooo' 'long.hostname', make_callback()) dns_resolver.resolve( 'toooooooooooooooooooooooooooooooooooooooooooooooooo' 'ooooooooooooooooooooooooooooooooooooooooooooooooooo' 'ooooooooooooooooooooooooooooooooooooooooooooooooooo' 'ooooooooooooooooooooooooooooooooooooooooooooooooooo' 'ooooooooooooooooooooooooooooooooooooooooooooooooooo' 'ooooooooooooooooooooooooooooooooooooooooooooooooooo' 'long.hostname', make_callback()) loop.run() # test black_hostname_list dns_resolver = DNSResolver(black_hostname_list=[]) assert type(dns_resolver._black_hostname_list) == list assert len(dns_resolver._black_hostname_list) == 0 dns_resolver.close() dns_resolver = DNSResolver(black_hostname_list=123) assert type(dns_resolver._black_hostname_list) == list assert len(dns_resolver._black_hostname_list) == 0 dns_resolver.close() dns_resolver = DNSResolver(black_hostname_list=None) assert type(dns_resolver._black_hostname_list) == list assert len(dns_resolver._black_hostname_list) == 0 dns_resolver.close() dns_resolver = DNSResolver() assert type(dns_resolver._black_hostname_list) == list assert dns_resolver._black_hostname_list.__len__() == 0 dns_resolver.close()