Skip to content

Commit

Permalink
More improvements after code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Sep 22, 2022
1 parent c089d39 commit 31d5d57
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
17 changes: 10 additions & 7 deletions libp2p/transports/tortransport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ const
type
TorTransport* = ref object of Transport
transportAddress: TransportAddress
flags: set[ServerFlags]
tcpTransport: TcpTransport

proc new*(
T: typedesc[TorTransport],
transportAddress: TransportAddress): T {.public.} =
transportAddress: TransportAddress,
flags: set[ServerFlags] = {},
upgrade: Upgrade): T {.public.} =
## Creates a Tor transport

T(
transportAddress: transportAddress,
tcpTransport: TcpTransport.new(upgrade = Upgrade()))
flags: flags,
tcpTransport: TcpTransport.new(upgrade = upgrade))

proc connHandler*(self: TorTransport,
client: StreamTransport,
Expand Down Expand Up @@ -88,8 +92,8 @@ proc connHandler*(self: TorTransport,
proc connectToTorServer(transportAddress: TransportAddress): Future[StreamTransport] {.async, gcsafe.} =
let transp = await connect(transportAddress)
try:
var bytesWritten = await transp.write(@[05'u8, 01, 00])
var resp = await transp.read(2)
discard await transp.write(@[05'u8, 01, 00])
discard await transp.read(2)
return transp
except CatchableError as err:
await transp.closeWait()
Expand All @@ -108,7 +112,7 @@ proc dialPeer(transp: StreamTransport, address: MultiAddress) {.async, gcsafe.}
let b = @[05'u8, 01, 00, 03] & dstAddr & dstPort

discard await transp.write(b)
discard await transp.read(10)
echo await transp.read(5)

method dial*(
self: TorTransport,
Expand All @@ -133,7 +137,6 @@ method start*(
## listen on the transport
##

#await procCall Transport(self).start(addrs)
var ipTcpAddrs: seq[MultiAddress]
var onion3Addrs: seq[MultiAddress]
for i, ma in addrs:
Expand All @@ -147,8 +150,8 @@ method start*(
onion3Addrs.add(onion3)

if len(ipTcpAddrs) != 0 and len(onion3Addrs) != 0:
await procCall Transport(self).start(onion3Addrs)
await self.tcpTransport.start(ipTcpAddrs)
self.addrs = onion3Addrs

method accept*(self: TorTransport): Future[Connection] {.async, gcsafe.} =
## accept a new TCP connection
Expand Down
6 changes: 3 additions & 3 deletions tests/testtortransport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ suite "Tor transport":
checkTrackers()

asyncTest "test dial":
let s = TorTransport.new(torServer)
let s = TorTransport.new(transportAddress = torServer, upgrade = Upgrade())
let ma = MultiAddress.init("/onion3/torchdeedp3i2jigzjdmfpn5ttjhthh5wbmda2rr3jvqjg5p77c54dqd:80")
let conn = await s.dial("", ma.tryGet())

Expand All @@ -33,7 +33,7 @@ suite "Tor transport":

asyncTest "test start":
proc a() {.async, raises:[].} =
let s = TorTransport.new(initTAddress("127.0.0.1", 9050.Port))
let s = TorTransport.new(transportAddress = torServer, upgrade = Upgrade())
let ma = MultiAddress.init("/onion3/a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcad:80")
let conn = await s.dial("", ma.tryGet())

Expand All @@ -45,7 +45,7 @@ suite "Tor transport":
#await s.stop()
echo string.fromBytes(resp)

let server = TorTransport.new(initTAddress("127.0.0.1", 9050.Port))
let server = TorTransport.new(transportAddress = torServer, upgrade = Upgrade())
let ma = @[MultiAddress.init("/ip4/127.0.0.1/tcp/8080/onion3/a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcad:80").tryGet()]
asyncSpawn server.start(ma)

Expand Down

0 comments on commit 31d5d57

Please sign in to comment.