diff --git a/README.md b/README.md index 6e21a2e..75a2346 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,16 @@ Based on the work by @RichardAH: https://github.com/RichardAH/xpop-generator To run this service & nginx in two separate preconfigured containers, simply run: ```bash -docker-compose up +TELEMETRY=YES URL_PREFIX=https://xpop.my-site.com docker-compose up --build ``` Unless specified otherwise (with environment variables) a connection to XRPL Testnet will be made. +The above command explained: +- `TELEMETRY=YES` sends the `URL_PREFIX` and request hostname to XRPL Labs to build an xPOP serving directory. Default: `NO`, change to `YES` to enable (much appreciated) if you want to run this service publicly for others to fetch xPOPs from (really really appreciate it 💕!) +- `URL_PREFIX` specifies a public URL (if applicable) you are serving your xPOPs on (mapped to this service) +- `--build` at the end makes sure you rebuild your service container, to make sure you're running the latest version of this code + ##### Cleanup The `docker-compose` machines also contain a clean up machine. diff --git a/bin/webserver.mjs b/bin/webserver.mjs index 5e0dee4..9ffd4e2 100644 --- a/bin/webserver.mjs +++ b/bin/webserver.mjs @@ -11,6 +11,32 @@ import 'wtfnode' import { lastLedger } from '../lib/onLedger.mjs' import { txCount } from '../lib/onTransaction.mjs' +const telemetry = { + host: null, + proto: null, + url: process.env?.URL_PREFIX, + collected: false, + sent: false, +} + +const sendTelemetry = async () => { + if (process.env?.TELEMETRY === 'YES') { + try { + console.log('Sending telemetry...', telemetry) + telemetry.sent = new Date() + const tcall = await fetch('https://xrpl.ws-stats.com/xpop/telemetry', { + body: JSON.stringify(telemetry), + method: 'POST', + headers: { 'content-type': 'application/json' }, + }) + const tbody = await tcall.text() + console.log('Telemetry response', tbody) + } catch (e) { + console.log('Error sending telemetry', e.message) + } + } +} + const startDate = new Date() let lastWsPushedLedger @@ -25,7 +51,7 @@ if (!wss) { nunjucks.configure(new URL('../', import.meta.url).pathname, { autoescape: true, express: app }) - app.enable('trust proxy') + app.enable('trust proxy') // , ['loopback', 'linklocal', 'uniquelocal']) - Needs more for Cloudflare etc. app.disable('x-powered-by') app.use(express.json()) app.use(morgan('combined', { })) @@ -39,6 +65,19 @@ if (!wss) { app.use('/', cors(), (req, res, next) => { + if (process.env?.TELEMETRY === 'YES' && !req.url.match(/health/)) { + const telemetryData = { + host: req.headers?.['host'] || 'localhost', + proto: (req.headers?.['x-forwarded-proto'] || 'http').split(',')[0], + collected: new Date(), + } + + if (!telemetry.collected || telemetryData.host !== telemetry.host) { + Object.assign(telemetry, telemetryData) + sendTelemetry() + } + } + if (req.url.split('?')?.[0].match(/\.json$/i)) { res.setHeader('content-type', 'application/json') } diff --git a/docker-compose.yml b/docker-compose.yml index e61cdcc..65e0988 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -35,6 +35,7 @@ services: - NOVALIDATIONLOG=${NOVALIDATIONLOG:-} - NOELIGIBLEFULLTXLOG=${NOELIGIBLEFULLTXLOG:-} - DEBUG=${DEBUG:-} + - TELEMETRY=${TELEMETRY:-NO} restart: unless-stopped healthcheck: test: wget --spider -q http://localhost:3000/health || exit 1