mirror of
https://github.com/cupcakearmy/docker-ddns-cloudflare.git
synced 2025-09-10 15:00:41 +00:00
Compare commits
22 Commits
v1.0.0
...
e0af9cd82e
Author | SHA1 | Date | |
---|---|---|---|
e0af9cd82e | |||
3ca15c532f | |||
500d7b2423 | |||
144d5abeb6 | |||
0b6e6e07dd | |||
|
43bc492c49 | ||
|
b9f77f24ca | ||
18a5a40b43 | |||
37cdc12439 | |||
02c27afd29 | |||
04b3acc509 | |||
a1af82501c | |||
fb7416dbec | |||
54eb94135b | |||
47c0adbfb6 | |||
|
d25c33d015 | ||
|
c007fbb848 | ||
|
f6d825b843 | ||
10b4d5473b | |||
157ce6b7b6 | |||
8904ffcbdd | |||
832bc1993a |
4
.dockerignore
Normal file
4
.dockerignore
Normal file
@@ -0,0 +1,4 @@
|
||||
*
|
||||
!src
|
||||
!package.json
|
||||
!bun.lockb
|
26
.github/workflows/build.yml
vendored
Normal file
26
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
name: Build Docker image
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
|
||||
concurrency:
|
||||
group: build
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
docker:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@v2
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v2
|
||||
with:
|
||||
install: true
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v3
|
||||
with:
|
||||
platforms: linux/amd64,linux/arm64
|
||||
push: false
|
42
.github/workflows/docker.yml
vendored
42
.github/workflows/docker.yml
vendored
@@ -1,39 +1,47 @@
|
||||
name: ci
|
||||
name: Publish Docker image
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
push:
|
||||
tags:
|
||||
- "v*.*.*"
|
||||
release:
|
||||
types: [published]
|
||||
|
||||
jobs:
|
||||
docker:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@v1
|
||||
uses: docker/setup-qemu-action@v2
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v1
|
||||
uses: docker/setup-buildx-action@v2
|
||||
with:
|
||||
install: true
|
||||
|
||||
- name: Docker Labels
|
||||
id: meta
|
||||
uses: crazy-max/ghaction-docker-meta@v2
|
||||
uses: docker/metadata-action@v4
|
||||
with:
|
||||
images: cupcakearmy/ddns-cloudflare
|
||||
images: |
|
||||
cupcakearmy/ddns-cloudflare
|
||||
ghcr.io/${{ github.repository }}
|
||||
tags: |
|
||||
type=semver,pattern={{version}}
|
||||
type=semver,pattern={{major}}.{{minor}}
|
||||
type=semver,pattern={{major}}
|
||||
- name: Login to DockerHub
|
||||
uses: docker/login-action@v1
|
||||
|
||||
- name: Log in to Docker Hub
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_TOKEN }}
|
||||
- name: Log in to the Container registry
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Build and push
|
||||
id: docker_build
|
||||
uses: docker/build-push-action@v2
|
||||
uses: docker/build-push-action@v3
|
||||
with:
|
||||
platforms: linux/amd64,linux/arm64
|
||||
push: true
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
- name: Image digest
|
||||
run: echo ${{ steps.docker_build.outputs.digest }}
|
||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,4 @@
|
||||
.env
|
||||
ip.log
|
||||
|
||||
node_modules
|
||||
.vscode
|
||||
|
@@ -1,9 +1,10 @@
|
||||
# Required
|
||||
EMAIL=my@mail.com
|
||||
KEY=my_api_key
|
||||
TOKEN=myapitoken
|
||||
ZONE=example.org
|
||||
DNS_RECORD=some.example.org
|
||||
|
||||
# Optional
|
||||
#PROXIED=false
|
||||
#CRON=* * * * *
|
||||
#RESOLVER=http://ipv4.icanhazip.com/
|
||||
#LOG_LEVEL=debug
|
||||
|
95
CHANGELOG.md
95
CHANGELOG.md
@@ -5,13 +5,102 @@ All notable changes to this project will be documented in this file.
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [1.4.1] - 2024-02-27
|
||||
|
||||
### Changed
|
||||
|
||||
- Use the alpine `bun` image to half the image size.
|
||||
|
||||
## [1.4.0] - 2024-02-27
|
||||
|
||||
### Changed
|
||||
|
||||
- Moved to bun.
|
||||
- Removed some dependencies.
|
||||
|
||||
## [1.3.4] - 2024-02-27
|
||||
|
||||
### Security
|
||||
|
||||
- Updated dependencies.
|
||||
|
||||
## [1.3.3] - 2023-08-20
|
||||
|
||||
### Security
|
||||
|
||||
- Updated dependencies.
|
||||
|
||||
## [1.3.2] - 2023-06-24
|
||||
|
||||
### Security
|
||||
|
||||
- Updated dependencies.
|
||||
|
||||
## [1.3.1] - 2022-11-28
|
||||
|
||||
### Added
|
||||
|
||||
- `y` is now a valid truthy value.
|
||||
|
||||
### Security
|
||||
|
||||
- Updated dependencies.
|
||||
|
||||
## [1.3.0] - 2022-10-20
|
||||
|
||||
### Removed
|
||||
|
||||
- Login with username and password as it's considered deprecated.
|
||||
|
||||
### Added
|
||||
|
||||
- `LOG_LEVEL` env variable.
|
||||
- Config parsing and validation.
|
||||
|
||||
### Changed
|
||||
|
||||
- Removed Cloudflare SDK due to outdated and bloated package.
|
||||
|
||||
## [1.2.1] - 2022-05-14
|
||||
|
||||
### Added
|
||||
|
||||
- Support for `proxied` parameter thanks to @borisbm.
|
||||
|
||||
### Security
|
||||
|
||||
- Updated dependencies.
|
||||
|
||||
## [1.2.0] - 2022-02-07
|
||||
|
||||
### Added
|
||||
|
||||
- Sigterm and Sigkill hooks for graceful shutdown
|
||||
|
||||
### Changed
|
||||
|
||||
- Multistage steps to reduce image size
|
||||
|
||||
## [1.1.1] - 2022-02-07
|
||||
|
||||
### Security
|
||||
|
||||
- Updated dependencies.
|
||||
|
||||
## [1.1.0] - 2021-10-22
|
||||
|
||||
### Changed
|
||||
|
||||
- Added typescript for type safety.
|
||||
- Switched to pnpm.
|
||||
|
||||
## [1.0.0] - 2021-05-05
|
||||
|
||||
### Added
|
||||
|
||||
- automated build & tagging
|
||||
- arm images
|
||||
- Automated build & tagging.
|
||||
- Arm images.
|
||||
|
||||
### Security
|
||||
|
||||
- updated dependencies
|
||||
- Updated dependencies.
|
||||
|
11
Dockerfile
11
Dockerfile
@@ -1,9 +1,10 @@
|
||||
FROM node:14-alpine
|
||||
FROM oven/bun:1-alpine as base
|
||||
|
||||
WORKDIR /app
|
||||
COPY package.json bun.lockb /app/
|
||||
RUN bun install --production --frozen-lockfile
|
||||
|
||||
ADD ./package.json yarn.lock ./
|
||||
RUN yarn
|
||||
ADD ./script.js ./
|
||||
COPY . .
|
||||
|
||||
CMD ["node", "script.js"]
|
||||
STOPSIGNAL SIGTERM
|
||||
CMD ["bun", "."]
|
||||
|
32
README.md
32
README.md
@@ -1,7 +1,8 @@
|
||||
# Docker DDNS Cloudflare
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
## Features 🌈
|
||||
|
||||
@@ -11,19 +12,18 @@
|
||||
|
||||
## Quickstart 🚀
|
||||
|
||||
1. Get your api token [here](https://dash.cloudflare.com/profile/api-tokens) (Top right -> My Profile -> API Tokens)
|
||||
1. Get your API Token [here](https://dash.cloudflare.com/profile/api-tokens) (Top right -> My Profile -> API Tokens)
|
||||
|
||||
Click create token. You can then use the Edit DNS Zone template. Give it a name.
|
||||
|
||||

|
||||
|
||||
2. Create an `.env` file:
|
||||
|
||||
```bash
|
||||
EMAIL=my@mail.com
|
||||
KEY=my_api_key
|
||||
TOKEN=mytoken
|
||||
|
||||
ZONE=example.org
|
||||
DNS_RECORD=some.example.org
|
||||
PROXIED=false
|
||||
```
|
||||
|
||||
3. Run the container
|
||||
@@ -32,12 +32,6 @@ DNS_RECORD=some.example.org
|
||||
docker run -d --name ddns --restart always --env-file .env cupcakearmy/ddns-cloudflare
|
||||
```
|
||||
|
||||
To check logs:
|
||||
|
||||
```bash
|
||||
docker logs ddns
|
||||
```
|
||||
|
||||
### Docker-Copmose
|
||||
|
||||
With docker-compose:
|
||||
@@ -49,6 +43,18 @@ cp .sample.env .env
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
## ENV Reference
|
||||
|
||||
| Env | Default | Description |
|
||||
| ------------ | ------------------------ | ----------------------------------------------------------------------------------------------------------- |
|
||||
| `TOKEN` | | API Token. |
|
||||
| `ZONE` | | Cloudflare zone where your domain is. |
|
||||
| `DNS_RECORD` | | The actual DNS record that should be updated. |
|
||||
| `PROXIED` | `true` | Whether the record is proxied by CloudFlare or not. |
|
||||
| `CRON` | `*/5 * * * *` | Frequency of updates. The [following syntax](https://croner.56k.guru/usage/pattern/) is supported |
|
||||
| `RESOLVER` | `https://api.ipify.org/` | The endpoint used to determine your public ip. |
|
||||
| `LOG_LEVEL` | `info` | Log level to run at. See [winston](https://github.com/winstonjs/winston#logging-levels) for possible values |
|
||||
|
||||
## Customize
|
||||
|
||||
### Custom CRON
|
||||
|
@@ -1,4 +1,4 @@
|
||||
version: '3.7'
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
ddns:
|
||||
|
20
package.json
20
package.json
@@ -1,14 +1,20 @@
|
||||
{
|
||||
"version": "1.4.1",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
"scripts": {
|
||||
"docker:build": "docker build -t cupcakearmy/ddns-cloudflare:lastest .",
|
||||
"docker:push": "docker push cupcakearmy/ddns-cloudflare:lastest",
|
||||
"docker:publish": "yarn run docker:build && yarn run docker:push"
|
||||
"check": "tsc --noEmit",
|
||||
"dev": "bun --watch ./src/index.ts",
|
||||
"start": "bun ./src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.21.1",
|
||||
"cloudflare": "^2.7.0",
|
||||
"cron": "^1.8.2",
|
||||
"dotenv": "^8.2.0"
|
||||
"axios": "^1.6.7",
|
||||
"croner": "^8.0.1",
|
||||
"winston": "^3.11.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bun": "^1.0.7",
|
||||
"typescript": "^5.3.3"
|
||||
}
|
||||
}
|
||||
|
69
script.js
69
script.js
@@ -1,69 +0,0 @@
|
||||
const { readFileSync, writeFileSync, existsSync } = require('fs')
|
||||
const Cloudflare = require('cloudflare')
|
||||
const Axios = require('axios')
|
||||
const { CronJob } = require('cron')
|
||||
|
||||
require('dotenv').config()
|
||||
|
||||
const { EMAIL, KEY, ZONE, DNS_RECORD, CRON, RESOLVER } = process.env
|
||||
|
||||
const cf = Cloudflare({
|
||||
email: EMAIL,
|
||||
key: KEY,
|
||||
})
|
||||
|
||||
function log(message) {
|
||||
const timestamp = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')
|
||||
console.log(timestamp + '\t' + message)
|
||||
}
|
||||
|
||||
async function getCurrentIp() {
|
||||
const { data } = await Axios({
|
||||
url: RESOLVER || 'https://api.ipify.org/',
|
||||
method: 'GET',
|
||||
})
|
||||
return data
|
||||
}
|
||||
|
||||
async function checkIfUpdateIsRequired() {
|
||||
const LOG = './ip.log'
|
||||
const current = await getCurrentIp()
|
||||
const saved = existsSync(LOG) ? readFileSync(LOG, 'utf-8') : null
|
||||
|
||||
if (current === saved) return false
|
||||
else {
|
||||
writeFileSync(LOG, current, { encoding: 'utf-8' })
|
||||
return current
|
||||
}
|
||||
}
|
||||
|
||||
async function update(newIP) {
|
||||
const { result: zones } = await cf.zones.browse({ name: ZONE })
|
||||
if (!zones.length) throw new Error(`No ZONE "${ZONE}" found`)
|
||||
|
||||
const zoneId = zones[0].id
|
||||
const { result: records } = await cf.dnsRecords.browse(zoneId, { name: DNS_RECORD, type: 'A' })
|
||||
|
||||
const recordAlreadyExists = records.length
|
||||
if (recordAlreadyExists) {
|
||||
const { id: recordId, ...rest } = records[0]
|
||||
await cf.dnsRecords.edit(zoneId, recordId, { ...rest, content: newIP })
|
||||
log(`Updated:\t${DNS_RECORD} → ${newIP} `)
|
||||
} else {
|
||||
await cf.dnsRecords.add(zoneId, {
|
||||
name: DNS_RECORD,
|
||||
type: 'A',
|
||||
content: newIP,
|
||||
})
|
||||
log(`Created:\t${DNS_RECORD} → ${newIP} `)
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const changed = await checkIfUpdateIsRequired()
|
||||
log(`Running. Update required: ${!!changed}`)
|
||||
if (changed) await update(changed).catch((e) => console.error(e.message))
|
||||
}
|
||||
|
||||
new CronJob(CRON || '*/5 * * * *', main, null, true, null, null, true)
|
||||
log('Started service.')
|
1
src/cache.ts
Normal file
1
src/cache.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const Cache = new Map<'ip' | 'zone', string>()
|
128
src/cloudflare.ts
Normal file
128
src/cloudflare.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import axios from 'axios'
|
||||
|
||||
import { Cache } from './cache.js'
|
||||
import { Config } from './config.js'
|
||||
import { logger } from './logger.js'
|
||||
|
||||
type DNSRecord = {
|
||||
id: string
|
||||
zone_id: string
|
||||
zone_name: string
|
||||
name: string
|
||||
type: string
|
||||
content: string
|
||||
proxiable: boolean
|
||||
proxied: boolean
|
||||
ttl: number // 1 means automatic
|
||||
locked: boolean
|
||||
}
|
||||
|
||||
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: {
|
||||
Authorization: `Bearer ${Config.auth.token}`,
|
||||
},
|
||||
})
|
||||
|
||||
export const API = {
|
||||
zones: {
|
||||
async findByName(name: string): Promise<string | null> {
|
||||
try {
|
||||
const { data } = await Base({
|
||||
url: '/zones',
|
||||
params: {
|
||||
name,
|
||||
},
|
||||
})
|
||||
return data.result[0].id
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
},
|
||||
},
|
||||
records: {
|
||||
async create(zoneId: string, zone: DNSRecordCreate): Promise<void> {
|
||||
await Base({
|
||||
url: `/zones/${zoneId}/dns_records`,
|
||||
method: 'POST',
|
||||
data: zone,
|
||||
})
|
||||
},
|
||||
async remove(zoneId: string, recordId: string): Promise<void> {
|
||||
await Base({
|
||||
url: `/zones/${zoneId}/dns_records/${recordId}`,
|
||||
method: 'DELETE',
|
||||
})
|
||||
},
|
||||
async patch(zoneId: string, recordId: string, data: DNSRecordPatch): Promise<void> {
|
||||
await Base({
|
||||
url: `/zones/${zoneId}/dns_records/${recordId}`,
|
||||
method: 'PATCH',
|
||||
data,
|
||||
})
|
||||
},
|
||||
async find(zoneId: string): Promise<DNSRecord[]> {
|
||||
const { data } = await Base({
|
||||
url: `/zones/${zoneId}/dns_records`,
|
||||
params: {
|
||||
type: 'A',
|
||||
name: Config.dns.record,
|
||||
},
|
||||
})
|
||||
return data.result as DNSRecord[]
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
export async function update(ip: string) {
|
||||
// Find zone
|
||||
if (!Cache.has('zone')) {
|
||||
l.debug('Fetching zone')
|
||||
const zone = await API.zones.findByName(Config.dns.zone)
|
||||
if (!zone) {
|
||||
l.error(`Zone "${Config.dns.zone}" not found`)
|
||||
process.exit(1)
|
||||
}
|
||||
Cache.set('zone', zone)
|
||||
}
|
||||
|
||||
const zoneId = Cache.get('zone')!
|
||||
l.debug(`Zone ID: ${zoneId}`)
|
||||
|
||||
// Set record
|
||||
const records = await API.records.find(zoneId)
|
||||
|
||||
l.debug('Updating record', ip)
|
||||
|
||||
switch (records.length) {
|
||||
case 0:
|
||||
// Create DNS Record
|
||||
l.debug('Creating DNS record')
|
||||
await API.records.create(zoneId, {
|
||||
content: ip,
|
||||
name: Config.dns.record,
|
||||
proxied: Config.dns.proxied,
|
||||
ttl: 1,
|
||||
type: 'A',
|
||||
})
|
||||
return
|
||||
case 1:
|
||||
// Only one record, thats fine
|
||||
break
|
||||
default:
|
||||
// More than one record, delete all but the first
|
||||
l.debug('Deleting other DNS records')
|
||||
for (const record of records.slice(1)) {
|
||||
await API.records.remove(zoneId, record.id)
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
// Update the remaining record
|
||||
await API.records.patch(zoneId, records[0]!.id, { content: ip, proxied: Config.dns.proxied })
|
||||
}
|
55
src/config.ts
Normal file
55
src/config.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
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
|
||||
function getEnv<T>(
|
||||
key: string,
|
||||
fallback: T,
|
||||
parse?: (value: string) => T,
|
||||
validator?: (s: string | T) => boolean
|
||||
): T | string {
|
||||
const value = process.env[key]
|
||||
const parsed = value === undefined ? fallback : parse ? parse(value) : value
|
||||
if (validator && !validator(parsed)) {
|
||||
console.error(`Invalid or missing value for ${key}: ${value}`)
|
||||
process.exit(1)
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
function parseBoolean(value: string): boolean {
|
||||
value = value.toLowerCase()
|
||||
const truthy = ['true', 'yes', '1', 'y']
|
||||
return truthy.includes(value)
|
||||
}
|
||||
|
||||
function isPresent(s: string): boolean {
|
||||
return s.length > 0
|
||||
}
|
||||
|
||||
export const Config = {
|
||||
version: pkg.version,
|
||||
logging: {
|
||||
level: getEnv('LOG_LEVEL', 'info'),
|
||||
},
|
||||
auth: {
|
||||
token: getEnv('TOKEN', '', undefined, isPresent),
|
||||
},
|
||||
dns: {
|
||||
zone: getEnv('ZONE', '', undefined, isPresent),
|
||||
record: getEnv('DNS_RECORD', '', undefined, isPresent),
|
||||
proxied: getEnv('PROXIED', false, parseBoolean),
|
||||
},
|
||||
runner: {
|
||||
cron: getEnv('CRON', '*/5 * * * *', undefined, (s) => {
|
||||
try {
|
||||
new Cron(s)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}),
|
||||
resolver: getEnv('RESOLVER', 'https://api.ipify.org'),
|
||||
},
|
||||
}
|
28
src/index.ts
Normal file
28
src/index.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
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 = 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.')
|
||||
cron.stop()
|
||||
process.exit(0)
|
||||
}
|
||||
process.on('SIGINT', terminate)
|
||||
process.on('SIGTERM', terminate)
|
||||
}
|
||||
|
||||
main()
|
22
src/ip.ts
Normal file
22
src/ip.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import axios from 'axios'
|
||||
|
||||
import { Cache } from './cache.js'
|
||||
import { Config } from './config.js'
|
||||
|
||||
export async function getCurrentIp(): Promise<string> {
|
||||
const { data } = await axios({
|
||||
url: Config.runner.resolver,
|
||||
method: 'GET',
|
||||
})
|
||||
return data as string
|
||||
}
|
||||
|
||||
export function checkIfUpdateIsRequired(newIP: string): boolean {
|
||||
// Check if IP has changed.
|
||||
const current = Cache.get('ip')
|
||||
if (newIP !== current) {
|
||||
Cache.set('ip', newIP)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
12
src/logger.ts
Normal file
12
src/logger.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import winston from 'winston'
|
||||
|
||||
import { Config } from './config.js'
|
||||
|
||||
export const logger = winston.createLogger({
|
||||
level: Config.logging.level,
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
format: winston.format.combine(winston.format.timestamp(), winston.format.colorize(), winston.format.simple()),
|
||||
}),
|
||||
],
|
||||
})
|
20
src/runner.ts
Normal file
20
src/runner.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
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)
|
||||
l.info(`Running. Update required: ${!!changed}`)
|
||||
if (changed) {
|
||||
try {
|
||||
await update(ip)
|
||||
l.info('Successfully updated DNS record')
|
||||
} catch (e) {
|
||||
l.error(e)
|
||||
l.error('Failed to update DNS record')
|
||||
}
|
||||
}
|
||||
}
|
12
tsconfig.json
Normal file
12
tsconfig.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"rootDir": "./src",
|
||||
"moduleResolution": "Bundler",
|
||||
"noEmit": true,
|
||||
"esModuleInterop": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
202
yarn.lock
202
yarn.lock
@@ -1,202 +0,0 @@
|
||||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||
# yarn lockfile v1
|
||||
|
||||
|
||||
agent-base@^4.3.0:
|
||||
version "4.3.0"
|
||||
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.3.0.tgz#8165f01c436009bccad0b1d122f05ed770efc6ee"
|
||||
integrity sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg==
|
||||
dependencies:
|
||||
es6-promisify "^5.0.0"
|
||||
|
||||
autocreate@^1.1.0:
|
||||
version "1.2.0"
|
||||
resolved "https://registry.yarnpkg.com/autocreate/-/autocreate-1.2.0.tgz#522167992c4172c15479e5f88f3486a452a40cba"
|
||||
integrity sha1-UiFnmSxBcsFUeeX4jzSGpFKkDLo=
|
||||
|
||||
axios@^0.21.1:
|
||||
version "0.21.1"
|
||||
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
|
||||
integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==
|
||||
dependencies:
|
||||
follow-redirects "^1.10.0"
|
||||
|
||||
capture-stack-trace@^1.0.0:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/capture-stack-trace/-/capture-stack-trace-1.0.1.tgz#a6c0bbe1f38f3aa0b92238ecb6ff42c344d4135d"
|
||||
integrity sha512-mYQLZnx5Qt1JgB1WEiMCf2647plpGeQ2NMR/5L0HNZzGQo4fuSPnK+wjfPnKZV0aiJDgzmWqqkV/g7JD+DW0qw==
|
||||
|
||||
cloudflare@^2.7.0:
|
||||
version "2.7.0"
|
||||
resolved "https://registry.yarnpkg.com/cloudflare/-/cloudflare-2.7.0.tgz#ff9249a63ff9b662bd863da21e965c8f82f76012"
|
||||
integrity sha512-yhroBpn2VBczFwiRLpyUc431XiWE+xNs8YvtjAsj1vEA1pVwhpje6BzgLW5iZbulmCuPX48lvX8HizeMWk713g==
|
||||
dependencies:
|
||||
autocreate "^1.1.0"
|
||||
es-class "^2.1.1"
|
||||
got "^6.3.0"
|
||||
https-proxy-agent "^2.1.1"
|
||||
object-assign "^4.1.0"
|
||||
should-proxy "^1.0.4"
|
||||
url-pattern "^1.0.3"
|
||||
|
||||
create-error-class@^3.0.0:
|
||||
version "3.0.2"
|
||||
resolved "https://registry.yarnpkg.com/create-error-class/-/create-error-class-3.0.2.tgz#06be7abef947a3f14a30fd610671d401bca8b7b6"
|
||||
integrity sha1-Br56vvlHo/FKMP1hBnHUAbyot7Y=
|
||||
dependencies:
|
||||
capture-stack-trace "^1.0.0"
|
||||
|
||||
cron@^1.8.2:
|
||||
version "1.8.2"
|
||||
resolved "https://registry.yarnpkg.com/cron/-/cron-1.8.2.tgz#4ac5e3c55ba8c163d84f3407bde94632da8370ce"
|
||||
integrity sha512-Gk2c4y6xKEO8FSAUTklqtfSr7oTq0CiPQeLBG5Fl0qoXpZyMcj1SG59YL+hqq04bu6/IuEA7lMkYDAplQNKkyg==
|
||||
dependencies:
|
||||
moment-timezone "^0.5.x"
|
||||
|
||||
debug@^3.1.0:
|
||||
version "3.2.7"
|
||||
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.7.tgz#72580b7e9145fb39b6676f9c5e5fb100b934179a"
|
||||
integrity sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==
|
||||
dependencies:
|
||||
ms "^2.1.1"
|
||||
|
||||
dotenv@^8.2.0:
|
||||
version "8.5.1"
|
||||
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.5.1.tgz#e3a4c7862daba51b92bce0da5c349f11faa28663"
|
||||
integrity sha512-qC1FbhCH7UH7B+BcRNUDhAk04d/n+tnGGB1ctwndZkVFeehYJOn39pRWWzmdzpFqImyX1KB8tO0DCHLf8yRaYQ==
|
||||
|
||||
duplexer3@^0.1.4:
|
||||
version "0.1.4"
|
||||
resolved "https://registry.yarnpkg.com/duplexer3/-/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2"
|
||||
integrity sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=
|
||||
|
||||
es-class@^2.1.1:
|
||||
version "2.1.1"
|
||||
resolved "https://registry.yarnpkg.com/es-class/-/es-class-2.1.1.tgz#6ec2243b5a1e3581c0b7eecee0130c9c0d6fb2b7"
|
||||
integrity sha1-bsIkO1oeNYHAt+7O4BMMnA1vsrc=
|
||||
|
||||
es6-promise@^4.0.3:
|
||||
version "4.2.8"
|
||||
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a"
|
||||
integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w==
|
||||
|
||||
es6-promisify@^5.0.0:
|
||||
version "5.0.0"
|
||||
resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203"
|
||||
integrity sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM=
|
||||
dependencies:
|
||||
es6-promise "^4.0.3"
|
||||
|
||||
follow-redirects@^1.10.0:
|
||||
version "1.14.0"
|
||||
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.0.tgz#f5d260f95c5f8c105894491feee5dc8993b402fe"
|
||||
integrity sha512-0vRwd7RKQBTt+mgu87mtYeofLFZpTas2S9zY+jIeuLJMNvudIgF52nr19q40HOwH5RrhWIPuj9puybzSJiRrVg==
|
||||
|
||||
get-stream@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-3.0.0.tgz#8e943d1358dc37555054ecbe2edb05aa174ede14"
|
||||
integrity sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ=
|
||||
|
||||
got@^6.3.0:
|
||||
version "6.7.1"
|
||||
resolved "https://registry.yarnpkg.com/got/-/got-6.7.1.tgz#240cd05785a9a18e561dc1b44b41c763ef1e8db0"
|
||||
integrity sha1-JAzQV4WpoY5WHcG0S0HHY+8ejbA=
|
||||
dependencies:
|
||||
create-error-class "^3.0.0"
|
||||
duplexer3 "^0.1.4"
|
||||
get-stream "^3.0.0"
|
||||
is-redirect "^1.0.0"
|
||||
is-retry-allowed "^1.0.0"
|
||||
is-stream "^1.0.0"
|
||||
lowercase-keys "^1.0.0"
|
||||
safe-buffer "^5.0.1"
|
||||
timed-out "^4.0.0"
|
||||
unzip-response "^2.0.1"
|
||||
url-parse-lax "^1.0.0"
|
||||
|
||||
https-proxy-agent@^2.1.1:
|
||||
version "2.2.4"
|
||||
resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-2.2.4.tgz#4ee7a737abd92678a293d9b34a1af4d0d08c787b"
|
||||
integrity sha512-OmvfoQ53WLjtA9HeYP9RNrWMJzzAz1JGaSFr1nijg0PVR1JaD/xbJq1mdEIIlxGpXp9eSe/O2LgU9DJmTPd0Eg==
|
||||
dependencies:
|
||||
agent-base "^4.3.0"
|
||||
debug "^3.1.0"
|
||||
|
||||
is-redirect@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/is-redirect/-/is-redirect-1.0.0.tgz#1d03dded53bd8db0f30c26e4f95d36fc7c87dc24"
|
||||
integrity sha1-HQPd7VO9jbDzDCbk+V02/HyH3CQ=
|
||||
|
||||
is-retry-allowed@^1.0.0:
|
||||
version "1.2.0"
|
||||
resolved "https://registry.yarnpkg.com/is-retry-allowed/-/is-retry-allowed-1.2.0.tgz#d778488bd0a4666a3be8a1482b9f2baafedea8b4"
|
||||
integrity sha512-RUbUeKwvm3XG2VYamhJL1xFktgjvPzL0Hq8C+6yrWIswDy3BIXGqCxhxkc30N9jqK311gVU137K8Ei55/zVJRg==
|
||||
|
||||
is-stream@^1.0.0:
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"
|
||||
integrity sha1-EtSj3U5o4Lec6428hBc66A2RykQ=
|
||||
|
||||
lowercase-keys@^1.0.0:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-1.0.1.tgz#6f9e30b47084d971a7c820ff15a6c5167b74c26f"
|
||||
integrity sha512-G2Lj61tXDnVFFOi8VZds+SoQjtQC3dgokKdDG2mTm1tx4m50NUHBOZSBwQQHyy0V12A0JTG4icfZQH+xPyh8VA==
|
||||
|
||||
moment-timezone@^0.5.x:
|
||||
version "0.5.33"
|
||||
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.33.tgz#b252fd6bb57f341c9b59a5ab61a8e51a73bbd22c"
|
||||
integrity sha512-PTc2vcT8K9J5/9rDEPe5czSIKgLoGsH8UNpA4qZTVw0Vd/Uz19geE9abbIOQKaAQFcnQ3v5YEXrbSc5BpshH+w==
|
||||
dependencies:
|
||||
moment ">= 2.9.0"
|
||||
|
||||
"moment@>= 2.9.0":
|
||||
version "2.29.1"
|
||||
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3"
|
||||
integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==
|
||||
|
||||
ms@^2.1.1:
|
||||
version "2.1.3"
|
||||
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
|
||||
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
|
||||
|
||||
object-assign@^4.1.0:
|
||||
version "4.1.1"
|
||||
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
|
||||
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
|
||||
|
||||
prepend-http@^1.0.1:
|
||||
version "1.0.4"
|
||||
resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-1.0.4.tgz#d4f4562b0ce3696e41ac52d0e002e57a635dc6dc"
|
||||
integrity sha1-1PRWKwzjaW5BrFLQ4ALlemNdxtw=
|
||||
|
||||
safe-buffer@^5.0.1:
|
||||
version "5.2.1"
|
||||
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
|
||||
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
|
||||
|
||||
should-proxy@^1.0.4:
|
||||
version "1.0.4"
|
||||
resolved "https://registry.yarnpkg.com/should-proxy/-/should-proxy-1.0.4.tgz#c805a501abf69539600634809e62fbf238ba35e4"
|
||||
integrity sha1-yAWlAav2lTlgBjSAnmL78ji6NeQ=
|
||||
|
||||
timed-out@^4.0.0:
|
||||
version "4.0.1"
|
||||
resolved "https://registry.yarnpkg.com/timed-out/-/timed-out-4.0.1.tgz#f32eacac5a175bea25d7fab565ab3ed8741ef56f"
|
||||
integrity sha1-8y6srFoXW+ol1/q1Zas+2HQe9W8=
|
||||
|
||||
unzip-response@^2.0.1:
|
||||
version "2.0.1"
|
||||
resolved "https://registry.yarnpkg.com/unzip-response/-/unzip-response-2.0.1.tgz#d2f0f737d16b0615e72a6935ed04214572d56f97"
|
||||
integrity sha1-0vD3N9FrBhXnKmk17QQhRXLVb5c=
|
||||
|
||||
url-parse-lax@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/url-parse-lax/-/url-parse-lax-1.0.0.tgz#7af8f303645e9bd79a272e7a14ac68bc0609da73"
|
||||
integrity sha1-evjzA2Rem9eaJy56FKxovAYJ2nM=
|
||||
dependencies:
|
||||
prepend-http "^1.0.1"
|
||||
|
||||
url-pattern@^1.0.3:
|
||||
version "1.0.3"
|
||||
resolved "https://registry.yarnpkg.com/url-pattern/-/url-pattern-1.0.3.tgz#0409292471b24f23c50d65a47931793d2b5acfc1"
|
||||
integrity sha1-BAkpJHGyTyPFDWWkeTF5PStaz8E=
|
Reference in New Issue
Block a user