-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_seed_nodes.py
145 lines (132 loc) · 4.33 KB
/
check_seed_nodes.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import sys
import socket
import socks
import requests
import pprint
from datetime import datetime
from levin.section import Section
from levin.bucket import Bucket
from levin.ctypes import *
from levin.constants import P2P_COMMANDS, LEVIN_SIGNATURE
testnet_id= b'\x12\x30\xF1\x71\x61\x04\x41\x61\x17\x31\x00\x82\x16\xA1\xA1\x11'
stagenet_id= b'\x12\x30\xF1\x71\x61\x04\x41\x61\x17\x31\x00\x82\x16\xA1\xA1\x12'
default_net_node = "https://raw.githubusercontent.com/monero-project/monero/master/src/p2p/net_node.inl"
def check_ip(host,ip):
global testnet_id,stagenet_id
network = None
if str(ip).startswith("3"):
network = stagenet_id
if str(ip).startswith("2"):
network = testnet_id
try:
sock = socket.socket()
if "onion" in host:
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050, True)
sock = socks.socksocket()
sock.settimeout(30)
sock.connect((host,ip))
except:
#sys.stderr.write("unable to connect to %s:%d\n" % (host, ip))
return False
bucket = Bucket.create_handshake_request(network_id=network)
try:
sock.send(bucket.header())
sock.send(bucket.payload())
except:
return False
# print(">> sent packet \'%s\'" % P2P_COMMANDS[bucket.command])
buckets = []
while 1:
buffer = sock.recv(8)
if not buffer:
#sys.stderr.write("Invalid response; exiting\n")
return False
if not buffer.startswith(bytes(LEVIN_SIGNATURE)):
#sys.stderr.write("Invalid response; exiting\n")
return False
return True
def main():
global default_net_node
args = sys.argv
if len(args) == 2:
default_net_node = args[1]
# overwrite net_node with file at url
r = requests.get(default_net_node)
lines = r.iter_lines(decode_unicode=True)
at_ipv4=0
at_anon=0
nodes = []
for line in lines:
if "::get_ip_seed_nodes()" in line:
at_ipv4 = 1
if "full_addrs.insert" in line and at_ipv4 == 1:
nodes.append(line.split("\"")[1])
if "return full_addrs;" in line and at_ipv4 == 1:
at_ipv4 = 0
if "onion" in line:
nodes.append(line.split("\"")[1])
statuses = {}
for node in nodes:
host = node.split(":")[0]
ip=node.split(":")[1]
attempts = 0
while attempts < 3:
try:
if check_ip(host, int(ip)):
print(f"{node} online")
attempts = 0
break
else:
attempts+=1
except:
print(f"{node} timeout")
attempts = 4
if attempts != 0:
print(f"{node} offline")
statuses[node] = "😡"
else:
statuses[node] = "🙂"
parse_readme(statuses)
def parse_readme(statuses):
# current dateTime
now = datetime.now()
date_time_str = now.strftime("%Y-%m-%d")
with open("../readme.md", "r") as f:
lines = f.readlines()
# get existing statuses then clean the slated
do=1
new_file=""
with open("../readme.md" ,"w") as f:
for line in lines:
print(line)
if "|---|" in line:
do = 0
f.write(line)
new_file+=line
continue
if not line.strip() and do == 0:
print("blank line and do=0")
do = 1
if do == 0:
node = line.split("|")[1]
status = line.split("|")[2]
if node in statuses:
statuses[node] = status + statuses[node]
if len(statuses[node]) > 7:
statuses[node] = statuses[node][1:]
if do == 1:
print(f"writing line")
f.write(line)
new_file+=line
with open("../readme.md" ,"w") as f:
for line in new_file.splitlines():
line+="\n"
if "|---|" in line:
f.write(line)
for node in statuses:
f.write(f"|{node}|{statuses[node]}|\n")
f.write(f"\nLast update: {date_time_str}")
continue
if "Last update:" not in line:
f.write(line)
main()