forked from PiRK/ElectrumABC
-
Notifications
You must be signed in to change notification settings - Fork 16
/
interface.py
585 lines (517 loc) · 21 KB
/
interface.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#!/usr/bin/env python3
#
# Electrum ABC - lightweight eCash client
# Copyright (C) 2020 The Electrum ABC developers
# Copyright (C) 2011 thomasv@gitorious
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import re
import socket
import ssl
import sys
import threading
import time
import traceback
from collections import namedtuple
from typing import Optional, Tuple
import requests
from pathvalidate import sanitize_filename
from . import pem, util, x509
from .printerror import PrintError, is_verbose, print_error, print_msg
from .utils import Event
ca_path = requests.certs.where()
PING_INTERVAL = 300
def Connection(server, queue, config_path, callback=None):
"""Makes asynchronous connections to a remote electrum server.
Returns the running thread that is making the connection.
Once the thread has connected, it finishes, placing a tuple on the
queue of the form (server, socket), where socket is None if
connection failed.
"""
host, port, protocol = server.rsplit(":", 2)
if protocol not in "st":
raise Exception("Unknown protocol: %s" % protocol)
c = TcpConnection(server, queue, config_path)
if callback:
callback(c)
c.start()
return c
class TcpConnection(threading.Thread, PrintError):
bad_certificate = Event()
def __init__(self, server, queue, config_path):
threading.Thread.__init__(self)
self.config_path = config_path
self.queue = queue
self.server = server
self.host, self.port, self.protocol = self.server.rsplit(":", 2)
self.host = str(self.host)
self.port = int(self.port)
self.use_ssl = self.protocol == "s"
self.daemon = True
def diagnostic_name(self):
return self.host
def check_host_name(self, peercert, name) -> bool:
"""Wrapper for ssl.match_hostname that never throws. Returns True if the
certificate matches, False otherwise. Supports whatever wildcard certs
and other bells and whistles supported by ssl.match_hostname."""
# Check that the peer has supplied a certificate.
# None/{} is not acceptable.
if not peercert:
return False
try:
ssl.match_hostname(peercert, name)
return True
except ssl.CertificateError as e:
self.print_error("SSL certificate hostname mismatch:", str(e))
return False
def get_simple_socket(self):
try:
addr_info = socket.getaddrinfo(
self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM
)
except OverflowError:
# This can happen if user specifies a huge port out of 32-bit range. See #985
self.print_error("port invalid:", self.port)
return
except socket.gaierror:
self.print_error("cannot resolve hostname")
return
except UnicodeError:
self.print_error("hostname cannot be decoded with 'idna' codec")
return
e = None
for res in addr_info:
try:
s = socket.socket(res[0], socket.SOCK_STREAM)
s.settimeout(10)
s.connect(res[4])
s.settimeout(2)
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
return s
except Exception as _e:
e = _e
continue
else:
self.print_error("failed to connect", str(e))
@staticmethod
def get_ssl_context(cert_reqs, ca_certs):
context = ssl.create_default_context(
purpose=ssl.Purpose.SERVER_AUTH, cafile=ca_certs
)
context.check_hostname = False
context.verify_mode = cert_reqs
context.minimum_version = ssl.TLSVersion.TLSv1_2
return context
def _get_socket_and_verify_ca_cert(
self, *, suppress_errors
) -> Tuple[Optional[ssl.SSLSocket], bool]:
"""Attempts to connect to the remote host, assuming it is using a CA
signed certificate. If the cert is valid then a tuple of: (wrapped
SSLSocket, False) is returned. Otherwise (None, bool) is returned on
error. If the second item in the tuple is True, then the entire
operation should be aborted due to low-level error."""
s = self.get_simple_socket()
if s is not None:
try:
context = self.get_ssl_context(
cert_reqs=ssl.CERT_REQUIRED, ca_certs=ca_path
)
s = context.wrap_socket(s, do_handshake_on_connect=True)
# validate cert
if s and self.check_host_name(s.getpeercert(), self.host):
self.print_error("SSL certificate signed by CA")
# it's good, return the wrapped socket
return s, False
# bad cert or other shenanigans, return None but inform caller
# to try alternate "pinned self-signed cert" code path
return None, False
except ssl.SSLError as e:
if not suppress_errors:
# Only show error if no pinned self-signed cert exists
self.print_error("SSL error:", e)
return None, False # inform caller to continue trying alternate
except Exception as e:
self.print_error(
"Unexpected exception in _get_socket_and_verify_ca_cert:", repr(e)
)
return None, True # inform caller to abort the operation
def get_socket(self):
if self.use_ssl:
# Try with CA first, since they are preferred over self-signed certs
# and are always accepted (even if a previous pinned self-signed
# cert exists).
cert_path = os.path.join(
self.config_path,
"certs",
sanitize_filename(self.host, replacement_text="_"),
)
has_pinned_self_signed = os.path.exists(cert_path)
s, give_up = self._get_socket_and_verify_ca_cert(
suppress_errors=has_pinned_self_signed
)
if s:
if has_pinned_self_signed:
# Delete pinned cert. They now have a valid CA-signed cert.
# This hopefully undoes the bug in previous EC versions that
# refused to consider CA-signed certs at all if the server
# ever had a self-signed cert in the past.
try:
os.remove(cert_path)
self.print_error(
"Server is now using a CA-signed certificate, deleted"
" previous self-signed certificate:",
cert_path,
)
except OSError:
pass
return s
elif give_up:
# low-level error in _get_socket_and_verify_ca_cert, give up
return
# if we get here, certificate is not CA signed, so try the alternate
# "pinned self-signed" method.
if not has_pinned_self_signed:
is_new = True
# get server certificate. Do not use ssl.get_server_certificate
# because it does not work with proxy
s = self.get_simple_socket()
if s is None:
return
try:
context = self.get_ssl_context(
cert_reqs=ssl.CERT_NONE, ca_certs=None
)
s = context.wrap_socket(s)
except ssl.SSLError as e:
self.print_error("SSL error retrieving SSL certificate:", e)
return
except Exception:
return
dercert = s.getpeercert(True)
s.close()
cert = ssl.DER_cert_to_PEM_cert(dercert)
# workaround android bug
cert = re.sub(
"([^\n])-----END CERTIFICATE-----",
"\\1\n-----END CERTIFICATE-----",
cert,
)
temporary_path = cert_path + ".temp"
util.assert_datadir_available(self.config_path)
with open(temporary_path, "w", encoding="utf-8") as f:
f.write(cert)
f.flush()
os.fsync(f.fileno())
else:
is_new = False
temporary_path = None
s = self.get_simple_socket()
if s is None:
return
if self.use_ssl:
try:
context = self.get_ssl_context(
cert_reqs=ssl.CERT_REQUIRED,
ca_certs=(temporary_path if is_new else cert_path),
)
s = context.wrap_socket(s, do_handshake_on_connect=True)
except socket.timeout:
self.print_error("timeout")
return
except ssl.SSLError as e:
self.print_error("SSL error:", e)
if e.errno != 1:
return
if is_new:
rej = cert_path + ".rej"
try:
if os.path.exists(rej):
os.unlink(rej)
os.rename(temporary_path, rej)
except OSError as e2:
self.print_error(
"Could not rename rejected certificate:", rej, repr(e2)
)
else:
util.assert_datadir_available(self.config_path)
with open(cert_path, encoding="utf-8") as f:
cert = f.read()
try:
b = pem.dePem(cert, "CERTIFICATE")
x = x509.X509(b)
except Exception:
if is_verbose:
self.print_error(
"Error checking certificate, traceback follows"
)
traceback.print_exc(file=sys.stderr)
self.print_error("wrong certificate")
self.bad_certificate(self.server, cert_path)
return
try:
x.check_date()
except Exception:
self.print_error("certificate has expired:", cert_path)
try:
os.unlink(cert_path)
self.print_error("Removed expired certificate:", cert_path)
except OSError as e2:
self.print_error(
"Could not remove expired certificate:",
cert_path,
repr(e2),
)
return
self.print_error("wrong certificate")
self.bad_certificate(self.server, cert_path)
if e.errno == 104:
return
return
if is_new:
self.print_error("saving certificate")
os.rename(temporary_path, cert_path)
return s
def run(self):
try:
socket = self.get_socket()
except OSError:
if is_verbose:
self.print_error("Error getting socket, traceback follows")
traceback.print_exc(file=sys.stderr)
socket = None
if socket:
self.print_error("connected")
self.queue.put((self.server, socket))
class Interface(PrintError):
"""The Interface class handles a socket connected to a single remote
electrum server. It's exposed API is:
- Member functions close(), fileno(), get_responses(), has_timed_out(),
ping_required(), queue_request(), send_requests()
- Member variable server.
"""
MODE_DEFAULT = "default"
MODE_BACKWARD = "backward"
MODE_BINARY = "binary"
MODE_CATCH_UP = "catch_up"
MODE_VERIFICATION = "verification"
def __init__(self, server, socket, *, max_message_bytes=0, config=None):
self.server = server
self.config = config
self.host, self.port, _ = server.rsplit(":", 2)
self.socket = socket
self.pipe = util.JSONSocketPipe(socket, max_message_bytes=max_message_bytes)
# Dump network messages. Set at runtime from the console.
self.debug = False
self.request_time = time.time()
self.unsent_requests = []
self.unanswered_requests = {}
self.last_send = time.time()
self.mode = None
def __repr__(self):
return "<{}.{} {}>".format(__name__, type(self).__name__, self.format_address())
def format_address(self):
return "{}:{}".format(self.host, self.port)
def set_mode(self, mode):
self.print_error("set_mode({})".format(mode))
self.mode = mode
def diagnostic_name(self):
return self.host
def fileno(self):
# Needed for select
return self.socket.fileno()
def close(self):
try:
self.socket.shutdown(socket.SHUT_RDWR)
except Exception:
pass
try:
self.socket.close()
except Exception:
pass
def queue_request(self, *args): # method, params, _id
"""Queue a request, later to be sent with send_requests when the
socket is available for writing."""
self.request_time = time.time()
self.unsent_requests.append(args)
ReqThrottleParams = namedtuple("ReqThrottleParams", "max chunkSize")
req_throttle_default = ReqThrottleParams(2000, 100)
@classmethod
def get_req_throttle_params(cls, config):
tup = config and config.get("network_unanswered_requests_throttle")
if not isinstance(tup, (list, tuple)) or len(tup) != 2:
tup = cls.req_throttle_default
tup = cls.ReqThrottleParams(*tup)
return tup
@classmethod
def set_req_throttle_params(cls, config, max=None, chunkSize=None):
if not config:
return
l_ = list(cls.get_req_throttle_params(config))
if max is not None:
l_[0] = max
if chunkSize is not None:
l_[1] = chunkSize
config.set_key("network_unanswered_requests_throttle", l_)
def num_requests(self):
"""If there are more than tup.max (default: 2000) unanswered requests,
don't send any more. Otherwise send more requests, but not more than tup.chunkSize
(default: 100) at a time."""
tup = self.get_req_throttle_params(self.config)
if len(self.unanswered_requests) >= tup.max:
return 0
return min(tup.chunkSize, len(self.unsent_requests))
def send_requests(self):
"""Sends queued requests. Returns False on failure."""
try:
try:
self.pipe.send_flush()
except util.timeout:
if self.debug:
self.print_error(
"still flushing send data... [{}]".format(
len(self.pipe.send_buf)
)
)
return True
self.last_send = time.time()
def make_dict(m, p, i):
return {"method": m, "params": p, "id": i}
n = self.num_requests()
wire_requests = self.unsent_requests[0:n]
self.pipe.send_all([make_dict(*r) for r in wire_requests])
except util.timeout:
# this is OK, the send is in the pipe and we'll flush it out
# eventually.
pass
except self.pipe.Closed as e:
self.print_error(str(e))
return False
except Exception:
traceback.print_exc(file=sys.stderr)
return False
self.unsent_requests = self.unsent_requests[n:]
for request in wire_requests:
if self.debug:
self.print_error("-->", request)
self.unanswered_requests[request[2]] = request
return True
def ping_required(self):
"""Returns True if a ping should be sent."""
return time.time() - self.last_send > PING_INTERVAL
def has_timed_out(self):
"""Returns True if the interface has timed out."""
if (
self.unanswered_requests
and time.time() - self.request_time > 10
and self.pipe.idle_time() > 10
):
self.print_error("timeout", len(self.unanswered_requests))
return True
return False
def get_responses(self):
"""Call if there is data available on the socket. Returns a list of
(request, response) pairs. Notifications are singleton
unsolicited responses presumably as a result of prior
subscriptions, so request is None and there is no 'id' member.
Otherwise it is a response, which has an 'id' member and a
corresponding request. If the connection was closed remotely
or the remote server is misbehaving, a (None, None) will appear.
"""
responses = []
while True:
response = None
try:
response = self.pipe.get()
except util.timeout:
break
except self.pipe.Closed as e:
self.print_error(str(e))
except Exception:
traceback.print_exc(file=sys.stderr)
if type(response) is not dict:
# time to close this connection.
if type(response) is not None:
self.print_error(
"received non-object type {}".format(type(response))
)
# signal that this connection is done.
responses.append((None, None))
break
if self.debug:
self.print_error("<--", response)
wire_id = response.get("id", None)
if wire_id is None: # Notification
# defend against funny/out-of-spec JSON
if not isinstance(response.get("method"), str):
# Malforned notification -- signal bad server
self.print_error(
"Server sent us a notification message without a 'method':",
response,
)
responses.append((None, None)) # Signal
break
# At this point the notification has a 'method' defined, so we know
# it's good.
responses.append((None, response))
else:
request = self.unanswered_requests.pop(wire_id, None)
if request:
responses.append((request, response))
else:
self.print_error("unknown wire ID", wire_id)
responses.append((None, None)) # Signal
break
return responses
def check_cert(host, cert):
try:
b = pem.dePem(cert, "CERTIFICATE")
x = x509.X509(b)
except Exception:
if is_verbose:
print_error("Error checking certificate, traceback follows")
traceback.print_exc(file=sys.stderr)
return
try:
x.check_date()
expired = False
except Exception:
expired = True
m = "host: %s\n" % host
m += "has_expired: %s\n" % expired
print_msg(m)
# Used by tests
def _match_hostname(name, val):
if val == name:
return True
return val.startswith("*.") and name.endswith(val[1:])
def test_certificates():
from .simple_config import SimpleConfig
config = SimpleConfig()
mydir = os.path.join(config.path, "certs")
certs = os.listdir(mydir)
for c in certs:
p = os.path.join(mydir, c)
with open(p, encoding="utf-8") as f:
cert = f.read()
check_cert(c, cert)
if __name__ == "__main__":
test_certificates()