2013-06-21 00:55:04 +02:00
|
|
|
import shared
|
|
|
|
import socket
|
|
|
|
import defaultKnownNodes
|
|
|
|
import pickle
|
2013-06-21 21:44:28 +02:00
|
|
|
import time
|
2013-06-21 00:55:04 +02:00
|
|
|
|
2017-01-11 14:27:19 +01:00
|
|
|
from configparser import BMConfigParser
|
2015-11-18 16:22:17 +01:00
|
|
|
from debug import logger
|
2015-11-26 23:45:44 +01:00
|
|
|
import socks
|
2017-01-11 17:00:00 +01:00
|
|
|
import state
|
2015-11-18 16:22:17 +01:00
|
|
|
|
2013-06-21 00:55:04 +02:00
|
|
|
def knownNodes():
|
|
|
|
try:
|
|
|
|
# We shouldn't have to use the shared.knownNodesLock because this had
|
|
|
|
# better be the only thread accessing knownNodes right now.
|
2017-01-11 17:00:00 +01:00
|
|
|
pickleFile = open(state.appdata + 'knownnodes.dat', 'rb')
|
2013-08-01 12:31:40 +02:00
|
|
|
loadedKnownNodes = pickle.load(pickleFile)
|
2013-06-21 00:55:04 +02:00
|
|
|
pickleFile.close()
|
2013-08-01 12:31:40 +02:00
|
|
|
# The old format of storing knownNodes was as a 'host: (port, time)'
|
|
|
|
# mapping. The new format is as 'Peer: time' pairs. If we loaded
|
|
|
|
# data in the old format, transform it to the new style.
|
|
|
|
for stream, nodes in loadedKnownNodes.items():
|
|
|
|
shared.knownNodes[stream] = {}
|
|
|
|
for node_tuple in nodes.items():
|
|
|
|
try:
|
2016-06-07 21:59:48 +02:00
|
|
|
host, (port, lastseen) = node_tuple
|
2017-01-12 06:58:35 +01:00
|
|
|
peer = state.Peer(host, port)
|
2013-08-01 12:31:40 +02:00
|
|
|
except:
|
2016-06-07 21:59:48 +02:00
|
|
|
peer, lastseen = node_tuple
|
|
|
|
shared.knownNodes[stream][peer] = lastseen
|
2013-06-21 00:55:04 +02:00
|
|
|
except:
|
2017-01-11 17:00:00 +01:00
|
|
|
shared.knownNodes = defaultKnownNodes.createDefaultKnownNodes(state.appdata)
|
2016-06-07 21:59:48 +02:00
|
|
|
# your own onion address, if setup
|
2017-01-11 14:27:19 +01:00
|
|
|
if BMConfigParser().has_option('bitmessagesettings', 'onionhostname') and ".onion" in BMConfigParser().get('bitmessagesettings', 'onionhostname'):
|
2017-01-12 06:58:35 +01:00
|
|
|
shared.knownNodes[1][state.Peer(BMConfigParser().get('bitmessagesettings', 'onionhostname'), BMConfigParser().getint('bitmessagesettings', 'onionport'))] = int(time.time())
|
2017-01-11 14:27:19 +01:00
|
|
|
if BMConfigParser().getint('bitmessagesettings', 'settingsversion') > 10:
|
2015-11-18 16:22:17 +01:00
|
|
|
logger.error('Bitmessage cannot read future versions of the keys file (keys.dat). Run the newer version of Bitmessage.')
|
2013-06-21 00:55:04 +02:00
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
def dns():
|
|
|
|
# DNS bootstrap. This could be programmed to use the SOCKS proxy to do the
|
|
|
|
# DNS lookup some day but for now we will just rely on the entries in
|
|
|
|
# defaultKnownNodes.py. Hopefully either they are up to date or the user
|
|
|
|
# has run Bitmessage recently without SOCKS turned on and received good
|
|
|
|
# bootstrap nodes using that method.
|
2017-01-11 14:27:19 +01:00
|
|
|
if BMConfigParser().get('bitmessagesettings', 'socksproxytype') == 'none':
|
2015-11-26 23:45:44 +01:00
|
|
|
try:
|
|
|
|
for item in socket.getaddrinfo('bootstrap8080.bitmessage.org', 80):
|
|
|
|
logger.info('Adding ' + item[4][0] + ' to knownNodes based on DNS bootstrap method')
|
2017-01-12 06:58:35 +01:00
|
|
|
shared.knownNodes[1][state.Peer(item[4][0], 8080)] = int(time.time())
|
2015-11-26 23:45:44 +01:00
|
|
|
except:
|
|
|
|
logger.error('bootstrap8080.bitmessage.org DNS bootstrapping failed.')
|
|
|
|
try:
|
|
|
|
for item in socket.getaddrinfo('bootstrap8444.bitmessage.org', 80):
|
|
|
|
logger.info ('Adding ' + item[4][0] + ' to knownNodes based on DNS bootstrap method')
|
2017-01-12 06:58:35 +01:00
|
|
|
shared.knownNodes[1][state.Peer(item[4][0], 8444)] = int(time.time())
|
2015-11-26 23:45:44 +01:00
|
|
|
except:
|
|
|
|
logger.error('bootstrap8444.bitmessage.org DNS bootstrapping failed.')
|
2017-01-11 14:27:19 +01:00
|
|
|
elif BMConfigParser().get('bitmessagesettings', 'socksproxytype') == 'SOCKS5':
|
2017-01-12 06:58:35 +01:00
|
|
|
shared.knownNodes[1][state.Peer('quzwelsuziwqgpt2.onion', 8444)] = int(time.time())
|
2015-11-27 12:13:10 +01:00
|
|
|
logger.debug("Adding quzwelsuziwqgpt2.onion:8444 to knownNodes.")
|
2015-11-26 23:45:44 +01:00
|
|
|
for port in [8080, 8444]:
|
|
|
|
logger.debug("Resolving %i through SOCKS...", port)
|
|
|
|
address_family = socket.AF_INET
|
|
|
|
sock = socks.socksocket(address_family, socket.SOCK_STREAM)
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
sock.settimeout(20)
|
|
|
|
proxytype = socks.PROXY_TYPE_SOCKS5
|
2017-01-11 14:27:19 +01:00
|
|
|
sockshostname = BMConfigParser().get(
|
2015-11-26 23:45:44 +01:00
|
|
|
'bitmessagesettings', 'sockshostname')
|
2017-01-11 14:27:19 +01:00
|
|
|
socksport = BMConfigParser().getint(
|
2015-11-26 23:45:44 +01:00
|
|
|
'bitmessagesettings', 'socksport')
|
|
|
|
rdns = True # Do domain name lookups through the proxy; though this setting doesn't really matter since we won't be doing any domain name lookups anyway.
|
2017-01-11 14:27:19 +01:00
|
|
|
if BMConfigParser().getboolean('bitmessagesettings', 'socksauthentication'):
|
|
|
|
socksusername = BMConfigParser().get(
|
2015-11-26 23:45:44 +01:00
|
|
|
'bitmessagesettings', 'socksusername')
|
2017-01-11 14:27:19 +01:00
|
|
|
sockspassword = BMConfigParser().get(
|
2015-11-26 23:45:44 +01:00
|
|
|
'bitmessagesettings', 'sockspassword')
|
|
|
|
sock.setproxy(
|
|
|
|
proxytype, sockshostname, socksport, rdns, socksusername, sockspassword)
|
|
|
|
else:
|
|
|
|
sock.setproxy(
|
|
|
|
proxytype, sockshostname, socksport, rdns)
|
2013-07-24 18:43:51 +02:00
|
|
|
try:
|
2015-11-26 23:45:44 +01:00
|
|
|
ip = sock.resolve("bootstrap" + str(port) + ".bitmessage.org")
|
|
|
|
sock.shutdown(socket.SHUT_RDWR)
|
|
|
|
sock.close()
|
2013-07-24 18:43:51 +02:00
|
|
|
except:
|
2015-11-26 23:45:44 +01:00
|
|
|
logger.error("SOCKS DNS resolving failed", exc_info=True)
|
2016-10-20 01:46:48 +02:00
|
|
|
else:
|
|
|
|
if ip is not None:
|
|
|
|
logger.info ('Adding ' + ip + ' to knownNodes based on SOCKS DNS bootstrap method')
|
2017-01-12 06:58:35 +01:00
|
|
|
shared.knownNodes[1][state.Peer(ip, port)] = time.time()
|
2015-11-26 23:45:44 +01:00
|
|
|
else:
|
|
|
|
logger.info('DNS bootstrap skipped because the proxy type does not support DNS resolution.')
|
2013-07-24 18:43:51 +02:00
|
|
|
|