Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple hosts #13

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}