Skip to content

Commit 94142af

Browse files
committed
feat: add TCP and WS filtering with /ipfs fragment
1 parent 0101bd2 commit 94142af

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,45 @@ const TCP = and(IP, base('tcp'))
1818
const UDP = and(IP, base('udp'))
1919
const UTP = and(UDP, base('utp'))
2020

21+
const TCP_IPFS = or(
22+
and(TCP, base('ipfs')),
23+
TCP
24+
)
25+
2126
const DNS = or(
2227
and(_DNS, base('tcp')),
2328
_DNS
2429
)
2530

31+
const DNS_IPFS = or(
32+
and(DNS, base('ipfs')),
33+
DNS
34+
)
35+
2636
const WebSockets = or(
2737
and(TCP, base('ws')),
2838
and(DNS, base('ws'))
2939
)
3040

41+
const WebSocketsIPFS = or(
42+
and(WebSockets, base('ipfs')),
43+
and(TCP_IPFS, base('ws')),
44+
and(DNS_IPFS, base('ws')),
45+
WebSockets
46+
)
47+
3148
const WebSocketsSecure = or(
3249
and(TCP, base('wss')),
3350
and(DNS, base('wss'))
3451
)
3552

53+
const WebSocketsSecureIPFS = or(
54+
and(WebSocketsSecure, base('ipfs')),
55+
and(TCP_IPFS, base('wss')),
56+
and(DNS_IPFS, base('wss')),
57+
WebSocketsSecure
58+
)
59+
3660
const HTTP = or(
3761
and(TCP, base('http')),
3862
and(DNS),
@@ -87,15 +111,19 @@ const IPFS = or(
87111
)
88112

89113
exports.DNS = DNS
114+
exports.DNS_IPFS = DNS_IPFS
90115
exports.DNS4 = DNS4
91116
exports.DNS6 = DNS6
92117
exports.IP = IP
93118
exports.TCP = TCP
119+
exports.TCP_IPFS = TCP_IPFS
94120
exports.UDP = UDP
95121
exports.UTP = UTP
96122
exports.HTTP = HTTP
97123
exports.WebSockets = WebSockets
124+
exports.WebSocketsIPFS = WebSocketsIPFS
98125
exports.WebSocketsSecure = WebSocketsSecure
126+
exports.WebSocketsSecureIPFS = WebSocketsSecureIPFS
99127
exports.WebRTCStar = WebRTCStar
100128
exports.WebRTCDirect = WebRTCDirect
101129
exports.Reliable = Reliable

test/index.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ describe('multiaddr validation', function () {
2020
'/ip4/127.0.0.1'
2121
]
2222

23+
const goodDnsIPFS = [
24+
'/dns/ipfs.io/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
25+
'/dns4/ipfs.io/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
26+
'/dns4/libp2p.io/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
27+
'/dns6/protocol.ai/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
28+
'/dns4/protocol.ai/tcp/80/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
29+
'/dns6/protocol.ai/tcp/80/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
30+
'/dns/protocol.ai/tcp/80/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
31+
]
32+
33+
const badDnsIPFS = [
34+
'/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
35+
]
36+
2337
const goodIP = [
2438
'/ip4/0.0.0.0',
2539
'/ip6/fc00::'
@@ -40,6 +54,11 @@ describe('multiaddr validation', function () {
4054
'/ip6/fc00::/udp/5523/tcp/9543'
4155
]
4256

57+
const goodTcpIPFS = [
58+
'/ip4/0.0.7.6/tcp/1234/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
59+
'/ip6/::/tcp/0/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
60+
]
61+
4362
const goodUDP = [
4463
'/ip4/0.0.7.6/udp/1234',
4564
'/ip6/::/udp/0'
@@ -72,6 +91,18 @@ describe('multiaddr validation', function () {
7291
'/ip6/::/tcp/0/wss'
7392
]
7493

94+
const goodWsIPFS = [
95+
'/dns/ipfs.io/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
96+
'/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
97+
'/ip6/::/tcp/0/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
98+
]
99+
100+
const goodWssIPFS = [
101+
'/dns/ipfs.io/wss/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
102+
'/ip4/1.2.3.4/tcp/3456/wss/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
103+
'/ip6/::/tcp/0/wss/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
104+
]
105+
75106
const goodWebRTCStar = [
76107
'/libp2p-webrtc-star/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
77108
'/libp2p-webrtc-star/dns/ipfs.io/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
@@ -147,6 +178,11 @@ describe('multiaddr validation', function () {
147178
assertMismatches(mafmt.DNS, badDNS, badIP, goodTCP)
148179
})
149180

181+
it('DNS IPFS validation', function () {
182+
assertMatches(mafmt.DNS_IPFS, goodDnsIPFS)
183+
assertMismatches(mafmt.DNS_IPFS, badDnsIPFS, badDNS, badIP, goodTCP)
184+
})
185+
150186
it('IP validation', function () {
151187
assertMatches(mafmt.IP, goodIP)
152188
assertMismatches(mafmt.IP, badIP, goodTCP)
@@ -157,6 +193,10 @@ describe('multiaddr validation', function () {
157193
assertMismatches(mafmt.TCP, badTCP, goodIP)
158194
})
159195

196+
it('TCP IPFS validation', function () {
197+
assertMatches(mafmt.TCP_IPFS, goodTcpIPFS)
198+
})
199+
160200
it('UDP validation', function () {
161201
assertMatches(mafmt.UDP, goodUDP)
162202
assertMismatches(mafmt.UDP, badUDP, goodIP, goodTCP, goodUTP)
@@ -182,6 +222,16 @@ describe('multiaddr validation', function () {
182222
assertMismatches(mafmt.WebSocketsSecure, goodIP, badWSS, goodUDP, badWS)
183223
})
184224

225+
it('WebSockets IPFS validation', function () {
226+
assertMatches(mafmt.WebSocketsIPFS, goodWsIPFS)
227+
assertMismatches(mafmt.WebSocketsIPFS, goodIP, goodUDP, badWS)
228+
})
229+
230+
it('WebSocketsSecure IPFS validation', function () {
231+
assertMatches(mafmt.WebSocketsSecureIPFS, goodWssIPFS)
232+
assertMismatches(mafmt.WebSocketsIPFS, goodIP, goodUDP, badWSS)
233+
})
234+
185235
it('WebRTC-star validation', function () {
186236
assertMatches(mafmt.WebRTCStar, goodWebRTCStar)
187237
assertMismatches(mafmt.WebRTCStar, goodIP, goodUDP, badWS)

0 commit comments

Comments
 (0)