From 6d8a1a959c043ee5f71b63b2215743dbce540d8f Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Thu, 8 Jul 2021 14:44:05 +0300 Subject: [PATCH] uuid generator included --- package-lock.json | 29 ++++++++++++++++++++++++++++- package.json | 4 +++- src/dns/cache.ts | 3 ++- src/index.ts | 9 +++++++-- src/ip/from-request.ts | 12 ++++-------- src/keys.ts | 5 +++++ src/log/Logger.ts | 8 +++++--- 7 files changed, 54 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5de34ee..a3deb67 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,12 +10,14 @@ "dependencies": { "cors": "^2.8.5", "express": "^4.17.1", - "express-async-errors": "^3.1.1" + "express-async-errors": "^3.1.1", + "uuid": "^8.3.2" }, "devDependencies": { "@types/cors": "^2.8.10", "@types/express": "^4.17.11", "@types/node": "^15.3.0", + "@types/uuid": "^8.3.1", "typescript": "^4.2.4" } }, @@ -101,6 +103,12 @@ "@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": { "version": "1.3.7", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", @@ -609,6 +617,14 @@ "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": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", @@ -701,6 +717,12 @@ "@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": { "version": "1.3.7", "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", "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": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", diff --git a/package.json b/package.json index 19f9a9a..d688892 100644 --- a/package.json +++ b/package.json @@ -15,12 +15,14 @@ "dependencies": { "cors": "^2.8.5", "express": "^4.17.1", - "express-async-errors": "^3.1.1" + "express-async-errors": "^3.1.1", + "uuid": "^8.3.2" }, "devDependencies": { "@types/cors": "^2.8.10", "@types/express": "^4.17.11", "@types/node": "^15.3.0", + "@types/uuid": "^8.3.1", "typescript": "^4.2.4" } } diff --git a/src/dns/cache.ts b/src/dns/cache.ts index 4d9a65f..29d8717 100644 --- a/src/dns/cache.ts +++ b/src/dns/cache.ts @@ -1,3 +1,4 @@ +import { logger } from '../log/Logger'; import { CachedZone, DNSRecord, SOARecord } from '../models/interfaces'; import { readZoneFile } from './reader'; import { DNSRecordType } from './records'; @@ -122,7 +123,7 @@ export class DNSCache { try { await this.rndc.reload(name); } catch (e) { - console.warn('%s automatic zone reload failed:', name, e.stack); + logger.warn('%s automatic zone reload failed:', name, e.stack); } } } diff --git a/src/index.ts b/src/index.ts index d9484f8..9de9798 100644 --- a/src/index.ts +++ b/src/index.ts @@ -515,7 +515,7 @@ api.post('/set-ip/:domain', domainAuthorization, async (req, res) => { message: 'Waiting for next request..', 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; } @@ -526,7 +526,7 @@ api.post('/set-ip/:domain', domainAuthorization, async (req, res) => { message: 'Successfully updated zone file.', 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) => { @@ -553,4 +553,9 @@ async function load() { 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)); diff --git a/src/ip/from-request.ts b/src/ip/from-request.ts index 77eda09..99bb9a1 100644 --- a/src/ip/from-request.ts +++ b/src/ip/from-request.ts @@ -2,19 +2,15 @@ import { Request } from "express"; import { validv4, validv6 } from "./validators"; 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; - let v6 = null; + let v6: null | string = null; const qv6 = req.query.ipv6 || req.body.ipv6; // Lets begin our trials - // Determine Address from request headers - if (req.header('x-forwarded-for')) { - v4 = req.header('x-forwarded-for'); - } else { - v4 = req.socket.remoteAddress; - } + // Determine Address from request IP + v4 = req.ip; if (v4 && !validv4(v4)) { v6 = v4 diff --git a/src/keys.ts b/src/keys.ts index 72cf561..716a624 100644 --- a/src/keys.ts +++ b/src/keys.ts @@ -1,4 +1,5 @@ import * as fs from 'fs/promises'; +import { v4 } from 'uuid'; import path from 'path'; export class Keys { @@ -27,4 +28,8 @@ export class Keys { getKey(domain: string): string | undefined { return Object.keys(this.keys).find((key) => this.keys[key] === domain); } + + static generateKey(): string { + return v4(); + } } diff --git a/src/log/Logger.ts b/src/log/Logger.ts index 7c5e90a..ea415a8 100644 --- a/src/log/Logger.ts +++ b/src/log/Logger.ts @@ -16,9 +16,10 @@ export class Logger { private day = 0; private stream?: WriteStream; - constructor(public logDir: string, public logToFile = true) { - this.day = new Date().getDate(); - } + constructor( + public logDir: string, + public logToFile = true + ) {} static formatLogDate(date: Date): string { return `${date.getFullYear()}-${p(date.getMonth() + 1)}-${p(date.getDate())}`; @@ -88,4 +89,5 @@ export class Logger { } } +/** Logger singleton */ export const logger = Logger.fromEnvironment();