Example #1
0
    def init_func(self,
                  creator_fd,
                  cs,
                  caddr,
                  config_name,
                  conn_timeout=60,
                  is_ipv6=False):
        self.__creator = creator_fd
        self.__caddr = caddr
        self.__cfg_name = config_name
        self.__is_ipv6 = is_ipv6
        self.__time = time.time()
        self.__conn_timeout = conn_timeout
        self.__is_sent_conn = False
        self.__conn_ok = False
        self.tcp_recv_buf_size = 4096
        self.tcp_loop_read_num = 20

        self.set_socket(cs)
        self.register(self.fileno)
        self.add_evt_read(self.fileno)
        self.set_timeout(self.fileno, 10)

        logging.print_general("relay_accept:%s" % config_name, caddr)

        return self.fileno
Example #2
0
 def connect_ok(self):
     logging.print_general("connect_ok", self.__address)
     self.register(self.fileno)
     self.add_evt_read(self.fileno)
     # 注意这里要加入写事件,让TLS能够握手成功
     self.add_evt_write(self.fileno)
     self.set_timeout(self.fileno, 10)
Example #3
0
    def tcp_delete(self):
        if self.debug:
            logging.print_general("disconnect", self.__caddr)
        self.dispatcher.free_packet_id(self.__packet_id)

        self.unregister(self.fileno)
        self.close()
Example #4
0
    def init_func(self, creator_fd, cs, caddr, auth_id, remote_info):
        self.__caddr = caddr
        self.__remote_info = remote_info
        self.__auth_id = auth_id
        self.__time = time.time()
        self.__timeout = remote_info["timeout"]
        self.__wait_fwd_data = []
        self.__conn_ok = False
        self.__wait_sent = []

        remote_addr = remote_info["address"]
        remote_port = remote_info["port"]
        is_ipv6 = remote_info["is_ipv6"]

        self.set_socket(cs)

        self.__session_id = self.dispatcher.send_conn_request(self.fileno,
                                                              auth_id,
                                                              remote_addr,
                                                              remote_port,
                                                              is_ipv6=is_ipv6)
        if not self.__session_id:
            sys.stderr.write("send conn request fail from auth_id:%s\r\n" %
                             auth_id)
            self.close()
            return -1

        self.register(self.fileno)
        self.add_evt_read(self.fileno)
        self.set_timeout(self.fileno, 10)

        self.tcp_loop_read_num = 200
        logging.print_general("accepted", self.__caddr)

        return self.fileno
Example #5
0
 def __handle_conn_timeout(self):
     t = time.time()
     if t - self.__update_time > self.__conn_timeout:
         logging.print_general("udp_timeout", self.__server_address)
         self.delete_handler(self.fileno)
         return
     self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)
    def send_ping(self):
        if self.dispatcher.debug:
            logging.print_general("send_ping", self.__caddr)
        data = self.rand_bytes()
        ping_data = self.__builder.build_ping(data)

        self.send_data(ping_data)
Example #7
0
 def udp_timeout(self):
     t = time.time()
     if t - self.__update_time > self.__conn_timeout:
         logging.print_general("udp_timeout", self.__server_address)
         self.delete_handler(self.fileno)
         return
     self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)
    def tcp_delete(self):
        logging.print_general("disconnect", self.__caddr)

        for packet_id, fd in self.__packet_id_map.items():
            self.delete_handler(fd)
        self.unregister(self.fileno)
        self.close()
    def init_func(self, creator_fd, cs, caddr, is_ipv6=False):
        self.__handshake_ok = False
        self.__caddr = caddr
        self.__packet_id_map = {}

        self.__parser = socks2https.parser()
        self.__builder = socks2https.builder()

        self.__time = time.time()
        self.tcp_recv_buf_size = 4096
        self.tcp_loop_read_num = 3

        if is_ipv6:
            self.__win_size = 1180
            self.__my_win_size = 1180
        else:
            self.__win_size = 1380
            self.__my_win_size = 1380

        self.set_socket(cs)
        self.register(self.fileno)
        self.add_evt_read(self.fileno)

        logging.print_general("connect_ok", self.__caddr)

        return self.fileno
