Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analytics prototype #325

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const _httpErrorPages = require('http-error-pages');
const URLON = require('urlon');
const favicon = require('serve-favicon');
const { X509Certificate } = require('crypto');
const requestIp = require('request-ip');
const cron = require('node-cron');
const { isEqual, isUndefined } = require('lodash');
const NodeCache = require("node-cache");
Expand All @@ -45,6 +46,7 @@ var ZITI_CONSTANTS = require('./lib/edge/constants');
const Mustache = require('mustache');
const he = require('he');

let uniqueIPs = new Set();

var latestBrowZerReleaseVersion;

Expand Down Expand Up @@ -215,6 +217,15 @@ var selects = [];
*/
const startBootstrapper = async ( logger ) => {

common.posthog_capture_event({
distinctId: browzer_bootstrapper_host,
event: "Bootstrapper Initialization",
properties: {
browZerHost: browzer_bootstrapper_host,
browZerVersion: pjson.version,
}
});

/** --------------------------------------------------------------------------------------------------
* Dynamically modify the proxied site's <head> element as we stream it back to the browser. We will:
* 1) inject the zitiConfig needed by the SDK
Expand Down Expand Up @@ -690,6 +701,14 @@ ${thirdPartyHTML}
next()
})

app.use((req, res, next) => {
const clientIP = requestIp.getClientIp(req)
if (clientIP) {
uniqueIPs.add(clientIP);
}
next();
});

app.use(verifyCache, function (req, res) {
proxy.web(req, res);
});
Expand Down Expand Up @@ -746,6 +765,54 @@ ${thirdPartyHTML}
}
});

/**
*
*/
cron.schedule('*/15 * * * *', () => { // ... run every 15 minutes
try {

common.posthog_capture_event({
distinctId: browzer_bootstrapper_host,
event: "Bootstrapper Heartbeat",
properties: {
browZerHost: browzer_bootstrapper_host,
browZerVersion: pjson.version,
}
});

} catch (e) {
console.log(`error: `, e);
}

});

/**
*
*/
cron.schedule('*/15 * * * *', () => { // ... run every 15 minutes

uniqueIPs.forEach(async function(ip) {
try {

common.posthog_capture_event({
distinctId: browzer_bootstrapper_host,
event: "BrowZer Client Unique IP",
properties: {
browZerHost: browzer_bootstrapper_host,
browZerVersion: pjson.version,
browZerClientIP: ip,
}
});


} catch (e) {
console.log(`error: `, e);
}
});

});


/**
* When listening on HTTPS, then detect certificate refreshes, and reload TLS context accordingly
*/
Expand Down
10 changes: 9 additions & 1 deletion lib/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ nconf
ZITI_BROWZER_BOOTSTRAPPER_SCHEME: 'http',
ZITI_BROWZER_BOOTSTRAPPER_WILDCARD_VHOSTS: false,
ZITI_BROWZER_BOOTSTRAPPER_LISTEN_PORT: 80,
ZITI_BROWZER_BOOTSTRAPPER_ANALYTICS: true,
ZITI_BROWZER_BOOTSTRAPPER_ANALYTICS_TOKEN: 'phc_qNO1itBGPxgdxGLj58LYYGe7oyNvbG2zoJYvsodr8Hs',
ZITI_BROWZER_BOOTSTRAPPER_ANALYTICS_HOST: 'https://us.i.posthog.com',

ZITI_BROWZER_LOAD_BALANCER_PORT: 443,

Expand Down Expand Up @@ -72,7 +75,12 @@ if (nconf.get('ZITI_BROWZER_BOOTSTRAPPER_LOG_TAGS')) {
}

nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_WILDCARD_VHOSTS', Boolean);

nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_ANALYTICS', Boolean);
if (nconf.get('ZITI_BROWZER_BOOTSTRAPPER_ANALYTICS')) {
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_ANALYTICS_TOKEN', String)
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_ANALYTICS_HOST', String)
}

nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_SCHEME', ['http', 'https']);

if (nconf.get('ZITI_BROWZER_LOAD_BALANCER_HOST')) {
Expand Down
8 changes: 8 additions & 0 deletions lib/http-proxy/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ var common = exports,
ZitiContext = require('../../lib/edge/context'),
Mutex = require('async-mutex').Mutex,
withTimeout = require('async-mutex').withTimeout,
PostHog = require('posthog-node').PostHog;
pjson = require('../../package.json');

var zitiContextMutex = withTimeout(new Mutex(), 3 * 1000, new Error('timeout on zitiContextMutex'));

const postHogClient = new PostHog(env('ZITI_BROWZER_BOOTSTRAPPER_ANALYTICS_TOKEN'), { host: env('ZITI_BROWZER_BOOTSTRAPPER_ANALYTICS_HOST'), disableGeoip: false })

var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i,
isSSL = /^https/;
Expand Down Expand Up @@ -651,3 +653,9 @@ common.trimFirstSection = function(hostname) {
common.delay = function(time) {
return new Promise(resolve => setTimeout(resolve, time));
}

common.posthog_capture_event = function(event) {
if (env('ZITI_BROWZER_BOOTSTRAPPER_ANALYTICS') ) {
postHogClient.capture(event);
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ziti-browzer-bootstrapper",
"version": "0.72.0",
"version": "0.73.0",
"compatibleControllerVersion": ">=0.27.9",
"description": "Ziti BrowZer Bootstrapper -- providing Ziti network access into Dark web server",
"main": "index.js",
Expand Down Expand Up @@ -57,6 +57,7 @@
"node-fetch": "^3.3.2",
"node_extra_ca_certs_mozilla_bundle": "^1.0.6",
"openid-client": "^5.6.5",
"posthog-node": "^4.2.0",
"pump": "^3.0.0",
"request-ip": "^3.3.0",
"requires-port": "^1.0.0",
Expand Down
22 changes: 22 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,15 @@ axios@^1.6.5:
form-data "^4.0.0"
proxy-from-env "^1.1.0"

axios@^1.7.4:
version "1.7.7"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.7.tgz#2f554296f9892a72ac8d8e4c5b79c14a91d0a47f"
integrity sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==
dependencies:
follow-redirects "^1.15.6"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

babel-runtime@^5.8.20:
version "5.8.38"
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-5.8.38.tgz#1c0b02eb63312f5f087ff20450827b425c9d4c19"
Expand Down Expand Up @@ -3965,6 +3974,14 @@ possible-typed-array-names@^1.0.0:
resolved "https://registry.yarnpkg.com/possible-typed-array-names/-/possible-typed-array-names-1.0.0.tgz#89bb63c6fada2c3e90adc4a647beeeb39cc7bf8f"
integrity sha512-d7Uw+eZoloe0EHDIYoe+bQ5WXnGMOpmiZFTuMWCwpjzzkL2nTjcKiAk4hh8TjnGye2TwWOk3UXucZ+3rbmBa8Q==

posthog-node@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/posthog-node/-/posthog-node-4.2.0.tgz#b7213200e12535ce60c9aaa26e8bd0470852ba19"
integrity sha512-hgyCYMyzMvuF3qWMw6JvS8gT55v7Mtp5wKWcnDrw+nu39D0Tk9BXD7I0LOBp0lGlHEPaXCEVYUtviNKrhMALGA==
dependencies:
axios "^1.7.4"
rusha "^0.8.14"

process-nextick-args@~2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"
Expand Down Expand Up @@ -4233,6 +4250,11 @@ rollup-plugin-css-only@^4.5.2:
dependencies:
"@rollup/pluginutils" "5"

rusha@^0.8.14:
version "0.8.14"
resolved "https://registry.yarnpkg.com/rusha/-/rusha-0.8.14.tgz#a977d0de9428406138b7bb90d3de5dcd024e2f68"
integrity sha512-cLgakCUf6PedEu15t8kbsjnwIFFR2D4RfL+W3iWFJ4iac7z4B0ZI8fxy4R3J956kAI68HclCFGL8MPoUVC3qVA==

sade@^1.7.3:
version "1.8.1"
resolved "https://registry.yarnpkg.com/sade/-/sade-1.8.1.tgz#0a78e81d658d394887be57d2a409bf703a3b2701"
Expand Down
Loading