@@ -29,13 +29,22 @@ import { autoNAT } from '@libp2p/autonat'
2929import { uPnPNAT } from '@libp2p/upnp-nat'
3030import { ping } from '@libp2p/ping'
3131import { dcutr } from '@libp2p/dcutr'
32- import { kadDHT , passthroughMapper } from '@libp2p/kad-dht'
32+ import {
33+ kadDHT ,
34+ passthroughMapper ,
35+ removePrivateAddressesMapper ,
36+ removePublicAddressesMapper
37+ } from '@libp2p/kad-dht'
3338// import { gossipsub } from '@chainsafe/libp2p-gossipsub'
3439
3540import { EVENTS , cidFromRawString } from '../../utils/index.js'
3641import { Transform } from 'stream'
3742import { Database } from '../database'
38- import { OceanNodeConfig , FindDDOResponse } from '../../@types/OceanNode'
43+ import {
44+ OceanNodeConfig ,
45+ FindDDOResponse ,
46+ dhtFilterMethod
47+ } from '../../@types/OceanNode.js'
3948// eslint-disable-next-line camelcase
4049import is_ip_private from 'private-ip'
4150import ip from 'ip'
@@ -275,8 +284,22 @@ export class OceanP2P extends EventEmitter {
275284 multiaddrs . filter ( ( m ) => this . shouldAnnounce ( m ) )
276285 }
277286 }
287+ const dhtOptions = {
288+ allowQueryWithZeroPeers : false ,
289+ maxInboundStreams : config . p2pConfig . dhtMaxInboundStreams ,
290+ maxOutboundStreams : config . p2pConfig . dhtMaxOutboundStreams ,
291+ clientMode : false , // always be a server
292+ kBucketSize : 20 ,
293+ protocol : '/ocean/nodes/1.0.0/kad/1.0.0' ,
294+ peerInfoMapper : passthroughMapper // see below
295+ }
296+ if ( config . p2pConfig . dhtFilter === dhtFilterMethod . filterPrivate )
297+ dhtOptions . peerInfoMapper = removePrivateAddressesMapper
298+ if ( config . p2pConfig . dhtFilter === dhtFilterMethod . filterPublic )
299+ dhtOptions . peerInfoMapper = removePublicAddressesMapper
278300 let servicesConfig = {
279301 identify : identify ( ) ,
302+ dht : kadDHT ( dhtOptions ) ,
280303 identifyPush : identifyPush ( ) ,
281304 /*
282305 pubsub: gossipsub({
@@ -292,27 +315,10 @@ export class OceanP2P extends EventEmitter {
292315 // enabled: true
293316 allowedTopics: ['oceanprotocol._peer-discovery._p2p._pubsub', 'oceanprotocol']
294317 }), */
295- dht : kadDHT ( {
296- // this is necessary because this node is not connected to the public network
297- // it can be removed if, for example bootstrappers are configured
298- allowQueryWithZeroPeers : true ,
299- maxInboundStreams : config . p2pConfig . dhtMaxInboundStreams ,
300- maxOutboundStreams : config . p2pConfig . dhtMaxOutboundStreams ,
301-
302- clientMode : false ,
303- kBucketSize : 20 ,
304- protocol : '/ocean/nodes/1.0.0/kad/1.0.0' ,
305- peerInfoMapper : passthroughMapper
306- // protocolPrefix: '/ocean/nodes/1.0.0'
307- // randomWalk: {
308- // enabled: true, // Allows to disable discovery (enabled by default)
309- // interval: 300e3,
310- // timeout: 10e3
311- // }
312- } ) ,
313318 ping : ping ( ) ,
314319 dcutr : dcutr ( )
315320 }
321+
316322 // eslint-disable-next-line no-constant-condition, no-self-compare
317323 if ( config . p2pConfig . enableCircuitRelayServer ) {
318324 P2P_LOGGER . info ( 'Enabling Circuit Relay Server' )
@@ -420,13 +426,6 @@ export class OceanP2P extends EventEmitter {
420426 this . _upnp_interval = setInterval ( this . UPnpCron . bind ( this ) , 3000 )
421427 }
422428
423- if ( config . p2pConfig . enableDHTServer ) {
424- try {
425- await node . services . dht . setMode ( 'server' )
426- } catch ( e ) {
427- P2P_LOGGER . warn ( `Failed to set mode server for DHT` )
428- }
429- }
430429 return node
431430 } catch ( e ) {
432431 P2P_LOGGER . logMessageWithEmoji (
@@ -598,6 +597,22 @@ export class OceanP2P extends EventEmitter {
598597 return finalmultiaddrs
599598 }
600599
600+ async findPeerInDht ( peerName : string , timeout ?: number ) {
601+ try {
602+ const peer = peerIdFromString ( peerName )
603+ const data = await this . _libp2p . peerRouting . findPeer ( peer , {
604+ signal :
605+ isNaN ( timeout ) || timeout === 0
606+ ? AbortSignal . timeout ( 5000 )
607+ : AbortSignal . timeout ( timeout ) ,
608+ useCache : true ,
609+ useNetwork : true
610+ } )
611+ return data
612+ } catch ( e ) { }
613+ return null
614+ }
615+
601616 async sendTo (
602617 peerName : string ,
603618 message : string ,
0 commit comments