network_tools.py 4.87 KB
Newer Older
Moul's avatar
Moul committed
1
from __future__ import unicode_literals
2
3
from ipaddress import ip_address
from json import loads
Moul's avatar
Moul committed
4
5
import socket
import urllib.request
6
from sys import exit, stderr
Moul's avatar
Moul committed
7

Moul's avatar
Moul committed
8

9
10
11
12
13
14
15
def discover_peers(ep, discover):
    """
    From first node, discover his known nodes.
    Remove from know nodes if nodes are down.
    If discover option: scan all network to know all nodes.
        display percentage discovering.
    """
16
    endpoints = parse_endpoints(get_request(ep, "network/peers")["peers"])
Moul's avatar
Moul committed
17
18
    if discover:
        print("Discovering network")
19
    for i, ep in enumerate(endpoints):
Moul's avatar
Moul committed
20
21
        if discover:
            print("{0:.0f}%".format(i / len(endpoints) * 100))
Moul's avatar
Moul committed
22
        if best_node(ep, False) is None:
Moul's avatar
Moul committed
23
24
25
            endpoints.remove(ep)
        elif discover:
            endpoints = recursive_discovering(endpoints, ep)
26
    return endpoints
27

Moul's avatar
Moul committed
28

29
30
31
32
33
def recursive_discovering(endpoints, ep):
    """
    Discover recursively new nodes.
    If new node found add it and try to found new node from his known nodes.
    """
34
    news = parse_endpoints(get_request(ep, "network/peers")["peers"])
35
    for new in news:
Moul's avatar
Moul committed
36
        if best_node(new, False) is not None and new not in endpoints:
37
38
            endpoints.append(new)
            recursive_discovering(endpoints, new)
39
    return endpoints
Moul's avatar
Moul committed
40

Moul's avatar
Moul committed
41
42
43
44
45
46
47
48
49
50
51
52

def parse_endpoints(rep):
    """
    endpoints[]{"domain", "ip4", "ip6", "pubkey"}
    list of dictionaries
    reps: raw endpoints
    """
    i, j, endpoints = 0, 0, []
    while (i < len(rep)):
        if (rep[i]["status"] == "UP"):
            while j < len(rep[i]["endpoints"]):
                ep = parse_endpoint(rep[i]["endpoints"][j])
Moul's avatar
Moul committed
53
                j += 1
54
55
56
                if ep is None:
                    break
                ep["pubkey"] = rep[i]["pubkey"]
Moul's avatar
Moul committed
57
                endpoints.append(ep)
Moul's avatar
Moul committed
58
59
        i += 1
        j = 0
60
    return endpoints
Moul's avatar
Moul committed
61

Moul's avatar
Moul committed
62

Moul's avatar
Moul committed
63
64
65
66
67
68
69
def parse_endpoint(rep):
    """
    rep: raw endpoint, sep: split endpoint
    domain, ip4 or ip6 could miss on raw endpoint
    """
    ep, sep = {}, rep.split(" ")
    if sep[0] == "BASIC_MERKLED_API":
Moul's avatar
Moul committed
70
71
        if check_port(sep[-1]):
            ep["port"] = sep[-1]
Moul's avatar
Moul committed
72
73
74
75
76
77
78
        if len(sep) == 5 and check_ip(sep[1]) == 0 and check_ip(sep[2]) == 4 and check_ip(sep[3]) == 6:
            ep["domain"], ep["ip4"], ep["ip6"] = sep[1], sep[2], sep[3]
        if len(sep) == 4:
            ep = endpoint_type(sep[1], ep)
            ep = endpoint_type(sep[2], ep)
        if len(sep) == 3:
            ep = endpoint_type(sep[1], ep)
79
        return ep
80
    else:
81
        return None
Moul's avatar
Moul committed
82

Moul's avatar
Moul committed
83

Moul's avatar
Moul committed
84
85
def endpoint_type(sep, ep):
    typ = check_ip(sep)
Moul's avatar
Moul committed
86
87
88
89
90
91
    if typ == 0:
        ep["domain"] = sep
    elif typ == 4:
        ep["ip4"] = sep
    elif typ == 6:
        ep["ip6"] = sep
92
    return ep
Moul's avatar
Moul committed
93

Moul's avatar
Moul committed
94

Moul's avatar
Moul committed
95
def check_ip(address):
Moul's avatar
Moul committed
96
    try:
97
        return ip_address(address).version
Moul's avatar
Moul committed
98
99
100
    except:
        return 0

Moul's avatar
Moul committed
101

102
def get_request(ep, path):
Moul's avatar
Moul committed
103
    address = best_node(ep, False)
Moul's avatar
Moul committed
104
105
    if address is None:
        return address
106
    url = "http://" + ep[address] + ":" + ep["port"] + "/" + path
107
108
    if ep["port"] == "443":
        url = "https://" + ep[address] + "/" + path
Moul's avatar
Moul committed
109
110
111
    request = urllib.request.Request(url)
    response = urllib.request.urlopen(request)
    encoding = response.info().get_content_charset('utf8')
112
    return loads(response.read().decode(encoding))
Moul's avatar
Moul committed
113

Moul's avatar
Moul committed
114

Tortue95's avatar
Tortue95 committed
115
def post_request(ep, path, postdata):
Moul's avatar
Moul committed
116
    address = best_node(ep, False)
Moul's avatar
Moul committed
117
118
    if address is None:
        return address
Tortue95's avatar
Tortue95 committed
119
120
121
    url = "http://" + ep[address] + ":" + ep["port"] + "/" + path
    if ep["port"] == "443":
        url = "https://" + ep[address] + "/" + path
Moul's avatar
Moul committed
122
    request = urllib.request.Request(url, bytes(postdata, 'utf-8'))
Tortue95's avatar
Tortue95 committed
123
124
125
    try:
        response = urllib.request.urlopen(request)
    except urllib.error.URLError as e:
126
127
        print(e, file=stderr)
        exit(1)
Tortue95's avatar
Tortue95 committed
128
    encoding = response.info().get_content_charset('utf8')
129
    return loads(response.read().decode(encoding))
Tortue95's avatar
Tortue95 committed
130

Moul's avatar
Moul committed
131

Moul's avatar
Moul committed
132
133
134
135
136
137
138
def best_node(ep, main):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    addresses, port = {"ip6", "ip4", "domain"}, int(ep["port"])
    for address in addresses:
        if address in ep:
            try:
                s.connect((ep[address], port))
139
                s.close()
140
                return address
Moul's avatar
Moul committed
141
142
143
            except:
                pass
    if main:
vincentux's avatar
vincentux committed
144
        print("Wrong node given as argument", file=stderr)
145
        exit(1)
146
    return None
Moul's avatar
Moul committed
147
148


Moul's avatar
Moul committed
149
150
151
152
def check_port(port):
    try:
        port = int(port)
    except:
153
        print("Port must be an integer", file=stderr)
154
        return False
Moul's avatar
Moul committed
155
    if (port < 0 or port > 65536):
156
        print("Wrong port number", file=stderr)
157
158
        return False
    return True
159

160

161
162
def get_current_block(ep):
    return get_request(ep, "blockchain/current")
163
164
165
166
167
168
169
170
171
172
173
174


class HeadBlock(object):
    __instance = None

    def __new__(cls, ep):
        if HeadBlock.__instance is None:
            HeadBlock.__instance = object.__new__(cls)
        return HeadBlock.__instance

    def __init__(self, ep):
        self.head_block = get_request(ep, "blockchain/current")