-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
4,166 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ dist/* | |
log/* | ||
!log/.gitkeep | ||
node_modules/* | ||
npm-debug.log* | ||
npm-debug.log* | ||
config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
# TypeScript Boilerplate | ||
# CloudFlare dynamic DNS updater |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.