Skip to content

Commit

Permalink
Hello, I've been working on this in a private Git server, I am now up…
Browse files Browse the repository at this point in the history
…loading this to gh. Hope you all find this useful.
  • Loading branch information
Austin Ward committed Apr 30, 2024
0 parents commit 1a0862c
Show file tree
Hide file tree
Showing 13 changed files with 391 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*env
*vscode
2 changes: 2 additions & 0 deletions CONFIG/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
X-Auth-Email: "test"
X-Auth-Key: "X-Auth-Key"
32 changes: 32 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Stage 1: Build the Go container
FROM golang:1.22.1-alpine AS builder

WORKDIR /app

COPY . .

RUN go build -o FlareAPI

# Stage 2: Create a new image and copy the built binary
FROM alpine:latest



WORKDIR /app

COPY --from=builder /app/FlareAPI .

# Add any additional dependencies or configurations here

# Set the entrypoint for the container
ENTRYPOINT ["./FlareAPI"]


# Stage 3: Push the new image to a container registry
# Replace <your-registry> with your actual container registry
# Replace <your-image-name> with the desired name for your image
# Replace <your-image-tag> with the desired tag for your image
# Uncomment the following lines to push the image
# RUN docker login <your-registry>
# RUN docker build -t <your-registry>/<your-image-name>:<your-image-tag> .
# RUN docker push <your-registry>/<your-image-name>:<your-image-tag>
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Austin ward

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## What is this?

This is a simple Golang script that uses CloudFlare's API to updates the A records IP address of a domain to the current public IP address of the machine running the script.

## Why did I make this?

I made this script because I needed to needed to update the A records of a domain I own that uses CloudFlare's DNS service. My ISP gave me a dynamic IP address and I needed a tool that would speed up this process.

## How do I use this?

### From source
1. Clone the repository
2. Update the `config.yaml` @ `./CONFIG/config.yaml` file with your CloudFlare API key and email.
3. Run the script with `go run main.go` or `go build main.go` and then run the executable.

### From Docker
1. Copy the Docker-compose file to your machine
2. copy and update the `config.yaml` file with your CloudFlare API key and email.
3. update the `docker-compose.yaml` file with the correct path to the `config.yaml` file and if logs are wanted add a path that is in the `/config` directory.

## How Do I get my CloudFlare API key?

Follow cloudflare's documentation on how to get your API key [here](https://developers.cloudflare.com/fundamentals/api/get-started/create-token/).

## Can I contribute?

Yes! Feel free to fork the repository and make a pull request.


### Source
<a src =https://developers.cloudflare.com/api> CloudFlare's API Documentation </a>

17 changes: 17 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: "3"
services:
CLOUDFLARE_API:
image: auswar/cloudflare_dns_update:v1
volumes:
- ./CONFIG/:/config
environment:
- LOGPATH="/config/log.txt"



# LOGPATH env should be something like "/config/log.txt" if you want the log file, if its not in the config folder the logs will be lost when the container is removed

# There needs to be a yaml file in the CONFIG folder that has the following format:
# Path: CONFIG/config.yaml
# X-Auth-Email: "cloudflair account email"
# X-Auth-Key: "X-Auth-Key from cloudflare"
38 changes: 38 additions & 0 deletions get_config/GetConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package get_config

import (
"log"
"os"

"gopkg.in/yaml.v2"
)

type Config struct {
Email string `yaml:"X-Auth-Email"`
Token string `yaml:"X-Auth-Key"`
}

func Get_account_info() (string, string) {
_, err := os.Stat("/.dockerenv")
var data []byte
if err != nil {
print("Not in Docker")
data, err = os.ReadFile("./CONFIG/config.yaml")

} else {
print("In Docker")
data, err = os.ReadFile("/config/config.yaml")
}



// Unmarshal the YAML data into a struct
var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
log.Fatalf("Failed to unmarshal YAML: %v", err)
}

// Access the values from the struct
return config.Email, config.Token
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module example.com/v1/CF

go 1.22.1

