diff --git a/.release-please.json b/.release-please.json index 3a33d3ae02..dd7eb77e66 100644 --- a/.release-please.json +++ b/.release-please.json @@ -22,6 +22,7 @@ "packages/peer-id-factory": {}, "packages/peer-record": {}, "packages/peer-store": {}, + "packages/protocol-autonat": {}, "packages/protocol-perf": {}, "packages/pubsub": {}, "packages/pubsub-floodsub": {}, diff --git a/doc/CONFIGURATION.md b/doc/CONFIGURATION.md index 2173ac59be..6f82e087bd 100644 --- a/doc/CONFIGURATION.md +++ b/doc/CONFIGURATION.md @@ -930,7 +930,7 @@ For more information see https://docs.libp2p.io/concepts/nat/autonat/#what-is-au ```ts import { createLibp2p } from 'libp2p' -import { autoNATService } from 'libp2p/autonat' +import { autoNATService } from '@libp2p/autonat' const node = await createLibp2p({ services: { diff --git a/doc/migrations/v0.46-v1.0.0.md b/doc/migrations/v0.46-v1.0.0.md index b4bee48972..92b956056e 100644 --- a/doc/migrations/v0.46-v1.0.0.md +++ b/doc/migrations/v0.46-v1.0.0.md @@ -1,28 +1,42 @@ + # Migrating to libp2p@1.0.0 A migration guide for refactoring your application code from libp2p `v0.46` to `v1.0.0`. ## Table of Contents -- [New features](#new-features) -- [Breaking changes](#breaking-changes) +- [AutoNAT](#autonat) - [KeyChain](#keychain) - [Metrics](#metrics) -## New features +## AutoNAT -... +The AutoNAT service is now published in its own package. -## Breaking changes +**Before** ```ts +import { createLibp2p } from 'libp2p' import { autoNATService } from 'libp2p/autonat' + +const node = await createLibp2p({ + services: { + autoNAT: autoNATService() + } +}) ``` **After** ```ts -import { autoNATService } from '@libp2p/autonat' +import { createLibp2p } from 'libp2p' +import { autoNAT } from '@libp2p/autonat' + +const node = await createLibp2p({ + services: { + autoNAT: autoNAT() + } +}) ``` ## KeyChain diff --git a/doc/migrations/v0.46-v1.0.md b/doc/migrations/v0.46-v1.0.md deleted file mode 100644 index 420048289a..0000000000 --- a/doc/migrations/v0.46-v1.0.md +++ /dev/null @@ -1,85 +0,0 @@ - -# Migrating to libp2p@1.0 - -A migration guide for refactoring your application code from libp2p v0.46 to v1.0. - -## Table of Contents - -- [API](#api) -- [Module Updates](#module-updates) -- [Metrics](#metrics) - -## API - - - -### KeyChain - -The KeyChain object is no longer included on Libp2p and must be instantiated explicitly if desired. - -**Before** - -```ts -import type { KeyChain } from '@libp2p/interface/keychain' - -const libp2p = await createLibp2p(...) - -const keychain: KeyChain = libp2p.keychain -``` - -***After*** - -```ts -import { DefaultKeyChain } from '@libp2p/keychain' -import type { KeyChain } from '@libp2p/interface/keychain' - -const libp2p = await createLibp2p({ - ... - services: { - keychain: (components) => new DefaultKeyChain(components, { - ...DefaultKeyChain.generateOptions() - }) - } -}) - -const keychain: KeyChain = libp2p.services.keychain -``` - -## Module Updates - -With this release you should update the following libp2p modules if you are relying on them: - - - -```json - -``` - -## Metrics - -The following metrics were renamed: - -`libp2p_dialler_pending_dials` => `libp2p_dial_queue_pending_dials` -`libp2p_dialler_in_progress_dials` => `libp2p_dial_queue_in_progress_dials` diff --git a/packages/interface/src/errors.ts b/packages/interface/src/errors.ts index a5911adb76..4f5f8b4c3a 100644 --- a/packages/interface/src/errors.ts +++ b/packages/interface/src/errors.ts @@ -65,3 +65,7 @@ export class InvalidCryptoTransmissionError extends Error { static readonly code = 'ERR_INVALID_CRYPTO_TRANSMISSION' } + +// Error codes + +export const ERR_TIMEOUT = 'ERR_TIMEOUT' diff --git a/packages/libp2p/package.json b/packages/libp2p/package.json index fa15779487..146f1b8e39 100644 --- a/packages/libp2p/package.json +++ b/packages/libp2p/package.json @@ -48,10 +48,6 @@ "types": "./dist/src/index.d.ts", "import": "./dist/src/index.js" }, - "./autonat": { - "types": "./dist/src/autonat/index.d.ts", - "import": "./dist/src/autonat/index.js" - }, "./circuit-relay": { "types": "./dist/src/circuit-relay/index.d.ts", "import": "./dist/src/circuit-relay/index.js" @@ -104,7 +100,6 @@ "prepublishOnly": "node scripts/update-version.js && npm run build", "build": "aegir build", "generate": "run-s generate:proto:*", - "generate:proto:autonat": "protons ./src/autonat/pb/index.proto", "generate:proto:circuit-relay": "protons ./src/circuit-relay/pb/index.proto", "generate:proto:dcutr": "protons ./src/dcutr/pb/message.proto", "generate:proto:fetch": "protons ./src/fetch/pb/proto.proto", @@ -148,7 +143,6 @@ "it-map": "^3.0.3", "it-merge": "^3.0.0", "it-pair": "^2.0.6", - "it-parallel": "^3.0.0", "it-pipe": "^3.0.1", "it-protobuf-stream": "^1.0.0", "it-stream-types": "^2.0.1", diff --git a/packages/libp2p/src/connection-manager/dial-queue.ts b/packages/libp2p/src/connection-manager/dial-queue.ts index c1b6841ec9..71c1fc6811 100644 --- a/packages/libp2p/src/connection-manager/dial-queue.ts +++ b/packages/libp2p/src/connection-manager/dial-queue.ts @@ -1,4 +1,4 @@ -import { AbortError, CodeError } from '@libp2p/interface/errors' +import { AbortError, CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors' import { setMaxListeners } from '@libp2p/interface/events' import { PeerMap } from '@libp2p/peer-collections' import { defaultAddressSort } from '@libp2p/utils/address-sort' @@ -269,7 +269,7 @@ export class DialQueue { // Error is a timeout if (signal.aborted) { - const error = new CodeError(err.message, codes.ERR_TIMEOUT) + const error = new CodeError(err.message, ERR_TIMEOUT) throw error } diff --git a/packages/libp2p/src/content-routing/index.ts b/packages/libp2p/src/content-routing/index.ts index f8a17a3cdd..a96c2bd9ab 100644 --- a/packages/libp2p/src/content-routing/index.ts +++ b/packages/libp2p/src/content-routing/index.ts @@ -1,7 +1,7 @@ import { CodeError } from '@libp2p/interface/errors' import merge from 'it-merge' import { pipe } from 'it-pipe' -import { messages, codes } from '../errors.js' +import { codes, messages } from '../errors.js' import { storeAddresses, uniquePeers, diff --git a/packages/libp2p/src/errors.ts b/packages/libp2p/src/errors.ts index a895c1b5b2..0b84215aa9 100644 --- a/packages/libp2p/src/errors.ts +++ b/packages/libp2p/src/errors.ts @@ -37,7 +37,6 @@ export enum codes { ERR_INVALID_PEER = 'ERR_INVALID_PEER', ERR_MUXER_UNAVAILABLE = 'ERR_MUXER_UNAVAILABLE', ERR_NOT_FOUND = 'ERR_NOT_FOUND', - ERR_TIMEOUT = 'ERR_TIMEOUT', ERR_TRANSPORT_UNAVAILABLE = 'ERR_TRANSPORT_UNAVAILABLE', ERR_TRANSPORT_DIAL_FAILED = 'ERR_TRANSPORT_DIAL_FAILED', ERR_UNSUPPORTED_PROTOCOL = 'ERR_UNSUPPORTED_PROTOCOL', diff --git a/packages/libp2p/src/fetch/index.ts b/packages/libp2p/src/fetch/index.ts index 6e345f0d58..b9fad11e92 100644 --- a/packages/libp2p/src/fetch/index.ts +++ b/packages/libp2p/src/fetch/index.ts @@ -1,4 +1,4 @@ -import { CodeError } from '@libp2p/interface/errors' +import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors' import { setMaxListeners } from '@libp2p/interface/events' import { logger } from '@libp2p/logger' import first from 'it-first' @@ -155,7 +155,7 @@ class DefaultFetchService implements Startable, FetchService { }) onAbort = () => { - stream?.abort(new CodeError('fetch timeout', codes.ERR_TIMEOUT)) + stream?.abort(new CodeError('fetch timeout', ERR_TIMEOUT)) } // make stream abortable diff --git a/packages/libp2p/src/ping/index.ts b/packages/libp2p/src/ping/index.ts index 1608f837b5..88e0cc39c1 100644 --- a/packages/libp2p/src/ping/index.ts +++ b/packages/libp2p/src/ping/index.ts @@ -1,5 +1,5 @@ import { randomBytes } from '@libp2p/crypto' -import { CodeError } from '@libp2p/interface/errors' +import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors' import { logger } from '@libp2p/logger' import first from 'it-first' import { pipe } from 'it-pipe' @@ -125,7 +125,7 @@ class DefaultPingService implements Startable, PingService { }) onAbort = () => { - stream?.abort(new CodeError('ping timeout', codes.ERR_TIMEOUT)) + stream?.abort(new CodeError('ping timeout', ERR_TIMEOUT)) } // make stream abortable diff --git a/packages/libp2p/src/upgrader.ts b/packages/libp2p/src/upgrader.ts index 98d7326737..7c6e6275ce 100644 --- a/packages/libp2p/src/upgrader.ts +++ b/packages/libp2p/src/upgrader.ts @@ -1,4 +1,4 @@ -import { CodeError } from '@libp2p/interface/errors' +import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors' import { setMaxListeners } from '@libp2p/interface/events' import * as mss from '@libp2p/multistream-select' import { peerIdFromString } from '@libp2p/peer-id' @@ -167,7 +167,7 @@ export class DefaultUpgrader implements Upgrader { const signal = AbortSignal.timeout(this.inboundUpgradeTimeout) const onAbort = (): void => { - maConn.abort(new CodeError('inbound upgrade timeout', codes.ERR_TIMEOUT)) + maConn.abort(new CodeError('inbound upgrade timeout', ERR_TIMEOUT)) } signal.addEventListener('abort', onAbort, { once: true }) diff --git a/packages/libp2p/test/connection-manager/direct.node.ts b/packages/libp2p/test/connection-manager/direct.node.ts index 72f478d332..4b225596da 100644 --- a/packages/libp2p/test/connection-manager/direct.node.ts +++ b/packages/libp2p/test/connection-manager/direct.node.ts @@ -5,7 +5,7 @@ import os from 'node:os' import path from 'node:path' import { yamux } from '@chainsafe/libp2p-yamux' import { type Connection, type ConnectionProtector, isConnection } from '@libp2p/interface/connection' -import { AbortError } from '@libp2p/interface/errors' +import { AbortError, ERR_TIMEOUT } from '@libp2p/interface/errors' import { TypedEventEmitter } from '@libp2p/interface/events' import { start, stop } from '@libp2p/interface/startable' import { mockConnection, mockConnectionGater, mockDuplex, mockMultiaddrConnection, mockUpgrader } from '@libp2p/interface-compliance-tests/mocks' @@ -218,7 +218,7 @@ describe('dialing (direct, TCP)', () => { await expect(dialer.dial(remoteAddr)) .to.eventually.be.rejectedWith(Error) - .and.to.have.property('code', ErrorCodes.ERR_TIMEOUT) + .and.to.have.property('code', ERR_TIMEOUT) }) it('should dial to the max concurrency', async () => { diff --git a/packages/libp2p/test/connection-manager/direct.spec.ts b/packages/libp2p/test/connection-manager/direct.spec.ts index 305ae4e524..7f9d9d1ab4 100644 --- a/packages/libp2p/test/connection-manager/direct.spec.ts +++ b/packages/libp2p/test/connection-manager/direct.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ import { yamux } from '@chainsafe/libp2p-yamux' -import { AbortError } from '@libp2p/interface/errors' +import { AbortError, ERR_TIMEOUT } from '@libp2p/interface/errors' import { TypedEventEmitter } from '@libp2p/interface/events' import { mockConnectionGater, mockDuplex, mockMultiaddrConnection, mockUpgrader, mockConnection } from '@libp2p/interface-compliance-tests/mocks' import { mplex } from '@libp2p/mplex' @@ -167,7 +167,7 @@ describe('dialing (direct, WebSockets)', () => { await expect(connectionManager.openConnection(remoteAddr)) .to.eventually.be.rejected() - .and.to.have.property('code', ErrorCodes.ERR_TIMEOUT) + .and.to.have.property('code', ERR_TIMEOUT) }) it('should throw when a peer advertises more than the allowed number of addresses', async () => { diff --git a/packages/libp2p/test/fetch/index.spec.ts b/packages/libp2p/test/fetch/index.spec.ts index 7b90a0f7db..dd7d1d921a 100644 --- a/packages/libp2p/test/fetch/index.spec.ts +++ b/packages/libp2p/test/fetch/index.spec.ts @@ -1,5 +1,6 @@ /* eslint-env mocha */ +import { ERR_TIMEOUT } from '@libp2p/interface/errors' import { TypedEventEmitter } from '@libp2p/interface/events' import { start, stop } from '@libp2p/interface/startable' import { mockRegistrar, mockUpgrader, connectionPair } from '@libp2p/interface-compliance-tests/mocks' @@ -135,7 +136,7 @@ describe('fetch', () => { await expect(localFetch.fetch(remoteComponents.peerId, key, { signal })) - .to.eventually.be.rejected.with.property('code', 'ERR_TIMEOUT') + .to.eventually.be.rejected.with.property('code', ERR_TIMEOUT) // should have closed stream expect(newStreamSpy).to.have.property('callCount', 1) diff --git a/packages/libp2p/test/ping/index.spec.ts b/packages/libp2p/test/ping/index.spec.ts index 9b6aef1385..dd6c877f50 100644 --- a/packages/libp2p/test/ping/index.spec.ts +++ b/packages/libp2p/test/ping/index.spec.ts @@ -1,5 +1,6 @@ /* eslint-env mocha */ +import { ERR_TIMEOUT } from '@libp2p/interface/errors' import { TypedEventEmitter } from '@libp2p/interface/events' import { start, stop } from '@libp2p/interface/startable' import { mockRegistrar, mockUpgrader, connectionPair } from '@libp2p/interface-compliance-tests/mocks' @@ -125,7 +126,7 @@ describe('ping', () => { await expect(localPing.ping(remoteComponents.peerId, { signal })) - .to.eventually.be.rejected.with.property('code', 'ERR_TIMEOUT') + .to.eventually.be.rejected.with.property('code', ERR_TIMEOUT) // should have closed stream expect(newStreamSpy).to.have.property('callCount', 1) diff --git a/packages/libp2p/typedoc.json b/packages/libp2p/typedoc.json index 8ed11ea9f3..5629b3c79d 100644 --- a/packages/libp2p/typedoc.json +++ b/packages/libp2p/typedoc.json @@ -1,7 +1,6 @@ { "entryPoints": [ "./src/index.ts", - "./src/autonat/index.ts", "./src/circuit-relay/index.ts", "./src/fetch/index.ts", "./src/identify/index.ts", diff --git a/packages/protocol-autonat/LICENSE b/packages/protocol-autonat/LICENSE new file mode 100644 index 0000000000..20ce483c86 --- /dev/null +++ b/packages/protocol-autonat/LICENSE @@ -0,0 +1,4 @@ +This project is dual licensed under MIT and Apache-2.0. + +MIT: https://www.opensource.org/licenses/mit +Apache-2.0: https://www.apache.org/licenses/license-2.0 diff --git a/packages/protocol-autonat/LICENSE-APACHE b/packages/protocol-autonat/LICENSE-APACHE new file mode 100644 index 0000000000..14478a3b60 --- /dev/null +++ b/packages/protocol-autonat/LICENSE-APACHE @@ -0,0 +1,5 @@ +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. diff --git a/packages/protocol-autonat/LICENSE-MIT b/packages/protocol-autonat/LICENSE-MIT new file mode 100644 index 0000000000..72dc60d84b --- /dev/null +++ b/packages/protocol-autonat/LICENSE-MIT @@ -0,0 +1,19 @@ +The MIT License (MIT) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/packages/protocol-autonat/README.md b/packages/protocol-autonat/README.md new file mode 100644 index 0000000000..e11bd46ab6 --- /dev/null +++ b/packages/protocol-autonat/README.md @@ -0,0 +1,64 @@ +[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/) +[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io) +[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p) +[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=master\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amaster) + +> Implementation of Autonat Protocol + +# About + +Use the `autoNATService` function to add support for the [AutoNAT protocol](https://docs.libp2p.io/concepts/nat/autonat/) +to libp2p. + +## Example + +```typescript +import { createLibp2p } from 'libp2p' +import { autoNATService } from '@libp2p/autonat' + +const node = await createLibp2p({ + // ...other options + services: { + autoNAT: autoNATService() + } +}) +``` + +## Table of contents + +- [About](#about) + - [Example](#example) +- [Install](#install) + - [Browser ` +``` + +# API Docs + +- + +# License + +Licensed under either of + +- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / ) +- MIT ([LICENSE-MIT](LICENSE-MIT) / ) + +# Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. diff --git a/packages/protocol-autonat/package.json b/packages/protocol-autonat/package.json new file mode 100644 index 0000000000..94793c15e0 --- /dev/null +++ b/packages/protocol-autonat/package.json @@ -0,0 +1,71 @@ +{ + "name": "@libp2p/autonat", + "version": "0.0.0", + "description": "Implementation of Autonat Protocol", + "license": "Apache-2.0 OR MIT", + "homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/protocol-autonat#readme", + "repository": { + "type": "git", + "url": "git+https://github.com/libp2p/js-libp2p.git" + }, + "bugs": { + "url": "https://github.com/libp2p/js-libp2p/issues" + }, + "type": "module", + "types": "./dist/src/index.d.ts", + "files": [ + "src", + "dist", + "!dist/test", + "!**/*.tsbuildinfo" + ], + "exports": { + ".": { + "types": "./dist/src/index.d.ts", + "import": "./dist/src/index.js" + } + }, + "eslintConfig": { + "extends": "ipfs", + "parserOptions": { + "sourceType": "module" + } + }, + "scripts": { + "start": "node dist/src/main.js", + "build": "aegir build", + "test": "aegir test", + "clean": "aegir clean", + "generate": "protons ./src/pb/index.proto", + "lint": "aegir lint", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "test:node": "aegir test -t node --cov", + "dep-check": "aegir dep-check" + }, + "dependencies": { + "@libp2p/interface": "^0.1.2", + "@libp2p/interface-internal": "^0.1.5", + "@libp2p/peer-id": "^3.0.2" , + "@libp2p/peer-id-factory": "^3.0.4" , + "@libp2p/logger": "^3.0.2", + "@multiformats/multiaddr": "^12.1.5", + "it-first": "^3.0.1", + "it-length-prefixed": "^9.0.1", + "it-map": "^3.0.3", + "it-parallel": "^3.0.0", + "it-pipe": "^3.0.1", + "private-ip": "^3.0.0", + "protons-runtime": "^5.0.0", + "uint8arraylist": "^2.4.3" + }, + "devDependencies": { + "aegir": "^41.0.2", + "it-all": "^3.0.1", + "it-pushable": "^3.2.0", + "sinon": "^17.0.0", + "sinon-ts": "^1.0.0" + } +} diff --git a/packages/libp2p/src/autonat/index.ts b/packages/protocol-autonat/src/autonat.ts similarity index 77% rename from packages/libp2p/src/autonat/index.ts rename to packages/protocol-autonat/src/autonat.ts index f958d76621..6650fead19 100644 --- a/packages/libp2p/src/autonat/index.ts +++ b/packages/protocol-autonat/src/autonat.ts @@ -1,27 +1,5 @@ -/** - * @packageDocumentation - * - * Use the `autoNATService` function to add support for the [AutoNAT protocol](https://docs.libp2p.io/concepts/nat/autonat/) - * to libp2p. - * - * @example - * - * ```typescript - * import { createLibp2p } from 'libp2p' - * import { autoNATService } from 'libp2p/autonat' - * - * const node = await createLibp2p({ - * // ...other options - * services: { - * autoNAT: autoNATService() - * } - * }) - * ``` - */ - -import { CodeError } from '@libp2p/interface/errors' +import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors' import { setMaxListeners } from '@libp2p/interface/events' -import { logger } from '@libp2p/logger' import { peerIdFromBytes } from '@libp2p/peer-id' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { multiaddr, protocols } from '@multiformats/multiaddr' @@ -31,72 +9,26 @@ import map from 'it-map' import parallel from 'it-parallel' import { pipe } from 'it-pipe' import isPrivateIp from 'private-ip' -import { codes } from '../errors.js' import { MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, PROTOCOL_NAME, PROTOCOL_PREFIX, PROTOCOL_VERSION, REFRESH_INTERVAL, STARTUP_DELAY, TIMEOUT } from './constants.js' import { Message } from './pb/index.js' +import type { AutoNATComponents, AutoNATServiceInit } from './index.js' +import type { Logger } from '@libp2p/interface' import type { Connection } from '@libp2p/interface/connection' import type { PeerId } from '@libp2p/interface/peer-id' import type { PeerInfo } from '@libp2p/interface/peer-info' -import type { PeerRouting } from '@libp2p/interface/peer-routing' import type { Startable } from '@libp2p/interface/startable' -import type { AddressManager } from '@libp2p/interface-internal/address-manager' -import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager' -import type { IncomingStreamData, Registrar } from '@libp2p/interface-internal/registrar' -import type { TransportManager } from '@libp2p/interface-internal/transport-manager' - -const log = logger('libp2p:autonat') +import type { IncomingStreamData } from '@libp2p/interface-internal/registrar' // if more than 3 peers manage to dial us on what we believe to be our external // IP then we are convinced that it is, in fact, our external IP // https://github.com/libp2p/specs/blob/master/autonat/README.md#autonat-protocol const REQUIRED_SUCCESSFUL_DIALS = 4 -export interface AutoNATServiceInit { - /** - * Allows overriding the protocol prefix used - */ - protocolPrefix?: string - - /** - * How long we should wait for a remote peer to verify our external address - */ - timeout?: number - - /** - * How long to wait after startup before trying to verify our external address - */ - startupDelay?: number - - /** - * Verify our external addresses this often - */ - refreshInterval?: number - - /** - * How many parallel inbound autoNAT streams we allow per-connection - */ - maxInboundStreams?: number - - /** - * How many parallel outbound autoNAT streams we allow per-connection - */ - maxOutboundStreams?: number -} - -export interface AutoNATComponents { - registrar: Registrar - addressManager: AddressManager - transportManager: TransportManager - peerId: PeerId - connectionManager: ConnectionManager - peerRouting: PeerRouting -} - -class DefaultAutoNATService implements Startable { +export class AutoNATService implements Startable { private readonly components: AutoNATComponents private readonly startupDelay: number private readonly refreshInterval: number @@ -106,9 +38,11 @@ class DefaultAutoNATService implements Startable { private readonly maxOutboundStreams: number private verifyAddressTimeout?: ReturnType private started: boolean + readonly #log: Logger constructor (components: AutoNATComponents, init: AutoNATServiceInit) { this.components = components + this.#log = components.logger.forComponent('libp2p:autonat') this.started = false this.protocol = `/${init.protocolPrefix ?? PROTOCOL_PREFIX}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}` this.timeout = init.timeout ?? TIMEOUT @@ -131,7 +65,7 @@ class DefaultAutoNATService implements Startable { await this.components.registrar.handle(this.protocol, (data) => { void this.handleIncomingAutonatStream(data) .catch(err => { - log.error('error handling incoming autonat stream', err) + this.#log.error('error handling incoming autonat stream', err) }) }, { maxInboundStreams: this.maxInboundStreams, @@ -157,7 +91,7 @@ class DefaultAutoNATService implements Startable { const signal = AbortSignal.timeout(this.timeout) const onAbort = (): void => { - data.stream.abort(new CodeError('handleIncomingAutonatStream timeout', codes.ERR_TIMEOUT)) + data.stream.abort(new CodeError('handleIncomingAutonatStream timeout', ERR_TIMEOUT)) } signal.addEventListener('abort', onAbort, { once: true }) @@ -179,7 +113,7 @@ class DefaultAutoNATService implements Startable { const buf = await first(stream) if (buf == null) { - log('no message received') + self.#log('no message received') yield Message.encode({ type: Message.MessageType.DIAL_RESPONSE, dialResponse: { @@ -196,7 +130,7 @@ class DefaultAutoNATService implements Startable { try { request = Message.decode(buf) } catch (err) { - log.error('could not decode message', err) + self.#log.error('could not decode message', err) yield Message.encode({ type: Message.MessageType.DIAL_RESPONSE, @@ -212,7 +146,7 @@ class DefaultAutoNATService implements Startable { const dialRequest = request.dial if (dialRequest == null) { - log.error('dial was missing from message') + self.#log.error('dial was missing from message') yield Message.encode({ type: Message.MessageType.DIAL_RESPONSE, @@ -229,7 +163,7 @@ class DefaultAutoNATService implements Startable { const peer = dialRequest.peer if (peer == null || peer.id == null) { - log.error('PeerId missing from message') + self.#log.error('PeerId missing from message') yield Message.encode({ type: Message.MessageType.DIAL_RESPONSE, @@ -245,7 +179,7 @@ class DefaultAutoNATService implements Startable { try { peerId = peerIdFromBytes(peer.id) } catch (err) { - log.error('invalid PeerId', err) + self.#log.error('invalid PeerId', err) yield Message.encode({ type: Message.MessageType.DIAL_RESPONSE, @@ -258,11 +192,11 @@ class DefaultAutoNATService implements Startable { return } - log('incoming request from %p', peerId) + self.#log('incoming request from %p', peerId) // reject any dial requests that arrive via relays if (!data.connection.remotePeer.equals(peerId)) { - log('target peer %p did not equal sending peer %p', peerId, data.connection.remotePeer) + self.#log('target peer %p did not equal sending peer %p', peerId, data.connection.remotePeer) yield Message.encode({ type: Message.MessageType.DIAL_RESPONSE, @@ -281,7 +215,7 @@ class DefaultAutoNATService implements Startable { .filter(ma => { const isFromSameHost = ma.toOptions().host === data.connection.remoteAddr.toOptions().host - log.trace('request to dial %a was sent from %a is same host %s', ma, data.connection.remoteAddr, isFromSameHost) + self.#log.trace('request to dial %a was sent from %a is same host %s', ma, data.connection.remoteAddr, isFromSameHost) // skip any Multiaddrs where the target node's IP does not match the sending node's IP return isFromSameHost }) @@ -289,7 +223,7 @@ class DefaultAutoNATService implements Startable { const host = ma.toOptions().host const isPublicIp = !(isPrivateIp(host) ?? false) - log.trace('host %s was public %s', host, isPublicIp) + self.#log.trace('host %s was public %s', host, isPublicIp) // don't try to dial private addresses return isPublicIp }) @@ -297,14 +231,14 @@ class DefaultAutoNATService implements Startable { const host = ma.toOptions().host const isNotOurHost = !ourHosts.includes(host) - log.trace('host %s was not our host %s', host, isNotOurHost) + self.#log.trace('host %s was not our host %s', host, isNotOurHost) // don't try to dial nodes on the same host as us return isNotOurHost }) .filter(ma => { const isSupportedTransport = Boolean(self.components.transportManager.transportForMultiaddr(ma)) - log.trace('transport for %a is supported %s', ma, isSupportedTransport) + self.#log.trace('transport for %a is supported %s', ma, isSupportedTransport) // skip any Multiaddrs that have transports we do not support return isSupportedTransport }) @@ -319,7 +253,7 @@ class DefaultAutoNATService implements Startable { // make sure we have something to dial if (multiaddrs.length === 0) { - log('no valid multiaddrs for %p in message', peerId) + self.#log('no valid multiaddrs for %p in message', peerId) yield Message.encode({ type: Message.MessageType.DIAL_RESPONSE, @@ -332,7 +266,7 @@ class DefaultAutoNATService implements Startable { return } - log('dial multiaddrs %s for peer %p', multiaddrs.map(ma => ma.toString()).join(', '), peerId) + self.#log('dial multiaddrs %s for peer %p', multiaddrs.map(ma => ma.toString()).join(', '), peerId) let errorMessage = '' let lastMultiaddr = multiaddrs[0] @@ -347,11 +281,11 @@ class DefaultAutoNATService implements Startable { }) if (!connection.remoteAddr.equals(multiaddr)) { - log.error('tried to dial %a but dialed %a', multiaddr, connection.remoteAddr) + self.#log.error('tried to dial %a but dialed %a', multiaddr, connection.remoteAddr) throw new Error('Unexpected remote address') } - log('Success %p', peerId) + self.#log('Success %p', peerId) yield Message.encode({ type: Message.MessageType.DIAL_RESPONSE, @@ -363,7 +297,7 @@ class DefaultAutoNATService implements Startable { return } catch (err: any) { - log('could not dial %p', peerId, err) + self.#log('could not dial %p', peerId, err) errorMessage = err.message } finally { if (connection != null) { @@ -385,7 +319,7 @@ class DefaultAutoNATService implements Startable { data.stream ) } catch (err) { - log.error('error handling incoming autonat stream', err) + this.#log.error('error handling incoming autonat stream', err) } finally { signal.removeEventListener('abort', onAbort) } @@ -394,7 +328,7 @@ class DefaultAutoNATService implements Startable { _verifyExternalAddresses (): void { void this.verifyExternalAddresses() .catch(err => { - log.error('error verifying external address', err) + this.#log.error('error verifying external address', err) }) } @@ -419,7 +353,7 @@ class DefaultAutoNATService implements Startable { }) if (multiaddrs.length === 0) { - log('no public addresses found, not requesting verification') + this.#log('no public addresses found, not requesting verification') this.verifyAddressTimeout = setTimeout(this._verifyExternalAddresses, this.refreshInterval) return @@ -434,7 +368,7 @@ class DefaultAutoNATService implements Startable { const self = this try { - log('verify multiaddrs %s', multiaddrs.map(ma => ma.toString()).join(', ')) + this.#log('verify multiaddrs %s', multiaddrs.map(ma => ma.toString()).join(', ')) const request = Message.encode({ type: Message.MessageType.DIAL, @@ -456,7 +390,7 @@ class DefaultAutoNATService implements Startable { let onAbort = (): void => {} try { - log('asking %p to verify multiaddr', peer.id) + this.#log('asking %p to verify multiaddr', peer.id) const connection = await self.components.connectionManager.openConnection(peer.id, { signal @@ -466,7 +400,7 @@ class DefaultAutoNATService implements Startable { signal }) - onAbort = () => { stream.abort(new CodeError('verifyAddress timeout', codes.ERR_TIMEOUT)) } + onAbort = () => { stream.abort(new CodeError('verifyAddress timeout', ERR_TIMEOUT)) } signal.addEventListener('abort', onAbort, { once: true }) @@ -478,13 +412,13 @@ class DefaultAutoNATService implements Startable { async (stream) => first(stream) ) if (buf == null) { - log('no response received from %p', connection.remotePeer) + this.#log('no response received from %p', connection.remotePeer) return undefined } const response = Message.decode(buf) if (response.type !== Message.MessageType.DIAL_RESPONSE || response.dialResponse == null) { - log('invalid autonat response from %p', connection.remotePeer) + this.#log('invalid autonat response from %p', connection.remotePeer) return undefined } @@ -500,12 +434,12 @@ class DefaultAutoNATService implements Startable { const octets = options.host.split(':') segment = octets[0] } else { - log('remote address "%s" was not IP4 or IP6?', options.host) + this.#log('remote address "%s" was not IP4 or IP6?', options.host) return undefined } if (networkSegments.includes(segment)) { - log('already have response from network segment %d - %s', segment, options.host) + this.#log('already have response from network segment %d - %s', segment, options.host) return undefined } @@ -514,7 +448,7 @@ class DefaultAutoNATService implements Startable { return response.dialResponse } catch (err) { - log.error('error asking remote to verify multiaddr', err) + this.#log.error('error asking remote to verify multiaddr', err) } finally { signal.removeEventListener('abort', onAbort) } @@ -533,7 +467,7 @@ class DefaultAutoNATService implements Startable { // they either told us which address worked/didn't work, or we only sent them one address const addr = dialResponse.addr == null ? multiaddrs[0] : multiaddr(dialResponse.addr) - log('autonat response for %a is %s', addr, dialResponse.status) + this.#log('autonat response for %a is %s', addr, dialResponse.status) if (dialResponse.status === Message.ResponseStatus.E_BAD_REQUEST) { // the remote could not parse our request @@ -551,7 +485,7 @@ class DefaultAutoNATService implements Startable { } if (!multiaddrs.some(ma => ma.equals(addr))) { - log('peer reported %a as %s but it was not in our observed address list', addr, dialResponse.status) + this.#log('peer reported %a as %s but it was not in our observed address list', addr, dialResponse.status) continue } @@ -569,19 +503,19 @@ class DefaultAutoNATService implements Startable { if (results[addrStr].success === REQUIRED_SUCCESSFUL_DIALS) { // we are now convinced - log('%a is externally dialable', addr) + this.#log('%a is externally dialable', addr) addressManager.confirmObservedAddr(addr) return } if (results[addrStr].failure === REQUIRED_SUCCESSFUL_DIALS) { // we are now unconvinced - log('%a is not externally dialable', addr) + this.#log('%a is not externally dialable', addr) addressManager.removeObservedAddr(addr) return } } catch (err) { - log.error('could not verify external address', err) + this.#log.error('could not verify external address', err) } } } finally { @@ -589,9 +523,3 @@ class DefaultAutoNATService implements Startable { } } } - -export function autoNATService (init: AutoNATServiceInit = {}): (components: AutoNATComponents) => unknown { - return (components) => { - return new DefaultAutoNATService(components, init) - } -} diff --git a/packages/libp2p/src/autonat/constants.ts b/packages/protocol-autonat/src/constants.ts similarity index 100% rename from packages/libp2p/src/autonat/constants.ts rename to packages/protocol-autonat/src/constants.ts diff --git a/packages/protocol-autonat/src/index.ts b/packages/protocol-autonat/src/index.ts new file mode 100644 index 0000000000..45aa266c39 --- /dev/null +++ b/packages/protocol-autonat/src/index.ts @@ -0,0 +1,77 @@ +/** + * @packageDocumentation + * + * Use the `autoNATService` function to add support for the [AutoNAT protocol](https://docs.libp2p.io/concepts/nat/autonat/) + * to libp2p. + * + * @example + * + * ```typescript + * import { createLibp2p } from 'libp2p' + * import { autoNAT } from '@libp2p/autonat' + * + * const node = await createLibp2p({ + * // ...other options + * services: { + * autoNAT: autoNAT() + * } + * }) + * ``` + */ + +import { AutoNATService } from './autonat.js' +import type { ComponentLogger } from '@libp2p/interface' +import type { PeerId } from '@libp2p/interface/peer-id' +import type { PeerRouting } from '@libp2p/interface/peer-routing' +import type { AddressManager } from '@libp2p/interface-internal/address-manager' +import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager' +import type { Registrar } from '@libp2p/interface-internal/registrar' +import type { TransportManager } from '@libp2p/interface-internal/transport-manager' + +export interface AutoNATServiceInit { + /** + * Allows overriding the protocol prefix used + */ + protocolPrefix?: string + + /** + * How long we should wait for a remote peer to verify our external address + */ + timeout?: number + + /** + * How long to wait after startup before trying to verify our external address + */ + startupDelay?: number + + /** + * Verify our external addresses this often + */ + refreshInterval?: number + + /** + * How many parallel inbound autoNAT streams we allow per-connection + */ + maxInboundStreams?: number + + /** + * How many parallel outbound autoNAT streams we allow per-connection + */ + maxOutboundStreams?: number +} + +export interface AutoNATComponents { + registrar: Registrar + addressManager: AddressManager + transportManager: TransportManager + peerId: PeerId + connectionManager: ConnectionManager + peerRouting: PeerRouting + logger: ComponentLogger +} + +export function autoNAT (init: AutoNATServiceInit = {}): (components: AutoNATComponents) => unknown { + return (components) => { + return new AutoNATService(components, init) + } +} diff --git a/packages/libp2p/src/autonat/pb/index.proto b/packages/protocol-autonat/src/pb/index.proto similarity index 100% rename from packages/libp2p/src/autonat/pb/index.proto rename to packages/protocol-autonat/src/pb/index.proto diff --git a/packages/libp2p/src/autonat/pb/index.ts b/packages/protocol-autonat/src/pb/index.ts similarity index 100% rename from packages/libp2p/src/autonat/pb/index.ts rename to packages/protocol-autonat/src/pb/index.ts diff --git a/packages/libp2p/test/autonat/index.spec.ts b/packages/protocol-autonat/test/index.spec.ts similarity index 96% rename from packages/libp2p/test/autonat/index.spec.ts rename to packages/protocol-autonat/test/index.spec.ts index 250033f7b3..c1358bae8f 100644 --- a/packages/libp2p/test/autonat/index.spec.ts +++ b/packages/protocol-autonat/test/index.spec.ts @@ -12,20 +12,18 @@ import { pushable } from 'it-pushable' import sinon from 'sinon' import { stubInterface } from 'sinon-ts' import { Uint8ArrayList } from 'uint8arraylist' -import { PROTOCOL_NAME, PROTOCOL_PREFIX, PROTOCOL_VERSION } from '../../src/autonat/constants.js' -import { autoNATService } from '../../src/autonat/index.js' -import { Message } from '../../src/autonat/pb/index.js' -import { defaultComponents } from '../../src/components.js' -import type { AutoNATServiceInit } from '../../src/autonat/index.js' -import type { Components } from '../../src/components.js' -import type { DefaultConnectionManager } from '../../src/connection-manager/index.js' +import { defaultLogger } from '../../logger/src/index.js' +import { AutoNATService } from '../src/autonat.js' +import { PROTOCOL_NAME, PROTOCOL_PREFIX, PROTOCOL_VERSION } from '../src/constants.js' +import { Message } from '../src/pb/index.js' +import type { AutoNATComponents, AutoNATServiceInit } from '../src/index.js' import type { Connection, Stream } from '@libp2p/interface/connection' import type { PeerId } from '@libp2p/interface/peer-id' import type { PeerInfo } from '@libp2p/interface/peer-info' import type { PeerRouting } from '@libp2p/interface/peer-routing' -import type { PeerStore } from '@libp2p/interface/peer-store' import type { Transport } from '@libp2p/interface/transport' import type { AddressManager } from '@libp2p/interface-internal/address-manager' +import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager' import type { Registrar } from '@libp2p/interface-internal/registrar' import type { TransportManager } from '@libp2p/interface-internal/transport-manager' import type { Multiaddr } from '@multiformats/multiaddr' @@ -42,13 +40,12 @@ const defaultInit: AutoNATServiceInit = { describe('autonat', () => { let service: any - let components: Components + let components: AutoNATComponents let peerRouting: StubbedInstance let registrar: StubbedInstance let addressManager: StubbedInstance - let connectionManager: StubbedInstance + let connectionManager: StubbedInstance let transportManager: StubbedInstance - let peerStore: StubbedInstance beforeEach(async () => { peerRouting = stubInterface() @@ -56,21 +53,20 @@ describe('autonat', () => { addressManager = stubInterface() addressManager.getAddresses.returns([]) - connectionManager = stubInterface() + connectionManager = stubInterface() transportManager = stubInterface() - peerStore = stubInterface() - components = defaultComponents({ + components = { peerId: await createEd25519PeerId(), + logger: defaultLogger(), peerRouting, registrar, addressManager, connectionManager, - transportManager, - peerStore - }) + transportManager + } - service = autoNATService(defaultInit)(components) + service = new AutoNATService(components, defaultInit) await start(components) await start(service) diff --git a/packages/protocol-autonat/tsconfig.json b/packages/protocol-autonat/tsconfig.json new file mode 100644 index 0000000000..fd38c2c239 --- /dev/null +++ b/packages/protocol-autonat/tsconfig.json @@ -0,0 +1,27 @@ +{ + "extends": "aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": [ + "src", + "test" + ], + "references": [ + { + "path": "../interface" + }, + { + "path": "../interface-internal" + }, + { + "path": "../peer-id" + }, + { + "path": "../peer-id-factory" + }, + { + "path": "../logger" + } + ] +} diff --git a/packages/protocol-autonat/typedoc.json b/packages/protocol-autonat/typedoc.json new file mode 100644 index 0000000000..f599dc728d --- /dev/null +++ b/packages/protocol-autonat/typedoc.json @@ -0,0 +1,5 @@ +{ + "entryPoints": [ + "./src/index.ts" + ] +}