Skip to content

Commit 548aeac

Browse files
authored
Merge pull request #623 from oceanprotocol/feature/add_getP2pNetworkStats_route
add getP2pNetworkStats route
2 parents 60ecedf + d6557ca commit 548aeac

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

docs/env.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Environmental variables are also tracked in `ENVIRONMENT_VARIABLES` within `src/
6969
- `P2P_AUTODIALCONCURRENCY`: When dialling peers from the peer book to keep the number of open connections, add dials for this many peers to the dial queue at once. Default: 5
7070
- `P2P_MAXPEERADDRSTODIAL`: Maximum number of addresses allowed for a given peer before giving up. Default: 5
7171
- `P2P_AUTODIALINTERVAL`: Auto dial interval (miliseconds). Amount of time between close and open of new peer connection. Default: 5000
72+
- `P2P_ENABLE_NETWORK_STATS`: Enables 'getP2pNetworkStats' http endpoint. Since this contains private informations (like your ip addresses), this is disabled by default
7273

7374
## Additional Nodes (Test Environments)
7475

src/components/P2P/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,16 @@ export class OceanP2P extends EventEmitter {
436436
// }
437437
}
438438

439+
async getNetworkingStats() {
440+
const ret: any = {}
441+
ret.binds = await this._libp2p.components.addressManager.getListenAddrs()
442+
ret.listen = await this._libp2p.components.transportManager.getAddrs()
443+
ret.observing = await this._libp2p.components.addressManager.getObservedAddrs()
444+
ret.announce = await this._libp2p.components.addressManager.getAnnounceAddrs()
445+
ret.connections = await this._libp2p.getConnections()
446+
return ret
447+
}
448+
439449
async getRunningOceanPeers() {
440450
return await this.getOceanPeers(true, false)
441451
}

src/components/httpRoutes/getOceanPeers.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,22 @@ import express, { Request, Response } from 'express'
22
import { getDefaultLevel } from '../../utils/logging/Logger.js'
33
import { P2P_LOGGER } from '../../utils/logging/common.js'
44
import { hasP2PInterface, sendMissingP2PResponse } from './index.js'
5-
5+
import { getBoolEnvValue } from '../../utils/config.js'
66
export const getOceanPeersRoute = express.Router()
77

8+
getOceanPeersRoute.get(
9+
'/getP2pNetworkStats',
10+
async (req: Request, res: Response): Promise<void> => {
11+
// only return values if env P2P_ENABLE_NETWORK_STATS is explicitly allowed
12+
if (hasP2PInterface && getBoolEnvValue('P2P_ENABLE_NETWORK_STATS', false)) {
13+
const stats = await req.oceanNode.getP2PNode().getNetworkingStats()
14+
P2P_LOGGER.log(getDefaultLevel(), `getP2pNetworkStats: ${stats}`, true)
15+
res.json(stats)
16+
} else {
17+
res.status(400).send('Not enabled or unavailable')
18+
}
19+
}
20+
)
821
getOceanPeersRoute.get(
922
'/getOceanPeers',
1023
async (req: Request, res: Response): Promise<void> => {

src/utils/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function getIntEnvValue(env: any, defaultValue: number) {
5757
return isNaN(num) ? defaultValue : num
5858
}
5959

60-
function getBoolEnvValue(envName: string, defaultValue: boolean): boolean {
60+
export function getBoolEnvValue(envName: string, defaultValue: boolean): boolean {
6161
if (!(envName in process.env)) {
6262
return defaultValue
6363
}

0 commit comments

Comments
 (0)