require gopkg.in/yaml.v2 v2.4.0
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
4 changes: 4 additions & 0 deletions log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
2024/04/25 11:59:22 Result: &{200 OK 200 HTTP/2.0 2 0 map[Cf-Cache-Status:[DYNAMIC] Cf-Ray:[87a03a0e1bdf6ac1-SEA] Content-Type:[application/json] Date:[Thu, 25 Apr 2024 17:59:25 GMT] Server:[cloudflare] Set-Cookie:[__cflb=0H28vgHxwvgAQtjUGUFqYFDiSDreGJnUprVLsfkNLgD; SameSite=Lax; path=/; expires=Thu, 25-Apr-24 20:29:26 GMT; HttpOnly __cfruid=f1f12df97fdac21b5356ee78049df0e74b3b09fd-1714067965; path=/; domain=.api.cloudflare.com; HttpOnly; Secure; SameSite=None] Vary:[Accept-Encoding]] 0xc00031c1e0 -1 [] false true map[] 0xc00031e120 0xc000110420}
2024/04/25 11:59:22 Result: &{200 OK 200 HTTP/2.0 2 0 map[Cf-Cache-Status:[DYNAMIC] Cf-Ray:[87a03a0ecc7f6ac1-SEA] Content-Type:[application/json] Date:[Thu, 25 Apr 2024 17:59:25 GMT] Server:[cloudflare] Set-Cookie:[__cflb=0H28vgHxwvgAQtjUGUFqYFDiSDreGJnUprVLsfkNLgD; SameSite=Lax; path=/; expires=Thu, 25-Apr-24 20:29:26 GMT; HttpOnly __cfruid=f1f12df97fdac21b5356ee78049df0e74b3b09fd-1714067965; path=/; domain=.api.cloudflare.com; HttpOnly; Secure; SameSite=None] Vary:[Accept-Encoding]] 0xc00031c570 -1 [] false true map[] 0xc00031e360 0xc000110420}
2024/04/25 11:59:23 Result: &{200 OK 200 HTTP/2.0 2 0 map[Cf-Cache-Status:[DYNAMIC] Cf-Ray:[87a03a0fed8e6ac1-SEA] Content-Type:[application/json] Date:[Thu, 25 Apr 2024 17:59:25 GMT] Server:[cloudflare] Set-Cookie:[__cflb=0H28vgHxwvgAQtjUGUFqYFDiSDreGJnUprVLsfkNLgD; SameSite=Lax; path=/; expires=Thu, 25-Apr-24 20:29:26 GMT; HttpOnly __cfruid=f1f12df97fdac21b5356ee78049df0e74b3b09fd-1714067965; path=/; domain=.api.cloudflare.com; HttpOnly; Secure; SameSite=None] Vary:[Accept-Encoding]] 0xc00031c900 -1 [] false true map[] 0xc00031e5a0 0xc000110420}
2024/04/25 11:59:23 Result: &{200 OK 200 HTTP/2.0 2 0 map[Cf-Cache-Status:[DYNAMIC] Cf-Ray:[87a03a110e906ac1-SEA] Content-Type:[application/json] Date:[Thu, 25 Apr 2024 17:59:25 GMT] Server:[cloudflare] Set-Cookie:[__cflb=0H28vgHxwvgAQtjUGUFqYFDiSDreGJnUprVLsfkNLgD; SameSite=Lax; path=/; expires=Thu, 25-Apr-24 20:29:26 GMT; HttpOnly __cfruid=f1f12df97fdac21b5356ee78049df0e74b3b09fd-1714067965; path=/; domain=.api.cloudflare.com; HttpOnly; Secure; SameSite=None] Vary:[Accept-Encoding]] 0xc00022ee40 -1 [] false true map[] 0xc000296120 0xc000110420}
230 changes: 230 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"

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

type DNS_REC struct {
zone_id interface{}
id string
name interface{}
typpe interface{}
proxied interface{}
comment interface{}
tags interface{}
ttl interface{}
content string
}

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")
print(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)

print(header)
log.Println(header)

var zones = http.Request{
Method: "GET",
URL: &url.URL{
Scheme: "https",
Host: "api.cloudflare.com",
Path: "/client/v4/zones",
},
Header: header,
}

netClient := &http.Client{}
response, err := netClient.Do(&zones)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
var data struct {
Result []struct {
ID string `json:"id"`
} `json:"result"`
}

log.Print(data.Result)

err = json.Unmarshal(body, &data)
if err != nil {
log.Fatal("No results found")
}

if len(data.Result) > 0 {
firstID := data.Result[0].ID
// fmt.Println("First ID:", firstID)
account_id = firstID
} else {
log.Fatal("No results found")
}

var list = http.Request{
Method: "GET",
URL: &url.URL{
Scheme: "https",
Host: "api.cloudflare.com",
Path: "/client/v4/zones/" + account_id + "/dns_records",
},
Header: header,
}

// netClient := &http.Client{}
response, err = netClient.Do(&list)
if err != nil {
log.Fatal(err)
}
body, err = ioutil.ReadAll(response.Body)
if err != nil {
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{})

for _, record := range a_records {
record := record.(map[string]interface{})

rec := DNS_REC{
id: record["id"].(string),
zone_id: record["zone_id"],
name: record["name"],
typpe: record["type"],
proxied: record["proxied"],
comment: record["comment"],

tags: record["tags"],

content: record["content"].(string),
ttl: record["ttl"],
}
dns_records.list = append(dns_records.list, rec)
}
for _, rec := range dns_records.list {
fmt.Println(rec)
}

A_rec := DNS_REC_LIST{}
for _, record := range dns_records.list {
if record.typpe == "A" {
log.Println(record.content)
A_rec.list = append(A_rec.list, record)

}
}
// Make API call to get IP address
test, err := http.Get("https://api.ipify.org")
if err != nil {
log.Fatal(err)
}
defer test.Body.Close()

// Read the response body
body, err = ioutil.ReadAll(test.Body)
if err != nil {
log.Fatal(err)
}

// Print the response body
var local_ip string = string(body)


for _, record := range A_rec.list {
if record.comment == nil {
record.comment = ""
}
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%v/dns_records/%v", record.zone_id, record.id)

payload := strings.NewReader(fmt.Sprintf(`{
"content": "%v",
"name": "%v",
"proxied": %v,
"type": "%v",
"comment": "%v",
"tags": [],
"ttl": %v
}`, local_ip, record.name, record.proxied, record.typpe, record.comment, record.ttl))

req, err := http.NewRequest("PATCH", url, payload)
if err != nil {
log.Fatal(err)
}

req.Header = header

res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}

defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}

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

}
logFile.Close()

}
Loading

0 comments on commit 1a0862c

Please sign in to comment.