def run(self): self.give_up = False self.ping_reply = True self.lockcond = threading.Condition() self.lockthrottle = threading.Condition() PingThread(self).start() ThrottleThread(self).start() while not self.give_up: try: log.debug('connecting') if self.socks5.lower() == 'true': log.debug("Using socks5 proxy %s:%d" % (self.socks5_host, self.socks5_port)) setdefaultproxy(PROXY_TYPE_SOCKS5, self.socks5_host, self.socks5_port, True) self.sock = socksocket() else: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect(self.serverport) if self.usessl.lower() == 'true': self.sock = ssl.wrap_socket(self.sock) self.fd = self.sock.makefile() self.password = None if self.given_password: self.password = self.given_password self.send_raw('CAP REQ :sasl') self.send_raw('USER %s b c :%s' % self.userrealname) self.nick = self.given_nick self.send_raw('NICK ' + self.nick) while 1: try: line = self.fd.readline() except AttributeError as e: raise IOError(repr(e)) if line is None: log.debug('line returned null') break if len(line) == 0: log.debug('line was zero length') break self.__handle_line(line) except IOError as e: import traceback log.debug(traceback.format_exc()) finally: try: self.fd.close() self.sock.close() except Exception as e: pass if self.on_disconnect: self.on_disconnect(self) log.debug('disconnected irc') if not self.give_up: time.sleep(30) log.debug('ending irc') self.give_up = True
def run(self): self.give_up = False self.ping_reply = True self.lockcond = threading.Condition() self.lockthrottle = threading.Condition() PingThread(self).start() ThrottleThread(self).start() while not self.give_up: try: config = jm_single().config log.debug('connecting') if config.get("MESSAGING", "socks5").lower() == 'true': log.debug("Using socks5 proxy %s:%d" % (self.socks5_host, self.socks5_port)) setdefaultproxy(PROXY_TYPE_SOCKS5, self.socks5_host, self.socks5_port, True) self.sock = socksocket() else: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect(self.serverport) if config.get("MESSAGING", "usessl").lower() == 'true': self.sock = ssl.wrap_socket(self.sock) self.fd = self.sock.makefile() self.password = None if self.given_password: self.password = self.given_password self.send_raw('CAP REQ :sasl') self.send_raw('USER %s b c :%s' % self.userrealname) self.nick = self.given_nick self.send_raw('NICK ' + self.nick) while 1: try: line = self.fd.readline() except AttributeError as e: raise IOError(repr(e)) if line is None: log.debug('line returned null') break if len(line) == 0: log.debug('line was zero length') break self.__handle_line(line) except IOError as e: import traceback log.debug(traceback.format_exc()) finally: try: self.fd.close() self.sock.close() except Exception as e: pass if self.on_disconnect: self.on_disconnect() log.debug('disconnected irc') if not self.give_up: time.sleep(30) log.debug('ending irc') self.give_up = True
def run(self): services = 0 #headers only st = int(time.time()) nonce = 0 start_height = 0 buffer_size = 4096 netaddr = create_net_addr(ip_to_hex('0.0.0.0'), 0) version_message = (pack('<iQQ', PROTOCOL_VERSION, services, st) + netaddr + netaddr + pack('<Q', nonce) + create_var_str(self.user_agent) + pack('<I', start_height) + ('\x01' if self.relay_txes else '\x00')) data = self.create_message('version', version_message) while True: try: log.info('connecting to bitcoin peer (magic=' + hex(self.magic) + ') at ' + str(self.remote_hostport) + ' with proxy ' + str(self.socks5_hostport)) if self.socks5_hostport == None: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) else: setdefaultproxy(PROXY_TYPE_SOCKS5, self.socks5_hostport[0], self.socks5_hostport[1], True) self.sock = socksocket() self.sock.settimeout(self.connect_timeout) self.sock.connect(self.remote_hostport) self.sock.sendall(data) break except IOError as e: if len(self.dns_seeds) == 0: raise e else: ##cycle to the next dns seed time.sleep(0.5) log.debug('connection attempts = ' + str(self.connection_attempts)) self.connection_attempts -= 1 if self.connection_attempts == 0: raise e self.dns_index = (self.dns_index + 1) % len(self.dns_seeds) self.remote_hostport = (self.dns_seeds[self.dns_index], self.remote_hostport[1]) log.info('connected') self.sock.settimeout(self.heartbeat_interval) self.closed = False try: recv_buffer = "" payload_length = -1 #-1 means waiting for header command = None checksum = None while not self.closed: try: recv_data = self.sock.recv(4096) if not recv_data or len(recv_data) == 0: raise EOFError() recv_buffer += recv_data #this is O(N^2) scaling in time, another way would be to store in a list #and combine at the end with "".join() #but this isnt really timing critical so didnt optimize it data_remaining = True while data_remaining and not self.closed: if payload_length == -1 and len(recv_buffer) >= HEADER_LENGTH: net_magic, command, payload_length, checksum = unpack('<I12sI4s', recv_buffer[:HEADER_LENGTH]) recv_buffer = recv_buffer[HEADER_LENGTH:] if net_magic != self.magic: log.error('wrong MAGIC: ' + hex(net_magic)) self.sock.close() break command = command.strip('\0') data_remaining = True else: data_remaining = False if payload_length >= 0 and len(recv_buffer) >= payload_length: payload = recv_buffer[:payload_length] recv_buffer = recv_buffer[payload_length:] if btc.bin_dbl_sha256(payload)[:4] == checksum: self.p2p_message_handler.handle_message(self, command, payload_length, payload) else: log.error('wrong checksum, dropping message, cmd=' + command + ' payloadlen=' + str(payload_length)) payload_length = -1 data_remaining = True else: data_remaining = False except socket.timeout: self.p2p_message_handler.check_keepalive(self) self.p2p_message_handler.on_heartbeat(self) except EOFError as e: self.closed = True except IOError as e: import traceback log.error("logging traceback from %s: \n" % traceback.format_exc()) self.closed = True finally: try: self.sock.close() except Exception as e: pass
def run(self): self.waiting = {} self.built_privmsg = {} self.give_up = False self.ping_reply = True self.lockcond = threading.Condition() self.lockthrottle = threading.Condition() PingThread(self).start() ThrottleThread(self).start() while not self.give_up: try: config = jm_single().config log.debug('connecting') if self.newnyms: log.debug("Grabbing new Tor identity") self.newnym() self.nick = random_nick() if config.get("MESSAGING", "socks5").lower() == 'true': log.debug("Using socks5 proxy %s:%d" % (self.socks5_host, self.socks5_port)) setdefaultproxy(PROXY_TYPE_SOCKS5, self.socks5_host, self.socks5_port, True) self.sock = socksocket() else: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect(self.serverport) if config.get("MESSAGING", "usessl").lower() == 'true': self.sock = ssl.wrap_socket(self.sock) self.fd = self.sock.makefile() self.password = None if self.given_password: self.password = self.given_password self.send_raw('CAP REQ :sasl') self.send_raw('USER %s b c :%s' % self.userrealname) self.nick = self.given_nick self.send_raw('NICK ' + self.nick) while 1: try: line = self.fd.readline() except AttributeError as e: raise IOError(repr(e)) if line is None: log.debug('line returned null') break if len(line) == 0: log.debug('line was zero length') break self.__handle_line(line) except IOError as e: log.debug(repr(e)) finally: try: self.fd.close() self.sock.close() except Exception as e: print(repr(e)) if self.on_disconnect: self.on_disconnect() log.debug('disconnected irc') if not self.give_up: time.sleep(self.reconnect_delay) if self.newnyms: time.sleep(random.randint(0,self.newnym_delay)) log.debug('ending irc') self.give_up = True