Check IP before adding to knownnodes #1065

Merged
g1itch merged 1 commits from exceptions into v0.6 2018-01-25 13:17:18 +01:00

View File

@ -9,6 +9,7 @@ import knownnodes
import socks import socks
import state import state
def addKnownNode(stream, peer, lastseen=None, self=False): def addKnownNode(stream, peer, lastseen=None, self=False):
if lastseen is None: if lastseen is None:
lastseen = time.time() lastseen = time.time()
@ -18,6 +19,7 @@ def addKnownNode(stream, peer, lastseen=None, self=False):
"self": self, "self": self,
} }
def knownNodes(): def knownNodes():
try: try:
with open(state.appdata + 'knownnodes.dat', 'rb') as pickleFile: with open(state.appdata + 'knownnodes.dat', 'rb') as pickleFile:
@ -38,26 +40,37 @@ def knownNodes():
logger.error('Bitmessage cannot read future versions of the keys file (keys.dat). Run the newer version of Bitmessage.') logger.error('Bitmessage cannot read future versions of the keys file (keys.dat). Run the newer version of Bitmessage.')
raise SystemExit raise SystemExit
def dns(): def dns():
# DNS bootstrap. This could be programmed to use the SOCKS proxy to do the # 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 # 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 # defaultKnownNodes.py. Hopefully either they are up to date or the user
# has run Bitmessage recently without SOCKS turned on and received good # has run Bitmessage recently without SOCKS turned on and received good
# bootstrap nodes using that method. # bootstrap nodes using that method.
if BMConfigParser().get('bitmessagesettings', 'socksproxytype') == 'none': def try_add_known_node(stream, addr, port, method=''):
try: try:
for item in socket.getaddrinfo('bootstrap8080.bitmessage.org', 80): socket.inet_aton(addr)
logger.info('Adding ' + item[4][0] + ' to knownNodes based on DNS bootstrap method') except (TypeError, socket.error):
addKnownNode(1, state.Peer(item[4][0], 8080)) return
except: logger.info(
logger.error('bootstrap8080.bitmessage.org DNS bootstrapping failed.') 'Adding %s to knownNodes based on %s DNS bootstrap method',
try: addr, method)
for item in socket.getaddrinfo('bootstrap8444.bitmessage.org', 80): addKnownNode(stream, state.Peer(addr, port))
logger.info ('Adding ' + item[4][0] + ' to knownNodes based on DNS bootstrap method')
addKnownNode(1, state.Peer(item[4][0], 8444)) proxy_type = BMConfigParser().get('bitmessagesettings', 'socksproxytype')
except:
logger.error('bootstrap8444.bitmessage.org DNS bootstrapping failed.') if proxy_type == 'none':
elif BMConfigParser().get('bitmessagesettings', 'socksproxytype') == 'SOCKS5': 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':
addKnownNode(1, state.Peer('quzwelsuziwqgpt2.onion', 8444)) addKnownNode(1, state.Peer('quzwelsuziwqgpt2.onion', 8444))
logger.debug("Adding quzwelsuziwqgpt2.onion:8444 to knownNodes.") logger.debug("Adding quzwelsuziwqgpt2.onion:8444 to knownNodes.")
for port in [8080, 8444]: for port in [8080, 8444]:
@ -89,8 +102,9 @@ def dns():
except: except:
logger.error("SOCKS DNS resolving failed", exc_info=True) logger.error("SOCKS DNS resolving failed", exc_info=True)
else: else:
if ip is not None: try_add_known_node(1, ip, port, 'SOCKS')
logger.info ('Adding ' + ip + ' to knownNodes based on SOCKS DNS bootstrap method')
addKnownNode(1, state.Peer(ip, port))
else: else:
logger.info('DNS bootstrap skipped because the proxy type does not support DNS resolution.') logger.info(
'DNS bootstrap skipped because the proxy type does not support'
' DNS resolution.'
)