/
net.py
285 lines (221 loc) · 8.37 KB
/
net.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
__copyright__ = "Copyright (C) 2018 The OctoPrint Project - Released under terms of the AGPLv3 License"
import logging
import os
import socket
import sys
import netaddr
import netifaces
import requests
import werkzeug.http
from werkzeug.utils import secure_filename
_cached_check_v6 = None
def check_v6():
global _cached_check_v6
def f():
if not socket.has_ipv6:
return False
try:
socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
except Exception:
# "[Errno 97] Address family not supported by protocol" or anything else really...
return False
return True
if _cached_check_v6 is None:
_cached_check_v6 = f()
return _cached_check_v6
HAS_V6 = check_v6()
if hasattr(socket, "IPPROTO_IPV6") and hasattr(socket, "IPV6_V6ONLY"):
# Dual stack support, hooray!
IPPROTO_IPV6 = socket.IPPROTO_IPV6
IPV6_V6ONLY = socket.IPV6_V6ONLY
else:
if sys.platform == "win32":
# Python on Windows lacks IPPROTO_IPV6, but supports the socket options just fine, let's redefine it
IPPROTO_IPV6 = 41
IPV6_V6ONLY = 27
else:
# Whatever we are running on here, we don't want to use V6 on here due to lack of dual stack support
HAS_V6 = False
def get_lan_ranges(additional_private=None):
logger = logging.getLogger(__name__)
if additional_private is None or not isinstance(additional_private, (list, tuple)):
additional_private = []
def to_ipnetwork(address):
prefix = address["netmask"]
if "/" in prefix:
# v6 notation in netifaces output, e.g. "ffff:ffff:ffff:ffff::/64"
_, prefix = prefix.split("/")
addr = strip_interface_tag(address["addr"])
return netaddr.IPNetwork(f"{addr}/{prefix}")
subnets = []
for interface in netifaces.interfaces():
addrs = netifaces.ifaddresses(interface)
for v4 in addrs.get(socket.AF_INET, ()):
try:
subnets.append(to_ipnetwork(v4))
except Exception:
if logger.isEnabledFor(logging.DEBUG):
logger.exception(
"Error while trying to add v4 network to local subnets: {!r}".format(
v4
)
)
if HAS_V6:
for v6 in addrs.get(socket.AF_INET6, ()):
try:
subnets.append(to_ipnetwork(v6))
except Exception:
if logger.isEnabledFor(logging.DEBUG):
logger.exception(
"Error while trying to add v6 network to local subnets: {!r}".format(
v6
)
)
for additional in additional_private:
try:
subnets.append(netaddr.IPNetwork(additional))
except Exception:
if logger.isEnabledFor(logging.DEBUG):
logger.exception(
"Error while trying to add additional private network to local subnets: {}".format(
additional
)
)
subnets += list(netaddr.ip.IPV4_PRIVATE) + [
netaddr.ip.IPV4_LOOPBACK,
netaddr.ip.IPV4_LINK_LOCAL,
]
if HAS_V6:
subnets += list(netaddr.ip.IPV6_PRIVATE) + [
netaddr.IPNetwork(netaddr.ip.IPV6_LOOPBACK),
netaddr.ip.IPV6_LINK_LOCAL,
]
return subnets
def is_lan_address(address, additional_private=None):
if not address:
# no address is LAN address
return True
try:
address = sanitize_address(address)
ip = netaddr.IPAddress(address)
subnets = get_lan_ranges(additional_private=additional_private)
if any(map(lambda subnet: ip in subnet, subnets)):
return True
return False
except Exception:
# we are extra careful here since an unhandled exception in this method will effectively nuke the whole UI
logging.getLogger(__name__).exception(
"Error while trying to determine whether {} is a local address".format(
address
)
)
return True
def sanitize_address(address):
address = unmap_v4_as_v6(address)
address = strip_interface_tag(address)
return address
def strip_interface_tag(address):
if "%" in address:
# interface comment, e.g. "fe80::457f:bbee:d579:1063%wlan0"
address = address[: address.find("%")]
return address
def unmap_v4_as_v6(address):
if address.lower().startswith("::ffff:") and "." in address:
# ipv6 mapped ipv4 address, unmap
address = address[len("::ffff:") :]
return address
def interface_addresses(family=None, interfaces=None, ignored=None):
"""
Retrieves all of the host's network interface addresses.
"""
import netifaces
if not family:
family = netifaces.AF_INET
if interfaces is None:
interfaces = netifaces.interfaces()
if ignored is not None:
interfaces = [i for i in interfaces if i not in ignored]
for interface in interfaces:
try:
ifaddresses = netifaces.ifaddresses(interface)
except Exception:
continue
if family in ifaddresses:
for ifaddress in ifaddresses[family]:
address = netaddr.IPAddress(ifaddress["addr"])
if not address.is_link_local() and not address.is_loopback():
yield ifaddress["addr"]
def address_for_client(
host, port, timeout=3.05, addresses=None, interfaces=None, ignored=None
):
"""
Determines the address of the network interface on this host needed to connect to the indicated client host and port.
"""
if addresses is None:
addresses = interface_addresses(interfaces=interfaces, ignored=ignored)
for address in addresses:
try:
if server_reachable(host, port, timeout=timeout, proto="udp", source=address):
return address
except Exception:
continue
def server_reachable(host, port, timeout=3.05, proto="tcp", source=None):
"""
Checks if a server is reachable
Args:
host (str): host to check against
port (int): port to check against
timeout (float): timeout for check
proto (str): ``tcp`` or ``udp``
source (str): optional, socket used for check will be bound against this address if provided
Returns:
boolean: True if a connection to the server could be opened, False otherwise
"""
import socket
if proto not in ("tcp", "udp"):
raise ValueError("proto must be either 'tcp' or 'udp'")
if HAS_V6:
families = [socket.AF_INET6, socket.AF_INET]
else:
families = [socket.AF_INET]
for family in families:
try:
sock = socket.socket(
family, socket.SOCK_DGRAM if proto == "udp" else socket.SOCK_STREAM
)
sock.settimeout(timeout)
if source is not None:
sock.bind((source, 0))
sock.connect((host, port))
return True
except Exception:
pass
return False
def resolve_host(host):
import socket
from octoprint.util import to_unicode
try:
return [to_unicode(x[4][0]) for x in socket.getaddrinfo(host, 80)]
except Exception:
return []
def download_file(url, folder, max_length=None, connect_timeout=3.05, read_timeout=7):
with requests.get(url, stream=True, timeout=(connect_timeout, read_timeout)) as r:
r.raise_for_status()
filename = None
if "Content-Disposition" in r.headers.keys():
_, options = werkzeug.http.parse_options_header(
r.headers["Content-Disposition"]
)
filename = options.get("filename")
if filename is None:
filename = url.split("/")[-1]
filename = secure_filename(filename)
assert len(filename) > 0
# TODO check content-length against safety limit
path = os.path.abspath(os.path.join(folder, filename))
assert path.startswith(folder)
with open(path, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
return path