mirror of
https://github.com/cupcakearmy/docker-ddns-cloudflare.git
synced 2024-12-22 07:46:24 +00:00
commit
47c0adbfb6
@ -2,6 +2,7 @@
|
|||||||
TOKEn=myapitoken
|
TOKEn=myapitoken
|
||||||
ZONE=example.org
|
ZONE=example.org
|
||||||
DNS_RECORD=some.example.org
|
DNS_RECORD=some.example.org
|
||||||
|
PROXIED=false
|
||||||
|
|
||||||
# Optional
|
# Optional
|
||||||
#CRON=* * * * *
|
#CRON=* * * * *
|
||||||
|
@ -26,6 +26,7 @@ KEY=Global_API_Key
|
|||||||
|
|
||||||
ZONE=example.org
|
ZONE=example.org
|
||||||
DNS_RECORD=some.example.org
|
DNS_RECORD=some.example.org
|
||||||
|
PROXIED=false
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Run the container
|
3. Run the container
|
||||||
@ -54,6 +55,7 @@ docker-compose up -d
|
|||||||
| `TOKEN` | API Token that can be used instead of `EMAIL` & `KEY`. | |
|
| `TOKEN` | API Token that can be used instead of `EMAIL` & `KEY`. | |
|
||||||
| `ZONE` | Cloudflare zone where your domain is. | |
|
| `ZONE` | Cloudflare zone where your domain is. | |
|
||||||
| `DNS_RECORD` | The actual DNS record that should be updated. | |
|
| `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 \* \* \* \* |
|
| `CRON` | Frequency of updates. | \*/5 \* \* \* \* |
|
||||||
| `RESOLVER` | The endpoint used to determine your public ip. | https://api.ipify.org/ |
|
| `RESOLVER` | The endpoint used to determine your public ip. | https://api.ipify.org/ |
|
||||||
|
|
||||||
|
10
src/index.ts
10
src/index.ts
@ -38,6 +38,7 @@ type DNSRecord = {
|
|||||||
zone: string
|
zone: string
|
||||||
record: string
|
record: string
|
||||||
ip: string
|
ip: string
|
||||||
|
proxied: boolean
|
||||||
}
|
}
|
||||||
async function update(cf: Cloudflare, options: DNSRecord) {
|
async function update(cf: Cloudflare, options: DNSRecord) {
|
||||||
// Find zone
|
// Find zone
|
||||||
@ -56,7 +57,7 @@ async function update(cf: Cloudflare, options: DNSRecord) {
|
|||||||
logger.debug(`Zone ID: ${zoneId}`)
|
logger.debug(`Zone ID: ${zoneId}`)
|
||||||
|
|
||||||
// Set record
|
// 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
|
zoneId
|
||||||
)) as any
|
)) as any
|
||||||
const relevant = records.result.filter((r) => r.name === options.record && r.type === 'A')
|
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',
|
type: 'A',
|
||||||
name: options.record,
|
name: options.record,
|
||||||
content: options.ip,
|
content: options.ip,
|
||||||
|
proxied: options.proxied,
|
||||||
ttl: 1,
|
ttl: 1,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@ -84,6 +86,7 @@ async function update(cf: Cloudflare, options: DNSRecord) {
|
|||||||
type: 'A',
|
type: 'A',
|
||||||
name: options.record,
|
name: options.record,
|
||||||
content: options.ip,
|
content: options.ip,
|
||||||
|
proxied: options.proxied,
|
||||||
ttl: record.ttl,
|
ttl: record.ttl,
|
||||||
})
|
})
|
||||||
logger.info(`Updated DNS record ${record.name}`)
|
logger.info(`Updated DNS record ${record.name}`)
|
||||||
@ -92,11 +95,12 @@ async function update(cf: Cloudflare, options: DNSRecord) {
|
|||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
config()
|
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) {
|
if (!ZONE || !DNS_RECORD) {
|
||||||
logger.error('Missing environment variables')
|
logger.error('Missing environment variables')
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
const PROXIED_B = (PROXIED === 'true');
|
||||||
|
|
||||||
// Initialize Cloudflare
|
// Initialize Cloudflare
|
||||||
const cf = new Cloudflare(TOKEN ? { token: TOKEN } : { email: EMAIL, key: KEY })
|
const cf = new Cloudflare(TOKEN ? { token: TOKEN } : { email: EMAIL, key: KEY })
|
||||||
@ -105,7 +109,7 @@ async function main() {
|
|||||||
const ip = await getCurrentIp(RESOLVER)
|
const ip = await getCurrentIp(RESOLVER)
|
||||||
const changed = checkIfUpdateIsRequired(ip)
|
const changed = checkIfUpdateIsRequired(ip)
|
||||||
logger.info(`Running. Update required: ${!!changed}`)
|
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)
|
const cron = new CronJob(CRON || '*/5 * * * *', fn, null, true, undefined, null, true)
|
||||||
|
Loading…
Reference in New Issue
Block a user