Skip to content

Commit

Permalink
Add log and update table view in list
Browse files Browse the repository at this point in the history
  • Loading branch information
navilg committed Mar 26, 2022
1 parent 489495e commit c52186b
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 27 deletions.
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module github.com/navilg/godaddy-ddns

go 1.17

require (
github.com/jedib0t/go-pretty/v6 v6.3.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c // indirect
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jedib0t/go-pretty/v6 v6.3.0 h1:QQ5yZPDUMEjbZRXDJtZlvwfDQqCYFaxV3yEzTkogUgk=
github.com/jedib0t/go-pretty/v6 v6.3.0/go.mod h1:FMkOpgGD3EZ91cW8g/96RfxoV7bdeJyzXPYgz1L1ln0=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c h1:uHnKXcvx6SNkuwC+nrzxkJ+TpPwZOtumbhWrrOYN5YA=
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
121 changes: 94 additions & 27 deletions godaddy-ddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"time"

"github.com/jedib0t/go-pretty/v6/table"
)

type CustomError struct {
Expand Down Expand Up @@ -64,8 +67,52 @@ var (
godaddy_api_version string = "v1"
max_record_size int = 5
daemon_poll_time time.Duration = 1 * time.Minute // Time in minute
log_file string = config_loc + "/godaddy-ddns/log/godaddy-ddns.log"
)

const (
ErrorLog string = "ERROR"
InformationLog string = "INFO"
WarningLog string = "WARN"
)

func GoDaddyDDNSLogger(logType, name, domain, message string) {
file, err := os.OpenFile(log_file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
defer file.Close()

var (
WarningLogger *log.Logger
InfoLogger *log.Logger
ErrorLogger *log.Logger
StdoutInfoLogger *log.Logger
StdoutWarningLogger *log.Logger
StdoutErrorLogger *log.Logger
)

InfoLogger = log.New(file, "INFO ", log.Ldate|log.Ltime)
WarningLogger = log.New(file, "WARN ", log.Ldate|log.Ltime)
ErrorLogger = log.New(file, "ERROR ", log.Ldate|log.Ltime)
StdoutInfoLogger = log.New(os.Stdout, "INFO ", log.Ldate|log.Ltime)
StdoutWarningLogger = log.New(os.Stdout, "WARN ", log.Ldate|log.Ltime)
StdoutErrorLogger = log.New(os.Stdout, "ERROR ", log.Ldate|log.Ltime)

if logType == "INFO" {
InfoLogger.Println(name+"."+domain, message)
StdoutInfoLogger.Println(name+"."+domain, message)
} else if logType == "WARN" {
WarningLogger.Println(name+"."+domain, message)
StdoutWarningLogger.Println(name+"."+domain, message)
} else if logType == "ERROR" {
ErrorLogger.Println(name+"."+domain, message)
StdoutErrorLogger.Println(name+"."+domain, message)
} else {
fmt.Println(name+"."+domain, message)
}
}

func init() {
if _, err := os.Stat(config_loc); os.IsNotExist(err) {
err := os.Mkdir(config_loc, config_dir_perm)
Expand Down Expand Up @@ -111,6 +158,17 @@ func init() {
}
file.Close()
}

if _, err := os.Stat(config_loc + "/godaddy-ddns/log"); os.IsNotExist(err) {
err := os.Mkdir(config_loc+"/godaddy-ddns/log", config_dir_perm)
if err != nil {
if err != nil {
fmt.Println("Failed to create directory,", config_loc+"/godaddy-ddns/log", err.Error())
return
}
return
}
}
}

func main() {
Expand Down Expand Up @@ -156,12 +214,12 @@ func main() {

err := addRecord(*domain, *name, *key, *secret, *ttl, false)
if err != nil {
fmt.Println("Failed to add record,", err.Error())
GoDaddyDDNSLogger(ErrorLog, *name, *domain, "Failed to add record. "+err.Error())
return
} else {
GoDaddyDDNSLogger(InformationLog, *name, *domain, "New record added.")
}

fmt.Println("Record added")

case "delete":
deleteCmd.Parse(os.Args[2:])
if *deleteDomain == "" || *deleteName == "" {
Expand All @@ -172,12 +230,12 @@ func main() {
}
err := deleteRecord(*deleteDomain, *deleteName)
if err != nil {
fmt.Println("Failed to delete record,", err.Error())
GoDaddyDDNSLogger(ErrorLog, *deleteName, *deleteDomain, "Failed to delete record. "+err.Error())
return
} else {
GoDaddyDDNSLogger(InformationLog, *deleteName, *deleteDomain, "Record removed from configuration. If not in use, delete the record manually from GoDaddy console.")
}

fmt.Println("Record removed from GoDaddy DDNS configuration. If required, Delete the record from GoDaddy console manually")

case "update":
updateCmd.Parse(os.Args[2:])
if *updateDomain == "" || *updateName == "" || *updateKey == "" || *updateSecret == "" {
Expand All @@ -194,10 +252,11 @@ func main() {
}
err := addRecord(*updateDomain, *updateName, *updateKey, *updateSecret, *updateTtl, true)
if err != nil {
fmt.Println("Failed to update record,", err.Error())
GoDaddyDDNSLogger(ErrorLog, *updateName, *updateDomain, "Failed to update record. "+err.Error())
return
} else {
GoDaddyDDNSLogger(InformationLog, *updateName, *updateDomain, "Record updated")
}
fmt.Println("Record updated")

case "daemon":
// ticker := time.NewTicker(daemon_poll_time * time.Minute)
Expand Down Expand Up @@ -230,7 +289,7 @@ func main() {
fmt.Printf("\tCheck version\n")
fmt.Printf("\n\nExamples\n")
fmt.Printf("\tgoddns list\n")
fmt.Printf("\tgoddns add --domain='example.com' --name='myweb' --key='kEyGeneratedFr0mG0DaddY' --secret='s3cRe7GeneratedFr0mG0DaddY'\n")
fmt.Printf("\tgoddns add --domain='example.com' --name='myweb' --ttl=1200 --key='kEyGeneratedFr0mG0DaddY' --secret='s3cRe7GeneratedFr0mG0DaddY'\n")
fmt.Printf("\tgoddns update --domain='example.com' --name='myweb' --key='kEyGeneratedFr0mG0DaddY' --secret='s3cRe7GeneratedFr0mG0DaddY'\n")
fmt.Printf("\tgoddns delete --domain='example.com' --name='myweb'\n")
fmt.Printf("\tgoddns version'\n")
Expand Down Expand Up @@ -532,14 +591,16 @@ func listRecord() error {
return &CustomError{ErrorCode: 1, Err: errors.New("no record exist")}
}

fmt.Printf("-------------------------------------------------------------------------\n")
fmt.Printf("|\tName\t\t|\tDomain\t\t\t|\tTTL\t|\n")
fmt.Printf("-------------------------------------------------------------------------\n")
t := table.NewWriter()

t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Name", "Domain", "TTL"})

for _, i := range config.Config {
fmt.Printf("|\t%v\t|\t%v\t|\t%v\t|\n", i.Name, i.Domain, i.TTL)
t.AppendRow(table.Row{i.Name, i.Domain, i.TTL})
}
fmt.Printf("-------------------------------------------------------------------------\n")
t.Render()

} else {
return &CustomError{ErrorCode: 1, Err: errors.New("no record exist")}
}
Expand All @@ -549,13 +610,15 @@ func listRecord() error {

func daemonDDNS() {

GoDaddyDDNSLogger(InformationLog, "", "", "Starting daemon process")

ticker := time.NewTicker(daemon_poll_time)
done := make(chan bool)

if _, err := os.Stat(config_loc + "/godaddy-ddns/" + "daemon.lock"); !os.IsNotExist(err) {
fmt.Println("A process already running in background")
err = os.Remove(config_loc + "/godaddy-ddns/" + "daemon.lock")
if err != nil {
GoDaddyDDNSLogger(ErrorLog, "", "", "Failed to release lock")
os.Exit(1)
}
}
Expand All @@ -570,32 +633,32 @@ func daemonDDNS() {
var config Configuration

if _, err := os.Stat(config_loc + "/godaddy-ddns/" + "daemon.lock"); !os.IsNotExist(err) {
fmt.Println("A process already running in background")
GoDaddyDDNSLogger(WarningLog, "", "", "A daemon is already running. Waiting to release lock")
continue
}

file, err := os.Create(config_loc + "/godaddy-ddns/" + "daemon.lock")
if err != nil {
fmt.Println("Failed to lock the daemon process")
GoDaddyDDNSLogger(ErrorLog, "", "", "Failed to apply lock")
continue
}
file.Close()

configFileContent, err := ioutil.ReadFile(config_loc + "/godaddy-ddns/" + config_file)
if err != nil {
fmt.Println(err.Error())
GoDaddyDDNSLogger(ErrorLog, "", "", "Failed to read configuration file. "+err.Error())
continue
}

if len(configFileContent) != 0 {
err = json.Unmarshal(configFileContent, &config)
if err != nil {
fmt.Println(err.Error())
GoDaddyDDNSLogger(ErrorLog, "", "", "Failed to read configuration file. "+err.Error())
continue
}

if len(config.Config) == 0 {
fmt.Println("no record exist")
GoDaddyDDNSLogger(WarningLog, "", "", "No record added in configuration")
continue
}

Expand All @@ -609,14 +672,14 @@ func daemonDDNS() {

body, err := getDNSRecord(name, domain, key, secret)
if err != nil {
fmt.Println(err.Error())
GoDaddyDDNSLogger(ErrorLog, name, domain, "Failed to get current state of record. "+err.Error())
continue
}

var recordsBody []GodaddyRecordBody
err = json.Unmarshal([]byte(body), &recordsBody)
if err != nil {
fmt.Println(err.Error())
GoDaddyDDNSLogger(ErrorLog, name, domain, "Failed to read current state of record. "+err.Error())
continue
}

Expand All @@ -633,25 +696,29 @@ func daemonDDNS() {

pubIp, err := getPubIP()
if err != nil {
fmt.Println(err.Error())
GoDaddyDDNSLogger(ErrorLog, name, domain, "Failed to get current Pub IP of server. "+err.Error())
continue
}

if ttl != existingTtl || pubIp != existingIp {
_, err := setDNSRecord(name, domain, key, secret, pubIp, ttl)
if err != nil {
fmt.Println(err.Error())
GoDaddyDDNSLogger(ErrorLog, name, domain, "Failed to update record. "+err.Error())
continue
} else {
GoDaddyDDNSLogger(InformationLog, name, domain, "Record updated (ttl: "+fmt.Sprintf("%d", existingTtl)+"->"+fmt.Sprintf("%d", ttl)+", ip: "+existingIp+"->"+pubIp+")")
}
} else {
GoDaddyDDNSLogger(InformationLog, name, domain, "Desired state is current state")
}

time.Sleep(10 * time.Second)
time.Sleep(10 * time.Second) // Wait for 10 seconds before picking next record.
}
}

err = os.Remove(config_loc + "/godaddy-ddns/" + "daemon.lock")
if err != nil {
fmt.Println(err.Error())
GoDaddyDDNSLogger(ErrorLog, "", "", "Failed to release lock")
continue
}
}
Expand All @@ -664,7 +731,7 @@ func daemonDDNS() {
signal.Notify(c, os.Interrupt)
go func() {
for range c {
fmt.Println("Interrupt cleanup.")
GoDaddyDDNSLogger(InformationLog, "", "", "Interupt signal received.")
ticker.Stop()
done <- true
if _, err := os.Stat(config_loc + "/godaddy-ddns/" + "daemon.lock"); !os.IsNotExist(err) {
Expand Down

0 comments on commit c52186b

Please sign in to comment.