Merge remote-tracking branch 'origin/master' into feature/#1891-set-ping-packet-size

# Conflicts:
#	server/util-server.js
#	src/languages/en.js
This commit is contained in:
Louis Lam 2023-01-25 16:12:33 +08:00
commit 5050ebc249
146 changed files with 22276 additions and 17092 deletions

View file

@ -1,5 +1,5 @@
const tcpp = require("tcp-ping");
const ping = require("ping");
const ping = require("@louislam/ping");
const { R } = require("redbean-node");
const { log, genSecret } = require("../src/util");
const passwordHash = require("./password-hash");
@ -14,11 +14,13 @@ const mssql = require("mssql");
const { Client } = require("pg");
const postgresConParse = require("pg-connection-string").parse;
const mysql = require("mysql2");
const { MongoClient } = require("mongodb");
const { NtlmClient } = require("axios-ntlm");
const { Settings } = require("./settings");
const grpc = require("@grpc/grpc-js");
const protojs = require("protobufjs");
const radiusClient = require("node-radius-client");
const redis = require("redis");
const {
dictionaries: {
rfc2865: { file, attributes },
@ -105,7 +107,7 @@ exports.pingAsync = function (hostname, ipv6 = false, size = 56) {
ping.promise.probe(hostname, {
v6: ipv6,
min_reply: 1,
timeout: 10,
deadline: 10,
packetSize: size,
}).then((res) => {
// If ping failed, it will set field to unknown
@ -138,7 +140,7 @@ exports.mqttAsync = function (hostname, topic, okMessage, options = {}) {
const { port, username, password, interval = 20 } = options;
// Adds MQTT protocol to the hostname if not already present
if (!/^(?:http|mqtt)s?:\/\//.test(hostname)) {
if (!/^(?:http|mqtt|ws)s?:\/\//.test(hostname)) {
hostname = "mqtt://" + hostname;
}
@ -148,10 +150,11 @@ exports.mqttAsync = function (hostname, topic, okMessage, options = {}) {
reject(new Error("Timeout"));
}, interval * 1000 * 0.8);
log.debug("mqtt", "MQTT connecting");
const mqttUrl = `${hostname}:${port}`;
let client = mqtt.connect(hostname, {
port,
log.debug("mqtt", `MQTT connecting to ${mqttUrl}`);
let client = mqtt.connect(mqttUrl, {
username,
password
});
@ -283,18 +286,23 @@ exports.postgresQuery = function (connectionString, query) {
const client = new Client({ connectionString });
client.connect();
return client.query(query)
.then(res => {
resolve(res);
})
.catch(err => {
client.connect((err) => {
if (err) {
reject(err);
})
.finally(() => {
client.end();
});
} else {
// Connected here
client.query(query, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
client.end();
});
}
});
});
};
@ -320,6 +328,23 @@ exports.mysqlQuery = function (connectionString, query) {
});
};
/**
* Connect to and Ping a MongoDB database
* @param {string} connectionString The database connection string
* @returns {Promise<(string[]|Object[]|Object)>}
*/
exports.mongodbPing = async function (connectionString) {
let client = await MongoClient.connect(connectionString);
let dbPing = await client.db().command({ ping: 1 });
await client.close();
if (dbPing["ok"] === 1) {
return "UP";
} else {
throw Error("failed");
}
};
/**
* Query radius server
* @param {string} hostname Hostname of radius server
@ -357,6 +382,30 @@ exports.radius = function (
});
};
/**
* Redis server ping
* @param {string} dsn The redis connection string
*/
exports.redisPingAsync = function (dsn) {
return new Promise((resolve, reject) => {
const client = redis.createClient({
url: dsn,
});
client.on("error", (err) => {
reject(err);
});
client.connect().then(() => {
client.ping().then((res, err) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
});
};
/**
* Retrieve value of setting based on key
* @param {string} key Key of setting to retrieve