Skip to content

Commit 528c606

Browse files
committed
add p2p-webrtc-direct pattern
1 parent 1dc3240 commit 528c606

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

patterns.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@ import (
44
"strings"
55

66
ma "github.com/multiformats/go-multiaddr"
7+
madns "github.com/multiformats/go-multiaddr-dns"
8+
)
9+
10+
// Define a dns4 format multiaddr
11+
var DNS4 = Base(madns.P_DNS4)
12+
13+
// Define a dns6 format multiaddr
14+
var DNS6 = Base(madns.P_DNS6)
15+
16+
var _DNS = Or(
17+
Base(madns.P_DNSADDR),
18+
DNS4,
19+
DNS6,
20+
)
21+
22+
// Define a dns4 or dns6 format multiaddr
23+
var DNS = Or(
24+
And(_DNS, Base(ma.P_TCP)),
25+
_DNS,
726
)
827

928
// Define IP as either ipv4 or ipv6
@@ -30,6 +49,26 @@ var Reliable = Or(TCP, UTP, QUIC)
3049
// IPFS can run over any reliable underlying transport protocol
3150
var IPFS = And(Reliable, Base(ma.P_IPFS))
3251

52+
// Define http over TCP or DNS or http over DNS format multiaddr
53+
var HTTP = Or(
54+
And(TCP, Base(ma.P_HTTP)),
55+
And(IP, Base(ma.P_HTTP)),
56+
And(DNS, Base(ma.P_HTTP)),
57+
And(DNS),
58+
)
59+
60+
// Define https over TCP or DNS or https over DNS format multiaddr
61+
var HTTPS = Or(
62+
And(TCP, Base(ma.P_HTTPS)),
63+
And(IP, Base(ma.P_HTTPS)),
64+
And(DNS, Base(ma.P_HTTPS)),
65+
)
66+
67+
// Define p2p-webrtc-direct over HTTP or p2p-webrtc-direct over HTTPS format multiaddr
68+
var WebRTCDirect = Or(
69+
And(HTTP, Base(ma.P_P2P_WEBRTC_DIRECT)),
70+
And(HTTPS, Base(ma.P_P2P_WEBRTC_DIRECT)))
71+
3372
const (
3473
or = iota
3574
and = iota

patterns_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ func assertMismatches(t *testing.T, p Pattern, args ...[]string) {
3838
}
3939

4040
func TestBasicMatching(t *testing.T) {
41+
good_dns := []string{
42+
"/dnsaddr/ipfs.io",
43+
"/dns4/ipfs.io",
44+
"/dns4/libp2p.io",
45+
"/dns6/protocol.ai",
46+
"/dns4/protocol.ai/tcp/80",
47+
"/dns6/protocol.ai/tcp/80",
48+
"/dnsaddr/protocol.ai/tcp/8",
49+
}
50+
51+
bad_dns := []string{
52+
"/ip4/127.0.0.1",
53+
}
54+
4155
good_ip := []string{
4256
"/ip4/0.0.0.0",
4357
"/ip6/fc00::",
@@ -90,6 +104,11 @@ func TestBasicMatching(t *testing.T) {
90104
"/quic",
91105
}
92106

107+
good_webrtcdirect := []string{
108+
"/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-direct",
109+
"/ip6/::/tcp/0/http/p2p-webrtc-direct",
110+
}
111+
93112
good_ipfs := []string{
94113
"/ip4/1.2.3.4/tcp/1234/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
95114
"/ip6/::/tcp/1234/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
@@ -106,6 +125,9 @@ func TestBasicMatching(t *testing.T) {
106125
"/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
107126
}
108127

128+
assertMatches(t, DNS, good_dns)
129+
assertMismatches(t, DNS, bad_dns, bad_ip)
130+
109131
assertMatches(t, IP, good_ip)
110132
assertMismatches(t, IP, bad_ip, good_tcp)
111133

@@ -127,6 +149,9 @@ func TestBasicMatching(t *testing.T) {
127149
assertMatches(t, Unreliable, good_udp)
128150
assertMismatches(t, Unreliable, good_ip, good_tcp, good_utp, good_ipfs, good_quic)
129151

152+
assertMatches(t, WebRTCDirect, good_webrtcdirect)
153+
assertMismatches(t, WebRTCDirect, good_ip, good_udp)
154+
130155
assertMatches(t, IPFS, good_ipfs)
131156
assertMismatches(t, IPFS, bad_ipfs, good_ip, good_tcp, good_utp, good_udp, good_quic)
132157
}

0 commit comments

Comments
 (0)