From d6557cacdfcd999728b5a407fd46d77bb767307d Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 15 Aug 2024 17:33:57 +0300 Subject: [PATCH] make it configurable --- docs/env.md | 1 + src/components/P2P/index.ts | 4 +++- src/components/httpRoutes/getOceanPeers.ts | 7 ++++--- src/utils/config.ts | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/env.md b/docs/env.md index 4934c2389..363b94fc5 100644 --- a/docs/env.md +++ b/docs/env.md @@ -69,6 +69,7 @@ Environmental variables are also tracked in `ENVIRONMENT_VARIABLES` within `src/ - `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 - `P2P_MAXPEERADDRSTODIAL`: Maximum number of addresses allowed for a given peer before giving up. Default: 5 - `P2P_AUTODIALINTERVAL`: Auto dial interval (miliseconds). Amount of time between close and open of new peer connection. Default: 5000 +- `P2P_ENABLE_NETWORK_STATS`: Enables 'getP2pNetworkStats' http endpoint. Since this contains private informations (like your ip addresses), this is disabled by default ## Additional Nodes (Test Environments) diff --git a/src/components/P2P/index.ts b/src/components/P2P/index.ts index 3013d8c8c..d5d820dfe 100644 --- a/src/components/P2P/index.ts +++ b/src/components/P2P/index.ts @@ -438,8 +438,10 @@ export class OceanP2P extends EventEmitter { async getNetworkingStats() { const ret: any = {} - ret.addrs = await this._libp2p.components.transportManager.getAddrs() + ret.binds = await this._libp2p.components.addressManager.getListenAddrs() + ret.listen = await this._libp2p.components.transportManager.getAddrs() ret.observing = await this._libp2p.components.addressManager.getObservedAddrs() + ret.announce = await this._libp2p.components.addressManager.getAnnounceAddrs() ret.connections = await this._libp2p.getConnections() return ret } diff --git a/src/components/httpRoutes/getOceanPeers.ts b/src/components/httpRoutes/getOceanPeers.ts index 54104b730..3d5469bc7 100644 --- a/src/components/httpRoutes/getOceanPeers.ts +++ b/src/components/httpRoutes/getOceanPeers.ts @@ -2,18 +2,19 @@ import express, { Request, Response } from 'express' import { getDefaultLevel } from '../../utils/logging/Logger.js' import { P2P_LOGGER } from '../../utils/logging/common.js' import { hasP2PInterface, sendMissingP2PResponse } from './index.js' - +import { getBoolEnvValue } from '../../utils/config.js' export const getOceanPeersRoute = express.Router() getOceanPeersRoute.get( '/getP2pNetworkStats', async (req: Request, res: Response): Promise => { - if (hasP2PInterface) { + // only return values if env P2P_ENABLE_NETWORK_STATS is explicitly allowed + if (hasP2PInterface && getBoolEnvValue('P2P_ENABLE_NETWORK_STATS', false)) { const stats = await req.oceanNode.getP2PNode().getNetworkingStats() P2P_LOGGER.log(getDefaultLevel(), `getP2pNetworkStats: ${stats}`, true) res.json(stats) } else { - sendMissingP2PResponse(res) + res.status(400).send('Not enabled or unavailable') } } ) diff --git a/src/utils/config.ts b/src/utils/config.ts index 74bbaf867..010a3cff5 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -57,7 +57,7 @@ function getIntEnvValue(env: any, defaultValue: number) { return isNaN(num) ? defaultValue : num } -function getBoolEnvValue(envName: string, defaultValue: boolean): boolean { +export function getBoolEnvValue(envName: string, defaultValue: boolean): boolean { if (!(envName in process.env)) { return defaultValue }