def on_accept_new_host(self, sock): # incoming new connection from host client, addr = self.base_sck.accept() common.set_sock_buff_size(client) self.logger.info(self.name + " accept connection from host " + str(addr)) self.__save_new_sock(client, self.proc_host_connection)
def main(self): actions = { 'pong': self.on_pong, "setup": self.on_setup, "registered": self.on_registered } while True: sock = common.connect_to(self.proxy_addr, self.proxy_port) if sock is None: time.sleep(10) continue self.logger.info("connected to %s:%d" % (self.proxy_addr, self.proxy_port)) try: common.set_sock_buff_size(sock) #sock.setblocking(1) sock.settimeout(5) self.registered = False text = '' socks_to_read = [sock] send_ping = True while True: if self.registered: if send_ping: sock.sendall("ping;") send_ping = False else: send_ping = True else: sock.send("register %s;" % self.my_name) readable, writable, exceptional = select.select( socks_to_read, [], [], 10) if len(readable): try: data = common.recv(sock, common.BUFFER_SIZE) except socket.error: self.logger.error(traceback.format_exc()) break if len(data) == 0: self.logger.warn( 'read exception, disconnected by remote') break else: text += data while True: action, params, text = common.get_action(text) if action is None: break if actions.has_key(action): actions[action](sock, params) except: self.logger.error(traceback.format_exc()) common.safe_close_socket(sock)
def run(self): actions = {"ping": self.on_ping, 'built': self.on_built} self.running.set() self.logger.info("thread of host %s begin" % self.name) while self.running.isSet(): try: inputs = self.listening_scks.keys() inputs.append(self.base_sck) excepts = inputs[:] readable, writable, exceptional = select.select( inputs, [], excepts, 1) for s in exceptional: self.running.clear() break for s in readable: if s == self.base_sck: self.__proc_host_actions(actions) else: #new connect client, addr = s.accept() common.set_sock_buff_size(client) outer_port, redirect_to = self.listening_scks[s] waiting_id = "%s %s" % (redirect_to, common.key_gen()) self.__add_waiting_sock(waiting_id, client) self.base_sck.send("setup %s;" % waiting_id) self.logger.info("setup tcp %s;" % waiting_id) #setup http tunnel while True: try: http_sock, data, redirect_to = self.http_request_queue.get( block=False) waiting_id = "%s %s" % (redirect_to, common.key_gen()) self.__add_waiting_sock(waiting_id, http_sock, data) self.base_sck.send("setup %s;" % waiting_id) self.logger.info("setup http %s;" % waiting_id) except Queue.Empty, e: #traceback.print_exc() break except socket.error, e: self.logger.error(traceback.format_exc()) self.running.clear()
def on_accept_new_http_req(self, sock): # incoming new connection from host client, addr = self.http_sck.accept() common.set_sock_buff_size(client) self.__save_new_sock(client, self.proc_http_connection)