@hivedev/hivesdk 1.0.3 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Common/Constants.js +33 -3
- package/Common/Utility/ConvertEnumerationKeyToTitleCase.js +5 -0
- package/Common/Utility/Delay.js +5 -0
- package/Common/Utility/ExtractIpFromUrl.js +9 -4
- package/Common/Utility/GenerateRandomId.js +6 -0
- package/Common/Utility/GetSha512Hash.js +5 -0
- package/Common/Utility/NormalizeIp.js +9 -0
- package/Common/Utility/Sha256.js +10 -1
- package/Database/DatabaseConnector.js +109 -0
- package/Server/Classes/HiveServerGlobals.js +20 -0
- package/Server/InitServer.js +16 -0
- package/Server/Utility/DecryptDataWithPassword.js +6 -0
- package/Server/Utility/DecryptFileWithPassword.js +6 -0
- package/Server/Utility/EncryptFileWithPassword.js +7 -0
- package/Server/Utility/ExtractConfigurationForService.js +17 -5
- package/Server/Utility/FindService.js +84 -0
- package/Server/Utility/GetAppdataDirectory.js +2 -1
- package/Server/Utility/IsRequestOriginatingFromService.js +48 -49
- package/Server/Utility/LoadConfiguration.js +27 -14
- package/Server/Utility/RegisterService.js +60 -0
- package/Server/Utility/SendMail.js +3 -3
- package/Server/Utility/SetupPassword.js +4 -2
- package/Server/server.js +26 -0
- package/build-server.js +11 -0
- package/dist/server/index.js +2 -11453
- package/hive-server.js +337 -0
- package/package.json +13 -16
- package/Client/Utility/DisableAllButtons.js +0 -4
- package/Client/Utility/GenerateUUID.js +0 -23
- package/Client/Utility/GetDeviceId.js +0 -21
- package/Client/Utility/IsInApp.js +0 -4
- package/Client/Utility/SetupAppUsage.js +0 -65
- package/Common/Utility/GetDirectoryName.js +0 -8
- package/server.js +0 -3
- /package/{client.js → hive-client.js} +0 -0
package/Common/Constants.js
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
|
-
|
|
1
|
+
let _serviceName = null;
|
|
2
|
+
let serviceNameSet = false;
|
|
2
3
|
|
|
4
|
+
|
|
5
|
+
export const serviceName = new Proxy({},
|
|
6
|
+
{
|
|
7
|
+
get()
|
|
8
|
+
{
|
|
9
|
+
if (!_serviceName)
|
|
10
|
+
{
|
|
11
|
+
throw new Error("Service name not set. Call setServiceName() first.");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return _serviceName;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Sets the service name for the current instance of the SDK.
|
|
21
|
+
* This is necessary before any other functions can be called.
|
|
22
|
+
* @param {string} name - The name of the service.
|
|
23
|
+
* @throws {Error} - If the service name is not a string.
|
|
24
|
+
*/
|
|
3
25
|
export function setServiceName(name)
|
|
4
26
|
{
|
|
5
|
-
|
|
6
|
-
|
|
27
|
+
if (typeof name !== "string")
|
|
28
|
+
{
|
|
29
|
+
throw new Error("Service name must be a string.");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
_serviceName = name;
|
|
33
|
+
serviceNameSet = true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts an enumeration key (e.g. SOME_ENUM_KEY) to title case (e.g. Some Enum Key)
|
|
3
|
+
* @param {string} input - The enumeration key to convert
|
|
4
|
+
* @returns {string} - The converted title case string
|
|
5
|
+
*/
|
|
1
6
|
export function convertEnumerationKeyToTitleCase(input)
|
|
2
7
|
{
|
|
3
8
|
return input
|
package/Common/Utility/Delay.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a promise that resolves after the specified number of milliseconds.
|
|
3
|
+
* @param {number} ms The number of milliseconds to wait.
|
|
4
|
+
* @returns {Promise} A promise that resolves after the specified time.
|
|
5
|
+
*/
|
|
1
6
|
export function delay(ms)
|
|
2
7
|
{
|
|
3
8
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { URL } from "url";
|
|
2
2
|
import dns from "dns/promises";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Extracts the IP address from the given URL.
|
|
6
|
+
* If the hostname is a valid IP address, it is returned directly.
|
|
7
|
+
* Otherwise, a DNS lookup is performed to resolve the hostname to an IP address.
|
|
8
|
+
* @param {string} url - The URL to extract the IP address from.
|
|
9
|
+
* @returns {Promise<string>} A promise that resolves with the extracted IP address.
|
|
10
|
+
*/
|
|
11
|
+
export async function extractIpFromUrl(url)
|
|
5
12
|
{
|
|
6
|
-
|
|
7
|
-
console.log(httpUrl);
|
|
8
|
-
const parsed = new URL(httpUrl);
|
|
13
|
+
const parsed = new URL(url);
|
|
9
14
|
const hostname = parsed.hostname;
|
|
10
15
|
|
|
11
16
|
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(hostname))
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a random id of a given length.
|
|
3
|
+
* The id is a string of alphanumeric characters.
|
|
4
|
+
* @param {number} length The length of the id to generate.
|
|
5
|
+
* @returns {string} A random id of the given length.
|
|
6
|
+
*/
|
|
1
7
|
export function generateRandomId(length)
|
|
2
8
|
{
|
|
3
9
|
const characters = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`;
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { createHash } from 'crypto';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Returns the SHA-512 hash of the given text as a hexadecimal string.
|
|
5
|
+
* @param {string} text - The text to hash.
|
|
6
|
+
* @returns {string} The SHA-512 hash of the given text as a hexadecimal string.
|
|
7
|
+
*/
|
|
3
8
|
export function getSha512Hash(text)
|
|
4
9
|
{
|
|
5
10
|
return createHash('sha512').update(text).digest('hex');
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalizes an IP address.
|
|
3
|
+
* If the IP address is an IPv6 address in the form "::ffff:<IPv4 address>",
|
|
4
|
+
* it is converted to the corresponding IPv4 address.
|
|
5
|
+
* If the IP address is "::1", it is converted to "127.0.0.1".
|
|
6
|
+
* Otherwise, the IP address is returned unchanged.
|
|
7
|
+
* @param {string} ip - The IP address to normalize.
|
|
8
|
+
* @returns {string} The normalized IP address.
|
|
9
|
+
*/
|
|
1
10
|
export function normalizeIp(ip)
|
|
2
11
|
{
|
|
3
12
|
if (ip.startsWith("::ffff:"))
|
package/Common/Utility/Sha256.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generates a SHA-256 hash of the given ASCII string.
|
|
5
|
+
* @param {string} ascii The string to hash.
|
|
6
|
+
* @returns {string} The SHA-256 hash of the given string.
|
|
7
|
+
* @example
|
|
8
|
+
* const hash = sha256("password");
|
|
9
|
+
* console.log(hash); // Output: 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
|
|
10
|
+
*/
|
|
2
11
|
export function sha256(ascii)
|
|
3
12
|
{
|
|
4
13
|
function rightRotate(value, amount)
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { MongoClient } from "mongodb";
|
|
2
|
+
import { loadConfiguration } from "../Server/Utility/LoadConfiguration.js";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class DatabaseConnector
|
|
6
|
+
{
|
|
7
|
+
static #databaseUrl = "mongodb://127.0.0.1:27017";
|
|
8
|
+
static #databaseName = "untitled";
|
|
9
|
+
static #username = "";
|
|
10
|
+
static #password = "";
|
|
11
|
+
static #mongoClient = null;
|
|
12
|
+
static #bConfigured = false;
|
|
13
|
+
|
|
14
|
+
static databaseObject = null;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Connects to the MongoDB database.
|
|
18
|
+
* If the database configuration is not set, it will call loadConfiguration() to load the configuration.
|
|
19
|
+
* If the database configuration is still not set after calling loadConfiguration(), it will return false.
|
|
20
|
+
* If the MongoClient object is not set, it will create a new MongoClient object with the database URL and credentials.
|
|
21
|
+
* It will then try to connect to the database using the MongoClient object.
|
|
22
|
+
* If the connection is successful, it will set the databaseObject to the database object returned by the MongoClient.
|
|
23
|
+
* If the connection fails, it will log an error message and return false.
|
|
24
|
+
* @returns {boolean} True if the connection is successful, false otherwise.
|
|
25
|
+
*/
|
|
26
|
+
static async connect()
|
|
27
|
+
{
|
|
28
|
+
if(!DatabaseConnector.#bConfigured)
|
|
29
|
+
{
|
|
30
|
+
await loadConfiguration();
|
|
31
|
+
|
|
32
|
+
if(!DatabaseConnector.#bConfigured)
|
|
33
|
+
{
|
|
34
|
+
console.log("Database configuration is not set! Please provide database credentials first.");
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if(!DatabaseConnector.#mongoClient)
|
|
40
|
+
{
|
|
41
|
+
DatabaseConnector.#mongoClient = new MongoClient(DatabaseConnector.#databaseUrl,
|
|
42
|
+
{
|
|
43
|
+
auth: { username: DatabaseConnector.#username, password: DatabaseConnector.#password }
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
try
|
|
48
|
+
{
|
|
49
|
+
await DatabaseConnector.#mongoClient.connect();
|
|
50
|
+
DatabaseConnector.databaseObject = DatabaseConnector.#mongoClient.db(DatabaseConnector.#databaseName);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
catch(exception)
|
|
54
|
+
{
|
|
55
|
+
console.log("Error connecting to database");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Disconnects from the MongoDB database.
|
|
63
|
+
* If the MongoClient object is not set, it does nothing.
|
|
64
|
+
* If the MongoClient object is set, it closes the connection to the database.
|
|
65
|
+
* It then sets the MongoClient object and the database object to null.
|
|
66
|
+
* Finally, it logs a message to the console indicating that the disconnection was successful.
|
|
67
|
+
*/
|
|
68
|
+
static async disconnect()
|
|
69
|
+
{
|
|
70
|
+
if(DatabaseConnector.#mongoClient)
|
|
71
|
+
{
|
|
72
|
+
await DatabaseConnector.#mongoClient.close();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
DatabaseConnector.#mongoClient = null;
|
|
76
|
+
DatabaseConnector.databaseObject = null;
|
|
77
|
+
|
|
78
|
+
console.log("Disconnected from database");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Sets the database credentials for the DatabaseConnector
|
|
83
|
+
* @param {Object} obj - contains the database host, username, password, and name
|
|
84
|
+
* @param {string} obj.host - The host of the database
|
|
85
|
+
* @param {string} obj.username - The username of the database
|
|
86
|
+
* @param {string} obj.password - The password of the database
|
|
87
|
+
* @param {string} obj.name - The name of the database
|
|
88
|
+
* @throws {Error} - If any of the database credentials are not set
|
|
89
|
+
*/
|
|
90
|
+
static setCredentials({ host, username, password, name})
|
|
91
|
+
{
|
|
92
|
+
if(host) DatabaseConnector.#databaseUrl = "mongodb://" + host + ":27017";
|
|
93
|
+
else throw new Error("Database host not set!");
|
|
94
|
+
|
|
95
|
+
if(username) DatabaseConnector.#username = username;
|
|
96
|
+
else throw new Error("Database username not set!");
|
|
97
|
+
|
|
98
|
+
if (password) DatabaseConnector.#password = password;
|
|
99
|
+
else throw new Error("Database password not set!");
|
|
100
|
+
|
|
101
|
+
if (name) DatabaseConnector.#databaseName = name;
|
|
102
|
+
else throw new Error("Database name not set!");
|
|
103
|
+
|
|
104
|
+
DatabaseConnector.#bConfigured = true;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export default DatabaseConnector;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
class HiveServerGlobals
|
|
2
|
+
{
|
|
3
|
+
static #serviceUrls = {};
|
|
4
|
+
|
|
5
|
+
static getServiceUrls(serviceName)
|
|
6
|
+
{
|
|
7
|
+
if(!serviceName)
|
|
8
|
+
{
|
|
9
|
+
return this.#serviceUrls;
|
|
10
|
+
}
|
|
11
|
+
else
|
|
12
|
+
{
|
|
13
|
+
return this.#serviceUrls[serviceName] || {};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default HiveServerGlobals;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { setupPassword } from "./Utility/SetupPassword.js";
|
|
2
|
+
|
|
3
|
+
let bInitialized = false;
|
|
4
|
+
|
|
5
|
+
export async function initServer()
|
|
6
|
+
{
|
|
7
|
+
if(bInitialized)
|
|
8
|
+
{
|
|
9
|
+
console.warn("Server already initialized! Skipping...");
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
await setupPassword();
|
|
14
|
+
|
|
15
|
+
bInitialized = true;
|
|
16
|
+
}
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import crypto from "crypto";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Decrypt data encrypted with the given password.
|
|
5
|
+
* @param {Buffer} data - The encrypted data.
|
|
6
|
+
* @param {string} password - The password used for encryption.
|
|
7
|
+
* @returns {string} The decrypted data as a UTF-8 string.
|
|
8
|
+
*/
|
|
3
9
|
export async function decryptDataWithPassword(data, password)
|
|
4
10
|
{
|
|
5
11
|
const iv = data.subarray(0, 16);
|
|
@@ -2,6 +2,12 @@ import { promises as fs } from "fs";
|
|
|
2
2
|
import crypto from "crypto";
|
|
3
3
|
import { decryptDataWithPassword } from "./DecryptDataWithPassword.js";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Decrypt a file encrypted with the given password.
|
|
7
|
+
* @param {string} fullPath - The full path to the file to decrypt.
|
|
8
|
+
* @param {string} password - The password to use for decryption.
|
|
9
|
+
* @returns {Promise<string|null>} The decrypted file contents, or null on failure.
|
|
10
|
+
*/
|
|
5
11
|
export async function decryptFileWithPassword(fullPath, password)
|
|
6
12
|
{
|
|
7
13
|
const payload = await fs.readFile(fullPath);
|
|
@@ -2,6 +2,13 @@ import { promises as fs } from "fs";
|
|
|
2
2
|
import path from "path";
|
|
3
3
|
import { encryptDataWithPassword } from "./EncryptDataWithPassword.js";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Encrypt the given data with the given password and store it in the given file.
|
|
7
|
+
* @param {string|Buffer} data - The data to encrypt.
|
|
8
|
+
* @param {string} fullPath - The full path to the file to store the encrypted data in.
|
|
9
|
+
* @param {string} password - The password to use for encryption.
|
|
10
|
+
* @returns {Promise<void>} - A promise that resolves when the file has been encrypted and stored.
|
|
11
|
+
*/
|
|
5
12
|
export async function encryptAndStoreFile(data, fullPath, password)
|
|
6
13
|
{
|
|
7
14
|
const payload = await encryptDataWithPassword(data, password);
|
|
@@ -1,9 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
import { serviceName } from "../../Common/Constants.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Extract the configuration for the given service name from the environment variables.
|
|
5
|
+
* This function expects the environment variables FIREBASE_ADMIN_CREDENTIALS, DATABASE_CREDENTIALS, PERMISSIONS, and SMTP_CREDENTIALS to be set.
|
|
6
|
+
* If any of the environment variables are not set, the corresponding configuration will be set to null.
|
|
7
|
+
* @returns {Object} An object containing the extracted configuration for the given service name.
|
|
8
|
+
* @property {Object|null} firebaseAdminCredentials - The Firebase admin credentials for the given service name.
|
|
9
|
+
* @property {Object|null} databaseCredentials - The database credentials for the given service name.
|
|
10
|
+
* @property {Object|null} permissions - The permissions for the given service name.
|
|
11
|
+
* @property {Object|null} smtpCredentials - The SMTP credentials for the given service name.
|
|
12
|
+
*/
|
|
13
|
+
export function extractConfigurationForService()
|
|
2
14
|
{
|
|
3
|
-
const firebaseAdminCredentials = process.env.
|
|
4
|
-
const databaseCredentials = process.env.
|
|
5
|
-
const permissions = process.env.
|
|
6
|
-
const smtpCredentials = process.env.
|
|
15
|
+
const firebaseAdminCredentials = process.env.FIREBASE_ADMIN_CREDENTIALS ? JSON.parse(process.env.FIREBASE_ADMIN_CREDENTIALS): null;
|
|
16
|
+
const databaseCredentials = process.env.DATABASE_CREDENTIALS ? JSON.parse(process.env.databaseCredentials): null;
|
|
17
|
+
const permissions = process.env.PERMISSIONS ? JSON.parse(process.env.PERMISSIONS): null;
|
|
18
|
+
const smtpCredentials = process.env.SMTP_CREDENTIALS ? JSON.parse(process.env.SMTP_CREDENTIALS): null;
|
|
7
19
|
|
|
8
20
|
const databaseCredentialsForService = databaseCredentials[serviceName]? databaseCredentials[serviceName]: null;
|
|
9
21
|
return { firebaseAdminCredentials: firebaseAdminCredentials, databaseCredentials: databaseCredentialsForService, permissions: permissions, smtpCredentials: smtpCredentials };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import dgram from "dgram";
|
|
2
|
+
|
|
3
|
+
const TARGET_PORT = 49153;
|
|
4
|
+
const RESPONSE_TIMEOUT = 3000;
|
|
5
|
+
|
|
6
|
+
export async function findService(serviceName)
|
|
7
|
+
{
|
|
8
|
+
const targetIp = process.env.HIVE_PORTAL_LAN_IP;
|
|
9
|
+
if (!targetIp) return { name: serviceName, urls: { local: "http://127.0.0.1:49152", remote: "" } };
|
|
10
|
+
|
|
11
|
+
const request = { serviceName };
|
|
12
|
+
const message = Buffer.from(JSON.stringify(request));
|
|
13
|
+
const socket = dgram.createSocket("udp4");
|
|
14
|
+
|
|
15
|
+
return await new Promise((resolve, reject) =>
|
|
16
|
+
{
|
|
17
|
+
let resolved = false;
|
|
18
|
+
|
|
19
|
+
socket.on("message", (msg, rinfo) =>
|
|
20
|
+
{
|
|
21
|
+
if (resolved) return;
|
|
22
|
+
try
|
|
23
|
+
{
|
|
24
|
+
const parsed = JSON.parse(msg.toString());
|
|
25
|
+
|
|
26
|
+
if (parsed.name === serviceName && parsed.urls)
|
|
27
|
+
{
|
|
28
|
+
resolved = true;
|
|
29
|
+
clearTimeout(timer);
|
|
30
|
+
socket.close();
|
|
31
|
+
resolve(parsed);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch
|
|
35
|
+
{
|
|
36
|
+
// ignore malformed responses
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
socket.once("error", (err) =>
|
|
41
|
+
{
|
|
42
|
+
if (resolved) return;
|
|
43
|
+
resolved = true;
|
|
44
|
+
clearTimeout(timer);
|
|
45
|
+
socket.close();
|
|
46
|
+
resolve(null);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
socket.bind(() =>
|
|
50
|
+
{
|
|
51
|
+
try
|
|
52
|
+
{
|
|
53
|
+
socket.send(message, TARGET_PORT, targetIp, (err) =>
|
|
54
|
+
{
|
|
55
|
+
if (err && !resolved)
|
|
56
|
+
{
|
|
57
|
+
resolved = true;
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
socket.close();
|
|
60
|
+
resolve(null);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
catch
|
|
65
|
+
{
|
|
66
|
+
if (!resolved)
|
|
67
|
+
{
|
|
68
|
+
resolved = true;
|
|
69
|
+
clearTimeout(timer);
|
|
70
|
+
socket.close();
|
|
71
|
+
resolve(null);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const timer = setTimeout(() =>
|
|
77
|
+
{
|
|
78
|
+
if (resolved) return;
|
|
79
|
+
resolved = true;
|
|
80
|
+
socket.close();
|
|
81
|
+
resolve(null);
|
|
82
|
+
}, RESPONSE_TIMEOUT);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import fs from "fs";
|
|
3
|
+
import { serviceName } from "../../Common/Constants.js";
|
|
3
4
|
|
|
4
5
|
export function getAppDataDirectory()
|
|
5
6
|
{
|
|
@@ -8,7 +9,7 @@ export function getAppDataDirectory()
|
|
|
8
9
|
? path.join(process.env.HOME, "Library", "Application Support")
|
|
9
10
|
: path.join(process.env.HOME, ".config"));
|
|
10
11
|
|
|
11
|
-
const hiveDir = path.join(baseDir,
|
|
12
|
+
const hiveDir = path.join(baseDir, serviceName);
|
|
12
13
|
|
|
13
14
|
fs.mkdirSync(hiveDir, { recursive: true });
|
|
14
15
|
|
|
@@ -1,55 +1,54 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
1
|
+
// import { normalizeIp } from "../../Common/Utility/NormalizeIp.js";
|
|
2
|
+
// import DatabaseQueryEngine from "../../Database/DatabaseQueryEngine.js";
|
|
3
|
+
// import { extractIpFromUrl } from "../../Common/Utility/ExtractIpFromUrl.js";
|
|
4
|
+
|
|
5
|
+
// export async function isRequestOriginatingFromService(request)
|
|
6
|
+
// {
|
|
7
|
+
// const serviceUrls = await DatabaseQueryEngine.getServiceUrls(null);
|
|
8
|
+
|
|
9
|
+
// if (!serviceUrls || serviceUrls.length === 0)
|
|
10
|
+
// {
|
|
11
|
+
// return false;
|
|
12
|
+
// }
|
|
13
|
+
|
|
14
|
+
// const ip = request.ip || request.connection.remoteAddress || request.socket.remoteAddress;
|
|
15
|
+
|
|
16
|
+
// const normalizedIp = normalizeIp(ip);
|
|
17
|
+
|
|
18
|
+
// for (const service of serviceUrls)
|
|
19
|
+
// {
|
|
20
|
+
// for (const key of ["local", "remote"])
|
|
21
|
+
// {
|
|
22
|
+
// console.log(service);
|
|
23
|
+
// console.log(key);
|
|
24
|
+
// console.log(service.urls[key]);
|
|
25
|
+
|
|
26
|
+
// try
|
|
27
|
+
// {
|
|
28
|
+
// const serviceUrl = await extractIpFromUrl(service.urls[key]);
|
|
29
|
+
// }
|
|
30
|
+
// catch(err)
|
|
31
|
+
// {
|
|
32
|
+
// console.log("ERROR");
|
|
33
|
+
// continue;
|
|
34
|
+
// }
|
|
36
35
|
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
// const serviceUrl = await extractIpFromUrl(service.urls[key]);
|
|
37
|
+
// if (!serviceUrl) continue;
|
|
39
38
|
|
|
40
|
-
|
|
39
|
+
// const normalizedServiceUrl = normalizeIp(serviceUrl);
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
// console.log(normalizedIp);
|
|
42
|
+
// console.log(normalizedServiceUrl);
|
|
44
43
|
|
|
45
|
-
|
|
44
|
+
// console.log("____")
|
|
46
45
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
// if (normalizedIp === normalizedServiceUrl)
|
|
47
|
+
// {
|
|
48
|
+
// return true;
|
|
49
|
+
// }
|
|
50
|
+
// }
|
|
51
|
+
// }
|
|
53
52
|
|
|
54
|
-
|
|
55
|
-
}
|
|
53
|
+
// return false;
|
|
54
|
+
// }
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { decryptFileWithPassword } from "./DecryptFileWithPassword.js";
|
|
2
|
-
import {
|
|
2
|
+
import { getAppDataDirectory } from "./GetAppdataDirectory.js";
|
|
3
3
|
import DatabaseConnector from "../../Database/DatabaseConnector.js";
|
|
4
|
+
import { serviceName } from "../../Common/Constants.js";
|
|
4
5
|
|
|
5
6
|
import path from "path";
|
|
6
7
|
import fs from "fs";
|
|
@@ -12,41 +13,53 @@ export async function loadConfiguration()
|
|
|
12
13
|
let permissions = "";
|
|
13
14
|
let smtpCredentials = "";
|
|
14
15
|
|
|
15
|
-
const firebaseAdminCredentialsPath = path.join(
|
|
16
|
-
const databaseCredentialsPath = path.join(
|
|
17
|
-
const permissionsPath = path.join(
|
|
18
|
-
const smtpCredentialsPath = path.join(
|
|
16
|
+
const firebaseAdminCredentialsPath = path.join(getAppDataDirectory(), "firebase_admin_credentials.dat");
|
|
17
|
+
const databaseCredentialsPath = path.join(getAppDataDirectory(), "database_credentials.dat");
|
|
18
|
+
const permissionsPath = path.join(getAppDataDirectory(), "permissions.dat");
|
|
19
|
+
const smtpCredentialsPath = path.join(getAppDataDirectory(), "smtp_credentials.dat");
|
|
19
20
|
|
|
20
21
|
if(fs.existsSync(firebaseAdminCredentialsPath))
|
|
21
22
|
{
|
|
22
|
-
firebaseAdminCredentials = await decryptFileWithPassword(path.join(
|
|
23
|
+
firebaseAdminCredentials = await decryptFileWithPassword(path.join(getAppDataDirectory(), "firebase_admin_credentials.dat"), process.env.SERVER_PASSWORD);
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
if(fs.existsSync(databaseCredentialsPath))
|
|
26
27
|
{
|
|
27
|
-
databaseCredentials = await decryptFileWithPassword(path.join(
|
|
28
|
+
databaseCredentials = await decryptFileWithPassword(path.join(getAppDataDirectory(), "database_credentials.dat"), process.env.SERVER_PASSWORD);
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
if(fs.existsSync(permissionsPath))
|
|
31
32
|
{
|
|
32
|
-
permissions = await decryptFileWithPassword(path.join(
|
|
33
|
+
permissions = await decryptFileWithPassword(path.join(getAppDataDirectory(), "permissions.dat"), process.env.SERVER_PASSWORD);
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
if(fs.existsSync(smtpCredentialsPath))
|
|
36
37
|
{
|
|
37
|
-
smtpCredentials = await decryptFileWithPassword(path.join(
|
|
38
|
+
smtpCredentials = await decryptFileWithPassword(path.join(getAppDataDirectory(), "smtp_credentials.dat"), process.env.SERVER_PASSWORD);
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
process.env.
|
|
41
|
-
process.env.
|
|
42
|
-
process.env.
|
|
43
|
-
process.env.
|
|
41
|
+
process.env.FIREBASE_ADMIN_CREDENTIALS = firebaseAdminCredentials;
|
|
42
|
+
process.env.DATABASE_CREDENTIALS = databaseCredentials;
|
|
43
|
+
process.env.PERMISSIONS = permissions;
|
|
44
|
+
process.env.SMTP_CREDENTIALS = smtpCredentials;
|
|
45
|
+
|
|
46
|
+
try
|
|
47
|
+
{
|
|
48
|
+
const smtpCredentialsObject = JSON.parse(smtpCredentials);
|
|
49
|
+
|
|
50
|
+
process.env.SMTP_PASSWORD = smtpCredentialsObject.password || "";
|
|
51
|
+
|
|
52
|
+
}
|
|
53
|
+
catch(e)
|
|
54
|
+
{
|
|
55
|
+
console.error(e);
|
|
56
|
+
}
|
|
44
57
|
|
|
45
58
|
if(databaseCredentials)
|
|
46
59
|
{
|
|
47
60
|
databaseCredentials = JSON.parse(databaseCredentials);
|
|
48
61
|
|
|
49
|
-
const hivePortalDatabaseCredentials = databaseCredentials[
|
|
62
|
+
const hivePortalDatabaseCredentials = databaseCredentials[serviceName];
|
|
50
63
|
DatabaseConnector.setCredentials
|
|
51
64
|
(
|
|
52
65
|
{
|