Skip to content

Commit

Permalink
include environment option (ote or production)
Browse files Browse the repository at this point in the history
  • Loading branch information
navilg committed May 25, 2024
1 parent 325c781 commit 1a10d24
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
8 changes: 6 additions & 2 deletions container-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ if [ "$GD_NAME" == "" -o "$GD_DOMAIN" == "" -o "$GD_TTL" == "" -o "$GD_KEY" == "
exit 1
fi

if [ "$GD_ENVIRONMENT" == "" ]; then
GD_ENVIRONMENT="production"
fi

if [ $GD_TTL -lt 600 ]; then
echo "ERROR TTL must be greater than or equal to 600."
exit 1
fi
if [ ! -f $HOME/.config/godaddy-ddns/config.json ]; then
/app/godaddyddns add --domain="$GD_DOMAIN" --name="$GD_NAME" --ttl=$GD_TTL --key="$GD_KEY" --secret="$GD_SECRET"
/app/godaddyddns add --domain="$GD_DOMAIN" --name="$GD_NAME" --ttl=$GD_TTL --key="$GD_KEY" --secret="$GD_SECRET" --environment="$GD_ENVIRONMENT"
else
echo "Configuration already exist. Syncing the record"
/app/godaddyddns update --domain="$GD_DOMAIN" --name="$GD_NAME" --ttl=$GD_TTL --key="$GD_KEY" --secret="$GD_SECRET"
/app/godaddyddns update --domain="$GD_DOMAIN" --name="$GD_NAME" --ttl=$GD_TTL --key="$GD_KEY" --secret="$GD_SECRET" --environment="$GD_ENVIRONMENT"
fi
/app/godaddyddns daemon
47 changes: 36 additions & 11 deletions godaddy-ddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type DNSRecord struct {
TTL int `json:"ttl"`
Key string `json:"key"`
Secret string `json:"secret"`
Env string `json:"env"`
}

type Configuration struct {
Expand Down Expand Up @@ -187,6 +188,7 @@ func main() {
ttl := addCmd.Int("ttl", 600, "Time-to-live in seconds. Minimum 600 seconds.")
key := addCmd.String("key", "", "Key value generated from godaddy developer console")
secret := addCmd.String("secret", "", "Secret value generated from godaddy developer console")
env := addCmd.String("environment", "production", "Environment of GoDaddy (production or ote)")

deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
deleteDomain := deleteCmd.String("domain", "", "Domain name e.g. example.com")
Expand All @@ -198,6 +200,7 @@ func main() {
updateTtl := updateCmd.Int("ttl", 600, "Time-to-live in seconds. Minimum 600 seconds.")
updateKey := updateCmd.String("key", "", "Key value generated from godaddy developer console")
updateSecret := updateCmd.String("secret", "", "Secret value generated from godaddy developer console")
updateEnv := addCmd.String("environment", "production", "Environment of GoDaddy (production or ote)")

var usage = func() {
fmt.Printf("\nUsage:\n")
Expand Down Expand Up @@ -252,7 +255,7 @@ func main() {
os.Exit(1)
}

err := addRecord(*domain, *name, *key, *secret, *ttl, false)
err := addRecord(*domain, *name, *key, *secret, *env, *ttl, false)
if err != nil {
GoDaddyDDNSLogger(ErrorLog, *name, *domain, err.Error()+" Failed to add record.")
os.Exit(1)
Expand Down Expand Up @@ -288,7 +291,7 @@ func main() {
updateCmd.PrintDefaults()
os.Exit(1)
}
err := addRecord(*updateDomain, *updateName, *updateKey, *updateSecret, *updateTtl, true)
err := addRecord(*updateDomain, *updateName, *updateKey, *updateSecret, *updateEnv, *updateTtl, true)
if err != nil {
GoDaddyDDNSLogger(ErrorLog, *updateName, *updateDomain, "Failed to update record. "+err.Error())
os.Exit(1)
Expand All @@ -313,20 +316,21 @@ func main() {

}

func addRecord(domain, name, key, secret string, ttl int, isUpdate bool) error {
func addRecord(domain, name, key, secret, env string, ttl int, isUpdate bool) error {
record := DNSRecord{
Domain: domain,
Name: name,
Key: key,
Secret: secret,
TTL: ttl,
Env: env,
}

var config Configuration
var updatedConfig Configuration
var hasUpdated bool = false

body, err := getDNSRecord(name, domain, key, secret)
body, err := getDNSRecord(name, domain, key, secret, env)
if err != nil {
return &CustomError{ErrorCode: 1, Err: errors.New("addRecord Error getting DNS record " + err.Error())}
// return err
Expand Down Expand Up @@ -397,7 +401,7 @@ func addRecord(domain, name, key, secret string, ttl int, isUpdate bool) error {
}

if ttl != existingTtl || pubIp != existingIp {
_, err := setDNSRecord(name, domain, key, secret, pubIp, ttl)
_, err := setDNSRecord(name, domain, key, secret, pubIp, env, ttl)
if err != nil {
return &CustomError{ErrorCode: 1, Err: errors.New("addRecord Error setting DNS record " + err.Error())}
// return err
Expand All @@ -416,10 +420,20 @@ func addRecord(domain, name, key, secret string, ttl int, isUpdate bool) error {
return nil
}

func getDNSRecord(name, domain, key, secret string) (string, error) {
func getDNSRecord(name, domain, key, secret, env string) (string, error) {
// Get record details from GoDaddy

gdURL := "https://api.godaddy.com/" + godaddy_api_version + "/domains/" + domain + "/records/A/" + name
var gdAPI string

if env == "production" {
gdAPI = "https://api.godaddy.com"
} else if env == "ote" {
gdAPI = "https://api.ote-godaddy.com"
} else {
return "", &CustomError{ErrorCode: 1, Err: errors.New("Invalid environment " + gdAPI)}
}

gdURL := gdAPI + "/" + godaddy_api_version + "/domains/" + domain + "/records/A/" + name
authorization := key + ":" + secret

apiclient := &http.Client{}
Expand Down Expand Up @@ -497,9 +511,19 @@ func getPubIP() (string, error) {

}

func setDNSRecord(name, domain, key, secret, pubIp string, ttl int) (string, error) {
func setDNSRecord(name, domain, key, secret, pubIp, env string, ttl int) (string, error) {

var gdAPI string

if env == "production" {
gdAPI = "https://api.godaddy.com"
} else if env == "ote" {
gdAPI = "https://api.ote-godaddy.com"
} else {
return "", &CustomError{ErrorCode: 1, Err: errors.New("Invalid environment " + gdAPI)}
}

gdURL := "https://api.godaddy.com/" + godaddy_api_version + "/domains/" + domain + "/records/A/" + name
gdURL := gdAPI + "/" + godaddy_api_version + "/domains/" + domain + "/records/A/" + name
authorization := key + ":" + secret

type Data struct {
Expand Down Expand Up @@ -707,8 +731,9 @@ func daemonDDNS() {
key := i.Key
secret := i.Secret
ttl := i.TTL
env := i.Env

body, err := getDNSRecord(name, domain, key, secret)
body, err := getDNSRecord(name, domain, key, secret, env)
if err != nil {
GoDaddyDDNSLogger(ErrorLog, name, domain, "Failed to get current state of record. "+err.Error())
continue
Expand Down Expand Up @@ -739,7 +764,7 @@ func daemonDDNS() {
}

if ttl != existingTtl || pubIp != existingIp {
_, err := setDNSRecord(name, domain, key, secret, pubIp, ttl)
_, err := setDNSRecord(name, domain, key, secret, pubIp, env, ttl)
if err != nil {
GoDaddyDDNSLogger(ErrorLog, name, domain, "Failed to update record. "+err.Error())
continue
Expand Down

0 comments on commit 1a10d24

Please sign in to comment.