mirror of
https://gitlab.durp.info/durfy/apps/durpdns.git
synced 2026-05-07 07:50:33 -05:00
initial commit
This commit is contained in:
parent
b75d297ac8
commit
9446758739
4 changed files with 212 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
.env
|
||||
/.idea/
|
||||
16
go.mod
Normal file
16
go.mod
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
module gitlab.com/durfy/durpdns
|
||||
|
||||
go 1.24.1
|
||||
|
||||
require (
|
||||
github.com/caarlos0/env/v6 v6.10.1
|
||||
github.com/cloudflare/cloudflare-go/v4 v4.1.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/tidwall/gjson v1.14.4 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.1 // indirect
|
||||
github.com/tidwall/sjson v1.2.5 // indirect
|
||||
)
|
||||
16
go.sum
Normal file
16
go.sum
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
github.com/caarlos0/env/v6 v6.10.1 h1:t1mPSxNpei6M5yAeu1qtRdPAK29Nbcf/n3G7x+b3/II=
|
||||
github.com/caarlos0/env/v6 v6.10.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
|
||||
github.com/cloudflare/cloudflare-go/v4 v4.1.0 h1:1SjQZaPbUe23fSoCuMuN7EblVo+RIldNGd4pfkPCpW4=
|
||||
github.com/cloudflare/cloudflare-go/v4 v4.1.0/go.mod h1:XcYpLe7Mf6FN87kXzEWVnJ6z+vskW/k6eUqgqfhFE9k=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
|
||||
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
|
||||
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
|
||||
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
|
||||
178
main.go
Normal file
178
main.go
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/caarlos0/env/v6"
|
||||
"github.com/cloudflare/cloudflare-go/v4"
|
||||
"github.com/cloudflare/cloudflare-go/v4/dns"
|
||||
"github.com/cloudflare/cloudflare-go/v4/option"
|
||||
"github.com/joho/godotenv"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type IPResponse struct {
|
||||
IPAddress string `json:"YourFuckingIPAddress"`
|
||||
Location string `json:"YourFuckingLocation"`
|
||||
Hostname string `json:"YourFuckingHostname"`
|
||||
ISP string `json:"YourFuckingISP"`
|
||||
TorExit bool `json:"YourFuckingTorExit"`
|
||||
City string `json:"YourFuckingCity"`
|
||||
Country string `json:"YourFuckingCountry"`
|
||||
CountryCode string `json:"YourFuckingCountryCode"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
CloudflareZoneID string `env:"CLOUDFLARE_ZONE_ID"`
|
||||
CloudFlareAPIKey string `env:"CLOUDFLARE_API_KEY"`
|
||||
CloudflareEmail string `env:"CLOUDFLARE_EMAIL"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
err := godotenv.Load(".env")
|
||||
if err != nil {
|
||||
log.Fatal("Failed to load env file")
|
||||
return
|
||||
}
|
||||
|
||||
config := &Config{}
|
||||
err = env.Parse(config)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to load env file")
|
||||
return
|
||||
}
|
||||
|
||||
if config.CloudflareEmail == "" || config.CloudFlareAPIKey == "" || config.CloudflareZoneID == "" {
|
||||
log.Fatal("Email, API and ZoneID need to be set")
|
||||
return
|
||||
}
|
||||
|
||||
client := cloudflare.NewClient(
|
||||
option.WithAPIKey(config.CloudFlareAPIKey),
|
||||
option.WithAPIEmail(config.CloudflareEmail),
|
||||
)
|
||||
|
||||
ExistingIP, err := getIP()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Your public IP address is:", ExistingIP.IPAddress)
|
||||
|
||||
do:
|
||||
for {
|
||||
CurrentIP, err := getIP()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
|
||||
if ExistingIP.IPAddress != CurrentIP.IPAddress {
|
||||
ExistingIP = CurrentIP
|
||||
fmt.Println(
|
||||
"Your public IP address has updated:",
|
||||
ExistingIP.IPAddress,
|
||||
)
|
||||
|
||||
err := updateDNSRecords(
|
||||
client,
|
||||
config.CloudflareZoneID,
|
||||
CurrentIP.IPAddress,
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
|
||||
} else {
|
||||
fmt.Println("Your public IP address has not changed.")
|
||||
}
|
||||
|
||||
time.Sleep(10 * time.Second)
|
||||
continue do
|
||||
}
|
||||
}
|
||||
|
||||
func getIP() (*IPResponse, error) {
|
||||
|
||||
var ip IPResponse
|
||||
resp, err := http.Get("https://myip.wtf/json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &ip)
|
||||
return &ip, err
|
||||
}
|
||||
|
||||
func getDNSRecords(
|
||||
client *cloudflare.Client,
|
||||
zoneID string,
|
||||
) (*[]dns.RecordResponse, error) {
|
||||
dnslist, err := client.DNS.Records.List(
|
||||
context.TODO(),
|
||||
dns.RecordListParams{
|
||||
ZoneID: cloudflare.F(zoneID),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var records []dns.RecordResponse
|
||||
for _, item := range dnslist.Result {
|
||||
|
||||
if item.Type == "A" {
|
||||
records = append(records, item)
|
||||
}
|
||||
}
|
||||
return &records, nil
|
||||
}
|
||||
|
||||
func updateDNSRecords(
|
||||
client *cloudflare.Client,
|
||||
zoneID string,
|
||||
ipAddress string,
|
||||
) error {
|
||||
records, err := getDNSRecords(client, zoneID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, record := range *records {
|
||||
|
||||
NewDNS := dns.ARecordParam{
|
||||
Comment: cloudflare.F(record.Comment),
|
||||
Content: cloudflare.F(ipAddress),
|
||||
Name: cloudflare.F(record.Name),
|
||||
Proxied: cloudflare.F(record.Proxied),
|
||||
TTL: cloudflare.F(record.TTL),
|
||||
Type: cloudflare.F(dns.ARecordTypeA),
|
||||
}
|
||||
|
||||
_, err = client.DNS.Records.Edit(
|
||||
context.TODO(), record.ID, dns.RecordEditParams{
|
||||
ZoneID: cloudflare.F(zoneID),
|
||||
Record: NewDNS,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Updated IP address for %s\n", record.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue