Skip to content

Commit

Permalink
remove IPv6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
boris1993 committed Mar 19, 2020
1 parent a954e52 commit cfa7ee8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 53 deletions.
20 changes: 6 additions & 14 deletions cfutil/cfutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

// ProcessRecords takes the configuration as well as the current IP address,
// then check and update each DNS record in CloudFlare
func ProcessRecords(config conf.Config, currentIPv4Address string, currentIPv6Address string) error {
func ProcessRecords(config conf.Config, currentAddress string) error {

if config.System.CloudFlareAPIEndpoint == "" {
return errors.New(constants.ErrCloudFlareAPIAddressEmpty)
Expand All @@ -30,7 +30,6 @@ func ProcessRecords(config conf.Config, currentIPv4Address string, currentIPv6Ad
if cloudFlareRecord.APIKey == "" ||
cloudFlareRecord.AuthEmail == "" ||
cloudFlareRecord.DomainName == "" ||
cloudFlareRecord.DomainType == "" ||
cloudFlareRecord.ZoneID == "" {
// Print error and skip to next record when bad configuration found
log.Errorln(constants.ErrCloudFlareRecordConfigIncomplete)
Expand All @@ -40,14 +39,7 @@ func ProcessRecords(config conf.Config, currentIPv4Address string, currentIPv6Ad
// Prints which record is being processed
log.Println(constants.MsgHeaderDomainProcessing, cloudFlareRecord.DomainName)

var newIpAddress = ""
if cloudFlareRecord.DomainType == "A" {
newIpAddress = currentIPv4Address
} else if cloudFlareRecord.DomainType == "AAAA" {
newIpAddress = currentIPv6Address
} else {
log.Errorln(constants.ErrInvalidDomainType)
}
var newIpAddress = currentAddress

// Then fetch the IP address of the specified DNS record
id, recordAddress, err := getCFDnsRecordIpAddress(cloudFlareRecord)
Expand All @@ -63,7 +55,7 @@ func ProcessRecords(config conf.Config, currentIPv4Address string, currentIPv6Ad
continue
} else {
// Update the IP address when changed.
status, err := updateCFDNSRecord(id, cloudFlareRecord.DomainType, newIpAddress, cloudFlareRecord)
status, err := updateCFDNSRecord(id, newIpAddress, cloudFlareRecord)

if err != nil {
log.Errorln(err)
Expand Down Expand Up @@ -96,7 +88,7 @@ func getCFDnsRecordIpAddress(cloudFlareRecord conf.CloudFlare) (string, string,
client := &http.Client{}

req, err := http.NewRequest(http.MethodGet,
APIEndpoint+"/zones/"+cloudFlareRecord.ZoneID+"/dns_records?type="+cloudFlareRecord.DomainType+"&name="+cloudFlareRecord.DomainName,
APIEndpoint+"/zones/"+cloudFlareRecord.ZoneID+"/dns_records?type=A&name="+cloudFlareRecord.DomainName,
nil)

if err != nil {
Expand Down Expand Up @@ -168,13 +160,13 @@ func getCFDnsRecordIpAddress(cloudFlareRecord conf.CloudFlare) (string, string,
//
// The return value is the status(true or false) of the update process,
// or an error will be returned if any error occurs.
func updateCFDNSRecord(id string, recordType string, address string, cloudFlareRecord conf.CloudFlare) (bool, error) {
func updateCFDNSRecord(id string, address string, cloudFlareRecord conf.CloudFlare) (bool, error) {
APIEndpoint := conf.Get().System.CloudFlareAPIEndpoint

client := &http.Client{}

updateRecordData := model.UpdateRecordData{}
updateRecordData.RecordType = recordType
updateRecordData.RecordType = "A"
updateRecordData.Name = cloudFlareRecord.DomainName
updateRecordData.Content = address

Expand Down
3 changes: 0 additions & 3 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type Config struct {
// System describes the System properties in Config.yaml
type System struct {
IPAddrAPI string `yaml:"IPAddrAPI"`
IPv6AddrAPI string `yaml:"IPv6AddrAPI"`
CloudFlareAPIEndpoint string `yaml:"CloudFlareAPIEndpoint"`
}

Expand All @@ -39,7 +38,6 @@ type CloudFlare struct {
ZoneID string `yaml:"ZoneID"`
AuthEmail string `yaml:"AuthEmail"`
DomainName string `yaml:"DomainName"`
DomainType string `yaml:"DomainType"`
}

type AliDNS struct {
Expand Down Expand Up @@ -105,7 +103,6 @@ func printDebugInfo() {
log.Debugf("%10v: %s", "ZoneID", item.ZoneID)
log.Debugf("%10v: %s", "AuthEmail", item.AuthEmail)
log.Debugf("%10v: %s", "DomainName", item.DomainName)
log.Debugf("%10v: %s", "DomainType", item.DomainType)
log.Debugln()
}

Expand Down
3 changes: 0 additions & 3 deletions config.yaml.template
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
---
System:
IPAddrAPI: "https://api.ipify.org/"
IPv6AddrAPI: "https://api6.ipify.org/"
CloudFlareAPIEndpoint: "https://api.cloudflare.com/client/v4"
CloudFlareRecords:
- APIKey: "YOUR_CLOUDFLARE_API_KEY"
ZoneID: "ZONE_ID_OF_YOUR_DOMAIN"
AuthEmail: "EMAIL_FOR_LOGGING_INTO_CLOUDFLARE"
DomainName: "FULL_DOMAIN_YOU_WANT_TO_UPDATE. e.g.:test1.example.com"
DomainType: "Type of this domain. A for IPv4 records, and AAAA for IPv6 records"
- APIKey: "YOUR_CLOUDFLARE_API_KEY"
ZoneID: "ZONE_ID_OF_YOUR_DOMAIN"
AuthEmail: "EMAIL_FOR_LOGGING_INTO_CLOUDFLARE"
DomainName: "FULL_DOMAIN_YOU_WANT_TO_UPDATE. e.g.:test2.example.com"
DomainType: "Type of this domain. A for IPv4 records, and AAAA for IPv6 records"
AliDNSRecords:
- AccessKeyID: "YOUR_ALIYUN_ACCESS_KEY_ID"
AccessKeySecret: "YOUR_ALIYUN_ACCESS_KEY_SECRET"
Expand Down
41 changes: 8 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func main() {
var config = conf.Get()

// Fetch the current external IP address.
ipAddress, ipv6Address, err := getCurrentIPAddress(config)
ipAddress, err := getCurrentIPAddress(config)

if err != nil {
log.Fatalln(err)
}

// Process CloudFlare records
err = cfutil.ProcessRecords(config, ipAddress, ipv6Address)
err = cfutil.ProcessRecords(config, ipAddress)

if err != nil {
log.Errorln(err)
Expand All @@ -53,9 +53,9 @@ func init() {
}

// getCurrentIPAddress returns the external IP address for your network
func getCurrentIPAddress(config conf.Config) (string, string, error) {
if config.System.IPAddrAPI == "" || config.System.IPv6AddrAPI == "" {
return "", "", errors.New(constants.ErrIPAddressFetchingAPIEmpty)
func getCurrentIPAddress(config conf.Config) (string, error) {
if config.System.IPAddrAPI == "" {
return "", errors.New(constants.ErrIPAddressFetchingAPIEmpty)
}

log.Println(constants.MsgCheckingCurrentIPAddr)
Expand All @@ -64,7 +64,7 @@ func getCurrentIPAddress(config conf.Config) (string, string, error) {
resp, err := http.Get(config.System.IPAddrAPI)

if err != nil {
return "", "", err
return "", err
}

// Handle errors when closing the HTTP connection
Expand All @@ -79,39 +79,14 @@ func getCurrentIPAddress(config conf.Config) (string, string, error) {
body, err := ioutil.ReadAll(resp.Body)

if err != nil {
return "", "", err
return "", err
}

// Body only contains the IP address
ipAddress := string(body)
//endregion

//region fetch your IPv6 address
resp, err = http.Get(config.System.IPv6AddrAPI)

if err != nil {
return "", "", err
}

defer func() {
err := resp.Body.Close()

if err != nil {
log.Errorln(constants.ErrCloseHTTPConnectionFail, err)
}
}()

body, err = ioutil.ReadAll(resp.Body)

if err != nil {
return "", "", err
}

ipv6Address := string(body)
//endregion

log.Println(constants.MsgHeaderCurrentIPAddr, ipAddress)
log.Println(constants.MsgHeaderCurrentIPv6Addr, ipv6Address)

return ipAddress, ipv6Address, nil
return ipAddress, nil
}

0 comments on commit cfa7ee8

Please sign in to comment.