diff --git a/libp2p/transports/tortransport.nim b/libp2p/transports/tortransport.nim index d003dea5f1..e816b2ecc0 100644 --- a/libp2p/transports/tortransport.nim +++ b/libp2p/transports/tortransport.nim @@ -118,7 +118,7 @@ proc readServerReply(transp: StreamTransport) {.async, gcsafe.} = if serverReply != Socks5ReplyType.Succeeded.byte: var socks5ReplyType: Socks5ReplyType if socks5ReplyType.checkedEnumAssign(serverReply): - raise newException(Socks5ServerReplyError, fmt"Server reply error: {Socks5ReplyType(serverReply)}") + raise newException(Socks5ServerReplyError, fmt"Server reply error: {socks5ReplyType}") else: raise newException(LPError, fmt"Unexpected server reply: {serverReply}") let atyp = firstFourOctets[3] @@ -128,23 +128,30 @@ proc readServerReply(transp: StreamTransport) {.async, gcsafe.} = of Socks5AddressType.FQDN.byte: let fqdnNumOctets = await transp.read(1) discard await transp.read(int(uint8.fromBytes(fqdnNumOctets)) + portNumOctets) - else: + of Socks5AddressType.IPv6.byte: discard await transp.read(ipV6NumOctets + portNumOctets) + else: + raise newException(LPError, "Address not supported") -proc parseOnion3(address: MultiAddress): (byte, seq[byte], seq[byte]) = +proc parseOnion3(address: MultiAddress): (byte, seq[byte], seq[byte]) {.raises: [LPError, ValueError].} = + var addressArray = ($address).split('/') + if addressArray.len < 2: raise newException(LPError, fmt"Onion address not supported {address}") + addressArray = addressArray[2].split(':') + if addressArray.len == 0: raise newException(LPError, fmt"Onion address not supported {address}") let - addressArray = ($address).split('/') - addressStr = addressArray[2].split(':')[0] & ".onion" + addressStr = addressArray[0] & ".onion" dstAddr = @(uint8(addressStr.len).toBytes()) & addressStr.toBytes() dstPort = address.data.buffer[37..38] return (Socks5AddressType.FQDN.byte, dstAddr, dstPort) -proc parseIpTcp(address: MultiAddress): (byte, seq[byte], seq[byte]) = - let (codec, atyp) = +proc parseIpTcp(address: MultiAddress): (byte, seq[byte], seq[byte]) {.raises: [LPError, ValueError].} = + let (codec, atyp) = if IPv4Tcp.match(address): (multiCodec("ip4"), Socks5AddressType.IPv4.byte) - else: + elif IPv6Tcp.match(address): (multiCodec("ip6"), Socks5AddressType.IPv6.byte) + else: + raise newException(LPError, fmt"IP address not supported {address}") let dstAddr = address[codec].get().protoArgument().get() dstPort = address[multiCodec("tcp")].get().protoArgument().get() @@ -159,9 +166,6 @@ proc parseDnsTcp(address: MultiAddress): (byte, seq[byte], seq[byte]) = proc dialPeer( transp: StreamTransport, address: MultiAddress) {.async, gcsafe.} = - # The address field contains a fully-qualified domain name. - # The first octet of the address field contains the number of octets of name that - # follow, there is no terminating NUL octet. let (atyp, dstAddr, dstPort) = if Onion3.match(address): parseOnion3(address) @@ -188,7 +192,7 @@ method dial*( ## dial a peer ## if not handlesDial(address): - raise newException(LPError, fmt"Not supported address: {address}") + raise newException(LPError, fmt"Address not supported: {address}") trace "Dialing remote peer", address = $address let transp = await connectToTorServer(self.transportAddress) diff --git a/tests/stubs.nim b/tests/stubs.nim index 8ece201848..aae661de44 100644 --- a/tests/stubs.nim +++ b/tests/stubs.nim @@ -45,9 +45,9 @@ proc start*(self: TorServerStub, address: TransportAddress) {.async.} = msg = newSeq[byte](4) await connSrc.readExactly(addr msg[0], 4) - let atyp = int(uint8.fromBytes(msg[3..3])) + let atyp = msg[3] let address = case atyp: - of Socks5AddressType.IPv4.ord: + of Socks5AddressType.IPv4.byte: let n = 4 + 2 # +2 bytes for the port msg = newSeq[byte](n) await connSrc.readExactly(addr msg[0], n) @@ -55,7 +55,7 @@ proc start*(self: TorServerStub, address: TransportAddress) {.async.} = for i, e in msg[0..^3]: ip[i] = e $(ipv4(ip)) & ":" & $(Port(fromBytesBE(uint16, msg[^2..^1]))) - of Socks5AddressType.IPv6.ord: + of Socks5AddressType.IPv6.byte: let n = 16 + 2 # +2 bytes for the port msg = newSeq[byte](n) # +2 bytes for the port await connSrc.readExactly(addr msg[0], n) @@ -63,7 +63,7 @@ proc start*(self: TorServerStub, address: TransportAddress) {.async.} = for i, e in msg[0..^3]: ip[i] = e $(ipv6(ip)) & ":" & $(Port(fromBytesBE(uint16, msg[^2..^1]))) - of Socks5AddressType.FQDN.ord: + of Socks5AddressType.FQDN.byte: await connSrc.readExactly(addr msg[0], 1) let n = int(uint8.fromBytes(msg[0..0])) + 2 # +2 bytes for the port msg = newSeq[byte](n)