Use the new Foolscap Tor handler, pass an endpoint for SOCKS connectivity

closes ticket:2813
This commit is contained in:
David Stainton 2016-09-02 03:17:45 +00:00
parent a03f68b787
commit 1307c7262d
2 changed files with 25 additions and 29 deletions

View File

@ -207,24 +207,10 @@ class Node(service.MultiService):
datadir = os.path.join(self.basedir, "private", "tor-statedir")
return tor.launch(data_directory=datadir, tor_binary=executable)
socksport = self.get_config("tor", "socks.port", None)
if socksport:
# this is nominally and endpoint string, but txtorcon requires
# TCP host and port. So parse it now, and reject non-TCP
# endpoints.
pieces = socksport.split(":")
if pieces[0] != "tcp" or len(pieces) != 3:
raise ValueError("'tahoe.cfg [tor] socks.port' = "
"is currently limited to 'tcp:HOST:PORT', "
"not '%s'" % (socksport,))
host = pieces[1]
try:
port = int(pieces[2])
except ValueError:
raise ValueError("'tahoe.cfg [tor] socks.port' used "
"non-numeric PORT value '%s'" % (pieces[2],))
return tor.socks_port(host, port)
socks_endpoint_desc = self.get_config("tor", "socks.port", None)
if socks_endpoint_desc:
socks_ep = endpoints.clientFromString(reactor, socks_endpoint_desc)
return tor.socks_endpoint(socks_ep)
controlport = self.get_config("tor", "control.port", None)
if controlport:

View File

@ -3,6 +3,7 @@ import mock
from io import BytesIO
from twisted.trial import unittest
from twisted.internet import reactor, endpoints
from twisted.internet.interfaces import IStreamClientEndpoint
from ConfigParser import SafeConfigParser
from foolscap.connections import tcp
from ..node import Node, PrivacyError
@ -71,33 +72,42 @@ class Tor(unittest.TestCase):
self.assertEqual(f.mock_calls, [exp])
self.assertIdentical(h, h1)
def test_socksport(self):
n = FakeNode(BASECONFIG+"[tor]\nsocks.port = tcp:127.0.0.1:1234\n")
def test_socksport_unix_endpoint(self):
n = FakeNode(BASECONFIG+"[tor]\nsocks.port = unix:/var/lib/fw-daemon/tor_socks.socket\n")
h1 = mock.Mock()
with mock.patch("foolscap.connections.tor.socks_port",
with mock.patch("foolscap.connections.tor.socks_endpoint",
return_value=h1) as f:
h = n._make_tor_handler()
self.assertEqual(f.mock_calls, [mock.call("127.0.0.1", 1234)])
self.assertTrue(IStreamClientEndpoint.providedBy(f.mock_calls[0]))
self.assertIdentical(h, h1)
def test_socksport_otherhost(self):
n = FakeNode(BASECONFIG+"[tor]\nsocks.port = tcp:otherhost:1234\n")
def test_socksport_endpoint(self):
n = FakeNode(BASECONFIG+"[tor]\nsocks.port = tcp:127.0.0.1:1234\n")
h1 = mock.Mock()
with mock.patch("foolscap.connections.tor.socks_port",
with mock.patch("foolscap.connections.tor.socks_endpoint",
return_value=h1) as f:
h = n._make_tor_handler()
self.assertEqual(f.mock_calls, [mock.call("otherhost", 1234)])
self.assertTrue(IStreamClientEndpoint.providedBy(f.mock_calls[0]))
self.assertIdentical(h, h1)
def test_socksport_endpoint_otherhost(self):
n = FakeNode(BASECONFIG+"[tor]\nsocks.port = tcp:otherhost:1234\n")
h1 = mock.Mock()
with mock.patch("foolscap.connections.tor.socks_endpoint",
return_value=h1) as f:
h = n._make_tor_handler()
self.assertTrue(IStreamClientEndpoint.providedBy(f.mock_calls[0]))
self.assertIdentical(h, h1)
def test_socksport_bad_endpoint(self):
n = FakeNode(BASECONFIG+"[tor]\nsocks.port = unix:unsupported\n")
n = FakeNode(BASECONFIG+"[tor]\nsocks.port = meow:unsupported\n")
e = self.assertRaises(ValueError, n._make_tor_handler)
self.assertIn("is currently limited to 'tcp:HOST:PORT'", str(e))
self.assertIn("Unknown endpoint type: 'meow'", str(e))
def test_socksport_not_integer(self):
n = FakeNode(BASECONFIG+"[tor]\nsocks.port = tcp:localhost:kumquat\n")
e = self.assertRaises(ValueError, n._make_tor_handler)
self.assertIn("used non-numeric PORT value", str(e))
self.assertIn("invalid literal for int() with base 10: 'kumquat'", str(e))
def test_controlport(self):
n = FakeNode(BASECONFIG+"[tor]\ncontrol.port = tcp:localhost:1234\n")