2013-06-21 00:55:04 +02:00
|
|
|
import socket
|
|
|
|
|
2017-02-08 13:41:56 +01:00
|
|
|
import knownnodes
|
2015-11-26 23:45:44 +01:00
|
|
|
import socks
|
2017-01-11 17:00:00 +01:00
|
|
|
import state
|
2018-05-03 12:49:43 +02:00
|
|
|
from bmconfigparser import BMConfigParser
|
|
|
|
from debug import logger
|
2013-06-21 00:55:04 +02:00
|
|
|
|
2017-10-07 16:28:32 +02:00
|
|
|
|
2013-06-21 00:55:04 +02:00
|
|
|
def dns():
|
2018-05-03 12:49:43 +02:00
|
|
|
"""
|
|
|
|
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-10-07 16:28:32 +02:00
|
|
|
def try_add_known_node(stream, addr, port, method=''):
|
2015-11-26 23:45:44 +01:00
|
|
|
try:
|
2017-10-07 16:28:32 +02:00
|
|
|
socket.inet_aton(addr)
|
|
|
|
except (TypeError, socket.error):
|
|
|
|
return
|
|
|
|
logger.info(
|
|
|
|
'Adding %s to knownNodes based on %s DNS bootstrap method',
|
|
|
|
addr, method)
|
2018-05-03 12:49:43 +02:00
|
|
|
knownnodes.addKnownNode(stream, state.Peer(addr, port))
|
2017-10-07 16:28:32 +02:00
|
|
|
|
|
|
|
proxy_type = BMConfigParser().get('bitmessagesettings', 'socksproxytype')
|
|
|
|
|
|
|
|
if proxy_type == 'none':
|
|
|
|
for port in [8080, 8444]:
|
|
|
|
try:
|
|
|
|
for item in socket.getaddrinfo(
|
|
|
|
'bootstrap%s.bitmessage.org' % port, 80):
|
|
|
|
try_add_known_node(1, item[4][0], port)
|
|
|
|
except:
|
|
|
|
logger.error(
|
|
|
|
'bootstrap%s.bitmessage.org DNS bootstrapping failed.',
|
|
|
|
port, exc_info=True
|
|
|
|
)
|
|
|
|
elif proxy_type == 'SOCKS5':
|
2018-05-03 12:49:43 +02:00
|
|
|
knownnodes.addKnownNode(1, state.Peer('quzwelsuziwqgpt2.onion', 8444))
|
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')
|
2018-05-03 12:49:43 +02:00
|
|
|
# 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.
|
|
|
|
rdns = True
|
|
|
|
if BMConfigParser().getboolean(
|
|
|
|
'bitmessagesettings', 'socksauthentication'):
|
2017-01-11 14:27:19 +01:00
|
|
|
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(
|
2018-05-03 12:49:43 +02:00
|
|
|
proxytype, sockshostname, socksport, rdns,
|
|
|
|
socksusername, sockspassword)
|
2015-11-26 23:45:44 +01:00
|
|
|
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:
|
2017-10-07 16:28:32 +02:00
|
|
|
try_add_known_node(1, ip, port, 'SOCKS')
|
2015-11-26 23:45:44 +01:00
|
|
|
else:
|
2017-10-07 16:28:32 +02:00
|
|
|
logger.info(
|
|
|
|
'DNS bootstrap skipped because the proxy type does not support'
|
|
|
|
' DNS resolution.'
|
|
|
|
)
|