Skip to content

Commit

Permalink
Ported to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
aequabit committed Jun 19, 2018
1 parent 71e6dc5 commit 6449d77
Show file tree
Hide file tree
Showing 16 changed files with 4,166 additions and 123 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ dist/*
log/*
!log/.gitkeep
node_modules/*
npm-debug.log*
npm-debug.log*
config.json
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# TypeScript Boilerplate
# CloudFlare dynamic DNS updater
4 changes: 0 additions & 4 deletions config.json

This file was deleted.

11 changes: 11 additions & 0 deletions config.json.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"debug": true,
"logPath": "log",
"email": "[email protected]",
"apikey": "",
"zoneId": "",
"domain": "dyn.example.org",
"type": "A",
"ttl": 120,
"proxied": false
}
20 changes: 13 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
{
"name": "boilerplate-typescript",
"name": "cloudflare-dyndns",
"version": "1.0.0",
"description": "TypeScript Boilerplate",
"description": "CloudFlare dynamic DNS updater",
"main": "dist/index.js",
"devDependencies": {
"@types/moment": "^2.13.0",
"@types/node": "^9.4.7",
"@types/npmlog": "^4.1.0",
"@types/request-promise": "^4.1.41",
"nodemon": "^1.14.3",
"ts-node": "^5.0.1",
"tslint": "^5.9.1",
"typescript": "^2.7.2"
},
"dependencies": {
"moment": "^2.21.0",
"winston": "^3.0.0-rc3"
"cloudflare": "^2.4.1",
"moment": "^2.22.2",
"npmlog": "^4.1.2",
"request": "^2.87.0",
"request-promise": "^4.2.2"
},
"scripts": {
"start": "nodemon --watch src --exec ts-node src/index.ts",
Expand All @@ -23,12 +29,12 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/aequabit/boilerplate-typescript.git"
"url": "git+https://github.com/aequabit/cloudflare-dyndns.git"
},
"author": "aequabit",
"license": "ISC",
"bugs": {
"url": "https://github.com/aequabit/boilerplate-typescript/issues"
"url": "https://github.com/aequabit/cloudflare-dyndns/issues"
},
"homepage": "https://github.com/aequabit/boilerplate-typescript#readme"
"homepage": "https://github.com/aequabit/cloudflare-dyndns#readme"
}
16 changes: 16 additions & 0 deletions src/core/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as config from '../../config.json';

export interface IConfig {
logPath: string;
email: string;
apikey: string;
zoneId: string;
domain: string;
type: string;
ttl: number;
proxied: boolean;
}

export default new Proxy((config as any) as IConfig, {
get: (obj, prop) => prop in obj ? obj[prop] : undefined
});
43 changes: 40 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
import Logger from './modules/Logger';
import * as request from 'request-promise';

import config from './core/config';

import CloudFlare from './lib/CloudFlare';
import Logger from './lib/Logger';

// CloudFlare has a limit of 1200 calls every five minutes
const UPDATE_INTERVAL = 10 * 1000;

const getIp = async (): Promise<string> => {
const response = await request({ uri: 'https://wtfismyip.com/json' });

try {
return JSON.parse(response).YourFuckingIPAddress;
} catch (err) {
return null;
}
}

(async (): Promise<void> => {
Logger.info('TypeScript Boilerplate');
})();
Logger.info('cloudflare', 'Started DNS updater');

const cf = new CloudFlare(config);

setInterval(async () => {
const ip = await getIp();

if (ip === null) {
Logger.error('ip', 'Failed to get IP address');
return;
}

try {
await cf.updateRecord(ip);
Logger.info('cloudflare', 'Updated IP for %s to %s', cf.getConfig().domain, ip);
} catch (err) {
Logger.error('cloudflare', 'Error while getting external IP: %s', err.message);
}

}, UPDATE_INTERVAL);
})();
45 changes: 45 additions & 0 deletions src/lib/CloudFlare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as cloudflare from 'cloudflare';

import { IConfig } from '../core/config';

export interface IRecord {
id: string;
}

export default class CloudFlare {
private readonly _config: IConfig = null;
private readonly _cloudFlare: any = null;

public constructor(config: IConfig) {
this._config = config;
this._cloudFlare = cloudflare({
email: this._config.email,
key: this._config.apikey
});
}

public getConfig(): IConfig {
return this._config;
}

public async updateRecord(ipAddress: string): Promise<void> {
const record = await this._getRecord();

this._cloudFlare.dnsRecords
.edit(this._config.zoneId, record.id, {
type: this._config.type,
name: this._config.domain,
content: ipAddress,
ttl: this._config.ttl,
proxied: this._config.proxied
});
}

private async _getRecord(): Promise<IRecord> {
const dnsRecord = await this._cloudFlare.dnsRecords
.browse(this._config.zoneId);

return dnsRecord.result
.find(x => x.name == this._config.domain) as IRecord;
}
}
41 changes: 41 additions & 0 deletions src/lib/Logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as fs from 'fs';
import * as moment from 'moment';
import * as path from 'path';
import { vsprintf } from 'sprintf-js';

import config from '../core/config';

export default class Logger {
private static readonly __logPath = path.join(
__dirname, '..', '..', config.logPath, moment().format('DD.MM.YYYY'));

public static log(level: string, prefix: string, format: string, ...args: any[]) {
const timestamp = moment().format('DD.MM.YYYY HH:mm:ss');
const log = vsprintf(format, args);
const final = `[${timestamp}] [${level}] [${prefix}] ${log}`;

// tslint:disable-next-line
console.log(final);

fs.appendFile(Logger.__logPath, final + '\n', {}, err => {
if (err !== null)
throw err;
});
}

public static error(prefix: string, format: string, ...args: any[]) {
this.log('error', prefix, format, ...args);
}

public static warn(prefix: string, format: string, ...args: any[]) {
this.log('warn', prefix, format, ...args);
}

public static info(prefix: string, format: string, ...args: any[]) {
this.log('info', prefix, format, ...args);
}

public static debug(prefix: string, format: string, ...args: any[]) {
this.log('debug', prefix, format, ...args);
}
}
57 changes: 0 additions & 57 deletions src/modules/Config.ts

This file was deleted.

38 changes: 0 additions & 38 deletions src/modules/Logger.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/test.ts

This file was deleted.

8 changes: 3 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
"module": "commonjs",
"noEmitOnError": true,
"noErrorTruncation": true,
"baseUrl": "src",
"outDir": "dist",
"pretty": true,
"sourceMap": true
"sourceMap": true,
"target": "es5"
},
"include": ["**/*.ts"],
"exclude": ["node_modules", "**/*.js"]
"exclude": ["node_modules"]
}

3 changes: 1 addition & 2 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"options": [120]
},
"arrow-parens": false,
"curly": [true, "as-needed"],
"curly": false,
"object-literal-sort-keys": false,
"member-ordering": [
true,
Expand Down Expand Up @@ -47,4 +47,3 @@
}
}
}

Loading

0 comments on commit 6449d77

Please sign in to comment.