2012-01-19 08:11:36 -08:00
|
|
|
#!/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/>.
|
|
|
|
|
|
|
|
|
2013-10-01 02:24:55 -07:00
|
|
|
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
|
2013-10-01 02:24:55 -07:00
|
|
|
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
|
2013-10-02 01:21:25 -07:00
|
|
|
from simple_config import SimpleConfig
|
2012-10-11 11:10:12 -07:00
|
|
|
|
2014-06-27 02:36:51 -07:00
|
|
|
import x509
|
2012-05-02 05:21:58 -07:00
|
|
|
|
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
|
|
|
|
2014-07-27 15:13:40 -07:00
|
|
|
import util
|
|
|
|
|
2013-10-02 01:21:25 -07:00
|
|
|
|
|
|
|
def cert_verify_hostname(s):
|
|
|
|
# hostname verification (disabled)
|
|
|
|
from backports.ssl_match_hostname import match_hostname, CertificateError
|
|
|
|
try:
|
|
|
|
match_hostname(s.getpeercert(True), host)
|
|
|
|
print_error("hostname matches", host)
|
|
|
|
except CertificateError, ce:
|
|
|
|
print_error("hostname did not match", host)
|
|
|
|
|
2013-10-02 00:22:13 -07:00
|
|
|
|
|
|
|
|
2014-07-29 00:28:27 -07:00
|
|
|
def Interface(server, config = None):
|
|
|
|
host, port, protocol = server.split(':')
|
|
|
|
port = int(port)
|
|
|
|
if protocol in 'st':
|
|
|
|
return TcpInterface(server, config)
|
|
|
|
elif protocol in 'hg':
|
|
|
|
return HttpInterface(server, config)
|
|
|
|
else:
|
|
|
|
raise Exception('Unknown protocol: %s'%protocol)
|
|
|
|
|
|
|
|
class TcpInterface(threading.Thread):
|
2012-10-11 11:10:12 -07:00
|
|
|
|
2013-10-06 03:28:45 -07:00
|
|
|
def __init__(self, server, config = None):
|
2013-10-01 00:01:46 -07:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.daemon = True
|
2013-10-06 03:28:45 -07:00
|
|
|
self.config = config if config is not None else SimpleConfig()
|
2013-10-01 00:01:46 -07:00
|
|
|
self.lock = threading.Lock()
|
|
|
|
|
|
|
|
self.is_connected = False
|
2013-10-04 07:00:20 -07:00
|
|
|
self.debug = False # dump network messages. can be changed at runtime using the console
|
2012-03-18 00:07:40 -07:00
|
|
|
self.message_id = 0
|
2012-05-04 00:28:44 -07:00
|
|
|
self.unanswered_requests = {}
|
2012-05-16 10:45:45 -07:00
|
|
|
|
2013-10-01 00:01:46 -07:00
|
|
|
# parse server
|
2013-10-06 03:28:45 -07:00
|
|
|
self.server = server
|
2014-07-29 00:28:27 -07:00
|
|
|
self.host, self.port, self.protocol = self.server.split(':')
|
|
|
|
self.port = int(self.port)
|
|
|
|
self.use_ssl = (self.protocol == 's')
|
2013-10-06 03:28:45 -07:00
|
|
|
self.proxy = self.parse_proxy_options(self.config.get('proxy'))
|
2013-10-01 02:24:55 -07:00
|
|
|
if self.proxy:
|
|
|
|
self.proxy_mode = proxy_modes.index(self.proxy["mode"]) + 1
|
2013-10-01 00:01:46 -07:00
|
|
|
|
|
|
|
|
2014-07-29 00:28:27 -07:00
|
|
|
def process_response(self, response):
|
2013-10-04 07:00:20 -07:00
|
|
|
if self.debug:
|
2014-07-29 00:28:27 -07:00
|
|
|
print_error("<--", response)
|
2012-07-06 13:36:13 -07:00
|
|
|
|
2014-07-29 00:28:27 -07:00
|
|
|
msg_id = response.get('id')
|
|
|
|
error = response.get('error')
|
|
|
|
result = response.get('result')
|
2014-07-27 15:13:40 -07:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
if error:
|
2014-07-29 00:28:27 -07:00
|
|
|
print_error("received error:", response)
|
2012-04-29 09:33:01 -07:00
|
|
|
return
|
2014-07-27 15:13:40 -07:00
|
|
|
|
2012-04-29 09:33:01 -07:00
|
|
|
if msg_id is not None:
|
2014-07-27 15:13:40 -07:00
|
|
|
with self.lock:
|
|
|
|
method, params, _id, queue = self.unanswered_requests.pop(msg_id)
|
|
|
|
if queue is None:
|
|
|
|
queue = self.response_queue
|
2012-03-17 15:32:36 -07:00
|
|
|
else:
|
2013-09-11 23:41:27 -07:00
|
|
|
# notification
|
2014-07-29 00:28:27 -07:00
|
|
|
method = response.get('method')
|
|
|
|
params = response.get('params')
|
2014-07-27 15:13:40 -07:00
|
|
|
_id = None
|
2014-07-29 00:28:27 -07:00
|
|
|
queue = self.response_queue
|
|
|
|
# restore parameters
|
2012-04-29 09:33:01 -07:00
|
|
|
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 = []
|
2012-04-29 09:33:01 -07:00
|
|
|
elif method == 'blockchain.address.subscribe':
|
|
|
|
addr = params[0]
|
|
|
|
result = params[1]
|
|
|
|
params = [addr]
|
|
|
|
|
2014-07-29 00:28:27 -07:00
|
|
|
if method == 'server.version':
|
|
|
|
self.server_version = result
|
|
|
|
return
|
|
|
|
|
2014-07-27 15:13:40 -07:00
|
|
|
queue.put((self, {'method':method, 'params':params, 'result':result, 'id':_id}))
|
2012-03-19 07:57:47 -07:00
|
|
|
|
|
|
|
|
2013-10-01 00:01:46 -07:00
|
|
|
def start_tcp(self):
|
2013-09-30 05:01:49 -07:00
|
|
|
|
2013-10-01 02:24:55 -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:
|
2013-10-06 03:28:45 -07:00
|
|
|
cert_path = os.path.join( self.config.path, 'certs', self.host)
|
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
|
2013-10-01 02:24:55 -07:00
|
|
|
# get server certificate.
|
|
|
|
# Do not use ssl.get_server_certificate because it does not work with proxy
|
2013-12-13 00:22:39 -08:00
|
|
|
try:
|
|
|
|
l = socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM)
|
|
|
|
except socket.gaierror:
|
|
|
|
print_error("error: cannot resolve", self.host)
|
|
|
|
return
|
|
|
|
|
|
|
|
for res in l:
|
2013-11-13 11:35:35 -08:00
|
|
|
try:
|
|
|
|
s = socket.socket( res[0], socket.SOCK_STREAM )
|
|
|
|
s.connect(res[4])
|
|
|
|
except:
|
|
|
|
s = None
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
s = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_SSLv3, cert_reqs=ssl.CERT_NONE, ca_certs=None)
|
|
|
|
except ssl.SSLError, e:
|
2013-11-14 00:23:38 -08:00
|
|
|
print_error("SSL error retrieving SSL certificate:", self.host, e)
|
2013-11-13 11:35:35 -08:00
|
|
|
s = None
|
2013-11-14 00:23:38 -08:00
|
|
|
|
2013-11-13 11:35:35 -08:00
|
|
|
break
|
|
|
|
|
2013-11-14 00:23:38 -08:00
|
|
|
if s is None:
|
2013-09-30 05:01:49 -07:00
|
|
|
return
|
2013-10-01 02:24:55 -07:00
|
|
|
|
|
|
|
dercert = s.getpeercert(True)
|
|
|
|
s.close()
|
|
|
|
cert = ssl.DER_cert_to_PEM_cert(dercert)
|
2013-10-08 04:08:13 -07:00
|
|
|
# workaround android bug
|
2013-10-08 04:24:54 -07:00
|
|
|
cert = re.sub("([^\n])-----END CERTIFICATE-----","\\1\n-----END CERTIFICATE-----",cert)
|
2013-10-02 00:22:13 -07:00
|
|
|
temporary_path = cert_path + '.temp'
|
|
|
|
with open(temporary_path,"w") as f:
|
2013-09-30 05:01:49 -07:00
|
|
|
f.write(cert)
|
2013-10-02 01:21:25 -07:00
|
|
|
|
2013-10-01 18:24:14 -07:00
|
|
|
else:
|
|
|
|
is_new = False
|
2013-09-30 05:01:49 -07:00
|
|
|
|
2013-12-13 00:22:39 -08:00
|
|
|
try:
|
|
|
|
addrinfo = socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM)
|
|
|
|
except socket.gaierror:
|
|
|
|
print_error("error: cannot resolve", self.host)
|
|
|
|
return
|
|
|
|
|
|
|
|
for res in addrinfo:
|
2013-11-13 11:35:35 -08:00
|
|
|
try:
|
|
|
|
s = socket.socket( res[0], socket.SOCK_STREAM )
|
|
|
|
s.settimeout(2)
|
|
|
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
|
|
|
s.connect(res[4])
|
|
|
|
except:
|
|
|
|
s = None
|
|
|
|
continue
|
|
|
|
break
|
2013-10-01 00:01:46 -07:00
|
|
|
|
2013-11-14 00:23:38 -08:00
|
|
|
if s is None:
|
2013-10-01 00:01:46 -07:00
|
|
|
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,
|
2013-10-02 00:22:13 -07:00
|
|
|
ca_certs= (temporary_path if is_new else cert_path),
|
2013-09-30 05:01:49 -07:00
|
|
|
do_handshake_on_connect=True)
|
2013-10-01 00:01:46 -07:00
|
|
|
except ssl.SSLError, e:
|
|
|
|
print_error("SSL error:", self.host, e)
|
2013-10-02 01:43:02 -07:00
|
|
|
if e.errno != 1:
|
|
|
|
return
|
2013-10-01 18:33:45 -07:00
|
|
|
if is_new:
|
2013-11-11 07:59:36 -08:00
|
|
|
rej = cert_path + '.rej'
|
|
|
|
if os.path.exists(rej):
|
|
|
|
os.unlink(rej)
|
|
|
|
os.rename(temporary_path, rej)
|
2013-10-02 01:36:29 -07:00
|
|
|
else:
|
2014-06-27 02:36:51 -07:00
|
|
|
with open(cert_path) as f:
|
|
|
|
cert = f.read()
|
|
|
|
try:
|
|
|
|
x = x509.X509()
|
|
|
|
x.parse(cert)
|
|
|
|
x.slow_parse()
|
|
|
|
except:
|
2014-07-29 00:28:27 -07:00
|
|
|
traceback.print_exc(file=sys.stderr)
|
2014-06-27 02:36:51 -07:00
|
|
|
print_error("wrong certificate", self.host)
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
x.check_date()
|
|
|
|
except:
|
2013-10-02 01:36:29 -07:00
|
|
|
print_error("certificate has expired:", cert_path)
|
|
|
|
os.unlink(cert_path)
|
2014-06-27 02:36:51 -07:00
|
|
|
return
|
|
|
|
print_error("wrong certificate", self.host)
|
2013-10-01 00:01:46 -07:00
|
|
|
return
|
2013-11-09 21:23:57 -08:00
|
|
|
except Exception:
|
2013-10-01 00:01:46 -07:00
|
|
|
print_error("wrap_socket failed", self.host)
|
2014-07-29 00:28:27 -07:00
|
|
|
traceback.print_exc(file=sys.stderr)
|
2013-09-30 05:01:49 -07:00
|
|
|
return
|
|
|
|
|
2013-10-02 00:22:13 -07:00
|
|
|
if is_new:
|
|
|
|
print_error("saving certificate for", self.host)
|
|
|
|
os.rename(temporary_path, cert_path)
|
|
|
|
|
2013-04-28 04:47:19 -07:00
|
|
|
s.settimeout(60)
|
|
|
|
self.s = s
|
|
|
|
self.is_connected = True
|
2013-10-01 00:01:46 -07:00
|
|
|
print_error("connected to", self.host, self.port)
|
2014-07-27 15:13:40 -07:00
|
|
|
self.pipe = util.SocketPipe(s)
|
2014-07-29 00:28:27 -07:00
|
|
|
|
2014-07-27 15:13:40 -07:00
|
|
|
|
|
|
|
def send_request(self, request, queue=None):
|
|
|
|
_id = request.get('id')
|
|
|
|
method = request.get('method')
|
|
|
|
params = request.get('params')
|
|
|
|
with self.lock:
|
2014-07-29 05:19:23 -07:00
|
|
|
try:
|
|
|
|
self.pipe.send({'id':self.message_id, 'method':method, 'params':params})
|
|
|
|
except socket.error:
|
|
|
|
self.is_connected = False
|
|
|
|
return
|
2014-07-27 15:13:40 -07:00
|
|
|
self.unanswered_requests[self.message_id] = method, params, _id, queue
|
2012-03-17 08:02:28 -07:00
|
|
|
self.message_id += 1
|
2014-07-27 15:13:40 -07:00
|
|
|
if self.debug:
|
|
|
|
print_error("-->", request)
|
2012-10-20 17:57:31 -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
|
|
|
|
|
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()
|
2013-12-17 09:20:54 -08:00
|
|
|
self.is_connected = False
|
|
|
|
|
2014-07-29 00:28:27 -07:00
|
|
|
def start(self, response_queue):
|
2014-07-27 15:13:40 -07:00
|
|
|
self.response_queue = response_queue
|
2013-03-10 01:24:42 -08:00
|
|
|
threading.Thread.start(self)
|
2013-09-08 08:23:01 -07:00
|
|
|
|
2012-10-20 17:57:31 -07:00
|
|
|
def run(self):
|
2014-07-29 00:28:27 -07:00
|
|
|
self.start_tcp()
|
|
|
|
self.change_status()
|
|
|
|
if not self.is_connected:
|
|
|
|
return
|
|
|
|
|
|
|
|
t = 0
|
|
|
|
while self.is_connected:
|
|
|
|
# ping the server with server.version
|
|
|
|
if time.time() - t > 60:
|
|
|
|
self.send_request({'method':'server.version', 'params':[ELECTRUM_VERSION, PROTOCOL_VERSION]})
|
|
|
|
t = time.time()
|
|
|
|
try:
|
|
|
|
response = self.pipe.get()
|
|
|
|
except util.timeout:
|
|
|
|
continue
|
|
|
|
if response is None:
|
|
|
|
self.is_connected = False
|
|
|
|
break
|
|
|
|
self.process_response(response)
|
|
|
|
|
2013-09-08 08:23:01 -07:00
|
|
|
self.change_status()
|
2014-07-29 03:13:21 -07:00
|
|
|
print_error("closing connection:", self.server)
|
|
|
|
|
2013-09-08 08:23:01 -07:00
|
|
|
def change_status(self):
|
2014-07-27 15:13:40 -07:00
|
|
|
# print_error( "change status", self.server, self.is_connected)
|
|
|
|
self.response_queue.put((self, None))
|
2013-09-30 05:01:49 -07:00
|
|
|
|
2014-01-29 07:48:00 -08:00
|
|
|
|
2013-09-30 05:01:49 -07:00
|
|
|
|
2014-07-29 00:28:27 -07:00
|
|
|
class HttpInterface(TcpInterface):
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.start_http()
|
|
|
|
if self.is_connected:
|
|
|
|
self.send_request({'method':'server.version', 'params':[ELECTRUM_VERSION, PROTOCOL_VERSION]})
|
|
|
|
self.change_status()
|
|
|
|
self.run_http()
|
|
|
|
self.change_status()
|
2014-07-27 15:13:40 -07:00
|
|
|
|
|
|
|
def send_request(self, request, queue=None):
|
|
|
|
import urllib2, json, time, cookielib
|
|
|
|
print_error( "send_http", messages )
|
|
|
|
|
|
|
|
if self.proxy:
|
|
|
|
socks.setdefaultproxy(self.proxy_mode, self.proxy["host"], int(self.proxy["port"]) )
|
|
|
|
socks.wrapmodule(urllib2)
|
|
|
|
|
|
|
|
cj = cookielib.CookieJar()
|
|
|
|
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
|
|
|
|
urllib2.install_opener(opener)
|
|
|
|
|
|
|
|
t1 = time.time()
|
|
|
|
|
|
|
|
data = []
|
|
|
|
ids = []
|
|
|
|
for m in messages:
|
|
|
|
method, params = m
|
|
|
|
if type(params) != type([]): params = [params]
|
|
|
|
data.append( { 'method':method, 'id':self.message_id, 'params':params } )
|
|
|
|
self.unanswered_requests[self.message_id] = method, params, callback
|
|
|
|
ids.append(self.message_id)
|
|
|
|
self.message_id += 1
|
|
|
|
|
|
|
|
if data:
|
|
|
|
data_json = json.dumps(data)
|
|
|
|
else:
|
|
|
|
# poll with GET
|
|
|
|
data_json = None
|
|
|
|
|
|
|
|
|
|
|
|
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 Exception:
|
|
|
|
return
|
|
|
|
|
|
|
|
for index, cookie in enumerate(cj):
|
|
|
|
if cookie.name=='SESSION':
|
|
|
|
self.session_id = cookie.value
|
|
|
|
|
|
|
|
response = response_stream.read()
|
|
|
|
self.bytes_received += len(response)
|
|
|
|
if response:
|
|
|
|
response = json.loads( response )
|
|
|
|
if type(response) is not type([]):
|
|
|
|
self.process_response(response)
|
|
|
|
else:
|
|
|
|
for item in response:
|
|
|
|
self.process_response(item)
|
|
|
|
if response:
|
|
|
|
self.poll_interval = 1
|
|
|
|
else:
|
|
|
|
if self.poll_interval < 15:
|
|
|
|
self.poll_interval += 1
|
|
|
|
#print self.poll_interval, response
|
|
|
|
self.rtime = time.time() - t1
|
|
|
|
self.is_connected = True
|
|
|
|
return ids
|
|
|
|
|
|
|
|
def poll(self):
|
|
|
|
self.send([], None)
|
|
|
|
|
|
|
|
def start_http(self):
|
2014-07-29 00:28:27 -07:00
|
|
|
self.rtime = 0
|
|
|
|
self.bytes_received = 0
|
|
|
|
self.poll_interval = 1
|
|
|
|
|
2014-07-27 15:13:40 -07:00
|
|
|
self.session_id = None
|
|
|
|
self.is_connected = True
|
|
|
|
self.connection_msg = ('https' if self.use_ssl else 'http') + '://%s:%d'%( self.host, self.port )
|
|
|
|
try:
|
|
|
|
self.poll()
|
|
|
|
except Exception:
|
|
|
|
print_error("http init session failed")
|
|
|
|
self.is_connected = False
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.session_id:
|
|
|
|
print_error('http session:',self.session_id)
|
|
|
|
self.is_connected = True
|
|
|
|
else:
|
|
|
|
self.is_connected = False
|
|
|
|
|
|
|
|
def run_http(self):
|
|
|
|
self.is_connected = True
|
|
|
|
while self.is_connected:
|
|
|
|
try:
|
|
|
|
if self.session_id:
|
|
|
|
self.poll()
|
|
|
|
time.sleep(self.poll_interval)
|
|
|
|
except socket.gaierror:
|
|
|
|
break
|
|
|
|
except socket.error:
|
|
|
|
break
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
|
|
|
break
|
|
|
|
|
|
|
|
self.is_connected = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-30 05:01:49 -07:00
|
|
|
|
2014-06-27 02:36:51 -07:00
|
|
|
|
|
|
|
def check_cert(host, cert):
|
|
|
|
try:
|
|
|
|
x = x509.X509()
|
|
|
|
x.parse(cert)
|
|
|
|
x.slow_parse()
|
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
x.check_date()
|
|
|
|
expired = False
|
|
|
|
except:
|
|
|
|
expired = True
|
|
|
|
|
|
|
|
m = "host: %s\n"%host
|
|
|
|
m += "has_expired: %s\n"% expired
|
|
|
|
print_msg(m)
|
|
|
|
|
|
|
|
|
|
|
|
def test_certificates():
|
|
|
|
config = SimpleConfig()
|
|
|
|
mydir = os.path.join(config.path, "certs")
|
|
|
|
certs = os.listdir(mydir)
|
|
|
|
for c in certs:
|
|
|
|
print c
|
|
|
|
p = os.path.join(mydir,c)
|
|
|
|
with open(p) as f:
|
|
|
|
cert = f.read()
|
|
|
|
check_cert(c, cert)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_certificates()
|