Example #10
0
    def tcp_delete(self):
        if self.__session_id:
            self.dispatcher.tell_del_all_app_proxy(self.__session_id)

        self.unregister(self.fileno)
        self.close()
        logging.print_general("tcp_disconnect", self.__address)
Example #11
0
    def init_func(self, creator, crypto, crypto_configs, cs, address, conn_timeout, over_http=False):
        http_configs = self.dispatcher.http_configs

        self.__address = address
        self.__conn_timeout = conn_timeout
        self.__update_time = time.time()
        self.__session_id = None

        self.__http_handshake_ok = False
        self.__over_http = over_http
        self.__http_auth_id = http_configs["auth_id"]

        self.set_socket(cs)
        self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)

        self.register(self.fileno)
        self.add_evt_read(self.fileno)

        self.__encrypt = crypto.encrypt()
        self.__decrypt = crypto.decrypt()

        self.__encrypt.config(crypto_configs)
        self.__decrypt.config(crypto_configs)

        logging.print_general("tcp_connect", address)

        return self.fileno
Example #12
0
    def add_session(self,
                    fileno,
                    username,
                    session_id,
                    address,
                    priv_data=None):
        """加入会话
        :param fileno:文件描述符
        :param username:用户名
        :param session_id: 会话ID
        :param address: (ipaddr,port)
        :param priv_data:你的私有数据,如果想要修改数据,priv_data应该是引用类型
        :return:
        """
        if self.session_exists(session_id): return

        self.__sessions[session_id] = [
            fileno,
            username,
            address,
            priv_data,
        ]
        self.__timer.set_timeout(session_id, self.__SESSION_TIMEOUT)
        self.__dispatcher.tell_register_session(session_id)
        logging.print_general("add_session:%s" % username, address)
Example #13
0
    def tcp_delete(self):
        self.dispatcher.tell_tunnel_close()
        self.unregister(self.fileno)
        self.close()

        if self.is_conn_ok():
            logging.print_general("disconnect", self.__server_address)
        return
Example #14
0
    def tcp_delete(self):
        self.dispatcher.tell_tunnel_close()
        self.unregister(self.fileno)
        self.close()

        if self.is_conn_ok():
            logging.print_general("disconnect", self.__server_address)
        return
    def handle_handshake_response(self):
        """处理握手响应
        :return:
        """
        size = self.reader.size()
        data = self.reader.read()

        p = data.find(b"\r\n\r\n")

        if p < 10 and size > 2048:
            logging.print_general("wrong_http_response_header", self.__address)
            self.delete_handler(self.fileno)
            return

        if p < 0:
            self.reader._putvalue(data)
            return
        p += 4

        self.reader._putvalue(data[p:])

        s = data[0:p].decode("iso-8859-1")

        try:
            resp, kv_pairs = httputils.parse_http1x_response_header(s)
        except httputils.Http1xHeaderErr:
            logging.print_general("wrong_http_reponse_header", self.__address)
            self.delete_handler(self.fileno)
            return

        version, status = resp

        if status.find("101") != 0:
            logging.print_general("https_handshake_error:%s" % status,
                                  self.__address)
            self.delete_handler(self.fileno)
            return

        accept_key = self.get_http_kv_pairs("sec-websocket-accept", kv_pairs)
        if wslib.gen_handshake_key(self.__http_handshake_key) != accept_key:
            logging.print_general(
                "https_handshake_error:wrong websocket response key",
                self.__address)
            self.delete_handler(self.fileno)
            return

        self.__http_handshake_ok = True
        logging.print_general("https_handshake_ok", self.__address)
        # 发送还没有连接的时候堆积的数据包
        if self.__wait_sent: self.add_evt_write(self.fileno)
        while 1:
            try:
                self.writer.write(self.__wait_sent.pop(0))
            except IndexError:
                break
            ''''''
        ''''''
Example #16
0
    def tcp_timeout(self):
        if not self.is_conn_ok():
            logging.print_general("connecting_timeout", self.__server_address)
            self.delete_handler(self.fileno)
            return

        if self.__enable_heartbeat:
            self.__handle_heartbeat_timeout()

        self.__handle_conn_timeout()
