Skip to content

Commit

Permalink
Optimize TTL and Number of mutating requests to API
Browse files Browse the repository at this point in the history
  • Loading branch information
IRus committed Jan 13, 2024
1 parent 6b40558 commit 937c4f6
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 46 deletions.
13 changes: 9 additions & 4 deletions cloudflare-worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@

export default {
fetch(request, env, ctx) {
return new Response(JSON.stringify(
return new Response(
JSON.stringify(
{
ip: request.headers.get('cf-connecting-ip'),
},
),
{
ip: request.headers.get('cf-connecting-ip'),
},
))
headers: {'content-type': 'application/json'},
}
)
}
}
33 changes: 8 additions & 25 deletions src/main/kotlin/io/heapy/ddns/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package io.heapy.ddns
import io.heapy.ddns.dns_clients.CloudflareDnsClient
import io.heapy.ddns.dns_clients.DigitalOceanDnsClient
import io.heapy.ddns.dns_clients.DnsClient
import io.heapy.ddns.ip_provider.IpProvider
import io.heapy.ddns.ip_provider.ServerIpProvider
import io.heapy.ddns.notifiers.Notifier
import io.heapy.ddns.notifiers.TelegramNotifier
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.cio.CIO
import io.ktor.serialization.kotlinx.json.json
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.post
import kotlinx.coroutines.delay
import kotlinx.serialization.Serializable
import org.slf4j.LoggerFactory
import java.time.ZonedDateTime
import kotlin.concurrent.thread
Expand Down Expand Up @@ -75,6 +74,7 @@ open class ClientFactory(
?: error("DIGITALOCEAN_DOMAIN_NAME is not set"),
subdomain = config["DIGITALOCEAN_SUBDOMAIN"]
?: error("DIGITALOCEAN_SUBDOMAIN is not set"),
ttl = configuration.checkPeriod.inWholeSeconds,
)
}

Expand All @@ -93,6 +93,7 @@ open class ClientFactory(
?: error("CLOUDFLARE_TOKEN is not set"),
domainName = config["CLOUDFLARE_DOMAIN_NAME"]
?: error("CLOUDFLARE_DOMAIN_NAME is not set"),
ttl = configuration.checkPeriod.inWholeSeconds,
)
}

Expand Down Expand Up @@ -178,33 +179,15 @@ class SimpleUpdater(
IP = newIP
log.info("IP changed to $IP")
dnsClients.forEach {
it.createOrUpdateRecord(IP)
val oldIp = it.createOrUpdateRecord(IP)
if (oldIp != IP) {
notifier?.notify(IP)
}
}
notifier?.notify(IP)
}
}

companion object {
private val log = LoggerFactory.getLogger(SimpleUpdater::class.java)
}
}

interface IpProvider {
suspend fun getIp(): String
}

class ServerIpProvider(
private val httpClient: HttpClient,
private val serverUrl: String,
) : IpProvider {
@Serializable
data class Response(
val ip: String,
)

override suspend fun getIp(): String {
return httpClient.post(serverUrl)
.body<Response>()
.ip
}
}
35 changes: 23 additions & 12 deletions src/main/kotlin/io/heapy/ddns/dns_clients/CloudflareDnsClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,39 @@ class CloudflareDnsClient(
private val httpClient: HttpClient,
private val configuration: Configuration,
) : DnsClient {
override suspend fun createOrUpdateRecord(ip: String) {
override suspend fun createOrUpdateRecord(
ip: String,
): String? {
val record = getRecord(
name = configuration.domainName,
zoneId = configuration.zoneId,
token = configuration.token,
)
if (record != null) {
log.info("Updating record with id=${record.id}")
updateRecord(
id = record.id,
ip = ip,
name = configuration.domainName,
zoneId = configuration.zoneId,
token = configuration.token,
)
return if (record != null) {
if (record.content == ip) {
log.info("Record already exists and with up to date ip: $ip")
} else {
log.info("Updating record with id=${record.id}")
updateRecord(
id = record.id,
ip = ip,
name = configuration.domainName,
zoneId = configuration.zoneId,
token = configuration.token,
ttl = configuration.ttl,
)
}
record.content
} else {
log.info("Creating record with ip=$ip")
createRecord(
ip = ip,
name = configuration.domainName,
zoneId = configuration.zoneId,
token = configuration.token,
ttl = configuration.ttl,
)
null
}
}

Expand All @@ -44,7 +54,7 @@ class CloudflareDnsClient(
name: String,
zoneId: String,
token: String,
ttl: Long = 300,
ttl: Long,
proxied: Boolean = false,
recordType: String = "A",
) {
Expand Down Expand Up @@ -74,7 +84,7 @@ class CloudflareDnsClient(
name: String,
zoneId: String,
token: String,
ttl: Long = 300,
ttl: Long,
proxied: Boolean = false,
recordType: String = "A",
) {
Expand Down Expand Up @@ -122,6 +132,7 @@ class CloudflareDnsClient(
val zoneId: String,
val token: String,
val domainName: String,
val ttl: Long,
)

@Serializable
Expand Down
15 changes: 11 additions & 4 deletions src/main/kotlin/io/heapy/ddns/dns_clients/DigitalOceanDnsClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ class DigitalOceanDnsClient(
) : DnsClient {
override suspend fun createOrUpdateRecord(
ip: String,
) {
): String? {
val records = getDomainRecords()
val record = records.domain_records
.find { it.name == configuration.subdomain }

if (record == null) {
return if (record == null) {
log.info("Creating new record")
createDomainRecord(ip)
null
} else {
log.info("Record already exists, updating $record")
updateDomainRecord(record.id, ip)
if (record.data == ip) {
log.info("Record already exists and with up to date ip: $ip")
} else {
log.info("Record already exists, updating $record")
updateDomainRecord(record.id, ip)
}
record.data
}
}

Expand Down Expand Up @@ -108,6 +114,7 @@ class DigitalOceanDnsClient(
val token: String,
val domainName: String,
val subdomain: String,
val ttl: Long,
)

private val doUrl: String
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/heapy/ddns/dns_clients/DnsClient.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.heapy.ddns.dns_clients

interface DnsClient {
suspend fun createOrUpdateRecord(ip: String)
suspend fun createOrUpdateRecord(ip: String): String?
}
5 changes: 5 additions & 0 deletions src/main/kotlin/io/heapy/ddns/ip_provider/IpProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.heapy.ddns.ip_provider

interface IpProvider {
suspend fun getIp(): String
}
22 changes: 22 additions & 0 deletions src/main/kotlin/io/heapy/ddns/ip_provider/ServerIpProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.heapy.ddns.ip_provider

import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import kotlinx.serialization.Serializable

class ServerIpProvider(
private val httpClient: HttpClient,
private val serverUrl: String,
) : IpProvider {
@Serializable
data class Response(
val ip: String,
)

override suspend fun getIp(): String {
return httpClient.post(serverUrl)
.body<Response>()
.ip
}
}

0 comments on commit 937c4f6

Please sign in to comment.