Skip to content

Commit

Permalink
Merge pull request #15 from navilg/11-timeout-the-pubip-env-variable-…
Browse files Browse the repository at this point in the history
…set-in-container

11 timeout the pubip env variable set in container
  • Loading branch information
navilg authored Sep 8, 2024
2 parents 43abc95 + 004d12d commit 9ef5571
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 8 additions & 1 deletion docker-build.sh
Original file line number Diff line number Diff line change
@@ -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 .
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/navilg/namecheap-ddns-docker

go 1.22
go 1.23
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
4 changes: 3 additions & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
26 changes: 24 additions & 2 deletions updaterecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 9ef5571

Please sign in to comment.