Skip to content

Commit

Permalink
I have added a way to create an A rec using this script, the README e…
Browse files Browse the repository at this point in the history
…xplains how. A new docker image will be created and pushed shortly.
  • Loading branch information
Austin Ward committed Jul 17, 2024
1 parent 7d2e391 commit f3ea3a3
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*env
*vscode
config.yaml
config.yaml
log.txt
9 changes: 9 additions & 0 deletions CONFIG/create.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# content: "" # Should be the IP address
# name: "" # The FQDN goes here WITHOUT the schema
# Type: "A"
# comment: "" # Optional
# tags: [] # Optional
# ttl: 180 # 1 is auto or Value must be between 60 and 86400

# This file is used to create an A record
# To use uncomment all of the fields below and fill in all the fields to the desired value
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ I made this script because I needed to needed to update the A records of a domai
### From source
1. Clone the repository
2. Update the `config.yaml` @ `./CONFIG/config.yaml` file with your CloudFlare API key and email.
3. If you want to create a new A record for a domain, uncomment the /config/create.yaml file and fill in the fields with the correct information, and once ran recomment out the file so it does not run again.
3. Run the script with `go run main.go` or `go build main.go` and then run the executable.

### From Docker
Expand Down
51 changes: 51 additions & 0 deletions get_config/GetConfig.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package get_config

import (
"errors"
"log"
"os"
"strconv"
"strings"

"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -36,3 +39,51 @@ func Get_account_info() (string, string) {
// Access the values from the struct
return config.Email, config.Token
}


// create new A record

type Create struct {
Content string `yaml:"content"`
Name string `yaml:"name"`
Typpe string `yaml:"type"`
Proxied bool `yaml:"proxied"`
Comment string `yaml:"comment"`
Tags []string `yaml:"tags"`
Ttl int `yaml:"ttl"`
}

func (c Create) String() string {
return "Name: " + c.Name + "\nType: " + c.Typpe + "\nProxied: " + strconv.FormatBool(c.Proxied) + "\nComment: " + c.Comment + "\nTags: " + strings.Join(c.Tags, ", ") + "\nTTL: " + strconv.Itoa(c.Ttl) + "\nContent: " + c.Content
}

func Read_yaml() (*Create, error) {
_, err := os.Stat("/.dockerenv")
var data []byte
if err != nil {
println("Not in Docker")
data, err = os.ReadFile("./CONFIG/create.yaml")
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}

} else {
println("In Docker")
data, err = os.ReadFile("/config/create.yaml")
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
}
// Unmarshal the YAML data into a struct
var create Create
err = yaml.Unmarshal(data, &create)
if err != nil {
log.Fatalf("Failed to unmarshal YAML: %v", err)
}

if create.Content == "" {
return nil, errors.New("fileds must not be blank")
}
// Access the values from the struct
return &create, nil
}
4 changes: 0 additions & 4 deletions log.txt

This file was deleted.

65 changes: 31 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"net/http"
"net/url"
"os"
"slices"
"strings"

"example.com/v1/CF/get_config"
)


type DNS_REC struct {
zone_id interface{}
id string
Expand All @@ -25,38 +25,26 @@ type DNS_REC struct {
ttl interface{}
content string
}
func (d DNS_REC) String() string{
return fmt.Sprintf("Name: %v\nType: %v\nProxied: %v\nComment: %v\nTags: %v\nTTL: %v\nContent: %v\n", d.name, d.typpe, d.proxied, d.comment, d.tags, d.ttl, d.content)

func (d DNS_REC) String() string {
return fmt.Sprintf("Name: %v\nType: %v\nProxied: %v\nComment: %v\nTags: %v\nTTL: %v\nContent: %v\n", d.name, d.typpe, d.proxied, d.comment, d.tags, d.ttl, d.content)
}

type DNS_REC_LIST struct {
list []DNS_REC
}

func main() {

email, key := get_config.Get_account_info()

var account_id string = ""


// logPath := os.Getenv("LOGPATH")
// if logPath == "" {
// logPath = "./log.txt"
// }
// logFile, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
// if err != nil {
// log.Fatal(err)
// }
// defer logFile.Close()
// log.SetOutput(logFile)



var header = http.Header{}
header.Add("X-Auth-Email", email)
header.Add("Content-Type", "application/json")
header.Add("X-Auth-Key", key)


// get Zone IDs

var zones = http.Request{
Expand Down Expand Up @@ -84,7 +72,7 @@ func main() {
ID string `json:"id"`
} `json:"result"`
}

// log.Print(data.Result)

err = json.Unmarshal(body, &data)
Expand All @@ -100,7 +88,9 @@ func main() {
log.Fatal("No results found")
}

// Get DNS records
// If there exists a create.yaml file that is not empty then create a new DNS record and exit

// Get DNS records

var list = http.Request{
Method: "GET",
Expand All @@ -122,21 +112,14 @@ func main() {
log.Fatal(err)
}
defer response.Body.Close()
// log.Print("\n\n\n")
// fmt.Println(string(body))
// println("\n\n\n")

// Convert body to JSON
var data2 interface{}
err = json.Unmarshal(body, &data2)
if err != nil {
log.Print(err)
}

// log.Println(data.Result)

// Print the JSON data
// fmt.Println(data)

dns_records := DNS_REC_LIST{}

a_records := data2.(map[string]interface{})["result"].([]interface{})
Expand Down Expand Up @@ -189,19 +172,34 @@ func main() {
log.Println("local IP: ", local_ip)
fmt.Println("local IP: ", local_ip)

// Check if the IP address has changed if not exit
// check to see if there is data in the create.yaml file, if there is add a new record by overwriting the existing records in the rec list with the new record

new, err := get_config.Read_yaml()
if err == nil {
new_dns := DNS_REC{
content: "192.168.0.0",
name: new.Name,
typpe: new.Typpe,
proxied: new.Proxied,
comment: new.Comment,
tags: new.Tags,
ttl: new.Ttl,
}

A_rec.list = append(A_rec.list, new_dns)
slices.Reverse(A_rec.list)
}

// Check if the IP address has changed if not exit

if local_ip == A_rec.list[0].content {
log.Println("IP address has not changed")
fmt.Println("\033[0;31m IP address has not changed \033[0m")
logFile.Close()
os.Exit(0)
}


// Update the DNS A records


for _, record := range A_rec.list {
if record.comment == nil {
record.comment = ""
Expand Down Expand Up @@ -238,11 +236,10 @@ func main() {

log.Println(string(body))
log.Println(res)
result := res
result := res
// Print the result to the log file
log.Println("payload:", payload, "\nresults:", result)

}
// logFile.Close()

}

0 comments on commit f3ea3a3

Please sign in to comment.