Skip to content

Commit

Permalink
feat: add TCP and WS filtering with /ipfs fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
dryajov committed Aug 3, 2017
1 parent 0101bd2 commit 94142af
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,45 @@ const TCP = and(IP, base('tcp'))
const UDP = and(IP, base('udp'))
const UTP = and(UDP, base('utp'))

const TCP_IPFS = or(
and(TCP, base('ipfs')),
TCP
)

const DNS = or(
and(_DNS, base('tcp')),
_DNS
)

const DNS_IPFS = or(
and(DNS, base('ipfs')),
DNS
)

const WebSockets = or(
and(TCP, base('ws')),
and(DNS, base('ws'))
)

const WebSocketsIPFS = or(
and(WebSockets, base('ipfs')),
and(TCP_IPFS, base('ws')),
and(DNS_IPFS, base('ws')),
WebSockets
)

const WebSocketsSecure = or(
and(TCP, base('wss')),
and(DNS, base('wss'))
)

const WebSocketsSecureIPFS = or(
and(WebSocketsSecure, base('ipfs')),
and(TCP_IPFS, base('wss')),
and(DNS_IPFS, base('wss')),
WebSocketsSecure
)

const HTTP = or(
and(TCP, base('http')),
and(DNS),
Expand Down Expand Up @@ -87,15 +111,19 @@ const IPFS = or(
)

exports.DNS = DNS
exports.DNS_IPFS = DNS_IPFS
exports.DNS4 = DNS4
exports.DNS6 = DNS6
exports.IP = IP
exports.TCP = TCP
exports.TCP_IPFS = TCP_IPFS
exports.UDP = UDP
exports.UTP = UTP
exports.HTTP = HTTP
exports.WebSockets = WebSockets
exports.WebSocketsIPFS = WebSocketsIPFS
exports.WebSocketsSecure = WebSocketsSecure
exports.WebSocketsSecureIPFS = WebSocketsSecureIPFS
exports.WebRTCStar = WebRTCStar
exports.WebRTCDirect = WebRTCDirect
exports.Reliable = Reliable
Expand Down
50 changes: 50 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ describe('multiaddr validation', function () {
'/ip4/127.0.0.1'
]

const goodDnsIPFS = [
'/dns/ipfs.io/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/dns4/ipfs.io/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/dns4/libp2p.io/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/dns6/protocol.ai/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/dns4/protocol.ai/tcp/80/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/dns6/protocol.ai/tcp/80/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/dns/protocol.ai/tcp/80/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const badDnsIPFS = [
'/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const goodIP = [
'/ip4/0.0.0.0',
'/ip6/fc00::'
Expand All @@ -40,6 +54,11 @@ describe('multiaddr validation', function () {
'/ip6/fc00::/udp/5523/tcp/9543'
]

const goodTcpIPFS = [
'/ip4/0.0.7.6/tcp/1234/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip6/::/tcp/0/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const goodUDP = [
'/ip4/0.0.7.6/udp/1234',
'/ip6/::/udp/0'
Expand Down Expand Up @@ -72,6 +91,18 @@ describe('multiaddr validation', function () {
'/ip6/::/tcp/0/wss'
]

const goodWsIPFS = [
'/dns/ipfs.io/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip6/::/tcp/0/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const goodWssIPFS = [
'/dns/ipfs.io/wss/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip4/1.2.3.4/tcp/3456/wss/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip6/::/tcp/0/wss/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const goodWebRTCStar = [
'/libp2p-webrtc-star/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/libp2p-webrtc-star/dns/ipfs.io/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
Expand Down Expand Up @@ -147,6 +178,11 @@ describe('multiaddr validation', function () {
assertMismatches(mafmt.DNS, badDNS, badIP, goodTCP)
})

it('DNS IPFS validation', function () {
assertMatches(mafmt.DNS_IPFS, goodDnsIPFS)
assertMismatches(mafmt.DNS_IPFS, badDnsIPFS, badDNS, badIP, goodTCP)
})

it('IP validation', function () {
assertMatches(mafmt.IP, goodIP)
assertMismatches(mafmt.IP, badIP, goodTCP)
Expand All @@ -157,6 +193,10 @@ describe('multiaddr validation', function () {
assertMismatches(mafmt.TCP, badTCP, goodIP)
})

it('TCP IPFS validation', function () {
assertMatches(mafmt.TCP_IPFS, goodTcpIPFS)
})

it('UDP validation', function () {
assertMatches(mafmt.UDP, goodUDP)
assertMismatches(mafmt.UDP, badUDP, goodIP, goodTCP, goodUTP)
Expand All @@ -182,6 +222,16 @@ describe('multiaddr validation', function () {
assertMismatches(mafmt.WebSocketsSecure, goodIP, badWSS, goodUDP, badWS)
})

it('WebSockets IPFS validation', function () {
assertMatches(mafmt.WebSocketsIPFS, goodWsIPFS)
assertMismatches(mafmt.WebSocketsIPFS, goodIP, goodUDP, badWS)
})

it('WebSocketsSecure IPFS validation', function () {
assertMatches(mafmt.WebSocketsSecureIPFS, goodWssIPFS)
assertMismatches(mafmt.WebSocketsIPFS, goodIP, goodUDP, badWSS)
})

it('WebRTC-star validation', function () {
assertMatches(mafmt.WebRTCStar, goodWebRTCStar)
assertMismatches(mafmt.WebRTCStar, goodIP, goodUDP, badWS)
Expand Down

0 comments on commit 94142af

Please sign in to comment.