diff --git a/cfutil/cfutil.go b/cfutil/cfutil.go index 445d5b1..37c0d65 100644 --- a/cfutil/cfutil.go +++ b/cfutil/cfutil.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 { @@ -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 diff --git a/conf/config.go b/conf/config.go index fd60822..d7e70a0 100644 --- a/conf/config.go +++ b/conf/config.go @@ -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"` } @@ -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 { @@ -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() } diff --git a/config.yaml.template b/config.yaml.template index 164b5b4..631f0ac 100644 --- a/config.yaml.template +++ b/config.yaml.template @@ -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" diff --git a/main.go b/main.go index e8dc5a6..0f37589 100644 --- a/main.go +++ b/main.go @@ -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) @@ -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) @@ -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 @@ -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 }