Skip to content

Commit ef24fab

Browse files
greenSnotmzdws
and
mzdws
authoredAug 13, 2021
feat: custom protocol name (#962)
Co-authored-by: mzdws <8580712+mzdws@user.noreply.gitee.com>
1 parent 3b33fb4 commit ef24fab

File tree

9 files changed

+134
-29
lines changed

9 files changed

+134
-29
lines changed
 

‎doc/CONFIGURATION.md

+20
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,26 @@ By default under nodejs libp2p will attempt to use [UPnP](https://en.wikipedia.o
781781
782782
[NAT-PMP](http://miniupnp.free.fr/nat-pmp.html) is a feature of some modern routers which performs a similar job to UPnP. NAT-PMP is disabled by default, if enabled libp2p will try to use NAT-PMP and will fall back to UPnP if it fails.
783783
784+
#### Configuring protocol name
785+
786+
Changing the protocol name prefix can isolate default public network (IPFS) for custom purposes.
787+
788+
```js
789+
const node = await Libp2p.create({
790+
config: {
791+
protocolPrefix: 'ipfs' // default
792+
}
793+
})
794+
/*
795+
protocols: [
796+
"/ipfs/id/1.0.0", // identify service protocol (if we have multiplexers)
797+
"/ipfs/id/push/1.0.0", // identify service push protocol (if we have multiplexers)
798+
"/ipfs/ping/1.0.0", // built-in ping protocol
799+
]
800+
*/
801+
```
802+
803+
784804
## Configuration examples
785805
786806
As libp2p is designed to be a modular networking library, its usage will vary based on individual project needs. We've included links to some existing project configurations for your reference, in case you wish to replicate their configuration:

‎src/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const DefaultConfig = {
5757
}
5858
},
5959
config: {
60+
protocolPrefix: 'ipfs',
6061
dht: {
6162
enabled: false,
6263
kBucketSize: 20,

‎src/identify/consts.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
// @ts-ignore file not listed within the file list of projects
44
const libp2pVersion = require('../../package.json').version
55

6-
module.exports.PROTOCOL_VERSION = 'ipfs/0.1.0'
6+
module.exports.PROTOCOL_VERSION = 'ipfs/0.1.0' // deprecated
77
module.exports.AGENT_VERSION = `js-libp2p/${libp2pVersion}`
8-
module.exports.MULTICODEC_IDENTIFY = '/ipfs/id/1.0.0'
9-
module.exports.MULTICODEC_IDENTIFY_PUSH = '/ipfs/id/push/1.0.0'
8+
module.exports.MULTICODEC_IDENTIFY = '/ipfs/id/1.0.0' // deprecated
9+
module.exports.MULTICODEC_IDENTIFY_PUSH = '/ipfs/id/push/1.0.0' // deprecated
10+
11+
module.exports.IDENTIFY_PROTOCOL_VERSION = '0.1.0'
12+
module.exports.MULTICODEC_IDENTIFY_PROTOCOL_NAME = 'id'
13+
module.exports.MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME = 'id/push'
14+
module.exports.MULTICODEC_IDENTIFY_PROTOCOL_VERSION = '1.0.0'
15+
module.exports.MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION = '1.0.0'

‎src/identify/index.js

+25-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ const PeerRecord = require('../record/peer-record')
2323
const {
2424
MULTICODEC_IDENTIFY,
2525
MULTICODEC_IDENTIFY_PUSH,
26-
PROTOCOL_VERSION
26+
IDENTIFY_PROTOCOL_VERSION,
27+
MULTICODEC_IDENTIFY_PROTOCOL_NAME,
28+
MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME,
29+
MULTICODEC_IDENTIFY_PROTOCOL_VERSION,
30+
MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION
2731
} = require('./consts')
2832

2933
const { codes } = require('../errors')
@@ -39,6 +43,16 @@ const { codes } = require('../errors')
3943
*/
4044

4145
class IdentifyService {
46+
/**
47+
* @param {import('../')} libp2p
48+
*/
49+
static getProtocolStr (libp2p) {
50+
return {
51+
identifyProtocolStr: `/${libp2p._config.protocolPrefix}/${MULTICODEC_IDENTIFY_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PROTOCOL_VERSION}`,
52+
identifyPushProtocolStr: `/${libp2p._config.protocolPrefix}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION}`
53+
}
54+
}
55+
4256
/**
4357
* @class
4458
* @param {Object} options
@@ -53,9 +67,13 @@ class IdentifyService {
5367

5468
this.handleMessage = this.handleMessage.bind(this)
5569

70+
const protocolStr = IdentifyService.getProtocolStr(libp2p)
71+
this.identifyProtocolStr = protocolStr.identifyProtocolStr
72+
this.identifyPushProtocolStr = protocolStr.identifyPushProtocolStr
73+
5674
// Store self host metadata
5775
this._host = {
58-
protocolVersion: PROTOCOL_VERSION,
76+
protocolVersion: `${libp2p._config.protocolPrefix}/${IDENTIFY_PROTOCOL_VERSION}`,
5977
...libp2p._options.host
6078
}
6179

@@ -94,7 +112,7 @@ class IdentifyService {
94112

95113
const pushes = connections.map(async connection => {
96114
try {
97-
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY_PUSH)
115+
const { stream } = await connection.newStream(this.identifyPushProtocolStr)
98116

99117
await pipe(
100118
[Message.Identify.encode({
@@ -129,7 +147,7 @@ class IdentifyService {
129147
const connections = []
130148
let connection
131149
for (const peer of this.peerStore.peers.values()) {
132-
if (peer.protocols.includes(MULTICODEC_IDENTIFY_PUSH) && (connection = this.connectionManager.get(peer.id))) {
150+
if (peer.protocols.includes(this.identifyPushProtocolStr) && (connection = this.connectionManager.get(peer.id))) {
133151
connections.push(connection)
134152
}
135153
}
@@ -147,7 +165,7 @@ class IdentifyService {
147165
* @returns {Promise<void>}
148166
*/
149167
async identify (connection) {
150-
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY)
168+
const { stream } = await connection.newStream(this.identifyProtocolStr)
151169
const [data] = await pipe(
152170
[],
153171
stream,
@@ -224,9 +242,9 @@ class IdentifyService {
224242
*/
225243
handleMessage ({ connection, stream, protocol }) {
226244
switch (protocol) {
227-
case MULTICODEC_IDENTIFY:
245+
case this.identifyProtocolStr:
228246
return this._handleIdentify({ connection, stream })
229-
case MULTICODEC_IDENTIFY_PUSH:
247+
case this.identifyPushProtocolStr:
230248
return this._handlePush({ connection, stream })
231249
default:
232250
log.error('cannot handle unknown protocol %s', protocol)

‎src/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const PersistentPeerStore = require('./peer-store/persistent')
3131
const Registrar = require('./registrar')
3232
const ping = require('./ping')
3333
const IdentifyService = require('./identify')
34-
const IDENTIFY_PROTOCOLS = IdentifyService.multicodecs
3534
const NatManager = require('./nat-manager')
3635
const { updateSelfPeerRecord } = require('./record/utils')
3736

@@ -289,7 +288,7 @@ class Libp2p extends EventEmitter {
289288

290289
// Add the identify service since we can multiplex
291290
this.identifyService = new IdentifyService({ libp2p: this })
292-
this.handle(Object.values(IDENTIFY_PROTOCOLS), this.identifyService.handleMessage)
291+
this.handle(Object.values(IdentifyService.getProtocolStr(this)), this.identifyService.handleMessage)
293292
}
294293

295294
// Attach private network protector

‎src/ping/constants.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict'
22

33
module.exports = {
4-
PROTOCOL: '/ipfs/ping/1.0.0',
5-
PING_LENGTH: 32
4+
PROTOCOL: '/ipfs/ping/1.0.0', // deprecated
5+
PING_LENGTH: 32,
6+
PROTOCOL_VERSION: '1.0.0',
7+
PROTOCOL_NAME: 'ping'
68
}

‎src/ping/index.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { toBuffer } = require('it-buffer')
1313
const { collect, take } = require('streaming-iterables')
1414
const equals = require('uint8arrays/equals')
1515

16-
const { PROTOCOL, PING_LENGTH } = require('./constants')
16+
const { PROTOCOL_NAME, PING_LENGTH, PROTOCOL_VERSION } = require('./constants')
1717

1818
/**
1919
* @typedef {import('../')} Libp2p
@@ -30,11 +30,12 @@ const { PROTOCOL, PING_LENGTH } = require('./constants')
3030
* @returns {Promise<number>}
3131
*/
3232
async function ping (node, peer) {
33+
const protocol = `/${node._config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
3334
// @ts-ignore multiaddr might not have toB58String
34-
log('dialing %s to %s', PROTOCOL, peer.toB58String ? peer.toB58String() : peer)
35+
log('dialing %s to %s', protocol, peer.toB58String ? peer.toB58String() : peer)
3536

3637
const connection = await node.dial(peer)
37-
const { stream } = await connection.newStream(PROTOCOL)
38+
const { stream } = await connection.newStream(protocol)
3839

3940
const start = Date.now()
4041
const data = crypto.randomBytes(PING_LENGTH)
@@ -61,7 +62,7 @@ async function ping (node, peer) {
6162
* @param {Libp2p} node
6263
*/
6364
function mount (node) {
64-
node.handle(PROTOCOL, ({ stream }) => pipe(stream, stream))
65+
node.handle(`/${node._config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`, ({ stream }) => pipe(stream, stream))
6566
}
6667

6768
/**
@@ -70,7 +71,7 @@ function mount (node) {
7071
* @param {Libp2p} node
7172
*/
7273
function unmount (node) {
73-
node.unhandle(PROTOCOL)
74+
node.unhandle(`/${node._config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`)
7475
}
7576

7677
exports = module.exports = ping
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict'
2+
/* eslint-env mocha */
3+
4+
const { expect } = require('aegir/utils/chai')
5+
const mergeOptions = require('merge-options')
6+
7+
const { create } = require('../../src')
8+
const { baseOptions } = require('./utils')
9+
10+
describe('Protocol prefix is configurable', () => {
11+
let libp2p
12+
13+
it('protocolPrefix is provided', async () => {
14+
const testProtocol = 'test-protocol'
15+
libp2p = await create(mergeOptions(baseOptions, {
16+
config: {
17+
protocolPrefix: testProtocol
18+
}
19+
}))
20+
21+
const protocols = libp2p.peerStore.protoBook.get(libp2p.peerId);
22+
[
23+
'/libp2p/circuit/relay/0.1.0',
24+
`/${testProtocol}/id/1.0.0`,
25+
`/${testProtocol}/id/push/1.0.0`,
26+
`/${testProtocol}/ping/1.0.0`
27+
].forEach((i, idx) => {
28+
expect(protocols[idx]).equals(i)
29+
})
30+
await libp2p.stop()
31+
})
32+
33+
it('protocolPrefix is not provided', async () => {
34+
libp2p = await create(baseOptions)
35+
36+
const protocols = libp2p.peerStore.protoBook.get(libp2p.peerId);
37+
[
38+
'/libp2p/circuit/relay/0.1.0',
39+
'/ipfs/id/1.0.0',
40+
'/ipfs/id/push/1.0.0',
41+
'/ipfs/ping/1.0.0'
42+
].forEach((i, idx) => {
43+
expect(protocols[idx]).equals(i)
44+
})
45+
await libp2p.stop()
46+
})
47+
})

‎test/identify/index.spec.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ describe('Identify', () => {
6060
peerStore: localPeerStore,
6161
multiaddrs: listenMaddrs,
6262
isStarted: () => true,
63-
_options: { host: {} }
63+
_options: { host: {} },
64+
_config: { protocolPrefix: 'ipfs' }
6465
}
6566
})
6667
const remoteIdentify = new IdentifyService({
@@ -70,7 +71,8 @@ describe('Identify', () => {
7071
peerStore: remotePeerStore,
7172
multiaddrs: listenMaddrs,
7273
isStarted: () => true,
73-
_options: { host: {} }
74+
_options: { host: {} },
75+
_config: { protocolPrefix: 'ipfs' }
7476
}
7577
})
7678

@@ -119,7 +121,8 @@ describe('Identify', () => {
119121
peerStore: localPeerStore,
120122
multiaddrs: listenMaddrs,
121123
isStarted: () => true,
122-
_options: { host: { agentVersion } }
124+
_options: { host: { agentVersion } },
125+
_config: { protocolPrefix: 'ipfs' }
123126
}
124127
})
125128

@@ -131,7 +134,8 @@ describe('Identify', () => {
131134
peerStore: remotePeerStore,
132135
multiaddrs: listenMaddrs,
133136
isStarted: () => true,
134-
_options: { host: { agentVersion } }
137+
_options: { host: { agentVersion } },
138+
_config: { protocolPrefix: 'ipfs' }
135139
}
136140
})
137141

@@ -180,7 +184,8 @@ describe('Identify', () => {
180184
connectionManager: new EventEmitter(),
181185
peerStore: localPeerStore,
182186
multiaddrs: [],
183-
_options: { host: {} }
187+
_options: { host: {} },
188+
_config: { protocolPrefix: 'ipfs' }
184189
}
185190
})
186191
const remoteIdentify = new IdentifyService({
@@ -189,7 +194,8 @@ describe('Identify', () => {
189194
connectionManager: new EventEmitter(),
190195
peerStore: remotePeerStore,
191196
multiaddrs: [],
192-
_options: { host: {} }
197+
_options: { host: {} },
198+
_config: { protocolPrefix: 'ipfs' }
193199
}
194200
})
195201

@@ -231,7 +237,8 @@ describe('Identify', () => {
231237
host: {
232238
agentVersion
233239
}
234-
}
240+
},
241+
_config: { protocolPrefix: 'ipfs' }
235242
},
236243
protocols
237244
})
@@ -261,7 +268,8 @@ describe('Identify', () => {
261268
peerStore: localPeerStore,
262269
multiaddrs: listenMaddrs,
263270
isStarted: () => true,
264-
_options: { host: {} }
271+
_options: { host: {} },
272+
_config: { protocolPrefix: 'ipfs' }
265273
}
266274
})
267275

@@ -275,7 +283,8 @@ describe('Identify', () => {
275283
peerStore: remotePeerStore,
276284
multiaddrs: [],
277285
isStarted: () => true,
278-
_options: { host: {} }
286+
_options: { host: {} },
287+
_config: { protocolPrefix: 'ipfs' }
279288
}
280289
})
281290

@@ -333,7 +342,8 @@ describe('Identify', () => {
333342
peerStore: localPeerStore,
334343
multiaddrs: listenMaddrs,
335344
isStarted: () => true,
336-
_options: { host: {} }
345+
_options: { host: {} },
346+
_config: { protocolPrefix: 'ipfs' }
337347
}
338348
})
339349

@@ -347,6 +357,7 @@ describe('Identify', () => {
347357
peerStore: new PeerStore({ peerId: remotePeer }),
348358
multiaddrs: [],
349359
_options: { host: {} },
360+
_config: { protocolPrefix: 'ipfs' },
350361
isStarted: () => true
351362
}
352363
})

0 commit comments

Comments
 (0)
Please sign in to comment.