-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: do not dial private addresses in browsers (#1735)
Dialling is very expensive in browsers. If we're dialling addresses that include private networks, chances are they're not going to succeed so filter them out for browsers only. This can be re-enabled by configuring a permissive connection gater, see the upgrade guide for more information. BREAKING CHANGE: browsers will no longer try to dial private addresses by default
- Loading branch information
1 parent
94df577
commit e3deaa4
Showing
23 changed files
with
168 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { ConnectionGater } from '@libp2p/interface-connection-gater' | ||
import type { Multiaddr } from '@multiformats/multiaddr' | ||
import isPrivate from 'private-ip' | ||
|
||
/** | ||
* Returns a connection gater that disallows dialling private addresses by | ||
* default. Browsers are severely limited in their resource usage so don't | ||
* waste time trying to dial undiallable addresses. | ||
*/ | ||
export function connectionGater (gater: ConnectionGater = {}): ConnectionGater { | ||
return { | ||
denyDialPeer: async () => false, | ||
denyDialMultiaddr: async (multiaddr: Multiaddr) => { | ||
const tuples = multiaddr.stringTuples() | ||
|
||
if (tuples[0][0] === 4 || tuples[0][0] === 41) { | ||
return Boolean(isPrivate(`${tuples[0][1]}`)) | ||
} | ||
|
||
return false | ||
}, | ||
denyInboundConnection: async () => false, | ||
denyOutboundConnection: async () => false, | ||
denyInboundEncryptedConnection: async () => false, | ||
denyOutboundEncryptedConnection: async () => false, | ||
denyInboundUpgradedConnection: async () => false, | ||
denyOutboundUpgradedConnection: async () => false, | ||
filterMultiaddrForPeer: async () => true, | ||
...gater | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { ConnectionGater } from '@libp2p/interface-connection-gater' | ||
|
||
/** | ||
* Returns a default connection gater implementation that allows everything | ||
*/ | ||
export function connectionGater (gater: ConnectionGater = {}): ConnectionGater { | ||
return { | ||
denyDialPeer: async () => false, | ||
denyDialMultiaddr: async () => false, | ||
denyInboundConnection: async () => false, | ||
denyOutboundConnection: async () => false, | ||
denyInboundEncryptedConnection: async () => false, | ||
denyOutboundEncryptedConnection: async () => false, | ||
denyInboundUpgradedConnection: async () => false, | ||
denyOutboundUpgradedConnection: async () => false, | ||
filterMultiaddrForPeer: async () => true, | ||
...gater | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.