Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - feat: add TCP and WS filtering with /ipfs fragment #16

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,64 @@ const _DNS = or(
)

const IP = or(base('ip4'), base('ip6'))
const TCP = and(IP, base('tcp'))
const UDP = and(IP, base('udp'))
const UTP = and(UDP, base('utp'))

const DNS = or(
const _UDP = and(IP, base('udp'))
const UDP = or(
and(_UDP, base('ipfs')),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without wanting to go back and forth. Is there any reason why a transport code itself wouldn't be able to make two assertions .matchIPFS and .matchUDP so that if the first one happen, it decapsulates IPFS first and then does the matchUDP?

Note, that is how all the transports have been working:

There is a separation between what is a valid addr for a spec transport (i.e TCP was here before) and there is the addrs that our libp2p- accept.

libp2p-tcp accepts TCP multiaddrs or TCP + IPFS, but that assertion should happen on the filter function and not in mafmt, which is a utility to check capabilities/presence of certain addrs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically because of this - https://github.com/libp2p/js-libp2p-tcp/pull/80/files, we though at the time that having mafmt handle those special circuit cases such as /ip4/127.0.0.1/tcp/4001/ws/ipfs/QmRelay/p2p-circuit/ipfs/QmDst would be a better approach. I'm not so sure anymore, I think the initial approach of handling the special cases might be simpler and safer.

The two relevant PR that lead to this change are -

Copy link
Member Author

@dryajov dryajov Sep 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think that since then, we've also scratched listening on circuit addresses and the more advanced circuit-relay features - we might not even need this anymore (for now) because we're not going to be using that kind of addressing right now. I would say we leave it alone and revisit when we actually need this feature.

Glad you asked the question tho!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diasdavid seems like I spoke too soon, we still need this as we are supporting specific relay addrs. We can go with this approach or reopen the PRs linked above (handle circuit as a special case in .filter). I'm not so sure of which one is better. This PR is definitely higher risk as it affects everything that uses mafmt, handling a special case in .filter is uglier, but might be safer for the time being?

Here is @vyzo response regarding Go's handling of specific circuit addrs.

dryajov: it accepts them
10:13 but unless it's an active relay it's pretty much a hint
10:13 if it's an active relay, then it adds the address to its peerstore for the dial attempt
10:13 so it will honor them
10:14 ah,the dialer
10:14 sorry, i was mislieading
10:14 it handles them just fine
10:14 it adds to the peerstore for the dial attempt
10:14 so it's a hint
10:14 may or may not be used by the dialer
10:14 and maybe there is already an existing connection

_UDP
)

const _UTP = and(UDP, base('utp'))
const UTP = or(
and(_UTP, base('ipfs')),
_UTP
)

const _TCP = and(IP, base('tcp'))
const TCP = or(
and(_TCP, base('ipfs')),
_TCP
)

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

const WebSockets = or(
const DNS = or(
and(__DNS, base('ipfs')),
__DNS
)

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

const WebSocketsSecure = or(
const WebSockets = or(
and(_WebSockets, base('ipfs')),
_WebSockets
)

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

const WebSocketsSecure = or(
and(_WebSocketsSecure, base('ipfs')),
_WebSocketsSecure
)

const HTTP = or(
and(TCP, base('http')),
and(DNS),
and(DNS, base('http'))
)

const WebRTCStar = or(
and(WebSockets, base('p2p-webrtc-star'), base('ipfs')),
and(WebSocketsSecure, base('p2p-webrtc-star'), base('ipfs'))
and(_WebSockets, base('p2p-webrtc-star'), base('ipfs')),
and(_WebSocketsSecure, base('p2p-webrtc-star'), base('ipfs'))
)

const WebSocketsStar = or(
Expand All @@ -63,17 +93,14 @@ const Reliable = or(
)

let _IPFS = or(
and(Reliable, base('ipfs')),
WebRTCStar,
Reliable,
base('ipfs')
)

const _Circuit = or(
and(_IPFS, base('p2p-circuit'), _IPFS),
and(_IPFS, base('p2p-circuit')),
and(base('p2p-circuit'), _IPFS),
and(Reliable, base('p2p-circuit')),
and(base('p2p-circuit'), Reliable),
base('p2p-circuit')
)

Expand Down
46 changes: 39 additions & 7 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 @@ -32,7 +46,9 @@ describe('multiaddr validation', function () {

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

const badTCP = [
Expand All @@ -42,7 +58,9 @@ describe('multiaddr validation', function () {

const goodUDP = [
'/ip4/0.0.7.6/udp/1234',
'/ip6/::/udp/0'
'/ip6/::/udp/0',
'/ip4/0.0.7.6/udp/1234/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip6/::/udp/0/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const badUDP = [
Expand All @@ -52,7 +70,9 @@ describe('multiaddr validation', function () {

const goodUTP = [
'/ip4/1.2.3.4/udp/3456/utp',
'/ip6/::/udp/0/utp'
'/ip6/::/udp/0/utp',
'/ip4/1.2.3.4/udp/3456/utp/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip6/::/udp/0/utp/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

const badUTP = [
Expand All @@ -63,13 +83,19 @@ describe('multiaddr validation', function () {
const goodWS = [
'/dns/ipfs.io/ws',
'/ip4/1.2.3.4/tcp/3456/ws',
'/ip6/::/tcp/0/ws'
'/ip6/::/tcp/0/ws',
'/dns/ipfs.io/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip4/1.2.3.4/tcp/3456/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip6/::/tcp/0/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
]

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

const goodWebRTCStar = [
Expand Down Expand Up @@ -121,12 +147,13 @@ describe('multiaddr validation', function () {
]

const goodIPFS = [
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
'/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit',
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj'
].concat(goodCircuit)
]

function assertMatches (p) {
const tests = Array.from(arguments).slice(1)
Expand All @@ -151,6 +178,11 @@ describe('multiaddr validation', function () {
assertMismatches(mafmt.DNS, badDNS, badIP, goodTCP)
})

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

it('IP validation', function () {
assertMatches(mafmt.IP, goodIP)
assertMismatches(mafmt.IP, badIP, goodTCP)
Expand Down Expand Up @@ -207,6 +239,6 @@ describe('multiaddr validation', function () {
})

it('IPFS validation', function () {
assertMatches(mafmt.IPFS, goodIPFS)
assertMatches(mafmt.IPFS, goodIPFS, goodCircuit)
})
})