diff --git a/packages/multistream-select/src/handle.ts b/packages/multistream-select/src/handle.ts index d7b1f23ae0..c6494377e8 100644 --- a/packages/multistream-select/src/handle.ts +++ b/packages/multistream-select/src/handle.ts @@ -96,7 +96,7 @@ export async function handle > (stream: Str continue } - options.log('handle: respond with "na" for "%s"', protocol) + options.log.trace('handle: respond with "na" for "%s"', protocol) await multistream.write(lp, uint8ArrayFromString('na\n'), options) options.log('handle: responded with "na" for "%s"', protocol) } diff --git a/packages/protocol-identify/package.json b/packages/protocol-identify/package.json index 14db41f50a..54a256d74a 100644 --- a/packages/protocol-identify/package.json +++ b/packages/protocol-identify/package.json @@ -57,6 +57,7 @@ "@libp2p/interface-internal": "^2.0.4", "@libp2p/peer-id": "^5.0.4", "@libp2p/peer-record": "^8.0.4", + "@libp2p/utils": "^6.0.4", "@multiformats/multiaddr": "^12.2.3", "@multiformats/multiaddr-matcher": "^1.2.1", "it-drain": "^3.0.7", diff --git a/packages/protocol-identify/src/identify-push.ts b/packages/protocol-identify/src/identify-push.ts index 02cb9f0f5c..7489dc9412 100644 --- a/packages/protocol-identify/src/identify-push.ts +++ b/packages/protocol-identify/src/identify-push.ts @@ -145,6 +145,6 @@ export class IdentifyPush extends AbstractIdentify implements Startable, Identif return } - this.log('handled push from %p', connection.remotePeer) + this.log.trace('handled push from %p', connection.remotePeer) } } diff --git a/packages/protocol-identify/src/identify.ts b/packages/protocol-identify/src/identify.ts index bc4c688c09..c6d96332dc 100644 --- a/packages/protocol-identify/src/identify.ts +++ b/packages/protocol-identify/src/identify.ts @@ -1,9 +1,10 @@ /* eslint-disable complexity */ import { publicKeyFromProtobuf, publicKeyToProtobuf } from '@libp2p/crypto/keys' -import { InvalidMessageError, serviceCapabilities, setMaxListeners } from '@libp2p/interface' +import { InvalidMessageError, UnsupportedProtocolError, serviceCapabilities, setMaxListeners } from '@libp2p/interface' import { peerIdFromCID } from '@libp2p/peer-id' import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record' +import { isPrivateIp } from '@libp2p/utils/private-ip' import { protocols } from '@multiformats/multiaddr' import { IP_OR_DOMAIN } from '@multiformats/multiaddr-matcher' import { pbStream } from 'it-protobuf-stream' @@ -29,7 +30,15 @@ export class Identify extends AbstractIdentify implements Startable, IdentifyInt // When a new connection happens, trigger identify components.events.addEventListener('connection:open', (evt) => { const connection = evt.detail - this.identify(connection).catch(err => { this.log.error('error during identify trigged by connection:open', err) }) + this.identify(connection) + .catch(err => { + if (err.name === UnsupportedProtocolError.name) { + // the remote did not support identify, ignore the error + return + } + + this.log.error('error during identify trigged by connection:open', err) + }) }) } } @@ -67,7 +76,6 @@ export class Identify extends AbstractIdentify implements Startable, IdentifyInt return message } catch (err: any) { - this.log.error('error while reading identify message', err) stream?.abort(err) throw err } @@ -100,12 +108,16 @@ export class Identify extends AbstractIdentify implements Startable, IdentifyInt const cleanObservedAddr = getCleanMultiaddr(observedAddr) this.log('identify completed for peer %p and protocols %o', id, protocols) - this.log('our observed address is %a', cleanObservedAddr) - if (cleanObservedAddr != null && - this.addressManager.getObservedAddrs().length < (this.maxObservedAddresses ?? Infinity)) { - this.log('storing our observed address %a', cleanObservedAddr) - this.addressManager.addObservedAddr(cleanObservedAddr) + if (cleanObservedAddr != null) { + this.log('our observed address was %a', cleanObservedAddr) + + if (isPrivateIp(cleanObservedAddr?.nodeAddress().address) === true) { + this.log('our observed address was private') + } else if (this.addressManager.getObservedAddrs().length < (this.maxObservedAddresses ?? Infinity)) { + this.log('storing our observed address') + this.addressManager.addObservedAddr(cleanObservedAddr) + } } return consumeIdentifyMessage(this.peerStore, this.events, this.log, connection, message) diff --git a/packages/protocol-identify/src/utils.ts b/packages/protocol-identify/src/utils.ts index 0ec47633f8..71d3420b9e 100644 --- a/packages/protocol-identify/src/utils.ts +++ b/packages/protocol-identify/src/utils.ts @@ -88,7 +88,7 @@ export async function consumeIdentifyMessage (peerStore: PeerStore, events: Type // if the peer record has been sent, prefer the addresses in the record as they are signed by the remote peer if (message.signedPeerRecord != null) { - log('received signedPeerRecord from %p', connection.remotePeer) + log.trace('received signedPeerRecord from %p', connection.remotePeer) let peerRecordEnvelope = message.signedPeerRecord const envelope = await RecordEnvelope.openAndCertify(peerRecordEnvelope, PeerRecord.DOMAIN) @@ -150,7 +150,7 @@ export async function consumeIdentifyMessage (peerStore: PeerStore, events: Type log('%p did not send a signed peer record', connection.remotePeer) } - log('patching %p with', connection.remotePeer, peer) + log.trace('patching %p with', connection.remotePeer, peer) await peerStore.patch(connection.remotePeer, peer) if (message.agentVersion != null || message.protocolVersion != null) { @@ -164,7 +164,7 @@ export async function consumeIdentifyMessage (peerStore: PeerStore, events: Type metadata.ProtocolVersion = uint8ArrayFromString(message.protocolVersion) } - log('merging %p metadata', connection.remotePeer, metadata) + log.trace('merging %p metadata', connection.remotePeer, metadata) await peerStore.merge(connection.remotePeer, { metadata })