diff --git a/README.md b/README.md index b05cf2b..007ca43 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ { "SecretId": "SecretId", // 腾讯云API密钥ID "SecretKey": "SecretKey", // 腾讯云API密钥Key - "GetIPAPI": "IPIP", // 获取IP的API,选填 LanceAPI 或 IPIP + "GetIPAPI": "IPIP", // 获取IP的API,选填 LanceAPI IPIP SB "InstanceId": "InstanceId", // 服务器的实例ID "InstanceRegion": "InstanceRegion", // 服务器的地域,参见下文附录 "MaxRetries": "3", // 获取IP地址时出现错误的最大重试次数 @@ -86,7 +86,8 @@ https://api.lance.fun/ip/ IPIP // 推荐在中国大陆使用 https://myip.ipip.net/ip - +SB // 全球通用 但效率较慢 +https://api-ipv4.ip.sb/ip ``` > 更多IP正在适配中 \ No newline at end of file diff --git a/main.go b/main.go index 291b709..460e563 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "os" "reflect" "strconv" + "strings" "time" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" @@ -18,7 +19,7 @@ import ( ) var ( - version = " Development" + version = "Development" httpClient = &http.Client{ Timeout: time.Second * 10, } @@ -121,7 +122,7 @@ func getconfig() Config { if !checkPassing { os.Exit(1) } - fmt.Printf("Config load successfully\n") + fmt.Printf("Config loaded\n") return configData } @@ -129,6 +130,7 @@ func getip(api string, maxretries int) string { if api == "LanceAPI" { for i := 0; i < maxretries; i++ { req, _ := http.NewRequest("GET", "https://api.lance.fun/ip", nil) + req.Header.Set("User-Agent", "QCIP") resp, err := httpClient.Do(req) if err != nil { fmt.Printf("IP API call failed, retrying %d time...\n", i+1) @@ -144,6 +146,7 @@ func getip(api string, maxretries int) string { } else if api == "IPIP" { for i := 0; i < maxretries; i++ { req, _ := http.NewRequest("GET", "https://myip.ipip.net/ip", nil) + req.Header.Set("User-Agent", "QCIP") resp, err := httpClient.Do(req) if err != nil { fmt.Printf("IP API call failed, retrying %d time...\n", i+1) @@ -162,6 +165,23 @@ func getip(api string, maxretries int) string { } fmt.Printf("IP API call failed %d times, exiting...\n", maxretries) os.Exit(1) + } else if api == "SB" { + for i := 0; i < maxretries; i++ { + req, _ := http.NewRequest("GET", "https://api-ipv4.ip.sb/ip", nil) + req.Header.Set("User-Agent", "QCIP") + resp, err := httpClient.Do(req) + if err != nil { + fmt.Printf("IP API call failed, retrying %d time...\n", i+1) + time.Sleep(1 * time.Second) + } else { + defer resp.Body.Close() + ipo, _ := io.ReadAll(resp.Body) + ip := strings.TrimRight(string(ipo), "\n") + return ip + } + } + fmt.Printf("IP API call failed %d times, exiting...\n", maxretries) + os.Exit(1) } else { fmt.Printf("Error: IP API %s not supported\n", api) os.Exit(1) @@ -233,6 +253,7 @@ func modifyrules(credential *common.Credential, InstanceRegion string, InstanceI _, err := client.ModifyFirewallRules(request) if _, ok := err.(*errors.TencentCloudSDKError); ok { fmt.Printf("An API error has returned: %s\n", err) + os.Exit(1) return } if err != nil {