Skip to content

Commit

Permalink
Connect to bootstrap nodes by name
Browse files Browse the repository at this point in the history
  • Loading branch information
g1itch committed Sep 25, 2019
1 parent 42a89ad commit 0a06567
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
10 changes: 9 additions & 1 deletion src/knownnodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import state
from bmconfigparser import BMConfigParser
from debug import logger
from helper_bootstrap import dns

knownNodesLock = threading.Lock()
knownNodes = {stream: {} for stream in range(1, 4)}
Expand Down Expand Up @@ -123,6 +122,8 @@ def readKnownNodes():
logger.debug(
'Failed to read nodes from knownnodes.dat', exc_info=True)
createDefaultKnownNodes()
if BMConfigParser().get('bitmessagesettings', 'socksproxytype') == 'SOCKS5':
createDefaultKnownNodes(onion=True)

config = BMConfigParser()

Expand Down Expand Up @@ -177,6 +178,13 @@ def trimKnownNodes(recAddrStream=1):
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():
"""
Cleanup knownnodes: remove old nodes and nodes with low rating
Expand Down
3 changes: 2 additions & 1 deletion src/network/connectionchooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def chooseConnection(stream):
# onion addresses have a higher priority when SOCKS
if peer.host.endswith('.onion') and rating > 0:
rating = 1
else:
# TODO: need better check
elif not peer.host.startswith('bootstrap'):
encodedAddr = protocol.encodeHost(peer.host)
# don't connect to local IPs when using SOCKS
if not protocol.checkIPAddress(encodedAddr, False):
Expand Down
3 changes: 1 addition & 2 deletions src/network/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import time

import asyncore_pollchoose as asyncore
import helper_bootstrap
import helper_random
import knownnodes
import protocol
Expand Down Expand Up @@ -185,7 +184,7 @@ def loop(self): # pylint: disable=too-many-branches, too-many-statements
# pylint: disable=too-many-nested-blocks
if spawnConnections:
if not knownnodes.knownNodesActual:
helper_bootstrap.dns()
knownnodes.dns()
if not self.bootstrapped:
self.bootstrapped = True
Proxy.proxy = (
Expand Down
10 changes: 4 additions & 6 deletions src/network/socks5.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import socket
import struct

import state
from proxy import GeneralProxyError, Proxy, ProxyError


Expand Down Expand Up @@ -160,9 +161,6 @@ def proxy_sock_name(self):

class Socks5Connection(Socks5):
"""Child socks5 class used for making outbound connections."""
def __init__(self, address):
Socks5.__init__(self, address=address)

def state_auth_done(self):
"""Request connection to be made"""
# Now we can request the actual connection
Expand All @@ -172,9 +170,9 @@ def state_auth_done(self):
try:
self.ipaddr = socket.inet_aton(self.destination[0])
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.
if Proxy._remote_dns: # pylint: disable=protected-access
if self._remote_dns:
# Resolve remotely
self.ipaddr = None
self.append_write_buf(chr(0x03).encode() + chr(
Expand Down Expand Up @@ -202,7 +200,7 @@ class Socks5Resolver(Socks5):
def __init__(self, host):
self.host = host
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):
"""Perform resolving"""
Expand Down
13 changes: 8 additions & 5 deletions src/network/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ def __init__(self, address=None, sock=None):
logger.debug(
'Connecting to %s:%i',
self.destination.host, self.destination.port)
encodedAddr = protocol.encodeHost(self.destination.host)
self.local = all([
protocol.checkIPAddress(encodedAddr, True),
not protocol.checkSocksIP(self.destination.host)
])
try:
self.local = (
protocol.checkIPAddress(
protocol.encodeHost(self.destination.host), True) and
not protocol.checkSocksIP(self.destination.host)
)
except socket.error:
pass # it's probably a hostname
ObjectTracker.__init__(self) # pylint: disable=non-parent-init-called
self.bm_proto_reset()
self.set_state("bm_header", expectBytes=protocol.Header.size)
Expand Down
5 changes: 4 additions & 1 deletion src/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ def assembleVersionMessage(remoteHost, remotePort, participatingStreams, server=
else:
# use first 16 bytes if host data is longer
# for example in case of onion v3 service
payload += encodeHost(remoteHost)[:16]
try:
payload += encodeHost(remoteHost)[:16]
except socket.error:
payload += encodeHost('127.0.0.1')
payload += pack('>H', remotePort) # remote IPv6 and port

# bitflags of the services I offer.
Expand Down

0 comments on commit 0a06567

Please sign in to comment.