diff --git a/libp2p/builders.nim b/libp2p/builders.nim index 611e057c87..de14a97c6a 100644 --- a/libp2p/builders.nim +++ b/libp2p/builders.nim @@ -147,9 +147,6 @@ proc withTransport*(b: SwitchBuilder, prov: TransportProvider): SwitchBuilder {. proc withTcpTransport*(b: SwitchBuilder, flags: set[ServerFlags] = {}): SwitchBuilder {.public.} = b.withTransport(proc(upgr: Upgrade): Transport = TcpTransport.new(flags, upgr)) -proc withTorTransport*(b: SwitchBuilder, torServerForDialing: TransportAddress, flags: set[ServerFlags] = {}): SwitchBuilder {.public} = - b.withTransport(proc(upgr: Upgrade): Transport = TorTransport.new(torServerForDialing, flags, upgr)) - proc withRng*(b: SwitchBuilder, rng: ref HmacDrbgContext): SwitchBuilder {.public.} = b.rng = rng b diff --git a/libp2p/transports/tortransport.nim b/libp2p/transports/tortransport.nim index 4adc42a67e..87c511ca84 100644 --- a/libp2p/transports/tortransport.nim +++ b/libp2p/transports/tortransport.nim @@ -20,6 +20,8 @@ import stew/[byteutils, endians2, results, objects] import ../multicodec import transport, tcptransport, + ../switch, + ../builders, ../stream/[lpstream, connection, chronosstream], ../multiaddress, ../upgrademngrs/upgrade @@ -29,7 +31,7 @@ const NMethods = byte(1) type - TorTransport* = ref object of Transport + TorTransport = ref object of Transport transportAddress: TransportAddress tcpTransport: TcpTransport @@ -59,7 +61,7 @@ type Socks5VersionError* = object of Socks5Error Socks5ServerReplyError* = object of Socks5Error -proc new*( +proc new( T: typedesc[TorTransport], transportAddress: TransportAddress, flags: set[ServerFlags] = {}, @@ -204,3 +206,39 @@ method stop*(self: TorTransport) {.async, gcsafe.} = method handles*(t: TorTransport, address: MultiAddress): bool {.gcsafe.} = if procCall Transport(t).handles(address): return handlesDial(address) or handlesStart(address) + +type + TorSwitch* = ref object of Switch + +proc new*( + T: typedesc[TorSwitch], + torServer: TransportAddress, + rng: ref HmacDrbgContext, + addresses: seq[MultiAddress] = @[], + flags: set[ServerFlags] = {}): TorSwitch + {.raises: [LPError], public.} = + var builder = SwitchBuilder.new() + .withRng(rng) + .withTransport(proc(upgr: Upgrade): Transport = TorTransport.new(torServer, flags, upgr)) + if addresses.len != 0: + builder = builder.withAddresses(addresses) + let switch = builder.withMplex() + .withNoise() + .build() + let torSwitch = T( + peerInfo: switch.peerInfo, + ms: switch.ms, + transports: switch.transports, + connManager: switch.connManager, + peerStore: switch.peerStore, + dialer: Dialer.new(switch.peerInfo.peerId, switch.connManager, switch.transports, switch.ms, nil), + nameResolver: nil) + + torSwitch.connManager.peerStore = switch.peerStore + return torSwitch + +method addTransport*(s: TorSwitch, t: Transport) = + doAssert(false, "not implemented!") + +method getTorTransport*(s: TorSwitch): Transport {.base.} = + return s.transports[0] diff --git a/tests/testtortransport.nim b/tests/testtortransport.nim index b716b23d11..40238fee98 100644 --- a/tests/testtortransport.nim +++ b/tests/testtortransport.nim @@ -33,37 +33,6 @@ suite "Tor transport": checkTrackers() asyncTest "test dial and start": - let server = TorTransport.new(torServer, {ReuseAddr}, Upgrade()) - let ma2 = @[MultiAddress.init("/ip4/127.0.0.1/tcp/8080/onion3/a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcad:80").tryGet()] - await server.start(ma2) - - proc runClient() {.async.} = - let client = TorTransport.new(transportAddress = torServer, upgrade = Upgrade()) - let conn = await client.dial("", server.addrs[0]) - - await conn.write("client") - var resp: array[6, byte] - await conn.readExactly(addr resp, 6) - await conn.close() - - check string.fromBytes(resp) == "server" - await client.stop() - - proc serverAcceptHandler() {.async, gcsafe.} = - let conn = await server.accept() - - var resp: array[6, byte] - await conn.readExactly(addr resp, 6) - check string.fromBytes(resp) == "client" - - await conn.write("server") - await conn.close() - await server.stop() - - asyncSpawn serverAcceptHandler() - await runClient() - - asyncTest "test dial and start with builder": const TestCodec = "/test/proto/1.0.0" # custom protocol string identifier type @@ -88,13 +57,7 @@ suite "Tor transport": let ma = MultiAddress.init("/ip4/127.0.0.1/tcp/8080/onion3/a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcad:80").tryGet() - let serverSwitch = SwitchBuilder.new() - .withRng(rng) - .withTorTransport(torServer, {ReuseAddr}) - .withAddress(ma) - .withMplex() - .withNoise() - .build() + let serverSwitch = TorSwitch.new(torServer, rng, @[ma], {ReuseAddr}) # setup the custom proto let testProto = TestProto.new() @@ -106,12 +69,7 @@ suite "Tor transport": let serverAddress = serverSwitch.peerInfo.addrs proc startClient() {.async.} = - let clientSwitch = SwitchBuilder.new() - .withRng(rng) - .withTorTransport(torServer) - .withMplex() - .withNoise() - .build() + let clientSwitch = TorSwitch.new(torServer = torServer, rng= rng, flags = {ReuseAddr}) let conn = await clientSwitch.dial(serverPeerId, serverAddress, TestCodec) @@ -127,11 +85,19 @@ suite "Tor transport": await serverSwitch.stop() - proc transProvider(): Transport = TorTransport.new(transportAddress = torServer, flags = {ReuseAddr}, upgrade = Upgrade()) + test "It's not possible to add another transport": + let torSwitch = TorSwitch.new(torServer = torServer, rng= rng, flags = {ReuseAddr}) + expect(AssertionDefect): + torSwitch.addTransport(TcpTransport.new(upgrade = Upgrade())) + waitFor torSwitch.stop() + + proc transProvider(): Transport = + try: + TorSwitch.new(torServer = torServer, rng= rng, flags = {ReuseAddr}).getTorTransport() + except: + raise newException(Defect, "Error when creating Tor Transport") commonTransportTest( transProvider, "/ip4/127.0.0.1/tcp/8080/onion3/a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcad:80", "/ip4/127.0.0.1/tcp/8081/onion3/a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcae:80") - -