-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- check if external IP has been changed by ISP using miniupnpc - remember new IP and send it to DNS provider - add some description
- Loading branch information
Showing
6 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/net/flyingelectrons/dnsupdater/service/ExternalIpRetrieverService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package net.flyingelectrons.dnsupdater.service | ||
|
||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class ExternalIpRetrieverService(private val processExecutorService: ProcessExecutorService) { | ||
|
||
fun getExternalIp(): String { | ||
val execute = processExecutorService.execute("external-ip") | ||
if (execute.isEmpty()) { | ||
throw java.lang.IllegalArgumentException("external IP is empty") | ||
} | ||
return execute | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/kotlin/net/flyingelectrons/dnsupdater/service/ProcessExecutorService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package net.flyingelectrons.dnsupdater.service | ||
|
||
import org.springframework.stereotype.Service | ||
import java.io.BufferedReader | ||
import java.io.InputStream | ||
import java.io.InputStreamReader | ||
import java.util.concurrent.Executors | ||
import java.util.function.Consumer | ||
|
||
@Service | ||
class ProcessExecutorService { | ||
|
||
fun execute(unixCommand: String): String { | ||
var out = "" | ||
val process: Process = Runtime.getRuntime() | ||
.exec(unixCommand) | ||
val streamGobbler = StreamGobbler( | ||
process.inputStream, fun(x: String?) { | ||
println(x) | ||
out = x.orEmpty() | ||
} | ||
) | ||
Executors.newSingleThreadExecutor().submit(streamGobbler) | ||
val exitCode = process.waitFor() | ||
assert(exitCode == 0) | ||
return out | ||
} | ||
} | ||
|
||
private class StreamGobbler(private val inputStream: InputStream, consumer: Consumer<String>) : | ||
Runnable { | ||
private val consumer: Consumer<String> | ||
override fun run() { | ||
BufferedReader(InputStreamReader(inputStream)).lines() | ||
.forEach(consumer) | ||
} | ||
|
||
init { | ||
this.consumer = consumer | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/net/flyingelectrons/dnsupdater/service/UpdateDnsSchedulingService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package net.flyingelectrons.dnsupdater.service | ||
|
||
import mu.KotlinLogging | ||
import net.flyingelectrons.dnsupdater.gateway.GandiClient | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class UpdateDnsSchedulingService @Autowired constructor( | ||
private val externalIpRetrieverService: ExternalIpRetrieverService, | ||
private val gandiClient: GandiClient, | ||
) { | ||
|
||
private var savedIp = "noIpKnownYet" | ||
|
||
@Scheduled(cron = "*/3 * * * * *") | ||
fun updateDns() { | ||
val externalIp: String = externalIpRetrieverService.getExternalIp() | ||
LOGGER.info("running scheduler. get external ip: $externalIp") | ||
if (externalIp != savedIp) { | ||
val ipWithfqdn = gandiClient.doUpdateIpWithfqdn(externalIp, "myfqdn") | ||
LOGGER.info("Updating myfqdn") | ||
LOGGER.info(ipWithfqdn.body) | ||
savedIp = externalIp | ||
} | ||
} | ||
|
||
companion object { | ||
private val LOGGER = KotlinLogging.logger { } | ||
} | ||
} |