Skip to content

Commit

Permalink
fix: send user agent during auto tls (#2932)
Browse files Browse the repository at this point in the history
Sends a js-libp2p specific user agent to libp2p.direct while
configuring the ACME DNS-01 response.
  • Loading branch information
achingbrain authored Feb 4, 2025
1 parent 52d06d9 commit 80ddad5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/auto-tls/src/auto-tls.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import process from 'node:process'
import { ClientAuth } from '@libp2p/http-fetch/auth'
import { serviceCapabilities, serviceDependencies, setMaxListeners, start, stop } from '@libp2p/interface'
import { debounce } from '@libp2p/utils/debounce'
Expand Down Expand Up @@ -53,6 +54,7 @@ export class AutoTLS implements AutoTLSInterface {
private readonly domain
private readonly domainMapper: DomainMapper
private readonly autoConfirmAddress: boolean
private readonly userAgent: string

constructor (components: AutoTLSComponents, init: AutoTLSInit = {}) {
this.log = components.logger.forComponent('libp2p:auto-tls')
Expand All @@ -77,6 +79,8 @@ export class AutoTLS implements AutoTLSInterface {
const base36EncodedPeer = base36.encode(this.components.peerId.toCID().bytes)
this.domain = `${base36EncodedPeer}.${this.forgeDomain}`
this.email = `${base36EncodedPeer}@${this.forgeDomain}`
this.userAgent = init.userAgent ?? `${this.components.nodeInfo.name}/${this.components.nodeInfo.version} ${process.release.name}/${process.version.replaceAll('v', '')}`
acme.axios.defaults.headers.common['User-Agent'] = this.userAgent

this.domainMapper = new DomainMapper(components, {
...init,
Expand Down Expand Up @@ -340,7 +344,8 @@ export class AutoTLS implements AutoTLSInterface {
const response = await this.clientAuth.authenticatedFetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'User-Agent': this.userAgent
},
body: JSON.stringify({
Value: keyAuthorization,
Expand Down
10 changes: 9 additions & 1 deletion packages/auto-tls/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
*/

import { AutoTLS as AutoTLSClass } from './auto-tls.js'
import type { PeerId, PrivateKey, ComponentLogger, Libp2pEvents, TypedEventTarget, TLSCertificate } from '@libp2p/interface'
import type { PeerId, PrivateKey, ComponentLogger, Libp2pEvents, TypedEventTarget, TLSCertificate, NodeInfo } from '@libp2p/interface'
import type { AddressManager } from '@libp2p/interface-internal'
import type { Keychain } from '@libp2p/keychain'
import type { Datastore } from 'interface-datastore'
Expand All @@ -73,6 +73,7 @@ export interface AutoTLSComponents {
events: TypedEventTarget<Libp2pEvents>
keychain: Keychain
datastore: Datastore
nodeInfo: NodeInfo
}

export interface AutoTLSInit {
Expand Down Expand Up @@ -181,6 +182,13 @@ export interface AutoTLSInit {
* @default false
*/
autoConfirmAddress?: boolean

/**
* The User-Agent header sent during HTTP requests
*
* @default "js-libp2p/${version} node/${version}"
*/
userAgent?: string
}

export interface AutoTLS {
Expand Down
9 changes: 7 additions & 2 deletions packages/auto-tls/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { AutoTLS } from '../src/auto-tls.js'
import { DEFAULT_CERTIFICATE_DATASTORE_KEY, DEFAULT_CERTIFICATE_PRIVATE_KEY_NAME } from '../src/constants.js'
import { importFromPem } from '../src/utils.js'
import { CERT, CERT_FOR_OTHER_KEY, EXPIRED_CERT, INVALID_CERT, PRIVATE_KEY_PEM } from './fixtures/cert.js'
import type { ComponentLogger, Libp2pEvents, Peer, PeerId, PrivateKey, RSAPrivateKey, TypedEventTarget } from '@libp2p/interface'
import type { ComponentLogger, Libp2pEvents, NodeInfo, Peer, PeerId, PrivateKey, RSAPrivateKey, TypedEventTarget } from '@libp2p/interface'
import type { AddressManager, NodeAddress } from '@libp2p/interface-internal'
import type { Keychain } from '@libp2p/keychain'
import type { StubbedInstance } from 'sinon-ts'
Expand All @@ -28,6 +28,7 @@ interface StubbedAutoTLSComponents {
events: TypedEventTarget<Libp2pEvents>
keychain: StubbedInstance<Keychain>
datastore: Datastore
nodeInfo: NodeInfo
}

describe('auto-tls', () => {
Expand All @@ -46,7 +47,11 @@ describe('auto-tls', () => {
addressManager: stubInterface<AddressManager>(),
events: new TypedEventEmitter(),
keychain: stubInterface<Keychain>(),
datastore: new MemoryDatastore()
datastore: new MemoryDatastore(),
nodeInfo: {
name: 'name',
version: 'version'
}
}

// a mixture of LAN and public addresses
Expand Down

0 comments on commit 80ddad5

Please sign in to comment.