def init_func(self, creator, address, debug=False, server_side=False, is_ipv6=False): if is_ipv6: fa = socket.AF_INET6 else: fa = socket.AF_INET self.__is_ipv6 = is_ipv6 s = socket.socket(fa, socket.SOCK_DGRAM) if server_side and is_ipv6: s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1) self.set_socket(s) self.__server_side = server_side if server_side: self.bind((address, 53)) else: self.connect((address, 53)) self.__debug = debug self.__host_match = host_match self.__timer = timer.timer() self.__host_match = host_match.host_match() self.set_timeout(self.fileno, self.__LOOP_TIMEOUT) self.register(self.fileno) self.add_evt_read(self.fileno) return self.fileno
def init_func(self, mode, with_dnsserver=False, debug=True): self.__packet_id_map = {} self.__relay_info = {} self.__cfg_path = "%s/fdslight_etc/s2hsc.ini" % BASE_DIR self.__host_rules_path = "%s/fdslight_etc/host_rules.txt" % BASE_DIR self.__ip_rules_path = "%s/fdslight_etc/ip_rules.txt" % BASE_DIR self.__udp_src_proxy_path = "%s/fdslight_etc/udp_src_proxy.txt" % BASE_DIR self.__cfg_relay_path = "%s/fdslight_etc/s2hsr.ini" % BASE_DIR self.__debug = debug self.__socks5http_listen_fd = -1 self.__socks5http_listen_fd6 = -1 self.__dnsserver_fd = -1 self.__dnsserver_fd6 = -1 self.__convert_fd = -1 self.__debug = debug self.create_poll() if not debug: signal.signal(signal.SIGINT, self.__exit) sys.stdout = open(LOG_FILE, "a+") sys.stderr = open(ERR_FILE, "a+") self.__configs = cfg.ini_parse_from_file(self.__cfg_path) self.__mode = mode if mode == "relay": self.create_relay_service() if mode == "proxy": if not debug: signal.signal(signal.SIGUSR1, self.__update_rules) self.__domain_match = host_match.host_match() self.__ip_match = ip_match.ip_match() self.__udp_src_match = ip_match.ip_match() # 首先第一次先更新规则 self.__update_rules(None, None) if with_dnsserver: self.create_dns_service() self.create_socks_http_service()
def init_func(self, mode, debug, configs, only_http_socks5=False, no_http_socks5=False): self.create_poll() signal.signal(signal.SIGINT, self.__exit) self.__router_timer = timer.timer() self.__routers = {} self.__configs = configs self.__host_match = host_match.host_match() self.__debug = debug self.__set_host_rules(None, None) conn = configs["connection"] m = "freenet.lib.crypto.%s" % conn["crypto_module"] try: self.__tcp_crypto = importlib.import_module( "%s.%s_tcp" % (m, conn["crypto_module"])) self.__udp_crypto = importlib.import_module( "%s.%s_udp" % (m, conn["crypto_module"])) except ImportError: print("cannot found tcp or udp crypto module") sys.exit(-1) crypto_fpath = "%s/fdslight_etc/%s" % (BASE_DIR, conn["crypto_configfile"]) if not os.path.isfile(crypto_fpath): print("crypto configfile not exists") sys.exit(-1) try: crypto_configs = proto_utils.load_crypto_configfile(crypto_fpath) except: print("crypto configfile should be json file") sys.exit(-1) self.__crypto_configs = crypto_configs if not no_http_socks5: if conn["tunnel_type"].lower() != "tcp": print("app proxy must be tcp tunnel") sys.exit(-1) import freenet.handlers.http_socks5 as http_socks5 app_proxy_configs = configs["app_proxy"] listen_ip = app_proxy_configs["listen_ip"] port = int(app_proxy_configs["listen_port"]) self.__http_socks5_fileno = self.create_handler( -1, http_socks5.http_socks5_listener, ( listen_ip, port, ), self.__host_match, is_ipv6=False, debug=self.__debug) signal.signal(signal.SIGUSR1, self.__set_host_rules) self.__only_http_socks5 = only_http_socks5 if only_http_socks5: return import freenet.handlers.dns_proxy as dns_proxy import freenet.handlers.tundev as tundev if mode == "local": self.__mode = _MODE_LOCAL else: self.__mode = _MODE_GW self.__mbuf = utils.mbuf() self.__tundev_fileno = self.create_handler(-1, tundev.tundevc, self.__DEVNAME) public = configs["public"] gateway = configs["gateway"] self.__enable_ipv6_traffic = bool(int(public["enable_ipv6_traffic"])) is_ipv6 = utils.is_ipv6_address(public["remote_dns"]) if self.__mode == _MODE_GW: self.__dns_fileno = self.create_handler(-1, dns_proxy.dnsc_proxy, gateway["dnsserver_bind"], self.__host_match, debug=debug, server_side=True, is_ipv6=False) self.get_handler(self.__dns_fileno).set_parent_dnsserver( public["remote_dns"], is_ipv6=is_ipv6) if self.__enable_ipv6_traffic: self.__dns_listen6 = self.create_handler( -1, dns_proxy.dnsc_proxy, gateway["dnsserver_bind6"], self.__host_match, debug=debug, server_side=True, is_ipv6=True) self.get_handler(self.__dns_listen6).set_parent_dnsserver( public["remote_dns"], is_ipv6=is_ipv6) else: self.__dns_fileno = self.create_handler(-1, dns_proxy.dnsc_proxy, public["remote_dns"], self.__host_match, debug=debug, server_side=False) self.__set_host_rules(None, None) if self.__mode == _MODE_GW: self.__load_kernel_mod() udp_global = bool(int(gateway["dgram_global_proxy"])) if udp_global: import freenet.handlers.traffic_pass as traffic_pass self.__dgram_fetch_fileno = self.create_handler( -1, traffic_pass.traffic_read, self.__configs["gateway"], enable_ipv6=self.__enable_ipv6_traffic) '''''' else: local = configs["local"] vir_dns = local["virtual_dns"] vir_dns6 = local["virtual_dns6"] self.set_router(vir_dns, is_ipv6=False, is_dynamic=False) if self.__enable_ipv6_traffic: self.set_router(vir_dns6, is_ipv6=True, is_dynamic=False) if not debug: sys.stdout = open(LOG_FILE, "a+") sys.stderr = open(ERR_FILE, "a+") return