2020-04-10 00:43:15 +02:00
|
|
|
import async from "async";
|
|
|
|
import crypto from "crypto";
|
2022-08-23 20:08:53 +02:00
|
|
|
import oldFs, { promises as fs } from "graceful-fs";
|
2020-04-10 00:43:15 +02:00
|
|
|
import glob from "glob";
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
import { config } from "../config";
|
2020-04-10 00:43:15 +02:00
|
|
|
import ErrorTypes from "../utils/errorTypes";
|
|
|
|
import Logger from "../logger";
|
2022-07-21 18:39:33 +02:00
|
|
|
import logger from "../logger";
|
2020-04-10 00:43:15 +02:00
|
|
|
import * as MailService from "../services/mailService";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { normalizeString } from "../shared/utils/strings";
|
|
|
|
import {
|
|
|
|
monitoringConfirmUrl,
|
|
|
|
monitoringDisableUrl,
|
|
|
|
} from "../utils/urlBuilder";
|
2022-07-14 20:06:05 +02:00
|
|
|
import {
|
2022-07-21 18:39:33 +02:00
|
|
|
BaseNode,
|
2022-07-18 17:49:42 +02:00
|
|
|
Coordinates,
|
2022-07-21 18:39:33 +02:00
|
|
|
CreateOrUpdateNode,
|
2022-07-18 17:49:42 +02:00
|
|
|
EmailAddress,
|
2022-07-14 20:06:05 +02:00
|
|
|
FastdKey,
|
2022-07-18 17:49:42 +02:00
|
|
|
Hostname,
|
2022-07-28 14:09:46 +02:00
|
|
|
isFastdKey,
|
|
|
|
isHostname,
|
|
|
|
isMAC,
|
|
|
|
isMonitoringToken,
|
2022-07-21 18:39:33 +02:00
|
|
|
isStoredNode,
|
2022-07-28 14:09:46 +02:00
|
|
|
isToken,
|
2022-07-14 20:06:05 +02:00
|
|
|
MAC,
|
2022-07-18 17:49:42 +02:00
|
|
|
MailType,
|
2022-07-14 20:06:05 +02:00
|
|
|
MonitoringState,
|
|
|
|
MonitoringToken,
|
2022-07-18 17:49:42 +02:00
|
|
|
Nickname,
|
2022-07-14 20:06:05 +02:00
|
|
|
NodeSecrets,
|
|
|
|
NodeStatistics,
|
2022-07-21 18:39:33 +02:00
|
|
|
StoredNode,
|
2022-07-14 20:06:05 +02:00
|
|
|
Token,
|
2022-07-28 14:09:46 +02:00
|
|
|
TypeGuard,
|
2022-07-18 12:55:19 +02:00
|
|
|
UnixTimestampMilliseconds,
|
2022-08-23 20:08:53 +02:00
|
|
|
UnixTimestampSeconds,
|
2022-07-14 20:06:05 +02:00
|
|
|
} from "../types";
|
2020-04-10 00:43:15 +02:00
|
|
|
import util from "util";
|
2022-09-06 19:09:25 +02:00
|
|
|
import { filterUndefinedFromJSON } from "../shared/utils/json";
|
|
|
|
import { unhandledEnumField } from "../shared/utils/enums";
|
|
|
|
import { toUnixTimestampSeconds } from "../shared/utils/time";
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const pglob = util.promisify(glob);
|
|
|
|
|
|
|
|
type NodeFilter = {
|
2022-08-23 20:08:53 +02:00
|
|
|
hostname?: Hostname;
|
|
|
|
mac?: MAC;
|
|
|
|
key?: FastdKey;
|
|
|
|
token?: Token;
|
|
|
|
monitoringToken?: MonitoringToken;
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
type NodeFilenameParsed = {
|
2022-08-23 20:08:53 +02:00
|
|
|
hostname?: Hostname;
|
|
|
|
mac?: MAC;
|
|
|
|
key?: FastdKey;
|
|
|
|
token?: Token;
|
|
|
|
monitoringToken?: MonitoringToken;
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-14 20:06:05 +02:00
|
|
|
enum LINE_PREFIX {
|
|
|
|
HOSTNAME = "# Knotenname: ",
|
|
|
|
NICKNAME = "# Ansprechpartner: ",
|
|
|
|
EMAIL = "# Kontakt: ",
|
|
|
|
COORDS = "# Koordinaten: ",
|
|
|
|
MAC = "# MAC: ",
|
|
|
|
TOKEN = "# Token: ",
|
|
|
|
MONITORING = "# Monitoring: ",
|
|
|
|
MONITORING_TOKEN = "# Monitoring-Token: ",
|
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
function generateToken<
|
|
|
|
Type extends string & { readonly __tag: symbol } = never
|
|
|
|
>(): Type {
|
|
|
|
return crypto.randomBytes(8).toString("hex") as Type;
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function toNodeFilesPattern(filter: NodeFilter): string {
|
2022-07-14 20:06:05 +02:00
|
|
|
const fields: (string | undefined)[] = [
|
|
|
|
filter.hostname,
|
2022-07-18 17:49:42 +02:00
|
|
|
filter.mac,
|
|
|
|
filter.key,
|
|
|
|
filter.token,
|
|
|
|
filter.monitoringToken,
|
2022-07-14 20:06:05 +02:00
|
|
|
];
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const pattern = fields.map((value) => value || "*").join("@");
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
return config.server.peersPath + "/" + pattern.toLowerCase();
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function findNodeFiles(filter: NodeFilter): Promise<string[]> {
|
|
|
|
return pglob(toNodeFilesPattern(filter));
|
|
|
|
}
|
|
|
|
|
|
|
|
function findNodeFilesSync(filter: NodeFilter) {
|
|
|
|
return glob.sync(toNodeFilesPattern(filter));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function findFilesInPeersPath(): Promise<string[]> {
|
2022-08-23 20:08:53 +02:00
|
|
|
const files = await pglob(config.server.peersPath + "/*");
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
return await async.filter(files, (file, fileCallback) => {
|
2022-08-23 20:08:53 +02:00
|
|
|
if (file[0] === ".") {
|
2020-04-10 00:43:15 +02:00
|
|
|
return fileCallback(null, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.lstat(file)
|
2022-08-23 20:08:53 +02:00
|
|
|
.then((stats) => fileCallback(null, stats.isFile()))
|
2020-04-10 00:43:15 +02:00
|
|
|
.catch(fileCallback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseNodeFilename(filename: string): NodeFilenameParsed {
|
2022-08-23 20:08:53 +02:00
|
|
|
const parts = filename.split("@", 5);
|
2022-07-28 14:09:46 +02:00
|
|
|
|
|
|
|
function get<T>(isT: TypeGuard<T>, index: number): T | undefined {
|
2022-08-23 20:08:53 +02:00
|
|
|
const value =
|
|
|
|
index >= 0 && index < parts.length ? parts[index] : undefined;
|
2022-07-28 14:09:46 +02:00
|
|
|
return isT(value) ? value : undefined;
|
2022-07-28 13:16:13 +02:00
|
|
|
}
|
2022-07-28 14:09:46 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
hostname: get(isHostname, 0),
|
|
|
|
mac: get(isMAC, 1),
|
|
|
|
key: get(isFastdKey, 2),
|
|
|
|
token: get(isToken, 3),
|
|
|
|
monitoringToken: get(isMonitoringToken, 4),
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
function isDuplicate(filter: NodeFilter, token?: Token): boolean {
|
2020-04-10 00:43:15 +02:00
|
|
|
const files = findNodeFilesSync(filter);
|
|
|
|
if (files.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (files.length > 1 || !token /* node is being created*/) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
return parseNodeFilename(files[0]).token !== token;
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
function checkNoDuplicates(
|
|
|
|
token: Token | undefined,
|
|
|
|
node: BaseNode,
|
|
|
|
nodeSecrets: NodeSecrets
|
|
|
|
): void {
|
|
|
|
if (isDuplicate({ hostname: node.hostname }, token)) {
|
|
|
|
throw {
|
|
|
|
data: { msg: "Already exists.", field: "hostname" },
|
|
|
|
type: ErrorTypes.conflict,
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (node.key) {
|
2022-08-23 20:08:53 +02:00
|
|
|
if (isDuplicate({ key: node.key }, token)) {
|
|
|
|
throw {
|
|
|
|
data: { msg: "Already exists.", field: "key" },
|
|
|
|
type: ErrorTypes.conflict,
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
if (isDuplicate({ mac: node.mac }, token)) {
|
|
|
|
throw {
|
|
|
|
data: { msg: "Already exists.", field: "mac" },
|
|
|
|
type: ErrorTypes.conflict,
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
if (
|
|
|
|
nodeSecrets.monitoringToken &&
|
|
|
|
isDuplicate({ monitoringToken: nodeSecrets.monitoringToken }, token)
|
|
|
|
) {
|
|
|
|
throw {
|
|
|
|
data: { msg: "Already exists.", field: "monitoringToken" },
|
|
|
|
type: ErrorTypes.conflict,
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
function toNodeFilename(
|
|
|
|
token: Token,
|
|
|
|
node: BaseNode,
|
|
|
|
nodeSecrets: NodeSecrets
|
|
|
|
): string {
|
|
|
|
return (
|
|
|
|
config.server.peersPath +
|
|
|
|
"/" +
|
2020-04-10 00:43:15 +02:00
|
|
|
(
|
2022-08-23 20:08:53 +02:00
|
|
|
(node.hostname || "") +
|
|
|
|
"@" +
|
|
|
|
(node.mac || "") +
|
|
|
|
"@" +
|
|
|
|
(node.key || "") +
|
|
|
|
"@" +
|
|
|
|
(token || "") +
|
|
|
|
"@" +
|
|
|
|
(nodeSecrets.monitoringToken || "")
|
|
|
|
).toLowerCase()
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
function getNodeValue(
|
|
|
|
prefix: LINE_PREFIX,
|
|
|
|
token: Token,
|
|
|
|
node: CreateOrUpdateNode,
|
|
|
|
monitoringState: MonitoringState,
|
|
|
|
nodeSecrets: NodeSecrets
|
|
|
|
): string {
|
2022-07-14 20:06:05 +02:00
|
|
|
switch (prefix) {
|
|
|
|
case LINE_PREFIX.HOSTNAME:
|
|
|
|
return node.hostname;
|
|
|
|
case LINE_PREFIX.NICKNAME:
|
|
|
|
return node.nickname;
|
|
|
|
case LINE_PREFIX.EMAIL:
|
|
|
|
return node.email;
|
|
|
|
case LINE_PREFIX.COORDS:
|
|
|
|
return node.coords || "";
|
|
|
|
case LINE_PREFIX.MAC:
|
2022-07-18 17:49:42 +02:00
|
|
|
return node.mac;
|
2022-07-14 20:06:05 +02:00
|
|
|
case LINE_PREFIX.TOKEN:
|
2022-07-21 18:39:33 +02:00
|
|
|
return token;
|
2022-07-14 20:06:05 +02:00
|
|
|
case LINE_PREFIX.MONITORING:
|
2022-07-21 18:39:33 +02:00
|
|
|
if (node.monitoring && monitoringState === MonitoringState.ACTIVE) {
|
2022-07-14 20:06:05 +02:00
|
|
|
return "aktiv";
|
2022-08-23 20:08:53 +02:00
|
|
|
} else if (
|
|
|
|
node.monitoring &&
|
|
|
|
monitoringState === MonitoringState.PENDING
|
|
|
|
) {
|
2022-07-14 20:06:05 +02:00
|
|
|
return "pending";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
case LINE_PREFIX.MONITORING_TOKEN:
|
2022-07-18 17:49:42 +02:00
|
|
|
return nodeSecrets.monitoringToken || "";
|
2022-07-14 20:06:05 +02:00
|
|
|
default:
|
|
|
|
return unhandledEnumField(prefix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 00:43:15 +02:00
|
|
|
async function writeNodeFile(
|
|
|
|
isUpdate: boolean,
|
|
|
|
token: Token,
|
2022-07-21 18:39:33 +02:00
|
|
|
node: CreateOrUpdateNode,
|
|
|
|
monitoringState: MonitoringState,
|
2022-08-23 20:08:53 +02:00
|
|
|
nodeSecrets: NodeSecrets
|
2022-07-21 18:39:33 +02:00
|
|
|
): Promise<StoredNode> {
|
2020-04-10 00:43:15 +02:00
|
|
|
const filename = toNodeFilename(token, node, nodeSecrets);
|
2022-08-23 20:08:53 +02:00
|
|
|
let data = "";
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-14 20:06:05 +02:00
|
|
|
for (const prefix of Object.values(LINE_PREFIX)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
data += `${prefix}${getNodeValue(
|
|
|
|
prefix,
|
|
|
|
token,
|
|
|
|
node,
|
|
|
|
monitoringState,
|
|
|
|
nodeSecrets
|
|
|
|
)}\n`;
|
2022-07-14 20:06:05 +02:00
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
if (node.key) {
|
2022-07-14 20:06:05 +02:00
|
|
|
data += `key "${node.key}";\n`;
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
// since node.js is single threaded we don't need a lock when working with synchronous operations
|
2020-04-10 00:43:15 +02:00
|
|
|
if (isUpdate) {
|
2022-08-23 20:08:53 +02:00
|
|
|
const files = findNodeFilesSync({ token: token });
|
2020-04-10 00:43:15 +02:00
|
|
|
if (files.length !== 1) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Node not found.", type: ErrorTypes.notFound };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
checkNoDuplicates(token, node, nodeSecrets);
|
|
|
|
|
|
|
|
const file = files[0];
|
|
|
|
try {
|
|
|
|
oldFs.unlinkSync(file);
|
2022-07-14 20:06:05 +02:00
|
|
|
} catch (error) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("node", "save").error(
|
|
|
|
"Could not delete old node file: " + file,
|
|
|
|
error
|
|
|
|
);
|
|
|
|
throw {
|
|
|
|
data: "Could not remove old node data.",
|
|
|
|
type: ErrorTypes.internalError,
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-07-21 18:39:33 +02:00
|
|
|
checkNoDuplicates(undefined, node, nodeSecrets);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-08-23 20:08:53 +02:00
|
|
|
oldFs.writeFileSync(filename, data, "utf8");
|
|
|
|
const { node: storedNode } = await parseNodeFile(filename);
|
2022-07-21 18:39:33 +02:00
|
|
|
return storedNode;
|
2022-07-14 20:06:05 +02:00
|
|
|
} catch (error) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("node", "save").error(
|
|
|
|
"Could not write node file: " + filename,
|
|
|
|
error
|
|
|
|
);
|
|
|
|
throw {
|
|
|
|
data: "Could not write node data.",
|
|
|
|
type: ErrorTypes.internalError,
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteNodeFile(token: Token): Promise<void> {
|
|
|
|
let files;
|
|
|
|
try {
|
2022-08-23 20:08:53 +02:00
|
|
|
files = await findNodeFiles({ token: token });
|
2022-07-14 20:06:05 +02:00
|
|
|
} catch (error) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("node", "delete").error(
|
|
|
|
"Could not find node file: " + files,
|
|
|
|
error
|
|
|
|
);
|
|
|
|
throw {
|
|
|
|
data: "Could not delete node.",
|
|
|
|
type: ErrorTypes.internalError,
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (files.length !== 1) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Node not found.", type: ErrorTypes.notFound };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
oldFs.unlinkSync(files[0]);
|
2022-07-14 20:06:05 +02:00
|
|
|
} catch (error) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("node", "delete").error(
|
|
|
|
"Could not delete node file: " + files,
|
|
|
|
error
|
|
|
|
);
|
|
|
|
throw {
|
|
|
|
data: "Could not delete node.",
|
|
|
|
type: ErrorTypes.internalError,
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
class StoredNodeBuilder {
|
2022-07-18 17:49:42 +02:00
|
|
|
public token: Token = "" as Token; // FIXME: Either make token optional in Node or handle this!
|
|
|
|
public nickname: Nickname = "" as Nickname;
|
|
|
|
public email: EmailAddress = "" as EmailAddress;
|
|
|
|
public hostname: Hostname = "" as Hostname; // FIXME: Either make hostname optional in Node or handle this!
|
|
|
|
public coords?: Coordinates;
|
2022-07-14 20:06:05 +02:00
|
|
|
public key?: FastdKey;
|
2022-07-18 17:49:42 +02:00
|
|
|
public mac: MAC = "" as MAC; // FIXME: Either make mac optional in Node or handle this!
|
2022-07-14 20:06:05 +02:00
|
|
|
public monitoringState: MonitoringState = MonitoringState.DISABLED;
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
constructor(public readonly modifiedAt: UnixTimestampSeconds) {}
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
public build(): StoredNode {
|
|
|
|
const node = {
|
2022-07-14 20:06:05 +02:00
|
|
|
token: this.token,
|
|
|
|
nickname: this.nickname,
|
|
|
|
email: this.email,
|
|
|
|
hostname: this.hostname,
|
|
|
|
coords: this.coords,
|
|
|
|
key: this.key,
|
|
|
|
mac: this.mac,
|
|
|
|
monitoringState: this.monitoringState,
|
|
|
|
modifiedAt: this.modifiedAt,
|
2022-07-21 18:39:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!isStoredNode(node)) {
|
|
|
|
logger.tag("NodeService").error("Not a valid StoredNode:", node);
|
2022-08-23 20:08:53 +02:00
|
|
|
throw {
|
|
|
|
data: "Could not build StoredNode.",
|
|
|
|
type: ErrorTypes.internalError,
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
2022-07-21 18:39:33 +02:00
|
|
|
|
|
|
|
return node;
|
2022-07-14 20:06:05 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
function setNodeValue(
|
|
|
|
prefix: LINE_PREFIX,
|
|
|
|
node: StoredNodeBuilder,
|
|
|
|
nodeSecrets: NodeSecrets,
|
|
|
|
value: string
|
|
|
|
) {
|
2022-07-14 20:06:05 +02:00
|
|
|
switch (prefix) {
|
|
|
|
case LINE_PREFIX.HOSTNAME:
|
2022-07-18 17:49:42 +02:00
|
|
|
node.hostname = value as Hostname;
|
2022-07-14 20:06:05 +02:00
|
|
|
break;
|
|
|
|
case LINE_PREFIX.NICKNAME:
|
2022-07-18 17:49:42 +02:00
|
|
|
node.nickname = value as Nickname;
|
2022-07-14 20:06:05 +02:00
|
|
|
break;
|
|
|
|
case LINE_PREFIX.EMAIL:
|
2022-07-18 17:49:42 +02:00
|
|
|
node.email = value as EmailAddress;
|
2022-07-14 20:06:05 +02:00
|
|
|
break;
|
|
|
|
case LINE_PREFIX.COORDS:
|
2022-07-18 17:49:42 +02:00
|
|
|
node.coords = value as Coordinates;
|
2022-07-14 20:06:05 +02:00
|
|
|
break;
|
|
|
|
case LINE_PREFIX.MAC:
|
2022-07-18 17:49:42 +02:00
|
|
|
node.mac = value as MAC;
|
2022-07-14 20:06:05 +02:00
|
|
|
break;
|
|
|
|
case LINE_PREFIX.TOKEN:
|
2022-07-18 17:49:42 +02:00
|
|
|
node.token = value as Token;
|
2022-07-14 20:06:05 +02:00
|
|
|
break;
|
2022-08-23 21:38:37 +02:00
|
|
|
case LINE_PREFIX.MONITORING: {
|
2022-08-23 20:08:53 +02:00
|
|
|
const active = value === "aktiv";
|
|
|
|
const pending = value === "pending";
|
|
|
|
node.monitoringState = active
|
|
|
|
? MonitoringState.ACTIVE
|
|
|
|
: pending
|
|
|
|
? MonitoringState.PENDING
|
|
|
|
: MonitoringState.DISABLED;
|
2022-07-14 20:06:05 +02:00
|
|
|
break;
|
2022-08-23 21:38:37 +02:00
|
|
|
}
|
2022-07-14 20:06:05 +02:00
|
|
|
case LINE_PREFIX.MONITORING_TOKEN:
|
2022-07-18 17:49:42 +02:00
|
|
|
nodeSecrets.monitoringToken = value as MonitoringToken;
|
2022-07-14 20:06:05 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return unhandledEnumField(prefix);
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-18 12:55:19 +02:00
|
|
|
async function getModifiedAt(file: string): Promise<UnixTimestampSeconds> {
|
2022-08-23 20:08:53 +02:00
|
|
|
const modifiedAtMs = (await fs.lstat(file))
|
|
|
|
.mtimeMs as UnixTimestampMilliseconds;
|
2022-07-18 12:55:19 +02:00
|
|
|
return toUnixTimestampSeconds(modifiedAtMs);
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function parseNodeFile(
|
|
|
|
file: string
|
|
|
|
): Promise<{ node: StoredNode; nodeSecrets: NodeSecrets }> {
|
2022-07-14 20:06:05 +02:00
|
|
|
const contents = await fs.readFile(file);
|
2022-07-18 12:55:19 +02:00
|
|
|
const modifiedAt = await getModifiedAt(file);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-14 20:06:05 +02:00
|
|
|
const lines = contents.toString().split("\n");
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const node = new StoredNodeBuilder(modifiedAt);
|
2022-07-14 20:06:05 +02:00
|
|
|
const nodeSecrets: NodeSecrets = {};
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-14 20:06:05 +02:00
|
|
|
for (const line of lines) {
|
|
|
|
if (line.substring(0, 5) === 'key "') {
|
2022-07-18 17:49:42 +02:00
|
|
|
node.key = normalizeString(line.split('"')[1]) as FastdKey;
|
2022-07-14 20:06:05 +02:00
|
|
|
} else {
|
|
|
|
for (const prefix of Object.values(LINE_PREFIX)) {
|
|
|
|
if (line.substring(0, prefix.length) === prefix) {
|
2022-08-23 20:08:53 +02:00
|
|
|
const value = normalizeString(
|
|
|
|
line.substring(prefix.length)
|
|
|
|
);
|
2022-07-14 20:06:05 +02:00
|
|
|
setNodeValue(prefix, node, nodeSecrets, value);
|
|
|
|
break;
|
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
2022-07-14 20:06:05 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
return {
|
2022-07-14 20:06:05 +02:00
|
|
|
node: node.build(),
|
|
|
|
nodeSecrets,
|
2020-04-10 00:43:15 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function findNodeDataByFilePattern(
|
|
|
|
filter: NodeFilter
|
|
|
|
): Promise<{ node: StoredNode; nodeSecrets: NodeSecrets } | null> {
|
2020-04-10 00:43:15 +02:00
|
|
|
const files = await findNodeFiles(filter);
|
|
|
|
|
|
|
|
if (files.length !== 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const file = files[0];
|
|
|
|
return await parseNodeFile(file);
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function getNodeDataByFilePattern(
|
|
|
|
filter: NodeFilter
|
|
|
|
): Promise<{ node: StoredNode; nodeSecrets: NodeSecrets }> {
|
2020-04-10 00:43:15 +02:00
|
|
|
const result = await findNodeDataByFilePattern(filter);
|
|
|
|
if (!result) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Node not found.", type: ErrorTypes.notFound };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function sendMonitoringConfirmationMail(
|
|
|
|
node: StoredNode,
|
|
|
|
nodeSecrets: NodeSecrets
|
|
|
|
): Promise<void> {
|
2020-04-10 00:43:15 +02:00
|
|
|
const monitoringToken = nodeSecrets.monitoringToken;
|
|
|
|
if (!monitoringToken) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "confirmation").error(
|
|
|
|
"Could not enqueue confirmation mail. No monitoring token found."
|
|
|
|
);
|
|
|
|
throw { data: "Internal error.", type: ErrorTypes.internalError };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const confirmUrl = monitoringConfirmUrl(monitoringToken);
|
|
|
|
const disableUrl = monitoringDisableUrl(monitoringToken);
|
|
|
|
|
|
|
|
await MailService.enqueue(
|
|
|
|
config.server.email.from,
|
2022-08-23 20:08:53 +02:00
|
|
|
node.nickname + " <" + node.email + ">",
|
2022-07-18 17:49:42 +02:00
|
|
|
MailType.MONITORING_CONFIRMATION,
|
2020-04-10 00:43:15 +02:00
|
|
|
{
|
2022-08-23 21:01:58 +02:00
|
|
|
node: filterUndefinedFromJSON(node),
|
2020-04-10 00:43:15 +02:00
|
|
|
confirmUrl: confirmUrl,
|
2022-08-23 20:08:53 +02:00
|
|
|
disableUrl: disableUrl,
|
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export async function createNode(
|
|
|
|
node: CreateOrUpdateNode
|
|
|
|
): Promise<StoredNode> {
|
2022-07-14 20:06:05 +02:00
|
|
|
const token: Token = generateToken();
|
2020-04-10 00:43:15 +02:00
|
|
|
const nodeSecrets: NodeSecrets = {};
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const monitoringState = node.monitoring
|
|
|
|
? MonitoringState.PENDING
|
|
|
|
: MonitoringState.DISABLED;
|
2020-04-10 00:43:15 +02:00
|
|
|
if (node.monitoring) {
|
2022-07-14 20:06:05 +02:00
|
|
|
nodeSecrets.monitoringToken = generateToken<MonitoringToken>();
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const createdNode = await writeNodeFile(
|
|
|
|
false,
|
|
|
|
token,
|
|
|
|
node,
|
|
|
|
monitoringState,
|
|
|
|
nodeSecrets
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
if (createdNode.monitoringState == MonitoringState.PENDING) {
|
|
|
|
await sendMonitoringConfirmationMail(createdNode, nodeSecrets);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
return createdNode;
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export async function updateNode(
|
|
|
|
token: Token,
|
|
|
|
node: CreateOrUpdateNode
|
|
|
|
): Promise<StoredNode> {
|
|
|
|
const { node: currentNode, nodeSecrets } =
|
|
|
|
await getNodeDataWithSecretsByToken(token);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
let monitoringState = MonitoringState.DISABLED;
|
|
|
|
let monitoringToken: MonitoringToken | undefined = undefined;
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
if (node.monitoring) {
|
2022-07-21 18:39:33 +02:00
|
|
|
switch (currentNode.monitoringState) {
|
|
|
|
case MonitoringState.DISABLED:
|
|
|
|
// monitoring just has been enabled
|
|
|
|
monitoringState = MonitoringState.PENDING;
|
2022-07-14 20:06:05 +02:00
|
|
|
monitoringToken = generateToken<MonitoringToken>();
|
2022-07-28 14:09:46 +02:00
|
|
|
break;
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
case MonitoringState.PENDING:
|
|
|
|
case MonitoringState.ACTIVE:
|
|
|
|
if (currentNode.email !== node.email) {
|
|
|
|
// new email so we need a new token and a reconfirmation
|
|
|
|
monitoringState = MonitoringState.PENDING;
|
|
|
|
monitoringToken = generateToken<MonitoringToken>();
|
|
|
|
} else {
|
|
|
|
// email unchanged, keep token (fix if not set) and confirmation state
|
|
|
|
monitoringState = currentNode.monitoringState;
|
2022-08-23 20:08:53 +02:00
|
|
|
monitoringToken =
|
|
|
|
nodeSecrets.monitoringToken ||
|
|
|
|
generateToken<MonitoringToken>();
|
2022-07-21 18:39:33 +02:00
|
|
|
}
|
2022-07-28 14:09:46 +02:00
|
|
|
break;
|
2022-07-21 18:39:33 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
unhandledEnumField(currentNode.monitoringState);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeSecrets.monitoringToken = monitoringToken;
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const storedNode = await writeNodeFile(
|
|
|
|
true,
|
|
|
|
token,
|
|
|
|
node,
|
|
|
|
monitoringState,
|
|
|
|
nodeSecrets
|
|
|
|
);
|
2022-07-21 18:39:33 +02:00
|
|
|
if (storedNode.monitoringState === MonitoringState.PENDING) {
|
2022-08-23 20:08:53 +02:00
|
|
|
await sendMonitoringConfirmationMail(storedNode, nodeSecrets);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
return storedNode;
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function internalUpdateNode(
|
|
|
|
token: Token,
|
2022-07-21 18:39:33 +02:00
|
|
|
node: CreateOrUpdateNode,
|
|
|
|
monitoringState: MonitoringState,
|
2022-08-23 20:08:53 +02:00
|
|
|
nodeSecrets: NodeSecrets
|
2022-07-21 18:39:33 +02:00
|
|
|
): Promise<StoredNode> {
|
|
|
|
return await writeNodeFile(true, token, node, monitoringState, nodeSecrets);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-14 20:06:05 +02:00
|
|
|
export async function deleteNode(token: Token): Promise<void> {
|
2020-04-10 00:43:15 +02:00
|
|
|
await deleteNodeFile(token);
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export async function getAllNodes(): Promise<StoredNode[]> {
|
2020-04-10 00:43:15 +02:00
|
|
|
let files;
|
|
|
|
try {
|
|
|
|
files = await findNodeFiles({});
|
|
|
|
} catch (error) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("nodes").error("Error getting all nodes:", error);
|
|
|
|
throw { data: "Internal error.", type: ErrorTypes.internalError };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const nodes: StoredNode[] = [];
|
2020-04-10 00:43:15 +02:00
|
|
|
for (const file of files) {
|
|
|
|
try {
|
2022-08-23 20:08:53 +02:00
|
|
|
const { node } = await parseNodeFile(file);
|
2020-04-10 00:43:15 +02:00
|
|
|
nodes.push(node);
|
|
|
|
} catch (error) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("nodes").error("Error getting all nodes:", error);
|
|
|
|
throw { data: "Internal error.", type: ErrorTypes.internalError };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export async function findNodeDataWithSecretsByMac(
|
|
|
|
mac: MAC
|
|
|
|
): Promise<{ node: StoredNode; nodeSecrets: NodeSecrets } | null> {
|
|
|
|
return await findNodeDataByFilePattern({ mac });
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export async function findNodeDataByMac(mac: MAC): Promise<StoredNode | null> {
|
2022-08-23 20:08:53 +02:00
|
|
|
const result = await findNodeDataByFilePattern({ mac });
|
2021-01-25 22:40:41 +01:00
|
|
|
return result ? result.node : null;
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export async function getNodeDataWithSecretsByToken(
|
|
|
|
token: Token
|
|
|
|
): Promise<{ node: StoredNode; nodeSecrets: NodeSecrets }> {
|
|
|
|
return await getNodeDataByFilePattern({ token: token });
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export async function getNodeDataByToken(token: Token): Promise<StoredNode> {
|
2022-08-23 20:08:53 +02:00
|
|
|
const { node } = await getNodeDataByFilePattern({ token: token });
|
2021-01-25 22:40:41 +01:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2022-07-14 20:06:05 +02:00
|
|
|
export async function getNodeDataWithSecretsByMonitoringToken(
|
2020-04-10 00:43:15 +02:00
|
|
|
monitoringToken: MonitoringToken
|
2022-08-23 20:08:53 +02:00
|
|
|
): Promise<{ node: StoredNode; nodeSecrets: NodeSecrets }> {
|
|
|
|
return await getNodeDataByFilePattern({ monitoringToken: monitoringToken });
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-14 20:06:05 +02:00
|
|
|
export async function getNodeDataByMonitoringToken(
|
2021-01-25 22:40:41 +01:00
|
|
|
monitoringToken: MonitoringToken
|
2022-07-21 18:39:33 +02:00
|
|
|
): Promise<StoredNode> {
|
2022-08-23 20:08:53 +02:00
|
|
|
const { node } = await getNodeDataByFilePattern({
|
|
|
|
monitoringToken: monitoringToken,
|
|
|
|
});
|
2021-01-25 22:40:41 +01:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-04-10 00:43:15 +02:00
|
|
|
export async function fixNodeFilenames(): Promise<void> {
|
|
|
|
const files = await findFilesInPeersPath();
|
|
|
|
|
|
|
|
for (const file of files) {
|
2022-08-23 20:08:53 +02:00
|
|
|
const { node, nodeSecrets } = await parseNodeFile(file);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const expectedFilename = toNodeFilename(node.token, node, nodeSecrets);
|
|
|
|
if (file !== expectedFilename) {
|
|
|
|
try {
|
|
|
|
await fs.rename(file, expectedFilename);
|
2022-07-14 20:06:05 +02:00
|
|
|
} catch (error) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw new Error(
|
2022-08-23 20:08:53 +02:00
|
|
|
"Cannot rename file " +
|
|
|
|
file +
|
|
|
|
" to " +
|
|
|
|
expectedFilename +
|
|
|
|
" => " +
|
|
|
|
error
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export async function findNodesModifiedBefore(
|
|
|
|
timestamp: UnixTimestampSeconds
|
|
|
|
): Promise<StoredNode[]> {
|
2021-07-26 21:40:17 +02:00
|
|
|
const nodes = await getAllNodes();
|
2022-08-23 20:08:53 +02:00
|
|
|
return nodes.filter((node) => node.modifiedAt < timestamp);
|
2021-07-26 21:40:17 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 00:43:15 +02:00
|
|
|
export async function getNodeStatistics(): Promise<NodeStatistics> {
|
|
|
|
const nodes = await getAllNodes();
|
|
|
|
|
|
|
|
const nodeStatistics: NodeStatistics = {
|
2022-07-28 13:16:13 +02:00
|
|
|
registered: nodes.length,
|
2020-04-10 00:43:15 +02:00
|
|
|
withVPN: 0,
|
|
|
|
withCoords: 0,
|
|
|
|
monitoring: {
|
|
|
|
active: 0,
|
2022-08-23 20:08:53 +02:00
|
|
|
pending: 0,
|
|
|
|
},
|
2020-04-10 00:43:15 +02:00
|
|
|
};
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
for (const node of nodes) {
|
2020-04-10 00:43:15 +02:00
|
|
|
if (node.key) {
|
|
|
|
nodeStatistics.withVPN += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.coords) {
|
|
|
|
nodeStatistics.withCoords += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const monitoringState = node.monitoringState;
|
|
|
|
switch (monitoringState) {
|
|
|
|
case MonitoringState.ACTIVE:
|
|
|
|
nodeStatistics.monitoring.active += 1;
|
2022-07-14 20:06:05 +02:00
|
|
|
break;
|
2020-04-10 00:43:15 +02:00
|
|
|
case MonitoringState.PENDING:
|
|
|
|
nodeStatistics.monitoring.pending += 1;
|
2022-07-14 20:06:05 +02:00
|
|
|
break;
|
2020-04-10 00:43:15 +02:00
|
|
|
case MonitoringState.DISABLED:
|
|
|
|
// Not counted seperately.
|
2022-07-14 20:06:05 +02:00
|
|
|
break;
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
default:
|
2022-07-14 20:06:05 +02:00
|
|
|
unhandledEnumField(monitoringState);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
2022-07-21 18:39:33 +02:00
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
return nodeStatistics;
|
|
|
|
}
|