Skip to content

Commit

Permalink
Merge pull request #10 from navilg/development
Browse files Browse the repository at this point in the history
Release 1.2.0
  • Loading branch information
navilg authored Mar 10, 2024
2 parents 08d0c50 + 1f96bb0 commit 43abc95
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 14 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.19.0-alpine3.16 as build
FROM golang:1.22.1-alpine3.19 as build
ARG OS
ARG ARCH
COPY . /build/
WORKDIR /build
RUN go mod download && go build -o ncddns

FROM alpine:3.16
FROM alpine:3.19
ARG VERSION
ARG user=ncddns
ARG group=ncddns
Expand Down
4 changes: 2 additions & 2 deletions docker-build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/ussr/bin/env bash

docker buildx create --use
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t linuxshots/namecheap-ddns:1.1.0 --push --pull .
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: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.19
go 1.22
2 changes: 1 addition & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func DDNSLogger(logType, host, domain, message string) {
)

StdoutInfoLogger = log.New(os.Stdout, "INFO ", log.Ldate|log.Ltime)
StdoutWarningLogger = log.New(os.Stdout, "WARN ", log.Ldate|log.Ltime)
StdoutWarningLogger = log.New(os.Stdout, "WARNING ", log.Ldate|log.Ltime)
StdoutErrorLogger = log.New(os.Stdout, "ERROR ", log.Ldate|log.Ltime)

if logType == "INFO" {
Expand Down
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ func main() {
if err != nil {
DDNSLogger(ErrorLog, *host, *domain, err.Error())
} else {
setDNSRecord(*host, *domain, *password, pubIp)
DDNSLogger(InformationLog, *host, *domain, "Record updated. "+pubIp)
if err = setDNSRecord(*host, *domain, *password, pubIp); err != nil {
DDNSLogger(ErrorLog, *host, *domain, err.Error())
DDNSLogger(WarningLog, *host, *domain, "Ignoring above error. If this is not right, Re-run the process after fixing the error")
} else {
DDNSLogger(InformationLog, *host, *domain, "Record updated. "+pubIp)
}
}

updateRecord(*domain, *host, *password)
Expand Down
2 changes: 1 addition & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (err *CustomError) Error() string {
}

var (
version string = "1.1.0-go1.19"
version string = "1.2.0-go1.22"
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
Expand Down
19 changes: 19 additions & 0 deletions sample-failure-reponse-body.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-16"?>
<interface-response>
<Command>SETDNSHOST</Command>
<Language>eng</Language>
<ErrCount>1</ErrCount>
<errors>
<Err1>Passwords do not match</Err1>
</errors>
<ResponseCount>1</ResponseCount>
<responses>
<response>
<Description>Passwords do not match</Description>
<ResponseNumber>304156</ResponseNumber>
<ResponseString>Validation error; invalid ; password</ResponseString>
</response>
</responses>
<Done>true</Done>
<debug><![CDATA[]]></debug>
</interface-response>
12 changes: 12 additions & 0 deletions sample-success-reponse-body.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-16"?>
<interface-response>
<Command>SETDNSHOST</Command>
<Language>eng</Language>
<IP>1.2.3.4</IP>
<ErrCount>0</ErrCount>
<errors />
<ResponseCount>0</ResponseCount>
<responses />
<Done>true</Done>
<debug><![CDATA[]]></debug>
</interface-response>
53 changes: 48 additions & 5 deletions updaterecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package main

import (
"encoding/json"
"encoding/xml"
"errors"
"io/ioutil"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"strings"
"time"
)

Expand Down Expand Up @@ -84,7 +87,7 @@ func getPubIP() (string, error) {
}

defer response.Body.Close()
bodyBytes, err := ioutil.ReadAll(response.Body)
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
// fmt.Println(err.Error())
return "", &CustomError{ErrorCode: response.StatusCode, Err: errors.New("IP could not be fetched." + err.Error())}
Expand All @@ -97,7 +100,7 @@ func getPubIP() (string, error) {
}

if ipbody.IP == "" {
return "", &CustomError{ErrorCode: response.StatusCode, Err: errors.New("IP could not be fetched. Empty IP value detected.")}
return "", &CustomError{ErrorCode: response.StatusCode, Err: errors.New("IP could not be fetched. Empty IP value detected")}
}

return ipbody.IP, nil
Expand All @@ -106,6 +109,17 @@ func getPubIP() (string, error) {

func setDNSRecord(host, domain, password, pubIp string) error {

type InterfaceError struct {
Err1 string `xml:"Err1"`
}

type InterfaceResponse struct {
ErrorCount int `xml:"ErrCount"`
Errors InterfaceError `xml:"errors"`
}

var interfaceResponse InterfaceResponse

// Link from Namecheap knowledge article.
// https://www.namecheap.com/support/knowledgebase/article.aspx/29/11/how-to-dynamically-update-the-hosts-ip-with-an-http-request/

Expand All @@ -131,8 +145,37 @@ func setDNSRecord(host, domain, password, pubIp string) error {

defer response.Body.Close()

if response.StatusCode != 200 {
return &CustomError{ErrorCode: response.StatusCode, Err: errors.New(response.Status)}
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
return err
}

// Below function removes first line (below line) from response body because golang xml encoder does not support utf-16
// <?xml version="1.0" encoding="utf-16"?>
modifyBodyBytes := func(bodyBytes []byte) []byte {

bodyString := string(bodyBytes)

read_lines := strings.Split(bodyString, "\n")

var updatedString string

for i, line := range read_lines {
if i != 0 {
updatedString = fmt.Sprintf("%s%s\n", updatedString, line)
}
}

return []byte(updatedString)
}

err = xml.Unmarshal(modifyBodyBytes(bodyBytes), &interfaceResponse)
if err != nil {
return err
}

if interfaceResponse.ErrorCount != 0 {
return &CustomError{ErrorCode: -1, Err: errors.New(interfaceResponse.Errors.Err1)}
}

os.Setenv("NC_PUB_IP", pubIp)
Expand Down

0 comments on commit 43abc95

Please sign in to comment.