master
dsc 5 years ago
parent 15122052f8
commit 05f55ab33f
No known key found for this signature in database
GPG Key ID: 7BBC83D7A8810AAB

@ -55,26 +55,29 @@ class Bucket:
@staticmethod
def create_handshake_request(my_port: int = 0, network_id: bytes = None,
peer_id: bytes = b'\x41\x41\x41\x41\x41\x41\x41\x41'):
peer_id: bytes = b'\x41\x41\x41\x41\x41\x41\x41\x41',
verbose=False):
"""
Helper function to create a handshake request. Does not require
parameters but you can use them to impersonate a legit node.
:param my_port: defaults to 0
:param network_id: defaults to mainnet
:param peer_id:
:param verbose:
:return:
"""
handshake_section = Section.handshake_request(peer_id=peer_id, network_id=network_id, my_port=my_port)
bucket = Bucket.create_request(1001, section=handshake_section)
print(">> created packet \'%s\'" % P2P_COMMANDS[bucket.command])
if verbose:
print(">> created packet \'%s\'" % P2P_COMMANDS[bucket.command])
header = bucket.header()
body = bucket.payload()
return bucket
@classmethod
def from_buffer(cls, signature: c_uint64, sock: socket.socket):
def from_buffer(cls, signature: c_uint64, sock: socket.socket, verbose: bool = False):
if isinstance(signature, bytes):
signature = c_uint64(signature)
# if isinstance(buffer, bytes):
@ -108,12 +111,14 @@ class Bucket:
bucket.payload_section = None
print("<< received packet \'%s\'" % P2P_COMMANDS[bucket.command])
if verbose:
print("<< received packet \'%s\'" % P2P_COMMANDS[bucket.command])
from levin.reader import LevinReader
bucket.payload_section = LevinReader(BytesIO(bucket.payload)).read_payload()
print("<< parsed packet \'%s\'" % P2P_COMMANDS[bucket.command])
if verbose:
print("<< parsed packet \'%s\'" % P2P_COMMANDS[bucket.command])
return bucket
def header(self):

@ -3,7 +3,7 @@ import socket
from levin.section import Section
from levin.bucket import Bucket
from levin.ctypes import *
from levin.constants import *
from levin.constants import P2P_COMMANDS, LEVIN_SIGNATURE
args = sys.argv
if len(args) != 3:
@ -23,7 +23,8 @@ bucket = Bucket.create_handshake_request()
sock.send(bucket.header())
sock.send(bucket.payload())
print(">> sent packet \'%s\'" % P2P_COMMANDS[bucket.command])
# print(">> sent packet \'%s\'" % P2P_COMMANDS[bucket.command])
buckets = []
@ -35,7 +36,7 @@ while 1:
buckets.append(bucket)
if bucket.command == 1001:
peers = bucket.get_peers()
peers = bucket.get_peers() or []
for peer in peers:
print('%s:%d' % (peer['ip'].ipv4, peer['port'].value))

Loading…
Cancel
Save