-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
90 lines (80 loc) · 2.57 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import socket
import time
import select
import sys
sys.path.append("kaitai_gen/out")
from kaitai_gen.out import bitmessage as b
from kaitaistruct import ValidationNotEqualError
from assemble import *
invs = set()
def hello(s):
s.sendall(assemble_packet("version", assemble_version_payload()))
def handle_message(s, msg):
msg = msg.message
print(f"{msg.header.command} from {s.getpeername()}")
if (msg.header.command == "version"):
s.sendall(assemble_packet("verack", b""))
s.sendall(assemble_empty_addr())
s.sendall(assemble_empty_inv())
if (msg.header.command == "addr"):
remotes = []
for addr in msg.payload.addr_list:
import ipaddress
remote = ((ipaddress.ip_address(addr.payload.ip[-4:]).__str__(), addr.payload.port))
remotes.append(remote)
print(remotes)
exit()
if (msg.header.command == "inv"):
for vect in msg.payload.inventory:
if vect.hash not in invs:
s.sendall(assemble_simple_getdata(vect.hash))
invs.add(vect.hash)
print(f"There are {len(invs)} invs")
if (msg.header.command == "object"):
print(msg.payload.object_payload_type)
if(msg.payload.object_payload_type == "broadcast"):
print(msg.payload.object_payload)
exit()
def handle_socket(socket):
s = socket
try:
data = s.recv(2*1024)
except:
return
if data:
msg = b.Bitmessage.from_bytes(data)
while True:
try:
msg._read()
except:
return
handle_message(s, msg)
if __name__ == "__main__":
hosts = []
for host in socket.gethostbyname_ex("bootstrap8444.bitmessage.org")[2]:
if host not in hosts:
hosts.append((host, 8444))
for host in socket.gethostbyname_ex("bootstrap8080.bitmessage.org")[2]:
if host not in hosts:
hosts.append((host, 8080))
sockets = []
for i, host in enumerate(hosts):
print(f"#{i}")
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.25)
s.connect(host)
s.settimeout(None)
#s.setblocking(False)
except Exception as e:
print(e)
continue
sockets.append(s)
print(sockets)
hello(s)
handle_socket(s)
while True:
readable, _, _ = select.select(sockets, [], [])
for connection in readable:
#print(connection)
handle_socket(connection)