Skip to content

Commit

Permalink
chore: filter by http address
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Mar 14, 2024
1 parent 60365d2 commit ac06ff6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/block-brokers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@helia/interface": "^4.0.1",
"@libp2p/interface": "^1.1.4",
"@libp2p/utils": "^5.2.6",
"@multiformats/multiaddr-matcher": "^1.2.0",
"@multiformats/multiaddr-to-uri": "^10.0.1",
"interface-blockstore": "^5.2.10",
"ipfs-bitswap": "^20.0.2",
Expand Down
43 changes: 41 additions & 2 deletions packages/block-brokers/src/trustless-gateway/broker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { DEFAULT_SESSION_MIN_PROVIDERS, DEFAULT_SESSION_MAX_PROVIDERS, DEFAULT_SESSION_PROVIDER_QUERY_CONCURRENCY, DEFAULT_SESSION_PROVIDER_QUERY_TIMEOUT } from '@helia/interface'
import { PeerQueue } from '@libp2p/utils/peer-queue'
import { isPrivateIp } from '@libp2p/utils/private-ip'
import { DNS, HTTP, HTTPS } from '@multiformats/multiaddr-matcher'
import { multiaddrToUri } from '@multiformats/multiaddr-to-uri'
import pDefer from 'p-defer'
import { TrustlessGateway } from './trustless-gateway.js'
Expand All @@ -17,6 +19,22 @@ export interface CreateTrustlessGatewaySessionOptions extends CreateSessionOptio
* @default only-if-cached
*/
cacheControl?: string

/**
* By default we will only connect to peers with HTTPS addresses, pass true
* to also connect to HTTP addresses.
*
* @default false
*/
allowInsecure?: boolean

/**
* By default we will only connect to peers with public or DNS addresses, pass
* true to also connect to private addresses.
*
* @default false
*/
allowLocal?: boolean
}

/**
Expand Down Expand Up @@ -103,14 +121,30 @@ export class TrustlessGatewayBlockBroker implements BlockBroker<TrustlessGateway

Promise.resolve().then(async () => {
for await (const provider of this.routing.findProviders(root, options)) {
if (provider.protocols == null || !provider.protocols.includes('transport-ipfs-gateway-http')) {
const httpAddresses = provider.multiaddrs.filter(ma => {
if (HTTPS.matches(ma) || (options.allowInsecure === true && HTTP.matches(ma))) {
if (options.allowLocal === true) {
return true
}

if (DNS.matches(ma)) {
return true
}

return isPrivateIp(ma.toOptions().host) === false

Check warning on line 134 in packages/block-brokers/src/trustless-gateway/broker.ts

View check run for this annotation

Codecov / codecov/patch

packages/block-brokers/src/trustless-gateway/broker.ts#L129-L134

Added lines #L129 - L134 were not covered by tests
}

return false
})

if (httpAddresses.length === 0) {
continue
}

this.log('found transport-ipfs-gateway-http provider %p for cid %c', provider.id, root)

void queue.add(async () => {
for (const ma of provider.multiaddrs) {
for (const ma of httpAddresses) {
let uri: string | undefined

try {
Expand All @@ -125,6 +159,11 @@ export class TrustlessGatewayBlockBroker implements BlockBroker<TrustlessGateway
// be very widely implemented so as long as the remote responds
// we are happy they are valid
// https://specs.ipfs.tech/http-gateways/trustless-gateway/#head-ipfs-cid-path-params

// in the future we should be able to request `${uri}/.well-known/libp2p-http
// and discover an IPFS gateway from $.protocols['/ipfs/gateway'].path
// in the response
// https://github.com/libp2p/specs/pull/508/files
const response = await fetch(resource, {
method: 'HEAD',
headers: {
Expand Down
13 changes: 4 additions & 9 deletions packages/block-brokers/test/trustless-gateway.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import { TrustlessGatewayBlockBroker } from '../src/trustless-gateway/broker.js'
import { TrustlessGateway } from '../src/trustless-gateway/trustless-gateway.js'
import { createBlock } from './fixtures/create-block.js'
import type { Routing } from '@helia/interface'
import type { BlockBroker } from '@helia/interface/blocks'
import type { CID } from 'multiformats/cid'

describe('trustless-gateway-block-broker', () => {
let blocks: Array<{ cid: CID, block: Uint8Array }>
let gatewayBlockBroker: BlockBroker
let gatewayBlockBroker: TrustlessGatewayBlockBroker
let gateways: Array<StubbedInstance<TrustlessGateway>>
let routing: StubbedInstance<Routing>

Expand Down Expand Up @@ -165,9 +164,6 @@ describe('trustless-gateway-block-broker', () => {
id: await createEd25519PeerId(),
multiaddrs: [
multiaddr('/ip4/132.32.25.6/tcp/1234')
],
protocols: [
'transport-bitswap'
]
}
// expired peer info
Expand All @@ -180,16 +176,15 @@ describe('trustless-gateway-block-broker', () => {
id: await createEd25519PeerId(),
multiaddrs: [
uriToMultiaddr(process.env.TRUSTLESS_GATEWAY ?? '')
],
protocols: [
'transport-ipfs-gateway-http'
]
}
}())

const sessionBlockstore = await gatewayBlockBroker.createSession?.(blocks[0].cid, {
minProviders: 1,
providerQueryConcurrency: 1
providerQueryConcurrency: 1,
allowInsecure: true,
allowLocal: true
})

expect(sessionBlockstore).to.be.ok()
Expand Down

0 comments on commit ac06ff6

Please sign in to comment.