Example #17
0
    def tcp_timeout(self):
        if not self.is_conn_ok():
            logging.print_general("connecting_timeout", self.__server_address)
            self.delete_handler(self.fileno)
            return

        if self.__enable_heartbeat:
            self.__handle_heartbeat_timeout()

        self.__handle_conn_timeout()
Example #18
0
    def __handle_conn_timeout(self):
        t = time.time()
        if t - self.__update_time > self.__conn_timeout:
            logging.print_general("udp_timeout", self.__server_address)
            self.delete_handler(self.fileno)
            return

        if self.__enable_heartbeat:
            self.set_timeout(self.fileno, self.__heartbeat_timeout)
        else:
            self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)
Example #19
0
    def connect_ok(self):
        self.__update_time = time.time()
        self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)
        self.register(self.fileno)
        self.add_evt_read(self.fileno)

        if not self.writer.is_empty(): self.add_evt_write(self.fileno)

        logging.print_general("connected", self.__server_address)

        # 发送还没有连接的时候堆积的数据包
        return
Example #20
0
    def create_tunnel(self, server_address):
        server_ip = self.dispatcher.get_server_ip(server_address[0])

        try:
            self.connect((server_ip, server_address[1]), timeout=8)
            logging.print_general("connecting", server_address)
        except socket.gaierror:
            logging.print_general("not_found_host", server_address)
            return False

        self.__server_address = server_address
        return True
Example #21
0
    def handle_conn_request(self, address, path, auth_id, session_id, remote_addr, remote_port, is_ipv6):
        if session_id in self.__sessions:
            fd = self.__sessions[session_id]
            logging.print_general("delete %s,%s" % (auth_id, session_id,), (remote_addr, remote_port,))
            self.delete_handler(fd)
            del self.__sessions[session_id]

        fd = self.create_handler(-1, lan_fwd.client, address, path, auth_id, session_id=session_id,
                                 is_msg_tunnel=True, is_ipv6=is_ipv6)

        self.get_handler(fd).set_forwarding_addr((remote_addr, remote_port,), is_ipv6=is_ipv6)
        self.__sessions[session_id] = fd
    def tcp_timeout(self):
        t = time.time()
        if t - self.__time > self.dispatcher.conn_timeout:
            logging.print_general("conn_timeout", self.__caddr)
            self.delete_handler(self.fileno)
            return

        if t - self.__time > self.dispatcher.heartbeat_timeout:
            self.send_ping()
            return

        self.set_timeout(self.fileno, 10)
Example #23
0
    def create_tunnel(self, server_address):
        server_ip = self.dispatcher.get_server_ip(server_address[0])
        if not server_ip: return False

        try:
            self.connect((server_ip, server_address[1]), timeout=8)
            logging.print_general("connecting", server_address)
        except socket.gaierror:
            logging.print_general("not_found_host", server_address)
            return False

        self.__server_address = server_address
        return True
Example #24
0
    def tcp_timeout(self):
        if not self.is_conn_ok():
            logging.print_general("connecting_timeout", self.__server_address)
            self.delete_handler(self.fileno)
            return

        t = time.time()

        if t - self.__update_time > self.__conn_timeout:
            self.delete_handler(self.fileno)
            logging.print_general("connected_timeout", self.__server_address)
            return
        self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)
Example #25
0
 def do_ssl_handshake(self):
     try:
         self.socket.do_handshake()
         self.__ssl_handshake_ok = True
         logging.print_general("TLS_handshake_ok", self.__server_address)
         self.add_evt_read(self.fileno)
         self.send_handshake()
     except ssl.SSLWantReadError:
         self.add_evt_read(self.fileno)
     except ssl.SSLWantWriteError:
         self.add_evt_write(self.fileno)
     except:
         logging.print_error()
         self.delete_handler(self.fileno)
Example #26
0
    def del_session(self, session_id):
        """删除会话
        :param session_id:
        :return:
        """
        if session_id not in self.__sessions: return

        self.__timer.drop(session_id)
        self.handle_close(session_id)
        fileno, username, address, priv_data = self.__sessions[session_id]
        self.__dispatcher.tell_unregister_session(session_id, fileno)

        logging.print_general("del_session:%s" % username, address)
        del self.__sessions[session_id]
