Skip to content

Commit

Permalink
Add TorSwitch
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Oct 31, 2022
1 parent 6e9528a commit 808f379
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 52 deletions.
3 changes: 0 additions & 3 deletions libp2p/builders.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 40 additions & 2 deletions libp2p/transports/tortransport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import stew/[byteutils, endians2, results, objects]
import ../multicodec
import transport,
tcptransport,
../switch,
../builders,
../stream/[lpstream, connection, chronosstream],
../multiaddress,
../upgrademngrs/upgrade
Expand All @@ -29,7 +31,7 @@ const
NMethods = byte(1)

type
TorTransport* = ref object of Transport
TorTransport = ref object of Transport
transportAddress: TransportAddress
tcpTransport: TcpTransport

Expand Down Expand Up @@ -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] = {},
Expand Down Expand Up @@ -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]
60 changes: 13 additions & 47 deletions tests/testtortransport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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)

Expand All @@ -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")


0 comments on commit 808f379

Please sign in to comment.