Require auth key for node registration

This commit is contained in:
2023-06-07 17:06:26 +02:00
parent 3fc9ec53c0
commit 77b8c4356d
5 changed files with 26 additions and 0 deletions

1
.env.example Normal file
View File

@@ -0,0 +1 @@
NODE_AUTH_KEY=ay1234

1
.gitignore vendored
View File

@@ -1 +1,2 @@
.env
node_modules node_modules

17
package-lock.json generated
View File

@@ -10,6 +10,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"axios": "^1.2.2", "axios": "^1.2.2",
"dotenv": "^16.1.4",
"express": "^4.18.2", "express": "^4.18.2",
"uuid": "^9.0.0", "uuid": "^9.0.0",
"ws": "^8.13.0" "ws": "^8.13.0"
@@ -258,6 +259,17 @@
"npm": "1.2.8000 || >= 1.4.16" "npm": "1.2.8000 || >= 1.4.16"
} }
}, },
"node_modules/dotenv": {
"version": "16.1.4",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.1.4.tgz",
"integrity": "sha512-m55RtE8AsPeJBpOIFKihEmqUcoVncQIwo7x9U8ZwLEZw9ZpXboz2c+rvog+jUaJvVrZ5kBOeYQBX5+8Aa/OZQw==",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/motdotla/dotenv?sponsor=1"
}
},
"node_modules/ee-first": { "node_modules/ee-first": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
@@ -1245,6 +1257,11 @@
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz",
"integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==" "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg=="
}, },
"dotenv": {
"version": "16.1.4",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.1.4.tgz",
"integrity": "sha512-m55RtE8AsPeJBpOIFKihEmqUcoVncQIwo7x9U8ZwLEZw9ZpXboz2c+rvog+jUaJvVrZ5kBOeYQBX5+8Aa/OZQw=="
},
"ee-first": { "ee-first": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",

View File

@@ -12,6 +12,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"axios": "^1.2.2", "axios": "^1.2.2",
"dotenv": "^16.1.4",
"express": "^4.18.2", "express": "^4.18.2",
"uuid": "^9.0.0", "uuid": "^9.0.0",
"ws": "^8.13.0" "ws": "^8.13.0"

View File

@@ -1,3 +1,4 @@
require('dotenv').config()
const express = require("express"); const express = require("express");
var crypto = require('crypto'); var crypto = require('crypto');
const { finished } = require("stream"); const { finished } = require("stream");
@@ -82,6 +83,11 @@ wss.on('connection', function connection(ws) {
console.log('received: %s', data); console.log('received: %s', data);
if(request["register"]){ if(request["register"]){
if(request["register"]["auth_key"] !== process.env.NODE_AUTH_KEY) {
console.log("INVALID AUTH KEY. Disconnecting ", nodeID);
ws.close(4000, "Invalid auth key");
return;
}
ws.send(JSON.stringify({"welcome": { "clientId": nodeID }})); ws.send(JSON.stringify({"welcome": { "clientId": nodeID }}));
console.log("Node registered", nodeID); console.log("Node registered", nodeID);
} }