2020-04-10 00:43:15 +02:00
|
|
|
import _ from "lodash";
|
|
|
|
import async from "async";
|
|
|
|
import crypto from "crypto";
|
|
|
|
import oldFs, {promises as fs} from "graceful-fs";
|
|
|
|
import glob from "glob";
|
|
|
|
|
|
|
|
import {config} from "../config";
|
|
|
|
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";
|
|
|
|
import {normalizeString} from "../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-21 18:39:33 +02:00
|
|
|
isStoredNode,
|
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-18 12:55:19 +02:00
|
|
|
toUnixTimestampSeconds,
|
2022-07-14 20:06:05 +02:00
|
|
|
unhandledEnumField,
|
2022-07-18 12:55:19 +02:00
|
|
|
UnixTimestampMilliseconds,
|
2022-07-14 20:06:05 +02:00
|
|
|
UnixTimestampSeconds
|
|
|
|
} from "../types";
|
2020-04-10 00:43:15 +02:00
|
|
|
import util from "util";
|
|
|
|
|
|
|
|
const pglob = util.promisify(glob);
|
|
|
|
|
|
|
|
type NodeFilter = {
|
2022-07-14 20:06:05 +02:00
|
|
|
// TODO: Newtype
|
2020-04-10 00:43:15 +02:00
|
|
|
hostname?: string,
|
2022-07-14 20:06:05 +02:00
|
|
|
mac?: MAC,
|
|
|
|
key?: FastdKey,
|
2020-04-10 00:43:15 +02:00
|
|
|
token?: Token,
|
2022-07-14 20:06:05 +02:00
|
|
|
monitoringToken?: MonitoringToken,
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-14 20:06:05 +02:00
|
|
|
// TODO: Newtypes?
|
2020-04-10 00:43:15 +02:00
|
|
|
type NodeFilenameParsed = {
|
|
|
|
hostname?: string,
|
|
|
|
mac?: string,
|
|
|
|
key?: string,
|
2022-07-14 20:06:05 +02:00
|
|
|
token?: string,
|
2020-04-10 00:43:15 +02:00
|
|
|
monitoringToken?: string,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
const filenameParts = ['hostname', 'mac', 'key', 'token', 'monitoringToken'];
|
|
|
|
|
2022-07-18 17:49:42 +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
|
|
|
];
|
|
|
|
|
|
|
|
const pattern = fields.map((value) => value || '*').join('@');
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
return config.server.peersPath + '/' + pattern.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
function findNodeFiles(filter: NodeFilter): Promise<string[]> {
|
|
|
|
return pglob(toNodeFilesPattern(filter));
|
|
|
|
}
|
|
|
|
|
|
|
|
function findNodeFilesSync(filter: NodeFilter) {
|
|
|
|
return glob.sync(toNodeFilesPattern(filter));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function findFilesInPeersPath(): Promise<string[]> {
|
|
|
|
const files = await pglob(config.server.peersPath + '/*');
|
|
|
|
|
|
|
|
return await async.filter(files, (file, fileCallback) => {
|
|
|
|
if (file[0] === '.') {
|
|
|
|
return fileCallback(null, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.lstat(file)
|
|
|
|
.then(stats => fileCallback(null, stats.isFile()))
|
|
|
|
.catch(fileCallback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseNodeFilename(filename: string): NodeFilenameParsed {
|
|
|
|
const parts = _.split(filename, '@', filenameParts.length);
|
2022-07-14 20:06:05 +02:00
|
|
|
const parsed: { [key: string]: string | undefined } = {};
|
2020-04-10 00:43:15 +02:00
|
|
|
const zippedParts = _.zip<string, string>(filenameParts, parts);
|
|
|
|
_.each(zippedParts, part => {
|
|
|
|
const key = part[0];
|
|
|
|
if (key) {
|
|
|
|
parsed[key] = part[1];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
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-07-21 18:39:33 +02:00
|
|
|
function checkNoDuplicates(token: Token | undefined, node: BaseNode, nodeSecrets: NodeSecrets): void {
|
2022-07-14 20:06:05 +02:00
|
|
|
if (isDuplicate({hostname: node.hostname}, token)) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw {data: {msg: 'Already exists.', field: 'hostname'}, type: ErrorTypes.conflict};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.key) {
|
2022-07-14 20:06:05 +02:00
|
|
|
if (isDuplicate({key: node.key}, token)) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw {data: {msg: 'Already exists.', field: 'key'}, type: ErrorTypes.conflict};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 20:06:05 +02:00
|
|
|
if (isDuplicate({mac: node.mac}, token)) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw {data: {msg: 'Already exists.', field: 'mac'}, type: ErrorTypes.conflict};
|
|
|
|
}
|
|
|
|
|
2022-07-14 20:06:05 +02:00
|
|
|
if (nodeSecrets.monitoringToken && isDuplicate({monitoringToken: nodeSecrets.monitoringToken}, token)) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw {data: {msg: 'Already exists.', field: 'monitoringToken'}, type: ErrorTypes.conflict};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
function toNodeFilename(token: Token, node: BaseNode, nodeSecrets: NodeSecrets): string {
|
2020-04-10 00:43:15 +02:00
|
|
|
return config.server.peersPath + '/' +
|
|
|
|
(
|
|
|
|
(node.hostname || '') + '@' +
|
|
|
|
(node.mac || '') + '@' +
|
|
|
|
(node.key || '') + '@' +
|
|
|
|
(token || '') + '@' +
|
|
|
|
(nodeSecrets.monitoringToken || '')
|
|
|
|
).toLowerCase();
|
|
|
|
}
|
|
|
|
|
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-07-21 18:39:33 +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,
|
2020-04-10 00:43:15 +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);
|
|
|
|
let data = '';
|
|
|
|
|
2022-07-14 20:06:05 +02:00
|
|
|
for (const prefix of Object.values(LINE_PREFIX)) {
|
2022-07-21 18:39:33 +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-07-14 20:06:05 +02:00
|
|
|
const files = findNodeFilesSync({token: token});
|
2020-04-10 00:43:15 +02:00
|
|
|
if (files.length !== 1) {
|
|
|
|
throw {data: 'Node not found.', type: ErrorTypes.notFound};
|
|
|
|
}
|
|
|
|
|
|
|
|
checkNoDuplicates(token, node, nodeSecrets);
|
|
|
|
|
|
|
|
const file = files[0];
|
|
|
|
try {
|
|
|
|
oldFs.unlinkSync(file);
|
2022-07-14 20:06:05 +02:00
|
|
|
} catch (error) {
|
2020-04-10 00:43:15 +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};
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-21 18:39:33 +02:00
|
|
|
checkNoDuplicates(undefined, node, nodeSecrets);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
oldFs.writeFileSync(filename, data, 'utf8');
|
2022-07-21 18:39:33 +02:00
|
|
|
const {node: storedNode} = await parseNodeFile(filename);
|
|
|
|
return storedNode;
|
2022-07-14 20:06:05 +02:00
|
|
|
} catch (error) {
|
2020-04-10 00:43:15 +02:00
|
|
|
Logger.tag('node', 'save').error('Could not write node file: ' + filename, error);
|
|
|
|
throw {data: 'Could not write node data.', type: ErrorTypes.internalError};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteNodeFile(token: Token): Promise<void> {
|
|
|
|
let files;
|
|
|
|
try {
|
2022-07-14 20:06:05 +02:00
|
|
|
files = await findNodeFiles({token: token});
|
|
|
|
} catch (error) {
|
2020-04-10 00:43:15 +02:00
|
|
|
Logger.tag('node', 'delete').error('Could not find node file: ' + files, error);
|
|
|
|
throw {data: 'Could not delete node.', type: ErrorTypes.internalError};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (files.length !== 1) {
|
|
|
|
throw {data: 'Node not found.', type: ErrorTypes.notFound};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
oldFs.unlinkSync(files[0]);
|
2022-07-14 20:06:05 +02:00
|
|
|
} catch (error) {
|
2020-04-10 00:43:15 +02:00
|
|
|
Logger.tag('node', 'delete').error('Could not delete node file: ' + files, error);
|
|
|
|
throw {data: 'Could not delete node.', type: ErrorTypes.internalError};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
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-07-21 18:39:33 +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;
|
|
|
|
case LINE_PREFIX.MONITORING:
|
|
|
|
const active = value === 'aktiv';
|
|
|
|
const pending = value === 'pending';
|
|
|
|
node.monitoringState =
|
|
|
|
active ? MonitoringState.ACTIVE : (pending ? MonitoringState.PENDING : MonitoringState.DISABLED);
|
|
|
|
break;
|
|
|
|
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> {
|
|
|
|
const modifiedAtMs = (await fs.lstat(file)).mtimeMs as UnixTimestampMilliseconds;
|
|
|
|
return toUnixTimestampSeconds(modifiedAtMs);
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +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) {
|
|
|
|
const value = normalizeString(line.substring(prefix.length));
|
|
|
|
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-07-21 18:39:33 +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-07-21 18:39:33 +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) {
|
|
|
|
throw {data: 'Node not found.', type: ErrorTypes.notFound};
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +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) {
|
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'confirmation')
|
|
|
|
.error('Could not enqueue confirmation mail. No monitoring token found.');
|
|
|
|
throw {data: 'Internal error.', type: ErrorTypes.internalError};
|
|
|
|
}
|
|
|
|
|
|
|
|
const confirmUrl = monitoringConfirmUrl(monitoringToken);
|
|
|
|
const disableUrl = monitoringDisableUrl(monitoringToken);
|
|
|
|
|
|
|
|
await MailService.enqueue(
|
|
|
|
config.server.email.from,
|
|
|
|
node.nickname + ' <' + node.email + '>',
|
2022-07-18 17:49:42 +02:00
|
|
|
MailType.MONITORING_CONFIRMATION,
|
2020-04-10 00:43:15 +02:00
|
|
|
{
|
|
|
|
node: node,
|
|
|
|
confirmUrl: confirmUrl,
|
|
|
|
disableUrl: disableUrl
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +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-07-21 18:39:33 +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-07-21 18:39:33 +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-07-21 18:39:33 +02:00
|
|
|
export async function updateNode(token: Token, node: CreateOrUpdateNode): Promise<StoredNode> {
|
2021-01-25 22:40:41 +01:00
|
|
|
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-21 18:39:33 +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;
|
|
|
|
monitoringToken = nodeSecrets.monitoringToken || generateToken<MonitoringToken>();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
unhandledEnumField(currentNode.monitoringState);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeSecrets.monitoringToken = monitoringToken;
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const storedNode = await writeNodeFile(true, token, node, monitoringState, nodeSecrets);
|
|
|
|
if (storedNode.monitoringState === MonitoringState.PENDING) {
|
|
|
|
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,
|
|
|
|
nodeSecrets: NodeSecrets,
|
|
|
|
): 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) {
|
|
|
|
Logger.tag('nodes').error('Error getting all nodes:', error);
|
|
|
|
throw {data: 'Internal error.', type: ErrorTypes.internalError};
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
const {node} = await parseNodeFile(file);
|
|
|
|
nodes.push(node);
|
|
|
|
} catch (error) {
|
|
|
|
Logger.tag('nodes').error('Error getting all nodes:', error);
|
|
|
|
throw {data: 'Internal error.', type: ErrorTypes.internalError};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export async function findNodeDataWithSecretsByMac(mac: MAC): Promise<{ node: StoredNode, nodeSecrets: NodeSecrets } | null> {
|
2022-07-14 20:06:05 +02:00
|
|
|
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-07-14 20:06:05 +02:00
|
|
|
const result = await findNodeDataByFilePattern({mac});
|
2021-01-25 22:40:41 +01:00
|
|
|
return result ? result.node : null;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export async function getNodeDataWithSecretsByToken(token: Token): Promise<{ node: StoredNode, nodeSecrets: NodeSecrets }> {
|
2022-07-14 20:06:05 +02:00
|
|
|
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-07-14 20:06:05 +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-07-21 18:39:33 +02:00
|
|
|
): Promise<{ node: StoredNode, nodeSecrets: NodeSecrets }> {
|
2022-07-14 20:06:05 +02:00
|
|
|
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-07-14 20:06:05 +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) {
|
|
|
|
const {node, nodeSecrets} = await parseNodeFile(file);
|
|
|
|
|
|
|
|
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(
|
|
|
|
'Cannot rename file ' + file + ' to ' + expectedFilename + ' => ' + error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export async function findNodesModifiedBefore(timestamp: UnixTimestampSeconds): Promise<StoredNode[]> {
|
2021-07-26 21:40:17 +02:00
|
|
|
const nodes = await getAllNodes();
|
|
|
|
return _.filter(nodes, node => node.modifiedAt < timestamp);
|
|
|
|
}
|
|
|
|
|
2020-04-10 00:43:15 +02:00
|
|
|
export async function getNodeStatistics(): Promise<NodeStatistics> {
|
|
|
|
const nodes = await getAllNodes();
|
|
|
|
|
|
|
|
const nodeStatistics: NodeStatistics = {
|
|
|
|
registered: _.size(nodes),
|
|
|
|
withVPN: 0,
|
|
|
|
withCoords: 0,
|
|
|
|
monitoring: {
|
|
|
|
active: 0,
|
|
|
|
pending: 0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|