diff --git a/aliddns.yaml b/aliddns.yaml index f2c96b6..fb74cb1 100644 --- a/aliddns.yaml +++ b/aliddns.yaml @@ -4,4 +4,5 @@ maindomain: '*example.com' subdomainname: '*www' checkupdateinterval: 30 protocol: all -apiurl: ip.sb +ipv4ApiUrl: '' +ipv6ApiUrl: '' diff --git a/config/config.go b/config/config.go index 15263a8..fb87970 100644 --- a/config/config.go +++ b/config/config.go @@ -20,9 +20,11 @@ var ConfigModel = &models.ConfigModel{ SubDomainName: "*www", CheckUpdateInterval: 30, Protocol: "all", + Ipv4ApiUrl: "", + Ipv6ApiUrl: "", } -//将配置写入指定的路径的文件 +// 将配置写入指定的路径的文件 func WriteConfigFile(ConfigMode *models.ConfigModel, path string) (err error) { configByte, err := yaml.Marshal(ConfigMode) if err != nil { @@ -54,7 +56,7 @@ var SupportedProtocols = [3]string{"ipv4", "ipv6", "all"} func UseConfigFile() { //配置文件存在 log.Println("使用的配置文件位置:", ConfigFilePath) - content, err := ioutil.ReadFile(ConfigFilePath) + content, err := os.ReadFile(ConfigFilePath) if err != nil { log.Fatalln(err.Error()) return diff --git a/main.go b/main.go index 170c2b4..d73c588 100644 --- a/main.go +++ b/main.go @@ -98,12 +98,20 @@ func main() { Destination: &config.ConfigModel.Protocol, }, &cli.StringFlag{ - Name: "apiurl", - Aliases: []string{"a"}, - Value: config.ConfigModel.ApiUrl, - Usage: "ApiUrl", - EnvVars: []string{"ApiUrl"}, - Destination: &config.ConfigModel.ApiUrl, + Name: "ipv4ApiUrl", + Aliases: []string{"v4"}, + Value: config.ConfigModel.Ipv4ApiUrl, + Usage: "Ipv4ApiUrl", + EnvVars: []string{"Ipv4ApiUrl"}, + Destination: &config.ConfigModel.Ipv4ApiUrl, + }, + &cli.StringFlag{ + Name: "ipv6ApiUrl", + Aliases: []string{"v6"}, + Value: config.ConfigModel.Ipv6ApiUrl, + Usage: "Ipv6ApiUrl", + EnvVars: []string{"Ipv6ApiUrl"}, + Destination: &config.ConfigModel.Ipv6ApiUrl, }, }, Action: func(c *cli.Context) error { diff --git a/models/models.go b/models/models.go index d707e0a..ef17899 100644 --- a/models/models.go +++ b/models/models.go @@ -6,6 +6,7 @@ type ConfigModel struct { MainDomain string // 需要更新的主域名,例如 iotserv.com SubDomainName string // 需要更新的具体子域名,例如 www CheckUpdateInterval int // 检查域名是否改变的时间间隔,单位秒,默认30秒 - Protocol string // "ipv4"或"ipv6"或"all",默认"all" - ApiUrl string // 获取 IP 的 API 地址 + Protocol string // "ipv4"或"ipv6"或"all",默认"all" + Ipv4ApiUrl string // 获取 IPv4 的 API 地址 + Ipv6ApiUrl string // 获取 IPv6 的 API 地址 } diff --git a/utils/utils.go b/utils/utils.go index e990463..3c38b2f 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -4,7 +4,7 @@ import ( "github.com/OpenIoTHub/aliddns/config" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" "github.com/aliyun/alibaba-cloud-sdk-go/services/alidns" - "io/ioutil" + "io" "log" "net" "net/http" @@ -44,14 +44,16 @@ var Ipv6APIUrls = []string{ } func GetMyPublicIpv4() string { - Ipv4APIUrls = append(Ipv4APIUrls, config.ConfigModel.ApiUrl) + if config.ConfigModel.Ipv4ApiUrl != "" { + Ipv4APIUrls = append([]string{config.ConfigModel.Ipv4ApiUrl}, Ipv4APIUrls...) + } for _, url := range Ipv4APIUrls { resp, err := http.Get(url) if err != nil { log.Printf("get public ipv4 err:%s", err) continue } - bytes, err := ioutil.ReadAll(resp.Body) + bytes, err := io.ReadAll(resp.Body) if err != nil { log.Printf("get public ipv4 err:%s", err) _ = resp.Body.Close() @@ -69,7 +71,9 @@ func GetMyPublicIpv4() string { } func GetMyPublicIpv6() string { - Ipv6APIUrls = append(Ipv6APIUrls, config.ConfigModel.ApiUrl) + if config.ConfigModel.Ipv6ApiUrl != "" { + Ipv6APIUrls = append([]string{config.ConfigModel.Ipv6ApiUrl}, Ipv6APIUrls...) + } for _, url := range Ipv6APIUrls { resp, err := http.Get(url) if err != nil { @@ -77,7 +81,7 @@ func GetMyPublicIpv6() string { continue } // 读取 IPv6 - bytes, err := ioutil.ReadAll(resp.Body) + bytes, err := io.ReadAll(resp.Body) if err != nil { log.Printf("get public ipv6 err:%s", err) _ = resp.Body.Close() @@ -97,7 +101,7 @@ func GetMyPublicIpv6() string { return "" } -//TODO Test +// GetMyIPV6ByLocal TODO Test func GetMyIPV6ByLocal() string { s, err := net.InterfaceAddrs() if err != nil {