electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 87a28d31a457801cee68bfbd3458497b5407ff4e
parent 27f4a0c28e9b07f59342fdb790243ebb4f25b008
Author: thomasv <thomasv@gitorious>
Date:   Wed, 17 Oct 2012 11:35:24 +0200

client-side support for SSL

Diffstat:
Mlib/interface.py | 57+++++++++++++++++++++++++++++++++++----------------------
1 file changed, 35 insertions(+), 22 deletions(-)

diff --git a/lib/interface.py b/lib/interface.py @@ -27,7 +27,7 @@ from util import print_error DEFAULT_TIMEOUT = 5 DEFAULT_SERVERS = [ 'electrum.novit.ro:50001:t', - 'ecdsa.org:50001:t', + #'ecdsa.org:50002:s', 'electrum.bitcoins.sk:50001:t', 'uncle-enzo.info:50001:t', 'electrum.bytesized-hosting.com:50001:t', @@ -55,12 +55,13 @@ def pick_random_interface(): class InterfaceAncestor(threading.Thread): - def __init__(self, host, port, proxy=None): + def __init__(self, host, port, proxy=None, use_ssl=True): threading.Thread.__init__(self) self.daemon = True self.host = host self.port = port self.proxy = proxy + self.use_ssl = use_ssl self.servers = [] # actual list from IRC self.rtime = 0 @@ -125,9 +126,10 @@ class InterfaceAncestor(threading.Thread): class HttpStratumInterface(InterfaceAncestor): """ non-persistent connection. synchronous calls""" - def __init__(self, host, port, proxy=None): - InterfaceAncestor.__init__(self, host, port, proxy) + def __init__(self, host, port, proxy=None, use_ssl=True): + InterfaceAncestor.__init__(self, host, port, proxy, use_ssl) self.session_id = None + self.connection_msg = "http://%s:%d"%(self.host,self.port) def get_history(self, address): self.send([('blockchain.address.get_history', [address] )]) @@ -182,7 +184,11 @@ class HttpStratumInterface(InterfaceAncestor): # poll with GET data_json = None - host = 'http://%s:%d'%( self.host, self.port ) + if self.use_ssl: + host = 'https://%s:%d'%( self.host, self.port ) + else: + host = 'http://%s:%d'%( self.host, self.port ) + headers = {'content-type': 'application/json'} if self.session_id: headers['cookie'] = 'SESSION=%s'%self.session_id @@ -220,29 +226,37 @@ class HttpStratumInterface(InterfaceAncestor): class TcpStratumInterface(InterfaceAncestor): """json-rpc over persistent TCP connection, asynchronous""" - def __init__(self, host, port, proxy=None): - InterfaceAncestor.__init__(self, host, port, proxy) + def __init__(self, host, port, proxy=None, use_ssl=True): + InterfaceAncestor.__init__(self, host, port, proxy, use_ssl) self.init_socket() def init_socket(self): + import ssl global proxy_modes self.connection_msg = "%s:%d"%(self.host,self.port) if self.proxy is None: - self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) + s = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) else: self.connection_msg += " using proxy %s:%s:%s"%(self.proxy.get('mode'), self.proxy.get('host'), self.proxy.get('port')) import socks - self.s = socks.socksocket() - self.s.setproxy(proxy_modes.index(self.proxy["mode"]), self.proxy["host"], int(self.proxy["port"]) ) - self.s.settimeout(2) - self.s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) + s = socks.socksocket() + s.setproxy(proxy_modes.index(self.proxy["mode"]), self.proxy["host"], int(self.proxy["port"]) ) + + if self.use_ssl: + s = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_TLSv1) + + s.settimeout(2) + s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) + try: - self.s.connect(( self.host.encode('ascii'), int(self.port))) + s.connect(( self.host.encode('ascii'), int(self.port))) + s.settimeout(60) + self.s = s self.is_connected = True - self.s.settimeout(60) self.send([('server.version', [ELECTRUM_VERSION])]) except: self.is_connected = False + self.s = None def run(self): try: @@ -313,23 +327,22 @@ class Interface(TcpStratumInterface, HttpStratumInterface): self.server = host + ':%d:%s'%(port, protocol) #print protocol, host, port - if protocol == 't': - TcpStratumInterface.__init__(self, host, port, proxy) - elif protocol == 'h': - HttpStratumInterface.__init__(self, host, port, proxy) + if protocol in 'st': + TcpStratumInterface.__init__(self, host, port, proxy, use_ssl=(protocol=='s')) + elif protocol in 'gh': + HttpStratumInterface.__init__(self, host, port, proxy, use_ssl=(protocol=='g')) else: - print_error("Error: Unknown protocol") - TcpStratumInterface.__init__(self, host, port, proxy) + raise BaseException('Unknown protocol: %s'%protocol) def run(self): - if self.protocol == 't': + if self.protocol in 'st': TcpStratumInterface.run(self) else: HttpStratumInterface.run(self) def send(self, messages): - if self.protocol == 't': + if self.protocol in 'st': TcpStratumInterface.send(self, messages) else: HttpStratumInterface.send(self, messages)