Skip to content

Commit

Permalink
1.用户附加的获取ip的api放在所有url列表最前面
Browse files Browse the repository at this point in the history
2.附加api区分v4和v6,命令也同步区分,如果你不想附加请置空字符串
  • Loading branch information
matteriot committed Oct 14, 2023
1 parent a447f1e commit cfe730b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
3 changes: 2 additions & 1 deletion aliddns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ maindomain: '*example.com'
subdomainname: '*www'
checkupdateinterval: 30
protocol: all
apiurl: ip.sb
ipv4ApiUrl: ''
ipv6ApiUrl: ''
6 changes: 4 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
20 changes: 14 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 地址
}
16 changes: 10 additions & 6 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -69,15 +71,17 @@ 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 {
log.Printf("get public ipv6 err:%s", err)
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()
Expand All @@ -97,7 +101,7 @@ func GetMyPublicIpv6() string {
return ""
}

//TODO Test
// GetMyIPV6ByLocal TODO Test
func GetMyIPV6ByLocal() string {
s, err := net.InterfaceAddrs()
if err != nil {
Expand Down

0 comments on commit cfe730b

Please sign in to comment.