Skip to content

Commit 80ddad5

Browse files
authored
fix: send user agent during auto tls (#2932)
Sends a js-libp2p specific user agent to libp2p.direct while configuring the ACME DNS-01 response.
1 parent 52d06d9 commit 80ddad5

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

packages/auto-tls/src/auto-tls.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import process from 'node:process'
12
import { ClientAuth } from '@libp2p/http-fetch/auth'
23
import { serviceCapabilities, serviceDependencies, setMaxListeners, start, stop } from '@libp2p/interface'
34
import { debounce } from '@libp2p/utils/debounce'
@@ -53,6 +54,7 @@ export class AutoTLS implements AutoTLSInterface {
5354
private readonly domain
5455
private readonly domainMapper: DomainMapper
5556
private readonly autoConfirmAddress: boolean
57+
private readonly userAgent: string
5658

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

8185
this.domainMapper = new DomainMapper(components, {
8286
...init,
@@ -340,7 +344,8 @@ export class AutoTLS implements AutoTLSInterface {
340344
const response = await this.clientAuth.authenticatedFetch(endpoint, {
341345
method: 'POST',
342346
headers: {
343-
'Content-Type': 'application/json'
347+
'Content-Type': 'application/json',
348+
'User-Agent': this.userAgent
344349
},
345350
body: JSON.stringify({
346351
Value: keyAuthorization,

packages/auto-tls/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
*/
6161

6262
import { AutoTLS as AutoTLSClass } from './auto-tls.js'
63-
import type { PeerId, PrivateKey, ComponentLogger, Libp2pEvents, TypedEventTarget, TLSCertificate } from '@libp2p/interface'
63+
import type { PeerId, PrivateKey, ComponentLogger, Libp2pEvents, TypedEventTarget, TLSCertificate, NodeInfo } from '@libp2p/interface'
6464
import type { AddressManager } from '@libp2p/interface-internal'
6565
import type { Keychain } from '@libp2p/keychain'
6666
import type { Datastore } from 'interface-datastore'
@@ -73,6 +73,7 @@ export interface AutoTLSComponents {
7373
events: TypedEventTarget<Libp2pEvents>
7474
keychain: Keychain
7575
datastore: Datastore
76+
nodeInfo: NodeInfo
7677
}
7778

7879
export interface AutoTLSInit {
@@ -181,6 +182,13 @@ export interface AutoTLSInit {
181182
* @default false
182183
*/
183184
autoConfirmAddress?: boolean
185+
186+
/**
187+
* The User-Agent header sent during HTTP requests
188+
*
189+
* @default "js-libp2p/${version} node/${version}"
190+
*/
191+
userAgent?: string
184192
}
185193

186194
export interface AutoTLS {

packages/auto-tls/test/index.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { AutoTLS } from '../src/auto-tls.js'
1515
import { DEFAULT_CERTIFICATE_DATASTORE_KEY, DEFAULT_CERTIFICATE_PRIVATE_KEY_NAME } from '../src/constants.js'
1616
import { importFromPem } from '../src/utils.js'
1717
import { CERT, CERT_FOR_OTHER_KEY, EXPIRED_CERT, INVALID_CERT, PRIVATE_KEY_PEM } from './fixtures/cert.js'
18-
import type { ComponentLogger, Libp2pEvents, Peer, PeerId, PrivateKey, RSAPrivateKey, TypedEventTarget } from '@libp2p/interface'
18+
import type { ComponentLogger, Libp2pEvents, NodeInfo, Peer, PeerId, PrivateKey, RSAPrivateKey, TypedEventTarget } from '@libp2p/interface'
1919
import type { AddressManager, NodeAddress } from '@libp2p/interface-internal'
2020
import type { Keychain } from '@libp2p/keychain'
2121
import type { StubbedInstance } from 'sinon-ts'
@@ -28,6 +28,7 @@ interface StubbedAutoTLSComponents {
2828
events: TypedEventTarget<Libp2pEvents>
2929
keychain: StubbedInstance<Keychain>
3030
datastore: Datastore
31+
nodeInfo: NodeInfo
3132
}
3233

3334
describe('auto-tls', () => {
@@ -46,7 +47,11 @@ describe('auto-tls', () => {
4647
addressManager: stubInterface<AddressManager>(),
4748
events: new TypedEventEmitter(),
4849
keychain: stubInterface<Keychain>(),
49-
datastore: new MemoryDatastore()
50+
datastore: new MemoryDatastore(),
51+
nodeInfo: {
52+
name: 'name',
53+
version: 'version'
54+
}
5055
}
5156

5257
// a mixture of LAN and public addresses

0 commit comments

Comments
 (0)