Example #27
0
    def connect_ok(self):
        self.__update_time = time.time()
        self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)
        self.register(self.fileno)
        self.add_evt_read(self.fileno)

        # 发送还没有连接的时候堆积的数据包
        if not self.writer.is_empty():
            self.__update_time = time.time()
            self.add_evt_write(self.fileno)

        logging.print_general("connected", self.__server_address)

        return
Example #28
0
    def udp_timeout(self):
        t = time.time()
        v = t - self.__update_time

        if v > self.__conn_timeout:
            logging.print_general("udp_timeout", self.__server_address)
            self.delete_handler(self.fileno)
            return

        if self.__enable_heartbeat:
            if t >= self.__heartbeat_timeout:
                self.send_msg_to_tunnel(self.dispatcher.session_id, proto_utils.ACT_PING, proto_utils.rand_bytes())
            ''''''
        self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)
Example #29
0
    def del_session(self, session_id):
        """删除会话
        :param session_id:
        :return:
        """
        if session_id not in self.__sessions: return

        self.__timer.drop(session_id)
        self.handle_close(session_id)
        fileno, username, address, priv_data = self.__sessions[session_id]
        self.__dispatcher.tell_unregister_session(session_id,fileno)

        logging.print_general("del_session:%s" % username, address)
        del self.__sessions[session_id]
    def connect_ok(self):
        if self.dispatcher.debug:
            logging.print_general("connect_ok", self.__address)
        self.register(self.fileno)
        self.add_evt_read(self.fileno)
        self.get_handler(self.__creator).tell_conn_ok(self.__packet_id)
        self.set_timeout(self.fileno, 10)

        self.add_evt_write(self.fileno)
        while 1:
            try:
                data = self.__wait_sent.pop(0)
            except IndexError:
                break
            self.writer.write(data)
    def tcp_timeout(self):
        # 没有连接成功的处理方式
        if not self.is_conn_ok():
            logging.print_general("connect_fail", self.__address)
            self.delete_handler(self.fileno)
            return

        t = time.time()
        if t - self.__time > self.dispatcher.client_conn_timeout:
            logging.print_general("timeout", self.__address)
            self.delete_handler(self.fileno)
            return
        if t - self.__time > self.dispatcher.client_heartbeat_time:
            self.send_ping()
        self.set_timeout(self.fileno, 10)
Example #32
0
    def add_session(self, fileno, username, session_id, address, priv_data=None):
        """加入会话
        :param fileno:文件描述符
        :param username:用户名
        :param session_id: 会话ID
        :param address: (ipaddr,port)
        :param priv_data:你的私有数据,如果想要修改数据,priv_data应该是引用类型
        :return:
        """
        if self.session_exists(session_id): return

        self.__sessions[session_id] = [fileno, username, address, priv_data, ]
        self.__timer.set_timeout(session_id, self.__SESSION_TIMEOUT)
        self.__dispatcher.tell_register_session(session_id)
        logging.print_general("add_session:%s" % username, address)
    def handle_request_data(self):
        rdata = self.reader.read()
        self.__parser.input(rdata)
        self.__time = time.time()

        while 1:
            try:
                self.__parser.parse()
            except socks2https.FrameError:
                if self.dispatcher.debug:
                    logging.print_general("wrong frame", self.__caddr)
                self.delete_handler(self.fileno)
                return
            rs = self.__parser.get_result()
            if not rs: break
            frame_type, info = rs

            if frame_type == socks2https.FRAME_TYPE_PING:
                self.send_pong()
                continue
            if frame_type == socks2https.FRAME_TYPE_PONG:
                self.handle_pong()
                continue
            if frame_type == socks2https.FRAME_TYPE_TCP_CONN:
                self.handle_tcp_conn_request(info)
                continue
            if frame_type == socks2https.FRAME_TYPE_UDP_CONN:
                self.handle_udp_udplite_conn(info, is_udplite=False)
                continue
            if frame_type == socks2https.FRAME_TYPE_UDPLite_CONN:
                self.handle_udp_udplite_conn(info, is_udplite=True)
                continue
            if frame_type == socks2https.FRAME_TYPE_CONN_STATE:
                self.handle_tcp_conn_state(info)
                continue
            if frame_type == socks2https.FRAME_TYPE_TCP_DATA:
                self.handle_tcp_data(info)
                continue
            if frame_type == socks2https.FRAME_TYPE_UDP_DATA:
                self.handle_udp_udplite_data(info)
                continue
            if frame_type == socks2https.FRAME_TYPE_UDPLITE_DATA:
                self.handle_udp_udplite_data(info)
                continue
            ''''''
        return
