diff --git a/main.ts b/main.ts index c077934..b081a1c 100644 --- a/main.ts +++ b/main.ts @@ -2,7 +2,10 @@ import { createVerifiedFetch, type VerifiedFetch } from "@helia/verified-fetch"; import { createHelia } from "helia"; import { bootstrap } from "@libp2p/bootstrap"; import { webSockets } from "@libp2p/websockets"; -import { multiaddr } from "@multiformats/multiaddr"; +import { ConnectionGater } from "@libp2p/interface"; +import { Multiaddr, multiaddr } from "@multiformats/multiaddr"; +import { bitswap } from "@helia/block-brokers"; +import { WebSockets } from "@multiformats/multiaddr-matcher"; import { webRTC } from "@libp2p/webrtc"; import { circuitRelayTransport } from "@libp2p/circuit-relay-v2"; @@ -102,6 +105,48 @@ const dialWithAbortSignal = async (node: any, multiaddressString: string) => { } }; +export function connectionGater(gater: ConnectionGater = {}): ConnectionGater { + return { + denyDialPeer: async () => false, + denyDialMultiaddr: async (multiaddress: Multiaddr) => { + // do not connect to insecure websockets by default + if (WebSockets.matches(multiaddress)) { + return false; + } + + // check if it matches with FLUORINE_WEBSOCKETS, BISMUTH_WEBSOCKETS or CERIUM_WEBSOCKETS + if (multiaddress.toString() === FLUORINE_WEBSOCKETS) { + return false; + } + if (multiaddress.toString() === BISMUTH_WEBSOCKETS) { + return false; + } + if (multiaddress.toString() === CERIUM_WEBSOCKETS) { + return false; + } + // Deny everything else + return true; + + // const tuples = multiaddress.stringTuples(); + + // // do not connect to private addresses by default + // if (tuples[0][0] === CODEC_IP4 || tuples[0][0] === CODEC_IP6) { + // return Boolean(isPrivateIp(`${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, + }; +} + let verifiedFetchFn: VerifiedFetch; let helia: any; async function runDemo() { @@ -116,13 +161,15 @@ async function runDemo() { tagName: "cluster-peer", }), ], + connectionGater: connectionGater(), }, + blockBrokers: [bitswap()], }); await connectToCluster(helia); verifiedFetchFn = await createVerifiedFetch(helia); const response = await verifiedFetchFn( - "ipns://k51qzi5uqu5dk89atnl883sr0g1cb2py631ckz9ng45qhk6dg0pj141jtxtx6l", + "ipfs://baguqeeraxym2g5iaecqzqc6uekitjaxn47dncq3o2mt36wqbqjt2q3ci42dq", ); const results = await response.json(); diff --git a/package.json b/package.json index 8e3034f..b0d4170 100644 --- a/package.json +++ b/package.json @@ -19,12 +19,14 @@ "vite-plugin-node-polyfills": "^0.23.0" }, "dependencies": { + "@helia/block-brokers": "^4.1.0", "@helia/verified-fetch": "^2.6.4", "@libp2p/bootstrap": "^11.0.33", "@libp2p/circuit-relay-v2": "^3.2.9", "@libp2p/webrtc": "^5.2.10", "@libp2p/websockets": "^9.2.9", "@multiformats/multiaddr": "^12.4.0", + "@multiformats/multiaddr-matcher": "^1.7.0", "helia": "^5.3.0" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3b59622..994f44c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,9 @@ importers: .: dependencies: + '@helia/block-brokers': + specifier: ^4.1.0 + version: 4.1.0 '@helia/verified-fetch': specifier: ^2.6.4 version: 2.6.4(react-native@0.79.0(@babel/core@7.26.10)(react@19.1.0)) @@ -26,6 +29,9 @@ importers: '@multiformats/multiaddr': specifier: ^12.4.0 version: 12.4.0 + '@multiformats/multiaddr-matcher': + specifier: ^1.7.0 + version: 1.7.0 helia: specifier: ^5.3.0 version: 5.3.0(react-native@0.79.0(@babel/core@7.26.10)(react@19.1.0))