Merge pull request #4 from borisbm/master

Added proxied option
This commit is contained in:
Nicco 2022-05-14 15:52:54 +02:00 committed by GitHub
commit 47c0adbfb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 3 deletions

View File

@ -2,6 +2,7 @@
TOKEn=myapitoken
ZONE=example.org
DNS_RECORD=some.example.org
PROXIED=false
# Optional
#CRON=* * * * *

View File

@ -26,6 +26,7 @@ KEY=Global_API_Key
ZONE=example.org
DNS_RECORD=some.example.org
PROXIED=false
```
3. Run the container
@ -54,6 +55,7 @@ docker-compose up -d
| `TOKEN` | API Token that can be used instead of `EMAIL` & `KEY`. | |
| `ZONE` | Cloudflare zone where your domain is. | |
| `DNS_RECORD` | The actual DNS record that should be updated. | |
| `PROXIED` | Whether the record is proxied by CloudFlare or not. | |
| `CRON` | Frequency of updates. | \*/5 \* \* \* \* |
| `RESOLVER` | The endpoint used to determine your public ip. | https://api.ipify.org/ |

View File

@ -38,6 +38,7 @@ type DNSRecord = {
zone: string
record: string
ip: string
proxied: boolean
}
async function update(cf: Cloudflare, options: DNSRecord) {
// Find zone
@ -56,7 +57,7 @@ async function update(cf: Cloudflare, options: DNSRecord) {
logger.debug(`Zone ID: ${zoneId}`)
// Set record
const records: { result: { id: string; type: string; name: string; ttl: number }[] } = (await cf.dnsRecords.browse(
const records: { result: { id: string; type: string; name: string; proxied: boolean; ttl: number }[] } = (await cf.dnsRecords.browse(
zoneId
)) as any
const relevant = records.result.filter((r) => r.name === options.record && r.type === 'A')
@ -67,6 +68,7 @@ async function update(cf: Cloudflare, options: DNSRecord) {
type: 'A',
name: options.record,
content: options.ip,
proxied: options.proxied,
ttl: 1,
})
} else {
@ -84,6 +86,7 @@ async function update(cf: Cloudflare, options: DNSRecord) {
type: 'A',
name: options.record,
content: options.ip,
proxied: options.proxied,
ttl: record.ttl,
})
logger.info(`Updated DNS record ${record.name}`)
@ -92,11 +95,12 @@ async function update(cf: Cloudflare, options: DNSRecord) {
async function main() {
config()
const { EMAIL, KEY, TOKEN, ZONE, DNS_RECORD, CRON, RESOLVER } = process.env
const { EMAIL, KEY, TOKEN, ZONE, DNS_RECORD, PROXIED, CRON, RESOLVER } = process.env
if (!ZONE || !DNS_RECORD) {
logger.error('Missing environment variables')
process.exit(1)
}
const PROXIED_B = (PROXIED === 'true');
// Initialize Cloudflare
const cf = new Cloudflare(TOKEN ? { token: TOKEN } : { email: EMAIL, key: KEY })
@ -105,7 +109,7 @@ async function main() {
const ip = await getCurrentIp(RESOLVER)
const changed = checkIfUpdateIsRequired(ip)
logger.info(`Running. Update required: ${!!changed}`)
if (changed) await update(cf, { ip, record: DNS_RECORD!, zone: ZONE! }).catch((e) => logger.error(e.message))
if (changed) await update(cf, { ip, record: DNS_RECORD!, zone: ZONE!, proxied: PROXIED_B! }).catch((e) => logger.error(e.message))
}
const cron = new CronJob(CRON || '*/5 * * * *', fn, null, true, undefined, null, true)