diff --git a/analytics b/analytics index 4e0a2a5..53e392a 160000 --- a/analytics +++ b/analytics @@ -1 +1 @@ -Subproject commit 4e0a2a507415decb674bbbccd293d9786e6f117a +Subproject commit 53e392a517bb5df8053c2271fe8db5740f725853 diff --git a/server/.env.example b/server/.env.example new file mode 100644 index 0000000..25e09e2 --- /dev/null +++ b/server/.env.example @@ -0,0 +1,37 @@ +# (required) Grafana URL which can be queried from hastic-server host (e.g. http://localhost:3000), +GRAFANA_URL=http://localhost:3000 + +# (required) API-key of your Grafana instance +# (e.g. eyJrIjoiVjZqMHY0dHk4UEE3eEN4MzgzRnd2aURlMWlIdXdHNW4iLCJuIjoiaGFzdGljIiwiaWQiOjF9), +# see https://grafana.com/docs/grafana/latest/http_api/auth/#create-api-token +HASTIC_API_KEY= + +# (optional) websockets URL to connect to hastic-server from analytics +HASTIC_SERVER_URL=ws://localhost:8002 + +# (optional) port you want to run server on, default: 8000 +HASTIC_PORT=8000 + +# (optional) use it if you want to get webhooks on detections (e.g. http://localhost:8080) +HASTIC_WEBHOOK_URL=http://localhost:8080 + +# (optional) type of alerts (detections) receiver (e.g. webhook or alertmanager) +HASTIC_ALERT_TYPE= + +# (optional) URL to send alerts if ALERT_TYPE is alertmanager (e.g http://localhost:9093) +HASTIC_ALERTMANAGER_URL= + +# (optional) Hastic instance name which is used in alerts +HASTIC_INSTANCE_NAME= + +# (optional) whether to send a chart in the notification +HASTIC_ALERT_IMAGE= + +# (optional) connection-string for MongoDB (not used with nedb), (e.g. hastic:password@mongodb.example.com:27017/hastic) +HASTIC_DB_CONNECTION_STRING= + +# (optional) database type. Can have the following values: nedb, mongodb +HASTIC_DB_CONNECTION_TYPE= + +# (optional) timezone offset in hours from utc (e.g -3:30) +HASTIC_TIMEZONE_OFFSET= diff --git a/server/package.json b/server/package.json index dd4a73c..aeb3146 100644 --- a/server/package.json +++ b/server/package.json @@ -38,6 +38,7 @@ "babel-polyfill": "^6.26.0", "babel-preset-env": "^1.7.0", "babel-preset-es2015": "^6.24.1", + "dotenv": "^8.2.0", "es6-promise": "^4.2.4", "event-stream": "3.3.4", "file-loader": "^1.1.11", diff --git a/server/src/config.ts b/server/src/config.ts index ec264e0..a6337e7 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -8,6 +8,8 @@ import * as path from 'path'; import * as fs from 'fs'; import * as os from 'os'; import { exit } from 'process'; // it's very bad to use it in config, but life is full of pain +import * as dotenv from 'dotenv'; +import { URL } from 'url'; const EXIT_CODE_MISSING_FIELD = 3; const EXIT_CODE_BAD_VALUE_FIELD = 4; @@ -18,6 +20,8 @@ declare const GIT_BRANCH: string; declare const GIT_COMMITHASH: string; declare const GIT_VERSION: string; +dotenv.config(); + let configFile = path.join(__dirname, '../../config.json'); let configExists = fs.existsSync(configFile); @@ -81,7 +85,7 @@ export const INSIDE_DOCKER = process.env.INSIDE_DOCKER !== undefined; export const PRODUCTION_MODE = process.env.NODE_ENV !== 'development'; // TODO: maybe rename it to "HASTIC_SERVER_ANALYTICS_URL" -export const HASTIC_SERVER_URL = getConfigFieldAndPrintOrExit('HASTIC_SERVER_URL', 'ws://localhost:8002'); +export const HASTIC_SERVER_URL = getHasticServerUrl(); export const HASTIC_INSTANCE_NAME = getConfigFieldAndPrintOrExit('HASTIC_INSTANCE_NAME', os.hostname()); @@ -158,3 +162,19 @@ function getTimeZoneOffset(): number { return serverUtcOffset; } } + +function getHasticServerUrl() { + const urlString = getConfigFieldAndPrintOrExit('HASTIC_SERVER_URL', 'ws://localhost:8002'); + + try { + const url = new URL(urlString); + if(url.protocol !== 'ws:') { + throw new Error(`Invalid protocol ${url.protocol}`); + } + + return url; + } catch(e) { + console.log(`Invalid HASTIC_SERVER_URL, value must be url, got: ${urlString}`); + exit(EXIT_CODE_BAD_VALUE_FIELD); + } +} diff --git a/server/src/services/analytics_service.ts b/server/src/services/analytics_service.ts index 031e216..5ca67eb 100644 --- a/server/src/services/analytics_service.ts +++ b/server/src/services/analytics_service.ts @@ -10,6 +10,7 @@ import * as childProcess from 'child_process' import * as fs from 'fs'; import * as path from 'path'; import * as _ from 'lodash'; +import { HASTIC_SERVER_URL } from '../config'; export class AnalyticsService { @@ -76,10 +77,10 @@ export class AnalyticsService { public get lastAlive(): Date { return this._lastAlive; } private async _init() { - this._socket_server = new WebSocket.Server({ port: 8002 }); + this._socket_server = new WebSocket.Server({ host: HASTIC_SERVER_URL.hostname, port: +HASTIC_SERVER_URL.port }); // TODO: move this to config OR use existing http server - console.log("Creating websocket server ... %s", 'ws://localhost:8002'); + console.log("Creating websocket server ... %s", HASTIC_SERVER_URL.origin); this._socket_server.on("connection", this._onNewConnection.bind(this)); // TODO: handle connection drop @@ -110,7 +111,7 @@ export class AnalyticsService { cwd: config.ANALYTICS_PATH, env: { ...process.env, - HASTIC_SERVER_URL: config.HASTIC_SERVER_URL + HASTIC_SERVER_URL: config.HASTIC_SERVER_URL.origin } };