WS Transport: handle 'tls/ws' (#914)

This commit is contained in:
Tanguy 2023-06-12 17:45:53 +02:00 committed by GitHub
parent eb78292d9c
commit c28d8bb353
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 7 deletions

View File

@ -416,6 +416,9 @@ const
MAProtocol( MAProtocol(
mcodec: multiCodec("wss"), kind: Marker, size: 0 mcodec: multiCodec("wss"), kind: Marker, size: 0
), ),
MAProtocol(
mcodec: multiCodec("tls"), kind: Marker, size: 0
),
MAProtocol( MAProtocol(
mcodec: multiCodec("ipfs"), kind: Length, size: 0, mcodec: multiCodec("ipfs"), kind: Length, size: 0,
coder: TranscoderP2P coder: TranscoderP2P
@ -468,7 +471,7 @@ const
IP* = mapOr(IP4, IP6) IP* = mapOr(IP4, IP6)
DNS_OR_IP* = mapOr(DNS, IP) DNS_OR_IP* = mapOr(DNS, IP)
TCP_DNS* = mapAnd(DNS, mapEq("tcp")) TCP_DNS* = mapAnd(DNS, mapEq("tcp"))
TCP_IP* =mapAnd(IP, mapEq("tcp")) TCP_IP* = mapAnd(IP, mapEq("tcp"))
TCP* = mapOr(TCP_DNS, TCP_IP) TCP* = mapOr(TCP_DNS, TCP_IP)
UDP_DNS* = mapAnd(DNS, mapEq("udp")) UDP_DNS* = mapAnd(DNS, mapEq("udp"))
UDP_IP* = mapAnd(IP, mapEq("udp")) UDP_IP* = mapAnd(IP, mapEq("udp"))
@ -479,9 +482,10 @@ const
WS_DNS* = mapAnd(TCP_DNS, mapEq("ws")) WS_DNS* = mapAnd(TCP_DNS, mapEq("ws"))
WS_IP* = mapAnd(TCP_IP, mapEq("ws")) WS_IP* = mapAnd(TCP_IP, mapEq("ws"))
WS* = mapAnd(TCP, mapEq("ws")) WS* = mapAnd(TCP, mapEq("ws"))
WSS_DNS* = mapAnd(TCP_DNS, mapEq("wss")) TLS_WS* = mapOr(mapEq("wss"), mapAnd(mapEq("tls"), mapEq("ws")))
WSS_IP* = mapAnd(TCP_IP, mapEq("wss")) WSS_DNS* = mapAnd(TCP_DNS, TLS_WS)
WSS* = mapAnd(TCP, mapEq("wss")) WSS_IP* = mapAnd(TCP_IP, TLS_WS)
WSS* = mapAnd(TCP, TLS_WS)
WebSockets_DNS* = mapOr(WS_DNS, WSS_DNS) WebSockets_DNS* = mapOr(WS_DNS, WSS_DNS)
WebSockets_IP* = mapOr(WS_IP, WSS_IP) WebSockets_IP* = mapOr(WS_IP, WSS_IP)
WebSockets* = mapOr(WS, WSS) WebSockets* = mapOr(WS, WSS)

View File

@ -191,9 +191,10 @@ const MultiCodecList = [
("p2p", 0x01A5), ("p2p", 0x01A5),
("http", 0x01E0), ("http", 0x01E0),
("https", 0x01BB), ("https", 0x01BB),
("tls", 0x01C0),
("quic", 0x01CC), ("quic", 0x01CC),
("ws", 0x01DD), ("ws", 0x01DD),
("wss", 0x01DE), # not in multicodec list ("wss", 0x01DE),
("p2p-websocket-star", 0x01DF), # not in multicodec list ("p2p-websocket-star", 0x01DF), # not in multicodec list
("p2p-webrtc-star", 0x0113), # not in multicodec list ("p2p-webrtc-star", 0x0113), # not in multicodec list
("p2p-webrtc-direct", 0x0114), # not in multicodec list ("p2p-webrtc-direct", 0x0114), # not in multicodec list

View File

@ -156,8 +156,12 @@ method start*(
self.httpservers &= httpserver self.httpservers &= httpserver
let codec = if isWss: let codec =
MultiAddress.init("/wss") if isWss:
if ma.contains(multiCodec("tls")) == MaResult[bool].ok(true):
MultiAddress.init("/tls/ws")
else:
MultiAddress.init("/wss")
else: else:
MultiAddress.init("/ws") MultiAddress.init("/ws")

View File

@ -64,6 +64,8 @@ const
"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234", "/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC", "/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234", "/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
"/ip4/127.0.0.1/tcp/8000/wss/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/ip4/127.0.0.1/tcp/8000/tls/ws/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/unix/a/b/c/d/e", "/unix/a/b/c/d/e",
"/unix/stdio", "/unix/stdio",
"/ip4/1.2.3.4/tcp/80/unix/a/b/c/d/e/f", "/ip4/1.2.3.4/tcp/80/unix/a/b/c/d/e/f",

View File

@ -86,7 +86,9 @@ suite "WebSocket transport":
let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0/wss").tryGet()] let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0/wss").tryGet()]
let transport1 = WsTransport.new(Upgrade(), TLSPrivateKey.init(SecureKey), TLSCertificate.init(SecureCert), {TLSFlags.NoVerifyHost}) let transport1 = WsTransport.new(Upgrade(), TLSPrivateKey.init(SecureKey), TLSCertificate.init(SecureCert), {TLSFlags.NoVerifyHost})
const correctPattern = mapAnd(TCP, mapEq("wss"))
await transport1.start(ma) await transport1.start(ma)
check correctPattern.match(transport1.addrs[0])
proc acceptHandler() {.async, gcsafe.} = proc acceptHandler() {.async, gcsafe.} =
while true: while true:
let conn = await transport1.accept() let conn = await transport1.accept()
@ -108,3 +110,21 @@ suite "WebSocket transport":
await handlerWait.cancelAndWait() await handlerWait.cancelAndWait()
await transport1.stop() await transport1.stop()
asyncTest "handles tls/ws":
let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0/tls/ws").tryGet()]
let transport1 = wsSecureTranspProvider()
const correctPattern = mapAnd(TCP, mapEq("tls"), mapEq("ws"))
await transport1.start(ma)
check transport1.handles(transport1.addrs[0])
check correctPattern.match(transport1.addrs[0])
# Would raise somewhere if this wasn't handled:
let
inboundConn = transport1.accept()
outboundConn = await transport1.dial(transport1.addrs[0])
closing = outboundConn.close()
await (await inboundConn).close()
await closing
await transport1.stop()