electrum-bitcoinprivate/lib/interface.py

538 lines
17 KiB
Python
Raw Normal View History

#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2011 thomasv@gitorious
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import random, ast, re, errno, os
2012-03-31 02:47:16 -07:00
import threading, traceback, sys, time, json, Queue
2013-09-30 05:01:49 -07:00
import socks
import socket
import ssl
2012-02-03 06:45:41 -08:00
2012-11-06 13:20:54 -08:00
from version import ELECTRUM_VERSION, PROTOCOL_VERSION
2013-01-03 08:55:35 -08:00
from util import print_error, print_msg
2012-03-14 10:00:24 -07:00
DEFAULT_TIMEOUT = 5
2012-10-01 09:14:50 -07:00
proxy_modes = ['socks4', 'socks5', 'http']
2012-02-03 06:45:41 -08:00
2012-03-30 01:48:32 -07:00
class Interface(threading.Thread):
2013-09-08 08:23:01 -07:00
def __init__(self, config=None):
if config is None:
from simple_config import SimpleConfig
config = SimpleConfig()
threading.Thread.__init__(self)
self.daemon = True
self.config = config
self.connect_event = threading.Event()
self.subscriptions = {}
self.lock = threading.Lock()
self.rtime = 0
self.bytes_received = 0
self.is_connected = False
2012-03-30 05:15:05 -07:00
self.poll_interval = 1
2012-03-17 15:32:36 -07:00
2012-03-18 00:07:40 -07:00
#json
self.message_id = 0
2012-05-04 00:28:44 -07:00
self.unanswered_requests = {}
self.pending_transactions_for_notifications= []
2012-05-16 10:45:45 -07:00
# parse server
s = config.get('server')
host, port, protocol = s.split(':')
port = int(port)
if protocol not in 'ghst':
raise BaseException('Unknown protocol: %s'%protocol)
self.host = host
self.port = port
self.protocol = protocol
self.use_ssl = ( protocol in 'sg' )
self.proxy = self.parse_proxy_options(config.get('proxy'))
if self.proxy:
self.proxy_mode = proxy_modes.index(self.proxy["mode"]) + 1
self.server = host + ':%d:%s'%(port, protocol)
2012-03-12 09:55:33 -07:00
2012-03-23 06:37:02 -07:00
def queue_json_response(self, c):
# uncomment to debug
2012-11-05 01:26:28 -08:00
# print_error( "<--",c )
2012-03-17 15:32:36 -07:00
msg_id = c.get('id')
error = c.get('error')
2012-03-30 01:48:32 -07:00
2012-03-17 15:32:36 -07:00
if error:
2012-10-26 09:25:43 -07:00
print_error("received error:", c)
2012-11-14 06:33:44 -08:00
if msg_id is not None:
with self.lock:
2013-09-11 23:41:27 -07:00
method, params, callback = self.unanswered_requests.pop(msg_id)
callback(self,{'method':method, 'params':params, 'error':error, 'id':msg_id})
2012-11-14 06:33:44 -08:00
return
if msg_id is not None:
with self.lock:
2013-09-11 23:41:27 -07:00
method, params, callback = self.unanswered_requests.pop(msg_id)
result = c.get('result')
2012-03-17 15:32:36 -07:00
else:
2013-09-11 23:41:27 -07:00
# notification
method = c.get('method')
params = c.get('params')
if method == 'blockchain.numblocks.subscribe':
result = params[0]
params = []
2012-10-25 06:40:30 -07:00
elif method == 'blockchain.headers.subscribe':
result = params[0]
params = []
elif method == 'blockchain.address.subscribe':
addr = params[0]
result = params[1]
params = [addr]
2012-10-22 22:45:56 -07:00
with self.lock:
for k,v in self.subscriptions.items():
if (method, params) in v:
2013-09-11 23:41:27 -07:00
callback = k
2012-10-22 23:15:53 -07:00
break
2012-10-22 22:45:56 -07:00
else:
2012-10-26 09:25:43 -07:00
print_error( "received unexpected notification", method, params)
print_error( self.subscriptions )
2012-10-22 22:45:56 -07:00
return
2012-03-19 05:54:59 -07:00
2012-03-17 15:32:36 -07:00
2013-09-11 23:41:27 -07:00
callback(self, {'method':method, 'params':params, 'result':result, 'id':msg_id})
2012-03-19 07:57:47 -07:00
2013-09-11 23:41:27 -07:00
def on_version(self, i, result):
self.server_version = result
2012-10-11 16:13:54 -07:00
def init_http(self, host, port, proxy=None, use_ssl=True):
2012-03-31 02:47:16 -07:00
self.session_id = None
2013-04-07 12:48:37 -07:00
self.is_connected = True
2012-10-19 01:30:51 -07:00
self.connection_msg = ('https' if self.use_ssl else 'http') + '://%s:%d'%( self.host, self.port )
2013-01-03 12:36:25 -08:00
try:
self.poll()
except:
2013-04-09 23:03:17 -07:00
print_error("http init session failed")
self.is_connected = False
2013-01-03 12:36:25 -08:00
return
if self.session_id:
print_error('http session:',self.session_id)
self.is_connected = True
2013-04-09 23:03:17 -07:00
else:
self.is_connected = False
2012-03-17 03:56:31 -07:00
def run_http(self):
2012-03-31 02:47:16 -07:00
self.is_connected = True
2012-03-14 07:35:39 -07:00
while self.is_connected:
2012-02-08 04:37:14 -08:00
try:
2012-03-31 02:47:16 -07:00
if self.session_id:
self.poll()
2012-03-30 05:15:05 -07:00
time.sleep(self.poll_interval)
2012-03-14 07:35:39 -07:00
except socket.gaierror:
break
except socket.error:
break
2012-02-08 04:37:14 -08:00
except:
traceback.print_exc(file=sys.stdout)
2012-03-14 07:35:39 -07:00
break
self.is_connected = False
2012-02-08 04:37:14 -08:00
2012-03-19 11:20:55 -07:00
def poll(self):
2012-03-31 02:47:16 -07:00
self.send([])
2012-03-17 15:32:36 -07:00
2013-09-11 23:41:27 -07:00
def send_http(self, messages, callback):
2012-03-19 02:19:42 -07:00
import urllib2, json, time, cookielib
2013-04-09 23:03:17 -07:00
print_error( "send_http", messages )
2012-10-01 09:14:50 -07:00
if self.proxy:
socks.setdefaultproxy(self.proxy_mode, self.proxy["host"], int(self.proxy["port"]) )
socks.wrapmodule(urllib2)
2012-03-19 02:19:42 -07:00
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
2012-03-17 15:32:36 -07:00
2012-03-19 11:20:55 -07:00
t1 = time.time()
2012-03-17 15:32:36 -07:00
data = []
for m in messages:
method, params = m
if type(params) != type([]): params = [params]
data.append( { 'method':method, 'id':self.message_id, 'params':params } )
2013-09-11 23:41:27 -07:00
self.unanswered_requests[self.message_id] = method, params, callback
2012-03-17 15:32:36 -07:00
self.message_id += 1
2012-03-19 11:20:55 -07:00
if data:
data_json = json.dumps(data)
else:
# poll with GET
data_json = None
2012-03-19 02:19:42 -07:00
2012-10-17 02:35:24 -07:00
2012-03-19 02:19:42 -07:00
headers = {'content-type': 'application/json'}
if self.session_id:
headers['cookie'] = 'SESSION=%s'%self.session_id
try:
req = urllib2.Request(self.connection_msg, data_json, headers)
response_stream = urllib2.urlopen(req, timeout=DEFAULT_TIMEOUT)
except:
return
2012-03-19 02:19:42 -07:00
for index, cookie in enumerate(cj):
if cookie.name=='SESSION':
self.session_id = cookie.value
2012-03-19 11:20:55 -07:00
response = response_stream.read()
2012-05-17 04:15:55 -07:00
self.bytes_received += len(response)
2012-03-19 11:20:55 -07:00
if response:
response = json.loads( response )
if type(response) is not type([]):
2012-03-23 06:37:02 -07:00
self.queue_json_response(response)
2012-03-19 11:20:55 -07:00
else:
for item in response:
2012-03-23 06:37:02 -07:00
self.queue_json_response(item)
2012-03-17 15:32:36 -07:00
2012-03-30 05:15:05 -07:00
if response:
self.poll_interval = 1
else:
if self.poll_interval < 15:
self.poll_interval += 1
#print self.poll_interval, response
2012-03-14 08:31:31 -07:00
self.rtime = time.time() - t1
self.is_connected = True
2012-03-17 15:32:36 -07:00
2012-03-14 08:31:31 -07:00
2012-03-12 09:55:33 -07:00
def start_tcp(self):
2013-09-30 05:01:49 -07:00
if self.proxy is not None:
socks.setdefaultproxy(self.proxy_mode, self.proxy["host"], int(self.proxy["port"]))
socket.socket = socks.socksocket
# prevent dns leaks, see http://stackoverflow.com/questions/13184205/dns-over-proxy
def getaddrinfo(*args):
return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]
socket.getaddrinfo = getaddrinfo
2013-09-30 05:01:49 -07:00
if self.use_ssl:
cert_path = os.path.join( self.config.get('path'), 'certs', self.host)
2013-10-01 18:20:15 -07:00
2013-09-30 05:01:49 -07:00
if not os.path.exists(cert_path):
2013-10-01 18:24:14 -07:00
is_new = True
# get server certificate.
# Do not use ssl.get_server_certificate because it does not work with proxy
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
2013-09-30 05:01:49 -07:00
try:
s.connect((self.host, self.port))
2013-09-30 05:01:49 -07:00
except:
print_error("failed to connect", self.host, self.port)
2013-09-30 05:01:49 -07:00
return
s = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_SSLv3, cert_reqs=ssl.CERT_NONE, ca_certs=None)
dercert = s.getpeercert(True)
s.close()
cert = ssl.DER_cert_to_PEM_cert(dercert)
2013-10-01 18:20:15 -07:00
#from OpenSSL import crypto as c
#_cert = c.load_certificate(c.FILETYPE_PEM, cert)
#notAfter = _cert.get_notAfter()
#notBefore = _cert.get_notBefore()
#now = time.time()
#if now > time.mktime( time.strptime(notAfter[:-1] + "GMT", "%Y%m%d%H%M%S%Z") ):
# print "deprecated cert", host, notAfter
# return
#if now < time.mktime( time.strptime(notBefore[:-1] + "GMT", "%Y%m%d%H%M%S%Z") ):
# print "notbefore", host, notBefore
# return
2013-09-30 05:01:49 -07:00
with open(cert_path,"w") as f:
2013-10-01 18:20:15 -07:00
print_error("saving certificate for",self.host)
2013-09-30 05:01:49 -07:00
f.write(cert)
2013-10-01 18:24:14 -07:00
else:
is_new = False
2013-09-30 05:01:49 -07:00
2012-10-17 02:35:24 -07:00
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
s.settimeout(2)
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
try:
s.connect(( self.host.encode('ascii'), int(self.port)))
except:
print_error("failed to connect", self.host, self.port)
return
2013-09-30 05:01:49 -07:00
2012-10-17 02:35:24 -07:00
if self.use_ssl:
2013-09-30 05:01:49 -07:00
try:
s = ssl.wrap_socket(s,
ssl_version=ssl.PROTOCOL_SSLv3,
cert_reqs=ssl.CERT_REQUIRED,
ca_certs=cert_path,
do_handshake_on_connect=True)
except ssl.SSLError, e:
print_error("SSL error:", self.host, e)
2013-10-01 18:24:14 -07:00
# delete the certificate so we will download a new one
if is_new and e.errno == 1:
2013-10-01 18:20:15 -07:00
os.unlink(cert_path)
return
2013-09-30 05:01:49 -07:00
except:
traceback.print_exc(file=sys.stdout)
print_error("wrap_socket failed", self.host)
2013-09-30 05:01:49 -07:00
return
2013-10-01 18:20:15 -07:00
# hostname verification (disabled)
#from backports.ssl_match_hostname import match_hostname, CertificateError
#try:
# match_hostname(s.getpeercert(), self.host)
# print_error("hostname matches", self.host)
#except CertificateError, ce:
# print_error("hostname does not match", self.host, s.getpeercert())
# return
2013-09-30 05:01:49 -07:00
s.settimeout(60)
self.s = s
self.is_connected = True
print_error("connected to", self.host, self.port)
2013-09-30 05:01:49 -07:00
def run_tcp(self):
2012-03-31 02:47:16 -07:00
try:
#if self.use_ssl: self.s.do_handshake()
2012-03-13 08:43:56 -07:00
out = ''
2012-03-14 07:35:39 -07:00
while self.is_connected:
2012-10-27 01:24:43 -07:00
try:
2012-10-27 03:31:43 -07:00
timeout = False
2012-10-27 01:24:43 -07:00
msg = self.s.recv(1024)
except socket.timeout:
2012-10-27 03:31:43 -07:00
timeout = True
2012-10-27 01:24:43 -07:00
except ssl.SSLError:
2012-10-27 03:31:43 -07:00
timeout = True
2013-04-26 22:30:18 -07:00
except socket.error, err:
2013-04-27 01:22:01 -07:00
if err.errno in [11, 10035]:
2013-04-27 01:57:15 -07:00
print_error("socket errno", err.errno)
2013-04-26 22:30:18 -07:00
time.sleep(0.1)
continue
else:
traceback.print_exc(file=sys.stdout)
raise
2012-10-27 03:31:43 -07:00
if timeout:
# ping the server with server.version, as a real ping does not exist yet
2013-09-11 23:41:27 -07:00
self.send([('server.version', [ELECTRUM_VERSION, PROTOCOL_VERSION])], self.on_version)
2012-10-27 01:24:43 -07:00
continue
2012-10-27 03:31:43 -07:00
2012-03-13 08:43:56 -07:00
out += msg
2012-05-17 04:15:55 -07:00
self.bytes_received += len(msg)
2012-03-13 09:31:29 -07:00
if msg == '':
self.is_connected = False
2012-03-14 07:35:39 -07:00
2012-03-13 08:43:56 -07:00
while True:
s = out.find('\n')
if s==-1: break
c = out[0:s]
out = out[s+1:]
c = json.loads(c)
2012-03-23 06:37:02 -07:00
self.queue_json_response(c)
2012-03-17 15:32:36 -07:00
2012-03-13 08:43:56 -07:00
except:
traceback.print_exc(file=sys.stdout)
2012-03-14 07:35:39 -07:00
self.is_connected = False
2012-03-14 06:55:12 -07:00
2013-09-11 23:41:27 -07:00
def send_tcp(self, messages, callback):
"""return the ids of the requests that we sent"""
2012-03-17 08:02:28 -07:00
out = ''
ids = []
2012-03-17 08:02:28 -07:00
for m in messages:
method, params = m
request = json.dumps( { 'id':self.message_id, 'method':method, 'params':params } )
2013-09-11 23:41:27 -07:00
self.unanswered_requests[self.message_id] = method, params, callback
ids.append(self.message_id)
# uncomment to debug
# print "-->", request
2012-03-17 08:02:28 -07:00
self.message_id += 1
out += request + '\n'
while out:
try:
sent = self.s.send( out )
out = out[sent:]
2013-09-02 12:16:57 -07:00
except socket.error,e:
if e[0] in (errno.EWOULDBLOCK,errno.EAGAIN):
print_error( "EAGAIN: retrying")
time.sleep(0.1)
continue
else:
traceback.print_exc(file=sys.stdout)
# this happens when we get disconnected
print_error( "Not connected, cannot send" )
return None
return ids
2012-03-17 08:02:28 -07:00
2012-03-16 14:18:58 -07:00
2012-03-14 06:55:12 -07:00
def start_interface(self):
if self.protocol in 'st':
self.start_tcp()
elif self.protocol in 'gh':
self.start_http()
self.connect_event.set()
2013-09-12 05:58:42 -07:00
def stop_subscriptions(self):
for callback in self.subscriptions.keys():
callback(self, None)
self.subscriptions = {}
2013-09-11 23:41:27 -07:00
def send(self, messages, callback):
sub = []
for message in messages:
m, v = message
if m[-10:] == '.subscribe':
sub.append(message)
if sub:
with self.lock:
2013-09-11 23:41:27 -07:00
if self.subscriptions.get(callback) is None:
self.subscriptions[callback] = []
2012-10-28 02:22:12 -07:00
for message in sub:
2013-09-11 23:41:27 -07:00
if message not in self.subscriptions[callback]:
self.subscriptions[callback].append(message)
2012-10-11 16:13:54 -07:00
2013-03-10 08:24:52 -07:00
if not self.is_connected:
return
2012-10-17 02:35:24 -07:00
if self.protocol in 'st':
with self.lock:
2013-09-11 23:41:27 -07:00
out = self.send_tcp(messages, callback)
2012-10-11 16:13:54 -07:00
else:
# do not use lock, http is synchronous
2013-09-11 23:41:27 -07:00
out = self.send_http(messages, callback)
return out
2012-10-11 16:13:54 -07:00
2012-10-11 12:37:02 -07:00
def parse_proxy_options(self, s):
2012-10-12 02:56:41 -07:00
if type(s) == type({}): return s # fixme: type should be fixed
2012-10-11 12:37:02 -07:00
if type(s) != type(""): return None
if s.lower() == 'none': return None
proxy = { "mode":"socks5", "host":"localhost" }
args = s.split(':')
n = 0
if proxy_modes.count(args[n]) == 1:
proxy["mode"] = args[n]
n += 1
if len(args) > n:
proxy["host"] = args[n]
n += 1
if len(args) > n:
proxy["port"] = args[n]
else:
proxy["port"] = "8080" if proxy["mode"] == "http" else "1080"
return proxy
2013-09-11 23:41:27 -07:00
2012-11-24 11:31:07 -08:00
def stop(self):
2013-03-10 08:24:52 -07:00
if self.is_connected and self.protocol in 'st' and self.s:
2012-11-24 11:31:07 -08:00
self.s.shutdown(socket.SHUT_RDWR)
self.s.close()
2012-11-18 02:34:52 -08:00
2013-09-11 23:41:27 -07:00
def is_up_to_date(self):
return self.unanswered_requests == {}
2012-10-14 22:43:00 -07:00
def synchronous_get(self, requests, timeout=100000000):
# todo: use generators, unanswered_requests should be a list of arrays...
2013-09-14 12:07:54 -07:00
queue = Queue.Queue()
2013-09-11 23:41:27 -07:00
ids = self.send(requests, lambda i,r: queue.put(r))
id2 = ids[:]
res = {}
while ids:
2013-09-11 23:41:27 -07:00
r = queue.get(True, timeout)
_id = r.get('id')
if _id in ids:
ids.remove(_id)
res[_id] = r.get('result')
out = []
for _id in id2:
out.append(res[_id])
return out
2013-09-08 08:23:01 -07:00
def start(self, queue):
self.queue = queue
2013-03-10 01:24:42 -08:00
threading.Thread.start(self)
2013-09-08 08:23:01 -07:00
def run(self):
self.start_interface()
2013-09-08 08:23:01 -07:00
if self.is_connected:
2013-09-11 23:41:27 -07:00
self.send([('server.version', [ELECTRUM_VERSION, PROTOCOL_VERSION])], self.on_version)
2013-09-08 08:23:01 -07:00
self.change_status()
self.run_tcp() if self.protocol in 'st' else self.run_http()
self.change_status()
2013-09-11 23:41:27 -07:00
2013-09-08 08:23:01 -07:00
def change_status(self):
#print "change status", self.server, self.is_connected
2013-09-08 08:23:01 -07:00
self.queue.put(self)
2013-09-30 05:01:49 -07:00
if __name__ == "__main__":
q = Queue.Queue()
i = Interface({'server':'btc.it-zone.org:50002:s', 'path':'/extra/key/wallet', 'verbose':True})
i.start(q)
time.sleep(1)
exit()