uuid generator included

This commit is contained in:
Evert Prants 2021-07-08 14:44:05 +03:00
parent 76e85ad9ec
commit 6d8a1a959c
7 changed files with 54 additions and 16 deletions

29
package-lock.json generated
View File

@ -10,12 +10,14 @@
"dependencies": { "dependencies": {
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.17.1", "express": "^4.17.1",
"express-async-errors": "^3.1.1" "express-async-errors": "^3.1.1",
"uuid": "^8.3.2"
}, },
"devDependencies": { "devDependencies": {
"@types/cors": "^2.8.10", "@types/cors": "^2.8.10",
"@types/express": "^4.17.11", "@types/express": "^4.17.11",
"@types/node": "^15.3.0", "@types/node": "^15.3.0",
"@types/uuid": "^8.3.1",
"typescript": "^4.2.4" "typescript": "^4.2.4"
} }
}, },
@ -101,6 +103,12 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"node_modules/@types/uuid": {
"version": "8.3.1",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.1.tgz",
"integrity": "sha512-Y2mHTRAbqfFkpjldbkHGY8JIzRN6XqYRliG8/24FcHm2D2PwW24fl5xMRTVGdrb7iMrwCaIEbLWerGIkXuFWVg==",
"dev": true
},
"node_modules/accepts": { "node_modules/accepts": {
"version": "1.3.7", "version": "1.3.7",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz",
@ -609,6 +617,14 @@
"node": ">= 0.4.0" "node": ">= 0.4.0"
} }
}, },
"node_modules/uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/vary": { "node_modules/vary": {
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
@ -701,6 +717,12 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"@types/uuid": {
"version": "8.3.1",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.1.tgz",
"integrity": "sha512-Y2mHTRAbqfFkpjldbkHGY8JIzRN6XqYRliG8/24FcHm2D2PwW24fl5xMRTVGdrb7iMrwCaIEbLWerGIkXuFWVg==",
"dev": true
},
"accepts": { "accepts": {
"version": "1.3.7", "version": "1.3.7",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz",
@ -1088,6 +1110,11 @@
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
"integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
}, },
"uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="
},
"vary": { "vary": {
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",

View File

@ -15,12 +15,14 @@
"dependencies": { "dependencies": {
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.17.1", "express": "^4.17.1",
"express-async-errors": "^3.1.1" "express-async-errors": "^3.1.1",
"uuid": "^8.3.2"
}, },
"devDependencies": { "devDependencies": {
"@types/cors": "^2.8.10", "@types/cors": "^2.8.10",
"@types/express": "^4.17.11", "@types/express": "^4.17.11",
"@types/node": "^15.3.0", "@types/node": "^15.3.0",
"@types/uuid": "^8.3.1",
"typescript": "^4.2.4" "typescript": "^4.2.4"
} }
} }

View File

@ -1,3 +1,4 @@
import { logger } from '../log/Logger';
import { CachedZone, DNSRecord, SOARecord } from '../models/interfaces'; import { CachedZone, DNSRecord, SOARecord } from '../models/interfaces';
import { readZoneFile } from './reader'; import { readZoneFile } from './reader';
import { DNSRecordType } from './records'; import { DNSRecordType } from './records';
@ -122,7 +123,7 @@ export class DNSCache {
try { try {
await this.rndc.reload(name); await this.rndc.reload(name);
} catch (e) { } catch (e) {
console.warn('%s automatic zone reload failed:', name, e.stack); logger.warn('%s automatic zone reload failed:', name, e.stack);
} }
} }
} }

View File

@ -515,7 +515,7 @@ api.post('/set-ip/:domain', domainAuthorization, async (req, res) => {
message: 'Waiting for next request..', message: 'Waiting for next request..',
actions actions
}); });
logger.info('set-ip (partial) from %s: %s', req.ip, actions.join('\n')); logger.info('zone %s set-ip (partial) from %s: %s', domain, req.ip, actions.join('\n'));
return; return;
} }
@ -526,7 +526,7 @@ api.post('/set-ip/:domain', domainAuthorization, async (req, res) => {
message: 'Successfully updated zone file.', message: 'Successfully updated zone file.',
actions actions
}); });
logger.info('set-ip from %s: %s', req.ip, actions.join('\n')); logger.info('zone %s set-ip from %s: %s', domain, req.ip, actions.join('\n'));
}); });
const errorHandler: ErrorRequestHandler = (err: any, req: Request, res: Response, next: NextFunction) => { const errorHandler: ErrorRequestHandler = (err: any, req: Request, res: Response, next: NextFunction) => {
@ -553,4 +553,9 @@ async function load() {
app.listen(port, () => logger.info(`listening on ${port}`)); app.listen(port, () => logger.info(`listening on ${port}`));
} }
if (process.argv.includes('genkey')) {
console.log(Keys.generateKey());
process.exit();
}
load().catch((e) => console.error(e.stack)); load().catch((e) => console.error(e.stack));

View File

@ -2,19 +2,15 @@ import { Request } from "express";
import { validv4, validv6 } from "./validators"; import { validv4, validv6 } from "./validators";
export function fromRequest(req: Request): { v4: string | null, v6: string | null } { export function fromRequest(req: Request): { v4: string | null, v6: string | null } {
let v4 = null; let v4: null | string = null;
const qv4 = req.query.ipv4 || req.body.ipv4; const qv4 = req.query.ipv4 || req.body.ipv4;
let v6 = null; let v6: null | string = null;
const qv6 = req.query.ipv6 || req.body.ipv6; const qv6 = req.query.ipv6 || req.body.ipv6;
// Lets begin our trials // Lets begin our trials
// Determine Address from request headers // Determine Address from request IP
if (req.header('x-forwarded-for')) { v4 = req.ip;
v4 = req.header('x-forwarded-for');
} else {
v4 = req.socket.remoteAddress;
}
if (v4 && !validv4(v4)) { if (v4 && !validv4(v4)) {
v6 = v4 v6 = v4

View File

@ -1,4 +1,5 @@
import * as fs from 'fs/promises'; import * as fs from 'fs/promises';
import { v4 } from 'uuid';
import path from 'path'; import path from 'path';
export class Keys { export class Keys {
@ -27,4 +28,8 @@ export class Keys {
getKey(domain: string): string | undefined { getKey(domain: string): string | undefined {
return Object.keys(this.keys).find((key) => this.keys[key] === domain); return Object.keys(this.keys).find((key) => this.keys[key] === domain);
} }
static generateKey(): string {
return v4();
}
} }

View File

@ -16,9 +16,10 @@ export class Logger {
private day = 0; private day = 0;
private stream?: WriteStream; private stream?: WriteStream;
constructor(public logDir: string, public logToFile = true) { constructor(
this.day = new Date().getDate(); public logDir: string,
} public logToFile = true
) {}
static formatLogDate(date: Date): string { static formatLogDate(date: Date): string {
return `${date.getFullYear()}-${p(date.getMonth() + 1)}-${p(date.getDate())}`; return `${date.getFullYear()}-${p(date.getMonth() + 1)}-${p(date.getDate())}`;
@ -88,4 +89,5 @@ export class Logger {
} }
} }
/** Logger singleton */
export const logger = Logger.fromEnvironment(); export const logger = Logger.fromEnvironment();