def _patched_deser_version(f: BytesIO): # This function taken from vv - expanded to have fRelay actually optional (LGPL v3) # https://github.com/petertodd/python-bitcoinlib/blob/b5540e8a8a138f2a4872c34ce4223b8f4e6856d9/bitcoin/messages.py#L137-L162 c = messages.msg_version() c.nVersion = struct.unpack(b"<i", ser_read(f, 4))[0] if c.nVersion == 10300: c.nVersion = 300 c.nServices = struct.unpack(b"<Q", ser_read(f, 8))[0] c.nTime = struct.unpack(b"<q", ser_read(f, 8))[0] c.addrTo = CAddress.stream_deserialize(f, True) if c.nVersion >= 106: c.addrFrom = CAddress.stream_deserialize(f, True) c.nNonce = struct.unpack(b"<Q", ser_read(f, 8))[0] c.strSubVer = VarStringSerializer.stream_deserialize(f) if c.nVersion >= 209: c.nStartingHeight = struct.unpack(b"<i", ser_read(f, 4))[0] else: c.nStartingHeight = None else: c.addrFrom = None c.nNonce = None c.strSubVer = None c.nStartingHeight = None if c.nVersion >= 70001: relay_bytes = f.read(1) # modified: fRelay is OPTIONAL for some reason, luckily it is the last field if len(relay_bytes) == 1: c.fRelay = struct.unpack(b"<B", relay_bytes)[0] else: c.fRelay = True else: c.fRelay = True return c
def connection_mock(signed_transaction): connection = MagicMock() connection.getsockname.return_value = ('127.0.0.1', 8800) connection.getpeername.return_value = ('127.0.0.1', 8800) connection.getpeername.return_value = ('127.0.0.1', 8800) protocol_version = 6002 version = msg_version(protocol_version).to_bytes() verack = msg_verack(protocol_version).to_bytes() getdata = msg_getdata(protocol_version) getdata = getdata.msg_deser(BytesIO(b'\x01\x01\x00\x00\x00' + signed_transaction.tx.GetHash())).to_bytes() connection.recv.side_effect = ( version + verack, getdata, ) capture = BitcoinTestNet.capture_messages def capture_messages_mock(*args, **kwargs): if msg_reject in args[1]: return None else: return capture(*args, **kwargs) with patch('socket.create_connection', return_value=connection): with patch('socket.gethostbyname_ex', return_value=(None, None, ['127.0.0.1'])): with patch.object(BitcoinTestNet, 'capture_messages', new=capture_messages_mock): yield
def connect(self): info('connecting to dashd') self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.host, self.port)) self.sock.send(msg_version().to_bytes()) info('connected to dashd') return self
def handshake(self): # Send a "version" message to start the handshake msg = msg_version() # See BIP 111 (https://github.com/bitcoin/bips/blob/master/bip-0111.mediawiki) msg.nVersion = 70011 msg.fRelay = False # If false then broadcast transactions will not be announced until a filter{load,add,clear} command is received self.send_message(msg)
def _make_version_pkt(self): msg = msg_version() msg.nVersion = 70002 msg.addrTo.ip = self.link.ip msg.addrTo.port = self.link.port msg.addrFrom.ip = BitcoinSocket.client_ip msg.addrFrom.port = self.port return msg
def _make_version(self): msg = messages.msg_version() msg.nVersion = 70002 msg.addrTo.ip = self.ip msg.addrTo.port = self.port # Satoshi client does not fill addrFrom either -> # https://github.com/bitcoin/bitcoin/blob/c2c4dbaebd955ad2829364f7fa5b8169ca1ba6b9/src/net_processing.cpp#L494 return msg
def version_pkt(server_ip, server_port, ipv4=True): msg = msg_version() msg.nVersion = 60002 msg.addrTo.ip = server_ip msg.addrTo.port = server_port msg.addrFrom.ip = my_ipv4 if ipv4 else my_ipv6 msg.addrFrom.port = my_port return msg
def version_pkt(_client_ip, _server_ip): msg = msg_version() msg.nVersion = 70002 msg.addrTo.ip = _server_ip msg.addrTo.port = PORT msg.addrFrom.ip = _client_ip msg.addrFrom.port = PORT return msg
def version_pkt(client_ip, server_ip, port): msg = msg_version() msg.nVersion = 70002 msg.addrTo.ip = server_ip msg.addrTo.port = port msg.addrFrom.ip = client_ip msg.addrFrom.port = port return msg
def sendVersionMessage(self): #stuff version msg into sendbuf vt = msg_version() vt.addrTo.ip = self.dstaddr vt.addrTo.port = self.dstport vt.addrFrom.ip = "0.0.0.0" vt.addrFrom.port = 0 vt.nStartingHeight = self.node.chaindb.getheight() vt.strSubVer = MY_SUBVERSION self.send_message(vt)
def btc_version_pkt(s, spoofed_ip): c = msg_version() c.nVersion = 70002 c.addrTo.ip = s.target_ip c.addrTo.port = s.dport c.addrFrom.ip = spoofed_ip c.addrFrom.port = s.sport return s.btc_add_magic(c)
def send_version(self, connection): v = messages.msg_version() v.addr_from = net.CAddress() v.addr_from.ip = '127.0.0.1' v.addr_from.port = 8333 v.addr_recv = net.CAddress() v.addr_recv.ip = connection.host[0] v.addr_recv.port = connection.host[1] v.nStartingHeight = 0 connection.send('version', v)
def version_packet(self) -> bitcoin.messages.msg_version: ''' Creating version package for a current network. Returns: bitcoin.messages.msg_version ''' packet = msg_version(170002) packet.addrFrom.ip, packet.addrFrom.port = self.connection.getsockname( ) packet.addrTo.ip, packet.addrTo.port = self.connection.getpeername() return packet
def _run(self): self.log.info("Connecting to {}:{}" .format(self.dstaddr, self.dstport)) try: self.sock.connect((self.dstaddr, self.dstport)) except: self.handle_close() # stuff version msg into sendbuf vt = messages.msg_version() vt.addrTo.ip = self.dstaddr vt.addrTo.port = self.dstport vt.addrFrom.ip = "0.0.0.0" vt.addrFrom.port = 0 if self.settings['spv']: vt.nServices = 0 else: vt.nServices = 1 vt.nStartingHeight = self.chaindb.getheight() vt.strSubVer = MY_SUBVERSION self.send_message(vt) self.log.info("Connected to {}:{}" .format(self.dstaddr, self.dstport)) try: while True: try: t = self.sock.recv(8192) if len(t) <= 0: raise ValueError except (IOError, ValueError): self.handle_close() return self.recvbuf += t self.got_data() except Exception: self.log.error( "Connection to peer crashed! {}:{}, ver {}" .format(self.dstaddr, self.dstport, self.ver_send), exc_info=True) self.handle_close()
def __init__(self, dstaddr, dstport, log, peermgr, mempool, chaindb: ChainDb): Greenlet.__init__(self) self.dstaddr = dstaddr self.dstport = dstport self.mempool = mempool self.log = log self.chaindb = chaindb self.sock = gevent.socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.recvbuf = b"" self.ver_send = MIN_PROTO_VERSION self.ver_recv = MIN_PROTO_VERSION self.last_sent = 0 self.getblocks_ok = True self.last_block_rx = time.time() self.last_getblocks = 0 self.remote_height = -1 self.last_want = 0 self.addresses_seen = [] self.hash_continue = None self.log.info("connecting") try: self.sock.connect((dstaddr, dstport)) except: self.handle_close() vt = msg_version() vt.addrTo.ip = self.dstaddr vt.addrTo.port = self.dstport vt.addrFrom.ip = "0.0.0.0" vt.addrFrom.port = 0 vt.nVersion = PROTO_VERSION vt.nStartingHeight = 0 vt.strSubVer = MY_SUBVERSION self.send_message(vt) gevent.spawn(self.broadcast_listen)
def _run(self): self.log.info("Connecting to {}:{}".format(self.dstaddr, self.dstport)) try: self.sock.connect((self.dstaddr, self.dstport)) except: self.handle_close() # stuff version msg into sendbuf vt = messages.msg_version() vt.addrTo.ip = self.dstaddr vt.addrTo.port = self.dstport vt.addrFrom.ip = "0.0.0.0" vt.addrFrom.port = 0 if self.settings['spv']: vt.nServices = 0 else: vt.nServices = 1 vt.nStartingHeight = self.chaindb.getheight() vt.strSubVer = MY_SUBVERSION self.send_message(vt) self.log.info("Connected to {}:{}".format(self.dstaddr, self.dstport)) try: while True: try: t = self.sock.recv(8192) if len(t) <= 0: raise ValueError except (IOError, ValueError): self.handle_close() return self.recvbuf += t self.got_data() except Exception: self.log.error("Connection to peer crashed! {}:{}, ver {}".format( self.dstaddr, self.dstport, self.ver_send), exc_info=True) self.handle_close()
def version_packet(self): packet = msg_version(170002) packet.addrFrom.ip, packet.addrFrom.port = self.connection.getsockname( ) packet.addrTo.ip, packet.addrTo.port = self.connection.getpeername() return packet
'24.17.108.172', \ '123.58.55.110', \ '136.227.111.41', \ '159.19.99.225', \ '69.207.178.52', \ 'bitcoin.coinprism.com'] for HOST in HOSTS: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(1) data = "" try: s.connect((HOST, PORT)) msg = msg_version() s.send(msg.to_bytes()) data = s.recv(1024) except socket.timeout: print("Timeout") except socket.error: print("No clue but error") if re.search("Satoshi", str(data)): print(HOST, " Hurray! :)\n") else: print(HOST, " Boo! :(\n") s.close()