diff --git a/proxy/Dockerfile.bun b/proxy/Dockerfile.bun new file mode 100644 index 0000000..80a56ad --- /dev/null +++ b/proxy/Dockerfile.bun @@ -0,0 +1,20 @@ +FROM oven/bun:1-alpine + +WORKDIR /app + +# Install dependencies first (layer caching) +# bun install is compatible with npm package.json / package-lock.json +COPY package*.json ./ +RUN bun install --production + +# Copy application source +COPY . . + +# Expose proxy REST API port +EXPOSE 4000 + +# Health check +HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \ + CMD bun -e "require('http').get('http://localhost:4000/health', r => r.statusCode === 200 ? process.exit(0) : process.exit(1)).on('error', () => process.exit(1))" + +CMD ["bun", "run", "index.js"] diff --git a/proxy/INFO.md b/proxy/INFO.md new file mode 100644 index 0000000..e3ab5db --- /dev/null +++ b/proxy/INFO.md @@ -0,0 +1,108 @@ +# BackOne DPI Proxy — Deployment Reference + +Standalone Docker image for collecting Netify DPI data and writing to MongoDB. + +--- + +## Environment Variables + +### Required + +| Variable | Description | +|---|---| +| `NETIFY_SITE_UUIDS` | Comma-separated Netify site UUIDs (or single `NETIFY_SITE_UUID`) | +| `NETIFY_TOKEN` | Netify JWT token (or `NETIFY_JWT_TOKEN` / `NETIFY_API_KEY`) | + +### MongoDB + +| Variable | Default | Description | +|---|---|---| +| `MONGODB_URI` | `mongodb://127.0.0.1:27017/backone_dpi` | MongoDB connection string | + +### Collection Mode + +| Variable | Default | Description | +|---|---|---| +| `PROXY_COLLECT_MODE` | `all` | `all` = all agents, `agent` = single agent, `agents` = list of agents | +| `PROXY_AGENT_UUID` | _(none)_ | Single agent UUID (required if `mode=agent`) | +| `PROXY_AGENT_UUIDS` | _(none)_ | Comma-separated agent UUIDs (required if `mode=agents`) | +| `PROXY_AGENT_DELAY_MS` | `5000` | Delay (ms) between each agent collection to avoid rate-limiting | + +### Scheduling + +| Variable | Default | Description | +|---|---|---| +| `PROXY_CRON_SCHEDULE` | `*/5 * * * *` | Cron expression for collection interval | +| `PROXY_CAPACITY_LOG_INTERVAL_MS` | `86400000` | How often to log DB capacity usage (default: 24h) | + +### Limits + +| Variable | Default | Description | +|---|---|---| +| `PROXY_FLOW_LIMIT` | `1000000` | Max flows to fetch per agent per cycle | +| `PROXY_PORT` | `4000` | REST API listen port | + +### Netify API + +| Variable | Default | Description | +|---|---|---| +| `NETIFY_INFORMATICS_BASE_URL` | `https://informatics.netify.ai/api/v1` | Netify API base URL | + +--- + +## Docker Run Example + +```bash +docker run -d \ + --name backone_proxy \ + -p 4000:4000 \ + -e MONGODB_URI=mongodb://host.docker.internal:27017/backone_dpi \ + -e NETIFY_SITE_UUIDS=site-uuid-1,site-uuid-2 \ + -e NETIFY_TOKEN=your-jwt-token \ + -e PROXY_COLLECT_MODE=agents \ + -e PROXY_AGENT_UUIDS=agent-uuid-1,agent-uuid-2,agent-uuid-3 \ + backone-proxy +``` + +## Docker Compose Example + +```yaml +services: + proxy: + build: ./proxy + container_name: backone_proxy + restart: always + ports: + - "4000:4000" + environment: + - MONGODB_URI=mongodb://mongodb:27017/backone_dpi + - PROXY_PORT=4000 + - PROXY_COLLECT_MODE=all + - PROXY_COLLECT_MODE=${PROXY_COLLECT_MODE:-all} + - PROXY_AGENT_UUID=${PROXY_AGENT_UUID:-} + - PROXY_AGENT_UUIDS=${PROXY_AGENT_UUIDS:-} + - PROXY_AGENT_DELAY_MS=${PROXY_AGENT_DELAY_MS:-5000} + - PROXY_CRON_SCHEDULE=${PROXY_CRON_SCHEDULE:-*/5 * * * *} + - NETIFY_SITE_UUIDS=${NETIFY_SITE_UUIDS} + - NETIFY_TOKEN=${NETIFY_TOKEN} + - NETIFY_INFORMATICS_BASE_URL=${NETIFY_INFORMATICS_BASE_URL:-https://informatics.netify.ai/api/v1} +``` + +## REST API Endpoints + +| Method | Endpoint | Description | +|---|---|---| +| `GET` | `/health` | Liveness check (MongoDB status) | +| `GET` | `/status` | Scheduler status, mode, last run | +| `GET` | `/agents` | List agent UUIDs in MongoDB | +| `POST` | `/collect/all` | Manual trigger — all agents | +| `POST` | `/collect/:uuid` | Manual trigger — single agent | +| `POST` | `/collect/agents` | Manual trigger — multiple agents `{"uuids":[...], "delay_ms":5000}` | +| `GET` | `/latest` | Latest data from all collections (debug) | +| `GET` | `/domain-details?domain=...` | IP/MAC details for a domain | + +## Health Check + +```bash +curl http://localhost:4000/health +``` diff --git a/proxy/netifyClient.js b/proxy/netifyClient.js index a8ed62a..149b608 100644 --- a/proxy/netifyClient.js +++ b/proxy/netifyClient.js @@ -134,8 +134,9 @@ async function fetchFlows(limit = 10000, agentUuid = null, siteUuid = null) { const sniList = []; if (sniRaw && Array.isArray(sniRaw)) { for (const r of sniRaw) { - const sni = r.tls_sni; - if (sni && sni.trim() !== '') sniList.push(sni.replace(/^\*\./, '').trim()); + const sni = r.tls_sni ?? r.label; + const sniStr = (sni === null || sni === undefined) ? '' : String(sni); + if (sniStr.trim() !== '') sniList.push(sniStr.replace(/^\*\./, '').trim()); } } return raw.map(r => { diff --git a/proxy/package.json b/proxy/package.json index 285d3fb..99f1e35 100644 --- a/proxy/package.json +++ b/proxy/package.json @@ -5,6 +5,7 @@ "main": "index.js", "scripts": { "start": "node index.js", + "start:bun": "bun run index.js", "dev": "nodemon index.js" }, "dependencies": {