diff --git a/Dockerfile b/Dockerfile index 3f0eec1..f79fb50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,11 @@ -FROM golang:1.22.1-alpine3.19 as build +FROM golang:1.23.1-alpine3.20 as build ARG OS ARG ARCH COPY . /build/ WORKDIR /build RUN go mod download && go build -o ncddns -FROM alpine:3.19 +FROM alpine:3.20 ARG VERSION ARG user=ncddns ARG group=ncddns diff --git a/docker-build.sh b/docker-build.sh index bda34f1..2b16c13 100644 --- a/docker-build.sh +++ b/docker-build.sh @@ -1,5 +1,12 @@ #!/ussr/bin/env bash +if [ $# -ne 1 ]; then + echo "Exactly one argument required" + echo "bash docker-build.sh VERSION" + echo " e.g. bash docker-build.sh 1.3.0" + exit 1 +fi + docker buildx create --use --name mybuild -docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t linuxshots/namecheap-ddns:1.2.0 --push --pull . +docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t linuxshots/namecheap-ddns:$1 --push --pull . docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t linuxshots/namecheap-ddns:latest --push --pull . \ No newline at end of file diff --git a/go.mod b/go.mod index 0433ca3..3bf3e92 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/navilg/namecheap-ddns-docker -go 1.22 +go 1.23 diff --git a/main.go b/main.go index b0fce75..29c8718 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ func main() { domain := flag.String("domain", "", "Domain name e.g. example.com") host := flag.String("host", "", "Subdomain or hostname e.g. www") password := flag.String("password", "", "Dynamic DNS Password from Namecheap") + // iPCacheTimeOutSeconds := flag.Int("ip-cache-timeout", 86400, "IP cache timeout in seconds.") flag.Parse() if *domain == "" || *host == "" || *password == "" { diff --git a/model.go b/model.go index 287a967..0122abc 100644 --- a/model.go +++ b/model.go @@ -15,8 +15,10 @@ func (err *CustomError) Error() string { } var ( - version string = "1.2.0-go1.22" + version string = "1.3.0-go1.23" daemon_poll_time time.Duration = 1 * time.Minute // Time in minute gitrepo string = "https://github.com/navilg/namecheap-ddns-docker" httpTimeout time.Duration = 30 * time.Second + expiryTime float64 = 86400 // Ip env timeout in seconds (24hrs.) + // expiryTime float64 = 600 ) diff --git a/updaterecord.go b/updaterecord.go index f8834a7..2a254cf 100644 --- a/updaterecord.go +++ b/updaterecord.go @@ -33,9 +33,28 @@ func updateRecord(domain, host, password string) { } currentIp := os.Getenv("NC_PUB_IP") + lastIpUpdatedStr := os.Getenv("NC_PUB_IP_TIME") + var lastIpUpdatedDuration float64 - if currentIp == pubIp { - DDNSLogger(InformationLog, host, domain, "DNS record is same as current IP. "+pubIp) + fmt.Println(lastIpUpdatedStr) + lastIpUpdated, err := time.Parse("2006-01-02 15:04:05", lastIpUpdatedStr) + if err != nil { + DDNSLogger(WarningLog, host, domain, "Not able to fetch last IP updated time. "+err.Error()) + lastIpUpdatedDuration = 0 + } else { + currentTime := time.Now().Format("2006-01-02 15:04:05") + currentTimeF, err := time.Parse("2006-01-02 15:04:05", currentTime) + if err != nil { + DDNSLogger(WarningLog, host, domain, "Not able to fetch last IP updated time. "+err.Error()) + lastIpUpdatedDuration = 0 + } else { + lastIpUpdatedDuration = currentTimeF.Sub(lastIpUpdated).Seconds() + } + } + + if currentIp == pubIp && lastIpUpdatedDuration < expiryTime { + // If currentIp is same as whats set in env var NC_PUB_IP AND last time IP updated in NC was less than 24 hrs ago. + DDNSLogger(InformationLog, host, domain, "DNS record is same as current IP "+pubIp+". Last record update request made "+fmt.Sprintf("%f", lastIpUpdatedDuration)+" seconds ago.") } else { err = setDNSRecord(host, domain, password, pubIp) if err != nil { @@ -178,7 +197,10 @@ func setDNSRecord(host, domain, password, pubIp string) error { return &CustomError{ErrorCode: -1, Err: errors.New(interfaceResponse.Errors.Err1)} } + currentTime := time.Now() + os.Setenv("NC_PUB_IP", pubIp) + os.Setenv("NC_PUB_IP_TIME", currentTime.Format("2006-01-02 15:04:05")) return nil }