|
|
@ -5,7 +5,6 @@ const { debug } = require("../src/util");
|
|
|
|
const passwordHash = require("./password-hash");
|
|
|
|
const passwordHash = require("./password-hash");
|
|
|
|
const dayjs = require("dayjs");
|
|
|
|
const dayjs = require("dayjs");
|
|
|
|
const { Resolver } = require("dns");
|
|
|
|
const { Resolver } = require("dns");
|
|
|
|
const { allowAllOrigin } = require("./util-server");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Init or reset JWT secret
|
|
|
|
* Init or reset JWT secret
|
|
|
@ -24,7 +23,7 @@ exports.initJWTSecret = async () => {
|
|
|
|
jwtSecretBean.value = passwordHash.generate(dayjs() + "");
|
|
|
|
jwtSecretBean.value = passwordHash.generate(dayjs() + "");
|
|
|
|
await R.store(jwtSecretBean);
|
|
|
|
await R.store(jwtSecretBean);
|
|
|
|
return jwtSecretBean;
|
|
|
|
return jwtSecretBean;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.tcping = function (hostname, port) {
|
|
|
|
exports.tcping = function (hostname, port) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
@ -45,7 +44,7 @@ exports.tcping = function (hostname, port) {
|
|
|
|
resolve(Math.round(data.max));
|
|
|
|
resolve(Math.round(data.max));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.ping = async (hostname) => {
|
|
|
|
exports.ping = async (hostname) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -58,7 +57,7 @@ exports.ping = async (hostname) => {
|
|
|
|
throw e;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.pingAsync = function (hostname, ipv6 = false) {
|
|
|
|
exports.pingAsync = function (hostname, ipv6 = false) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
@ -70,13 +69,13 @@ exports.pingAsync = function (hostname, ipv6 = false) {
|
|
|
|
if (err) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
reject(err);
|
|
|
|
} else if (ms === null) {
|
|
|
|
} else if (ms === null) {
|
|
|
|
reject(new Error(stdout))
|
|
|
|
reject(new Error(stdout));
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
resolve(Math.round(ms))
|
|
|
|
resolve(Math.round(ms));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.dnsResolve = function (hostname, resolver_server, rrtype) {
|
|
|
|
exports.dnsResolve = function (hostname, resolver_server, rrtype) {
|
|
|
|
const resolver = new Resolver();
|
|
|
|
const resolver = new Resolver();
|
|
|
@ -99,8 +98,8 @@ exports.dnsResolve = function (hostname, resolver_server, rrtype) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.setting = async function (key) {
|
|
|
|
exports.setting = async function (key) {
|
|
|
|
let value = await R.getCell("SELECT `value` FROM setting WHERE `key` = ? ", [
|
|
|
|
let value = await R.getCell("SELECT `value` FROM setting WHERE `key` = ? ", [
|
|
|
@ -109,29 +108,29 @@ exports.setting = async function (key) {
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const v = JSON.parse(value);
|
|
|
|
const v = JSON.parse(value);
|
|
|
|
debug(`Get Setting: ${key}: ${v}`)
|
|
|
|
debug(`Get Setting: ${key}: ${v}`);
|
|
|
|
return v;
|
|
|
|
return v;
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
return value;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.setSetting = async function (key, value) {
|
|
|
|
exports.setSetting = async function (key, value) {
|
|
|
|
let bean = await R.findOne("setting", " `key` = ? ", [
|
|
|
|
let bean = await R.findOne("setting", " `key` = ? ", [
|
|
|
|
key,
|
|
|
|
key,
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
if (!bean) {
|
|
|
|
if (!bean) {
|
|
|
|
bean = R.dispense("setting")
|
|
|
|
bean = R.dispense("setting");
|
|
|
|
bean.key = key;
|
|
|
|
bean.key = key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bean.value = JSON.stringify(value);
|
|
|
|
bean.value = JSON.stringify(value);
|
|
|
|
await R.store(bean)
|
|
|
|
await R.store(bean);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.getSettings = async function (type) {
|
|
|
|
exports.getSettings = async function (type) {
|
|
|
|
let list = await R.getAll("SELECT `key`, `value` FROM setting WHERE `type` = ? ", [
|
|
|
|
let list = await R.getAll("SELECT `key`, `value` FROM setting WHERE `type` = ? ", [
|
|
|
|
type,
|
|
|
|
type,
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
let result = {};
|
|
|
|
let result = {};
|
|
|
|
|
|
|
|
|
|
|
@ -144,7 +143,7 @@ exports.getSettings = async function (type) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.setSettings = async function (type, data) {
|
|
|
|
exports.setSettings = async function (type, data) {
|
|
|
|
let keyList = Object.keys(data);
|
|
|
|
let keyList = Object.keys(data);
|
|
|
@ -164,12 +163,12 @@ exports.setSettings = async function (type, data) {
|
|
|
|
|
|
|
|
|
|
|
|
if (bean.type === type) {
|
|
|
|
if (bean.type === type) {
|
|
|
|
bean.value = JSON.stringify(data[key]);
|
|
|
|
bean.value = JSON.stringify(data[key]);
|
|
|
|
promiseList.push(R.store(bean))
|
|
|
|
promiseList.push(R.store(bean));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await Promise.all(promiseList);
|
|
|
|
await Promise.all(promiseList);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ssl-checker by @dyaa
|
|
|
|
// ssl-checker by @dyaa
|
|
|
|
// param: res - response object from axios
|
|
|
|
// param: res - response object from axios
|
|
|
@ -219,7 +218,7 @@ exports.checkCertificate = function (res) {
|
|
|
|
issuer,
|
|
|
|
issuer,
|
|
|
|
fingerprint,
|
|
|
|
fingerprint,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Check if the provided status code is within the accepted ranges
|
|
|
|
// Check if the provided status code is within the accepted ranges
|
|
|
|
// Param: status - the status code to check
|
|
|
|
// Param: status - the status code to check
|
|
|
@ -248,7 +247,7 @@ exports.checkStatusCode = function (status, accepted_codes) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.getTotalClientInRoom = (io, roomName) => {
|
|
|
|
exports.getTotalClientInRoom = (io, roomName) => {
|
|
|
|
|
|
|
|
|
|
|
@ -271,7 +270,7 @@ exports.getTotalClientInRoom = (io, roomName) => {
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.genSecret = () => {
|
|
|
|
exports.genSecret = () => {
|
|
|
|
let secret = "";
|
|
|
|
let secret = "";
|
|
|
@ -281,21 +280,21 @@ exports.genSecret = () => {
|
|
|
|
secret += chars.charAt(Math.floor(Math.random() * charsLength));
|
|
|
|
secret += chars.charAt(Math.floor(Math.random() * charsLength));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return secret;
|
|
|
|
return secret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.allowDevAllOrigin = (res) => {
|
|
|
|
exports.allowDevAllOrigin = (res) => {
|
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
exports.allowAllOrigin(res);
|
|
|
|
exports.allowAllOrigin(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.allowAllOrigin = (res) => {
|
|
|
|
exports.allowAllOrigin = (res) => {
|
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
|
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
|
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.checkLogin = (socket) => {
|
|
|
|
exports.checkLogin = (socket) => {
|
|
|
|
if (! socket.userID) {
|
|
|
|
if (! socket.userID) {
|
|
|
|
throw new Error("You are not logged in.");
|
|
|
|
throw new Error("You are not logged in.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|