From b42f536d23ab617ac56fc71f9a7743333ad499ef Mon Sep 17 00:00:00 2001 From: George McCandless <5fk7echy8@riseup.net> Date: Tue, 8 Oct 2019 20:08:42 +0000 Subject: [PATCH 1/2] Add a checkbox to the network settings tab that allows restricting outbound connections to onion services (i.e., hosts that end with '.onion'). --- src/bitmessageqt/settings.py | 10 ++++++++++ src/bitmessageqt/settings.ui | 7 +++++++ src/network/connectionchooser.py | 5 +++++ 3 files changed, 22 insertions(+) diff --git a/src/bitmessageqt/settings.py b/src/bitmessageqt/settings.py index 513f285b81..982328ccb9 100644 --- a/src/bitmessageqt/settings.py +++ b/src/bitmessageqt/settings.py @@ -99,6 +99,8 @@ def adjust_from_config(self, config): config.getboolean('bitmessagesettings', 'socksauthentication')) self.checkBoxSocksListen.setChecked( config.getboolean('bitmessagesettings', 'sockslisten')) + self.checkBoxOnionOnly.setChecked( + config.safeGetBoolean('bitmessagesettings', 'onionservicesonly')) proxy_type = config.safeGet( 'bitmessagesettings', 'socksproxytype', 'none') @@ -110,6 +112,7 @@ def adjust_from_config(self, config): self.lineEditSocksPassword.setEnabled(False) self.checkBoxAuthentication.setEnabled(False) self.checkBoxSocksListen.setEnabled(False) + self.checkBoxOnionOnly.setEnabled(False) elif proxy_type == 'SOCKS4a': self.comboBoxProxyType.setCurrentIndex(1) elif proxy_type == 'SOCKS5': @@ -200,11 +203,13 @@ def comboBoxProxyTypeChanged(self, comboBoxIndex): self.lineEditSocksPassword.setEnabled(False) self.checkBoxAuthentication.setEnabled(False) self.checkBoxSocksListen.setEnabled(False) + self.checkBoxOnionOnly.setEnabled(False) elif comboBoxIndex in (1, 2): self.lineEditSocksHostname.setEnabled(True) self.lineEditSocksPort.setEnabled(True) self.checkBoxAuthentication.setEnabled(True) self.checkBoxSocksListen.setEnabled(True) + self.checkBoxOnionOnly.setEnabled(True) if self.checkBoxAuthentication.isChecked(): self.lineEditSocksUsername.setEnabled(True) self.lineEditSocksPassword.setEnabled(True) @@ -334,6 +339,11 @@ def accept(self): self.lineEditSocksPassword.text())) self.config.set('bitmessagesettings', 'sockslisten', str( self.checkBoxSocksListen.isChecked())) + if self.checkBoxOnionOnly.isChecked() \ + and not self.config.safeGetBoolean('bitmessagesettings', 'onionservicesonly'): + self.net_restart_needed = True + self.config.set('bitmessagesettings', 'onionservicesonly', str( + self.checkBoxOnionOnly.isChecked())) try: # Rounding to integers just for aesthetics self.config.set('bitmessagesettings', 'maxdownloadrate', str( diff --git a/src/bitmessageqt/settings.ui b/src/bitmessageqt/settings.ui index 307c06c215..963f2e6470 100644 --- a/src/bitmessageqt/settings.ui +++ b/src/bitmessageqt/settings.ui @@ -403,6 +403,13 @@ + + + + Only connect to onion services (*.onion) + + + diff --git a/src/network/connectionchooser.py b/src/network/connectionchooser.py index ead8b31b8e..838ca45d5d 100644 --- a/src/network/connectionchooser.py +++ b/src/network/connectionchooser.py @@ -26,6 +26,8 @@ def getDiscoveredPeer(): def chooseConnection(stream): haveOnion = BMConfigParser().safeGet( "bitmessagesettings", "socksproxytype")[0:5] == 'SOCKS' + onionOnly = BMConfigParser().safeGetBoolean( + "bitmessagesettings", "onionservicesonly") if state.trustedPeer: return state.trustedPeer try: @@ -49,6 +51,9 @@ def chooseConnection(stream): logger.warning('Error in %s', peer) rating = 0 if haveOnion: + # do not connect to raw IP addresses--keep all traffic within Tor overlay + if onionOnly and not peer.host.endswith('.onion'): + continue # onion addresses have a higher priority when SOCKS if peer.host.endswith('.onion') and rating > 0: rating = 1 From f871cd450c15270209e33d3c350fa76b2936d147 Mon Sep 17 00:00:00 2001 From: George McCandless <5fk7echy8@riseup.net> Date: Thu, 24 Oct 2019 19:35:32 +0000 Subject: [PATCH 2/2] Add test for 'onionservicesonly' mode. Credit to Dmitri Bogomolov in commit 557a8cc6d2bec881b8a3c531d3f725460ed515f5. --- src/tests/core.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/tests/core.py b/src/tests/core.py index b01c6b69ce..8d24a76840 100644 --- a/src/tests/core.py +++ b/src/tests/core.py @@ -173,6 +173,22 @@ def _check_bootstrap(self): self.fail( 'Failed to connect during %s sec' % (time.time() - _started)) + def test_onionservicesonly(self): + """test onionservicesonly networking mode""" + BMConfigParser().set('bitmessagesettings', 'onionservicesonly', True) + self._initiate_bootstrap() + BMConfigParser().remove_option('bitmessagesettings', 'dontconnect') + for _ in range(360): + time.sleep(1) + for n, peer in enumerate(BMConnectionPool().outboundConnections): + if n > 2: + return + if not peer.host.endswith('.onion'): + self.fail( + 'Found non onion hostname %s in outbound connections!' + % peer.host) + self.fail('Failed to connect to at least 3 nodes within 360 sec') + def test_bootstrap(self): """test bootstrapping""" self._initiate_bootstrap()