Skip to content

Commit

Permalink
Impl: Delete records added from Tailscale
Browse files Browse the repository at this point in the history
  • Loading branch information
aaanh committed Apr 5, 2024
1 parent 682e418 commit 71cf675
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
10 changes: 6 additions & 4 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ func choiceHandler(choice int, config *structs.Config) {
case 4:
cf.ConfigureCloudflareZoneId(config)
case 5:
dryRun(*config)
case 6:
performSync(config)
case 6:
cf.DeleteAddedDnsRecords(config)
case 7:
dryRun(*config)
case 8:
{
fmt.Println("\n=== Thanks for using Tailflare :) ===")
fmt.Println("\n=== Thanks for using Tailflare :> ===")
os.Exit(0)
}
default:
{
fmt.Printf("\n\n> Invalid choice :(\n\n")
fmt.Printf("\n\n> Invalid choice :<\n\n")
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions app/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ func Menu(cfg *structs.Config) int {
return "Not added"
}
}()),
5: "Dry run (What-If)",
6: "Perform Sync",
7: "Exit",
5: "Perform Sync",
6: "Delete added records",
7: "Dry run (What-If)",
8: "Exit",
}

// Solve the misordered printing by sorting the keys in the map
Expand Down
42 changes: 38 additions & 4 deletions lib/cloudflare/cf.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ func GetDnsRecordsFromZoneId(cfg *structs.Config) []structs.CloudflareDnsRecord
client := &http.Client{}
endpoints := utils.GenerateEndpoints(cfg)

req, _ := http.NewRequest("GET", endpoints.CloudflareGetDomainFromZoneId, nil)
req.Header.Add("Content-Type", "application/json")
req, _ := http.NewRequest("GET", endpoints.CloudflareGetRecordsFromZoneId, nil)
req.Header.Add("Authorization", "Bearer "+cfg.Keys.CloudflareApiKey)

res, err := client.Do(req)
Expand Down Expand Up @@ -183,18 +182,53 @@ func DeleteAddedDnsRecords(cfg *structs.Config) {
Name string
Ipv4 string
}
for idx, device := range devices.Devices {
for _, device := range devices.Devices {
var current struct {
Name string
Ipv4 string
}
current.Ipv4 = device.Addresses[0]
current.Name = device.Name
devicesToDelete = append(devicesToDelete)
devicesToDelete = append(devicesToDelete, current)
}

records := GetDnsRecordsFromZoneId(cfg)

var recordsToDelete []string

for _, r := range records {
// Iterate over elements in array B
for _, d := range devicesToDelete {
fmt.Println(strings.Split(d.Name, ".")[0] + " == " + strings.Split(r.Name, ".")[0] + " - " + r.Content + " == " + d.Ipv4)
if strings.Split(r.Name, ".")[0] == strings.Split(d.Name, ".")[0] && r.Content == d.Ipv4 {
recordsToDelete = append(recordsToDelete, r.Id)
fmt.Println(recordsToDelete)
}
}
}

client := &http.Client{}
endpoints := utils.GenerateEndpoints(cfg)

for _, r := range recordsToDelete {
fmt.Printf(">>> Deleting record with ID %s\n", r)

req, _ := http.NewRequest("DELETE", endpoints.CloudflareDeleteRecordById+"/"+r, nil)

req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+cfg.Keys.CloudflareApiKey)

res, err := client.Do(req)
if err != nil {
fmt.Println(">>> Error occurred with Cloudflare API request.")
}

defer res.Body.Close()
body, _ := io.ReadAll(res.Body)

fmt.Println(res)
fmt.Println(string(body))
fmt.Printf("\n\n")
}

}
1 change: 1 addition & 0 deletions lib/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Endpoints struct {
CloudflareAddRecord string
CloudflareGetDomainFromZoneId string
CloudflareGetRecordsFromZoneId string
CloudflareDeleteRecordById string
}

type States struct {
Expand Down
1 change: 1 addition & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func GenerateEndpoints(cfg *structs.Config) structs.Endpoints {
CloudflareAddRecord: fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records", cfg.CloudflareZoneId),
CloudflareGetDomainFromZoneId: fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s", cfg.CloudflareZoneId),
CloudflareGetRecordsFromZoneId: fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records", cfg.CloudflareZoneId),
CloudflareDeleteRecordById: fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records", cfg.CloudflareZoneId),
}
return endpoints
}

0 comments on commit 71cf675

Please sign in to comment.