mirror of
https://github.com/cupcakearmy/docker-ddns-cloudflare.git
synced 2025-12-15 00:44:59 +00:00
use bun and remove some deps
This commit is contained in:
@@ -20,6 +20,8 @@ type DNSRecord = {
|
||||
type DNSRecordCreate = Pick<DNSRecord, 'name' | 'type' | 'ttl' | 'proxied' | 'content'>
|
||||
type DNSRecordPatch = Partial<DNSRecordCreate>
|
||||
|
||||
const l = logger.child({ context: 'cloudflare' })
|
||||
|
||||
const Base = axios.create({
|
||||
baseURL: 'https://api.cloudflare.com/client/v4',
|
||||
headers: {
|
||||
@@ -80,27 +82,27 @@ export const API = {
|
||||
export async function update(ip: string) {
|
||||
// Find zone
|
||||
if (!Cache.has('zone')) {
|
||||
logger.debug('Fetching zone')
|
||||
l.debug('Fetching zone')
|
||||
const zone = await API.zones.findByName(Config.dns.zone)
|
||||
if (!zone) {
|
||||
logger.error(`Zone "${Config.dns.zone}"" not found`)
|
||||
l.error(`Zone "${Config.dns.zone}" not found`)
|
||||
process.exit(1)
|
||||
}
|
||||
Cache.set('zone', zone)
|
||||
}
|
||||
|
||||
const zoneId = Cache.get('zone')!
|
||||
logger.debug(`Zone ID: ${zoneId}`)
|
||||
l.debug(`Zone ID: ${zoneId}`)
|
||||
|
||||
// Set record
|
||||
const records = await API.records.find(zoneId)
|
||||
|
||||
logger.debug('Updating record', ip)
|
||||
l.debug('Updating record', ip)
|
||||
|
||||
switch (records.length) {
|
||||
case 0:
|
||||
// Create DNS Record
|
||||
logger.debug('Creating DNS record')
|
||||
l.debug('Creating DNS record')
|
||||
await API.records.create(zoneId, {
|
||||
content: ip,
|
||||
name: Config.dns.record,
|
||||
@@ -114,7 +116,7 @@ export async function update(ip: string) {
|
||||
break
|
||||
default:
|
||||
// More than one record, delete all but the first
|
||||
logger.debug('Deleting other DNS records')
|
||||
l.debug('Deleting other DNS records')
|
||||
for (const record of records.slice(1)) {
|
||||
await API.records.remove(zoneId, record.id)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { config } from 'dotenv'
|
||||
import { validate } from 'node-cron'
|
||||
|
||||
config()
|
||||
import { Cron } from 'croner'
|
||||
import pkg from '../package.json'
|
||||
|
||||
function getEnv(key: string, fallback: string, parse?: undefined, validator?: (s: string) => boolean): string
|
||||
function getEnv<T>(key: string, fallback: T, parse: (value: string) => T, validator?: (T: string) => boolean): T
|
||||
@@ -31,7 +29,7 @@ function isPresent(s: string): boolean {
|
||||
}
|
||||
|
||||
export const Config = {
|
||||
version: getEnv('npm_package_version', 'unknown'),
|
||||
version: pkg.version,
|
||||
logging: {
|
||||
level: getEnv('LOG_LEVEL', 'info'),
|
||||
},
|
||||
@@ -44,7 +42,14 @@ export const Config = {
|
||||
proxied: getEnv('PROXIED', false, parseBoolean),
|
||||
},
|
||||
runner: {
|
||||
cron: getEnv('CRON', '*/5 * * * *', undefined, (s) => validate(s)),
|
||||
cron: getEnv('CRON', '*/5 * * * *', undefined, (s) => {
|
||||
try {
|
||||
new Cron(s)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}),
|
||||
resolver: getEnv('RESOLVER', 'https://api.ipify.org'),
|
||||
},
|
||||
}
|
||||
|
||||
16
src/index.ts
16
src/index.ts
@@ -1,14 +1,20 @@
|
||||
import { schedule } from 'node-cron'
|
||||
import process from 'process'
|
||||
import { Cron } from 'croner'
|
||||
import process from 'node:process'
|
||||
|
||||
import { Config } from './config.js'
|
||||
import { logger } from './logger.js'
|
||||
import { loop } from './runner.js'
|
||||
|
||||
async function main() {
|
||||
const cron = schedule(Config.runner.cron, loop)
|
||||
logger.info('Started service.', { version: Config.version })
|
||||
logger.debug('Config', Config)
|
||||
const cron = new Cron(Config.runner.cron, { protect: true }, loop)
|
||||
logger.info('started service', { version: Config.version })
|
||||
logger.debug('config', Config)
|
||||
|
||||
const nextRun = cron.nextRun()
|
||||
if (nextRun) {
|
||||
const pretty = new Intl.DateTimeFormat(undefined, { dateStyle: 'long', timeStyle: 'long' }).format(nextRun)
|
||||
logger.info(`next run scheduled for ${pretty}`, { nextRunAt: nextRun })
|
||||
}
|
||||
|
||||
function terminate() {
|
||||
logger.info('Stopping service.')
|
||||
|
||||
@@ -2,17 +2,19 @@ import { update } from './cloudflare.js'
|
||||
import { checkIfUpdateIsRequired, getCurrentIp } from './ip.js'
|
||||
import { logger } from './logger.js'
|
||||
|
||||
const l = logger.child({ context: 'runner' })
|
||||
|
||||
export async function loop() {
|
||||
const ip = await getCurrentIp()
|
||||
const changed = checkIfUpdateIsRequired(ip)
|
||||
logger.info(`Running. Update required: ${!!changed}`)
|
||||
l.info(`Running. Update required: ${!!changed}`)
|
||||
if (changed) {
|
||||
try {
|
||||
await update(ip)
|
||||
logger.info('Successfully updated DNS record')
|
||||
l.info('Successfully updated DNS record')
|
||||
} catch (e) {
|
||||
logger.error(e)
|
||||
logger.error('Failed to update DNS record')
|
||||
l.error(e)
|
||||
l.error('Failed to update DNS record')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user