Skip to content

Commit

Permalink
Merge pull request #13 from Raik176/master
Browse files Browse the repository at this point in the history
Add support for multiple hosts
  • Loading branch information
navilg authored Aug 15, 2024
2 parents 1f96bb0 + 02d215e commit 4e0b525
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ If your server do not have static IP, i.e. When Public IP of your server / route

* Copy the Dynamic DNS password which is generated after enabling Dynamic DNS. Keep it safe and handy.

* Add a record of type `A + Dynamic DNS` with required host name.
* Add a record of type `A + Dynamic DNS` with required host name(s).

* Install docker on server.

Expand All @@ -37,7 +37,7 @@ docker run --name server.example.com -d --restart unless-stopped -e NC_HOST='ser
```

Here,
`NC_HOST` is host name added in Namecheap record.
`NC_HOST` are the host name(s) added in the Namecheap record, seperated by a comma.

`NC_DOMAIN` is your domain name.

Expand Down
10 changes: 5 additions & 5 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
WarningLog string = "WARN"
)

func DDNSLogger(logType, host, domain, message string) {
func DDNSLogger(logType, hosts, domain, message string) {

var (
StdoutInfoLogger *log.Logger
Expand All @@ -25,12 +25,12 @@ func DDNSLogger(logType, host, domain, message string) {
StdoutErrorLogger = log.New(os.Stdout, "ERROR ", log.Ldate|log.Ltime)

if logType == "INFO" {
StdoutInfoLogger.Println(host+"."+domain, message)
StdoutInfoLogger.Println(hosts+"."+domain, message)
} else if logType == "WARN" {
StdoutWarningLogger.Println(host+"."+domain, message)
StdoutWarningLogger.Println(hosts+"."+domain, message)
} else if logType == "ERROR" {
StdoutErrorLogger.Println(host+"."+domain, message)
StdoutErrorLogger.Println(hosts+"."+domain, message)
} else {
fmt.Println(host+"."+domain, message)
fmt.Println(hosts+"."+domain, message)
}
}
22 changes: 13 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import (
"flag"
"fmt"
"os"
"strings"
)

func main() {
fmt.Println("Namecheap Dynamic DNS client Version", version)
fmt.Println("Git Repo:", gitrepo)

domain := flag.String("domain", "", "Domain name e.g. example.com")
host := flag.String("host", "", "Subdomain or hostname e.g. www")
hosts := flag.String("host", "", "Subdomain or hostname e.g. www")
password := flag.String("password", "", "Dynamic DNS Password from Namecheap")

flag.Parse()
if *domain == "" || *host == "" || *password == "" {
if *domain == "" || *hosts == "" || *password == "" {
fmt.Println("ERROR domain, host and Dynamic DDNS password are mandatory")
fmt.Printf("\nUsage of %s:\n", os.Args[1])
flag.PrintDefaults()
Expand All @@ -24,15 +25,18 @@ func main() {

pubIp, err := getPubIP()
if err != nil {
DDNSLogger(ErrorLog, *host, *domain, err.Error())
DDNSLogger(ErrorLog, *hosts, *domain, err.Error())
} else {
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)
DDNSLogger(InformationLog, *hosts, *domain, "Updating all hosts.")
for _, host := range strings.Split(*hosts, ",") {
if err = setDNSRecord(host, *domain, *password, pubIp); err != nil {
DDNSLogger(ErrorLog, *hosts, *domain, err.Error())
DDNSLogger(WarningLog, *hosts, *domain, "Above error occured while updating host " + host + ", ignoring. If this is not right, Re-run the process after fixing the error")
} else {
DDNSLogger(InformationLog, *hosts, *domain, "Record for " + host + " updated. "+pubIp)
}
}
}

updateRecord(*domain, *host, *password)
updateRecord(*domain, *hosts, *password)
}

0 comments on commit 4e0b525

Please sign in to comment.