Example #34
0
    def create_tunnel(self, server_address):
        server_ip = self.dispatcher.get_server_ip(server_address[0])
        try:
            self.connect((server_ip, server_address[1]))
        except socket.gaierror:
            logging.print_general("not_found_host", server_address)
            return False

        self.__server_address = server_address
        logging.print_general("udp_open", server_address)

        self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)
        self.__update_time = time.time()
        self.register(self.fileno)
        self.add_evt_read(self.fileno)

        return True
Example #35
0
    def connect_ok(self):
        self.__update_time = time.time()
        self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)
        self.register(self.fileno)
        self.add_evt_read(self.fileno)

        logging.print_general("connected", self.__server_address)

        # 发送还没有连接的时候堆积的数据包
        if not self.__sent_queue: self.add_evt_write(self.fileno)
        while 1:
            try:
                sent_pkt = self.__sent_queue.pop(0)
            except IndexError:
                break
            self.writer.write(sent_pkt)
        return
Example #36
0
    def connect_ok(self):
        self.__update_time = time.time()
        self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)
        self.register(self.fileno)
        self.add_evt_read(self.fileno)

        # 发送还没有连接的时候堆积的数据包
        if not self.writer.is_empty():
            self.__update_time = time.time()
            self.add_evt_write(self.fileno)

        logging.print_general("connected", self.__server_address)

        if self.__over_https:
            self.do_ssl_handshake()

        self.dispatcher.tunnel_conn_ok()
Example #37
0
    def init_func(self, creator_fd, cs, caddr, auth_id, is_ipv6=False):

        self.__handshake_ok = False
        self.__caddr = caddr
        self.__auth_id = auth_id

        self.__parser = intranet_pass.parser()
        self.__builder = intranet_pass.builder()

        self.__time = time.time()

        self.set_socket(cs)
        self.register(self.fileno)
        self.add_evt_read(self.fileno)

        logging.print_general("connect_ok", self.__caddr)

        return self.fileno
Example #38
0
    def create_tunnel(self, server_address):
        server_ip = self.dispatcher.get_server_ip(server_address[0])

        if not server_ip: return False

        try:
            self.connect((server_ip, server_address[1]))
        except socket.gaierror:
            logging.print_general("not_found_host", server_address)
            return False

        self.__server_address = server_address
        logging.print_general("udp_open", server_address)

        self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)
        self.__update_time = time.time()
        self.register(self.fileno)
        self.add_evt_read(self.fileno)

        return True
Example #39
0
    def init_func(self, creator, crypto, crypto_configs, cs, address, conn_timeout):
        self.__address = address
        self.__conn_timeout = conn_timeout
        self.__update_time = time.time()
        self.__session_id = None

        self.set_socket(cs)
        self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)

        self.register(self.fileno)
        self.add_evt_read(self.fileno)

        self.__encrypt = crypto.encrypt()
        self.__decrypt = crypto.decrypt()

        self.__encrypt.config(crypto_configs)
        self.__decrypt.config(crypto_configs)

        logging.print_general("tcp_connect", address)

        return self.fileno
Example #40
0
 def tcp_delete(self):
     self.unregister(self.fileno)
     self.close()
     logging.print_general("tcp_disconnect", self.__address)
Example #41
0
 def udp_delete(self):
     self.unregister(self.fileno)
     self.dispatcher.tell_tunnel_close()
     self.close()
     logging.print_general("udp_close", self.__server_address)
Example #42
0
 def tcp_error(self):
     logging.print_general("tcp_error", self.__server_address)
     self.delete_handler(self.fileno)