Connect to bootstrap nodes by name

This commit is contained in:
Dmitri Bogomolov 2019-07-12 18:19:06 +03:00
parent 42a89ad367
commit 0a06567071
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
6 changed files with 28 additions and 16 deletions

View File

@ -11,7 +11,6 @@ import time
import state import state
from bmconfigparser import BMConfigParser from bmconfigparser import BMConfigParser
from debug import logger from debug import logger
from helper_bootstrap import dns
knownNodesLock = threading.Lock() knownNodesLock = threading.Lock()
knownNodes = {stream: {} for stream in range(1, 4)} knownNodes = {stream: {} for stream in range(1, 4)}
@ -123,6 +122,8 @@ def readKnownNodes():
logger.debug( logger.debug(
'Failed to read nodes from knownnodes.dat', exc_info=True) 'Failed to read nodes from knownnodes.dat', exc_info=True)
createDefaultKnownNodes() createDefaultKnownNodes()
if BMConfigParser().get('bitmessagesettings', 'socksproxytype') == 'SOCKS5':
createDefaultKnownNodes(onion=True)
config = BMConfigParser() config = BMConfigParser()
@ -177,6 +178,13 @@ def trimKnownNodes(recAddrStream=1):
del knownNodes[recAddrStream][oldest] del knownNodes[recAddrStream][oldest]
def dns():
"""Add DNS names to knownnodes"""
for port in [8080, 8444]:
addKnownNode(
1, state.Peer('bootstrap%s.bitmessage.org' % port, port))
def cleanupKnownNodes(): def cleanupKnownNodes():
""" """
Cleanup knownnodes: remove old nodes and nodes with low rating Cleanup knownnodes: remove old nodes and nodes with low rating

View File

@ -46,7 +46,8 @@ def chooseConnection(stream):
# onion addresses have a higher priority when SOCKS # onion addresses have a higher priority when SOCKS
if peer.host.endswith('.onion') and rating > 0: if peer.host.endswith('.onion') and rating > 0:
rating = 1 rating = 1
else: # TODO: need better check
elif not peer.host.startswith('bootstrap'):
encodedAddr = protocol.encodeHost(peer.host) encodedAddr = protocol.encodeHost(peer.host)
# don't connect to local IPs when using SOCKS # don't connect to local IPs when using SOCKS
if not protocol.checkIPAddress(encodedAddr, False): if not protocol.checkIPAddress(encodedAddr, False):

View File

@ -8,7 +8,6 @@ import socket
import time import time
import asyncore_pollchoose as asyncore import asyncore_pollchoose as asyncore
import helper_bootstrap
import helper_random import helper_random
import knownnodes import knownnodes
import protocol import protocol
@ -185,7 +184,7 @@ class BMConnectionPool(object):
# pylint: disable=too-many-nested-blocks # pylint: disable=too-many-nested-blocks
if spawnConnections: if spawnConnections:
if not knownnodes.knownNodesActual: if not knownnodes.knownNodesActual:
helper_bootstrap.dns() knownnodes.dns()
if not self.bootstrapped: if not self.bootstrapped:
self.bootstrapped = True self.bootstrapped = True
Proxy.proxy = ( Proxy.proxy = (

View File

@ -8,6 +8,7 @@ src/network/socks5.py
import socket import socket
import struct import struct
import state
from proxy import GeneralProxyError, Proxy, ProxyError from proxy import GeneralProxyError, Proxy, ProxyError
@ -160,9 +161,6 @@ class Socks5(Proxy):
class Socks5Connection(Socks5): class Socks5Connection(Socks5):
"""Child socks5 class used for making outbound connections.""" """Child socks5 class used for making outbound connections."""
def __init__(self, address):
Socks5.__init__(self, address=address)
def state_auth_done(self): def state_auth_done(self):
"""Request connection to be made""" """Request connection to be made"""
# Now we can request the actual connection # Now we can request the actual connection
@ -172,9 +170,9 @@ class Socks5Connection(Socks5):
try: try:
self.ipaddr = socket.inet_aton(self.destination[0]) self.ipaddr = socket.inet_aton(self.destination[0])
self.append_write_buf(chr(0x01).encode() + self.ipaddr) self.append_write_buf(chr(0x01).encode() + self.ipaddr)
except socket.error: except socket.error: # may be IPv6!
# Well it's not an IP number, so it's probably a DNS name. # Well it's not an IP number, so it's probably a DNS name.
if Proxy._remote_dns: # pylint: disable=protected-access if self._remote_dns:
# Resolve remotely # Resolve remotely
self.ipaddr = None self.ipaddr = None
self.append_write_buf(chr(0x03).encode() + chr( self.append_write_buf(chr(0x03).encode() + chr(
@ -202,7 +200,7 @@ class Socks5Resolver(Socks5):
def __init__(self, host): def __init__(self, host):
self.host = host self.host = host
self.port = 8444 self.port = 8444
Socks5.__init__(self, address=(self.host, self.port)) Socks5.__init__(self, address=state.Peer(self.host, self.port))
def state_auth_done(self): def state_auth_done(self):
"""Perform resolving""" """Perform resolving"""

View File

@ -73,11 +73,14 @@ class TCPConnection(BMProto, TLSDispatcher):
logger.debug( logger.debug(
'Connecting to %s:%i', 'Connecting to %s:%i',
self.destination.host, self.destination.port) self.destination.host, self.destination.port)
encodedAddr = protocol.encodeHost(self.destination.host) try:
self.local = all([ self.local = (
protocol.checkIPAddress(encodedAddr, True), protocol.checkIPAddress(
protocol.encodeHost(self.destination.host), True) and
not protocol.checkSocksIP(self.destination.host) not protocol.checkSocksIP(self.destination.host)
]) )
except socket.error:
pass # it's probably a hostname
ObjectTracker.__init__(self) # pylint: disable=non-parent-init-called ObjectTracker.__init__(self) # pylint: disable=non-parent-init-called
self.bm_proto_reset() self.bm_proto_reset()
self.set_state("bm_header", expectBytes=protocol.Header.size) self.set_state("bm_header", expectBytes=protocol.Header.size)

View File

@ -264,7 +264,10 @@ def assembleVersionMessage(remoteHost, remotePort, participatingStreams, server=
else: else:
# use first 16 bytes if host data is longer # use first 16 bytes if host data is longer
# for example in case of onion v3 service # for example in case of onion v3 service
try:
payload += encodeHost(remoteHost)[:16] payload += encodeHost(remoteHost)[:16]
except socket.error:
payload += encodeHost('127.0.0.1')
payload += pack('>H', remotePort) # remote IPv6 and port payload += pack('>H', remotePort) # remote IPv6 and port
# bitflags of the services I offer. # bitflags of the services I offer.