-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 8014bdb
Showing
10 changed files
with
333 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules/ | ||
.env | ||
.vscode | ||
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Ingar Helgesen | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,62 @@ | ||
# Deno DDNS Client | ||
A simple DDNS Client written in Typescript / Deno. | ||
|
||
## How to use (temporary) | ||
Currently it's pretty manual, I will add a dockerfile and a Deno bundle ASAP. | ||
```sh | ||
$ git clone https://github.com/sherex/ddns-client | ||
$ cp template.env .env | ||
$ vim .env # Or what editor you prefer | ||
# Run deno | ||
$ deno run --allow-read --allow-write --allow-net src/index.ts | ||
``` | ||
|
||
## TODO | ||
- [ ] Finish README | ||
- [ ] Create config overview | ||
- [ ] Create providers section | ||
- [ ] How to configure | ||
- [ ] Get API keys | ||
- [ ] Move config from .env to config.json | ||
- [ ] Optional .env? | ||
- [ ] Docker | ||
- [ ] Create dockerfile | ||
- [ ] Publish to Docker Hub | ||
- [ ] Bundle files with Deno | ||
- [ ] Create executable (Rust bundling) | ||
|
||
## Configuration | ||
### `DOMAIN` | ||
The domain you own that should receive the record | ||
|
||
### `RECORD_NAME = '@'` | ||
The name of the record to update. | ||
`@` is replaced with the `DOMAIN` variable. | ||
|
||
Ex. `subdomain.@` | ||
|
||
### `RECORD_TYPE = 'A'` | ||
The record type to update. | ||
|
||
### `RECORD_DATA = '!ip'` | ||
The data to put in the record, defaults to '!ip'. | ||
|
||
### `PROVIDER` | ||
The DNS provider that manages your records | ||
Supported providers: | ||
- [GoDaddy](https://godaddy.com/) | ||
|
||
### `GD_BASE_URL = 'https://api.godaddy.com/'` | ||
The base url for the provider GoDaddy, defaults to 'https://api.godaddy.com/'. | ||
Replace with 'https://api.ote-godaddy.com/' to use their OTE environment. (Requires seperate token and secret) | ||
|
||
Create your API token and secret at 'https://developer.godaddy.com/keys'. | ||
|
||
### `GD_TOKEN` | ||
Your GoDaddy token received from 'https://developer.godaddy.com/keys'. | ||
|
||
### `GD_SECRET` | ||
Your GoDaddy secret received from 'https://developer.godaddy.com/keys'. | ||
|
||
# License | ||
[MIT](LICENSE) |
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 { config } from "https://deno.land/x/dotenv/mod.ts"; | ||
import { log } from './lib/logger.ts' | ||
const envs = config() | ||
|
||
const validProviders = [ | ||
'godaddy' | ||
] | ||
|
||
if (!envs.DOMAIN) throw Error('Please enter a valid \'DOMAIN\' in the \'.env\' file') | ||
if (!envs.RECORD_NAME) { | ||
log('info', ['config', '\'RECORD_NAME\' is empty, defaulting to: \'@\'']) | ||
envs.RECORD_NAME = '@' | ||
} | ||
if (!envs.RECORD_TYPE) { | ||
log('info', ['config', '\'RECORD_TYPE\' is empty, defaulting to: \'A\'']) | ||
envs.RECORD_TYPE = 'A' | ||
} | ||
if (!envs.RECORD_DATA) { | ||
log('info', ['config', '\'RECORD_DATA\' is empty, defaulting to: \'!ip\' (your external IP)']) | ||
envs.RECORD_DATA = '!ip' | ||
} | ||
if (!envs.PROVIDER || !validProviders.includes(envs.PROVIDER)) throw Error('Please enter a valid \'PROVIDER\' in the \'.env\' file') | ||
if (envs.PROVIDER === 'godaddy') { | ||
if (!envs.GD_BASE_URL) { | ||
log('info', ['config', '\'GD_BASE_URL\' is empty, defaulting to: \'https://api.godaddy.com/\'']) | ||
envs.GD_BASE_URL = 'https://api.godaddy.com/' | ||
} | ||
if (!envs.GD_TOKEN) throw Error('Please enter a valid \'GD_TOKEN\' in the \'.env\' file') | ||
if (!envs.GD_SECRET) throw Error('Please enter a valid \'GD_SECRET\' in the \'.env\' file') | ||
} | ||
|
||
export default { | ||
DOMAIN: envs.DOMAIN, | ||
RECORD_NAME: envs.RECORD_NAME, | ||
RECORD_TYPE: envs.RECORD_TYPE, | ||
RECORD_DATA: envs.RECORD_DATA, | ||
PROVIDER: envs.PROVIDER, | ||
GD_BASE_URL: envs.GD_BASE_URL, | ||
GD_TOKEN: envs.GD_TOKEN, | ||
GD_SECRET: envs.GD_SECRET | ||
} |
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,46 @@ | ||
import config from './config.ts' | ||
import { log } from './lib/logger.ts' | ||
import getCurrentIp from "./lib/get-current-ip.ts"; | ||
import { setLastIp, getLastIp } from "./lib/last-ip.ts"; | ||
import { getRecord, setRecord } from "./providers/godaddy.ts"; | ||
|
||
let recordData = '' | ||
|
||
if (config.RECORD_DATA === '!ip') { | ||
const lastIp = await getLastIp() | ||
const currentIp = await getCurrentIp() | ||
|
||
if (lastIp && lastIp === currentIp) { | ||
log('debug', ['index', 'current IP is equal to last check', 'exiting']) | ||
Deno.exit(0) | ||
} | ||
recordData = currentIp | ||
} else { | ||
recordData = config.RECORD_DATA | ||
} | ||
|
||
const currentRecord = await getRecord(config.DOMAIN, { | ||
name: config.RECORD_NAME, | ||
type: config.RECORD_TYPE | ||
}) | ||
|
||
if (currentRecord && currentRecord.data === recordData) { | ||
log('debug', ['index', 'RECORD_DATA is equal to the DNS record', 'exiting']) | ||
if (config.RECORD_DATA === '!ip') { | ||
log('info', ['index', 'updating lastIp in config']) | ||
await setLastIp(currentRecord.data) | ||
} | ||
Deno.exit(0) | ||
} else { | ||
log('info', ['index', 'setting record', 'name', config.RECORD_NAME, 'type', config.RECORD_TYPE, 'data', recordData]) | ||
await setRecord(config.DOMAIN, { | ||
name: config.RECORD_NAME, | ||
type: config.RECORD_TYPE, | ||
data: recordData | ||
}) | ||
if (config.RECORD_DATA === '!ip') { | ||
log('info', ['index', 'updating lastIp in config']) | ||
await setLastIp(recordData) | ||
} | ||
} | ||
log('info', ['index', 'done!', 'exiting']) |
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,7 @@ | ||
async function getCurrentIp(): Promise<string> { | ||
const res = await fetch("https://api.ipify.org/"); | ||
if (!res.body) throw Error("Couldn't get the external IP"); | ||
return res.text(); | ||
} | ||
|
||
export default getCurrentIp; |
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,56 @@ | ||
import { log } from './logger.ts' | ||
|
||
interface Config { | ||
ip: string | null; | ||
} | ||
|
||
async function setLastIp(ip: string): Promise<void> { | ||
let config: Config = { | ||
ip: null, | ||
}; | ||
|
||
if (await exists("./config.json")) { | ||
try { | ||
config = JSON.parse(await Deno.readTextFile("./config.json")); | ||
config.ip = ip; | ||
|
||
const configJson = JSON.stringify(config, null, 2); | ||
await Deno.writeTextFile("./config.json", configJson); | ||
} catch (error) { | ||
log('warn', ['last-ip', 'Invalid format in \'config.json\'', 'skipping setting IP']); | ||
} | ||
} | ||
} | ||
|
||
async function getLastIp(): Promise<string | null> { | ||
let config: Config = { | ||
ip: null, | ||
}; | ||
|
||
if (await exists("./config.json")) { | ||
try { | ||
config = JSON.parse(await Deno.readTextFile("./config.json")); | ||
} catch (error) { | ||
log('warn', ['last-ip', 'Invalid format in \'config.json\'']); | ||
} | ||
} | ||
|
||
if (typeof config.ip === "string") return config.ip; | ||
|
||
return null; | ||
} | ||
|
||
async function exists(path: string): Promise<boolean> { | ||
try { | ||
await Deno.stat(path); | ||
return true; | ||
} catch (error) { | ||
if (error instanceof Deno.errors.NotFound) return false; | ||
throw error; | ||
} | ||
} | ||
|
||
export { | ||
setLastIp, | ||
getLastIp, | ||
}; |
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,19 @@ | ||
async function log(level: string, message: string | string[]) { | ||
message = Array.isArray(message) ? message : [message] | ||
const { date, time, milli } = getDateTime() | ||
console.log(`[ ${date} ${time}.${milli} ] < ${level.toUpperCase()} > ${message.join(' - ')}`) | ||
} | ||
|
||
function getDateTime() { | ||
const dateObject = new Date() | ||
const [date, time, milli] = dateObject.toISOString().split(/T|Z|\./) | ||
return { | ||
date, | ||
time, | ||
milli | ||
} | ||
} | ||
|
||
export { | ||
log | ||
} |
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,69 @@ | ||
import config from '../config.ts' | ||
import { log } from '../lib/logger.ts' | ||
|
||
interface DNSRecord { | ||
name: string, | ||
type: string, | ||
data?: string, | ||
ttl?: number | ||
} | ||
|
||
const fetchConfig: RequestInit = { | ||
headers: { | ||
Authorization: `sso-key ${config.GD_TOKEN}:${config.GD_SECRET}`, | ||
} | ||
} | ||
|
||
async function getRecord(domain: string, record: DNSRecord): Promise<DNSRecord | null> { | ||
record.name = record.name.replace('@', domain) | ||
|
||
log('info', ['godaddy', 'getting record for domain', domain, 'with name', record.name]) | ||
const res = await fetch(`${config.GD_BASE_URL}/v1/domains/${domain}/records/${record.type}/${record.name}`, fetchConfig) | ||
if (res.status !== 200) { | ||
log('error', ['godaddy', 'failed to GET record', 'statuscode', String(res.status), 'message', res.statusText]) | ||
throw Error(`Failed to fetch record. StatusCode: ${res.status}, msg: ${res.statusText}`) | ||
} | ||
let data = await res.json() | ||
log('info', ['godaddy', 'got record', data.length]) | ||
if (data && data.length > 0) { | ||
data = data[0] | ||
return { | ||
name: data.name.replace('@', domain), | ||
type: data.type, | ||
data: data.data, | ||
ttl: data.ttl | ||
} | ||
} else { | ||
return null | ||
} | ||
} | ||
|
||
async function setRecord(domain: string, record: DNSRecord): Promise<void> { | ||
record.name = record.name.replace('@', domain) | ||
const body = JSON.stringify([ | ||
{ | ||
data: record.data, | ||
ttl: record.ttl || 3600 | ||
} | ||
]) | ||
log('info', ['godaddy', 'setting record', 'data', record.data ? record.data : '\'\'']) | ||
const res = await fetch(`${config.GD_BASE_URL}/v1/domains/${domain}/records/${record.type}/${record.name}`, { | ||
headers: { | ||
...fetchConfig.headers, | ||
'Content-Type': 'application/json' | ||
}, | ||
body, | ||
method: 'PUT' | ||
}) | ||
if (res.status !== 200) { | ||
let body = await res.text() | ||
log('error', ['godaddy', 'failed to PUT record', 'statuscode', String(res.status), 'message', res.statusText, 'body', body]) | ||
throw Error(`Failed to PUT record. StatusCode: ${res.status}, msg: ${res.statusText}`) | ||
} | ||
} | ||
|
||
export { | ||
getRecord, | ||
setRecord, | ||
DNSRecord | ||
} |
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,8 @@ | ||
DOMAIN = example.com | ||
RECORD_NAME = sub.@ | ||
RECORD_TYPE = A | ||
RECORD_DATA = !ip | ||
PROVIDER = godaddy | ||
GD_BASE_URL = https://api.godaddy.com/ | ||
GD_TOKEN = token from https://developer.godaddy.com/keys | ||
GD_SECRET = secret from https://developer.godaddy.com/keys |