Skip to content

Commit

Permalink
Code: Supported SBAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlancehu committed Jul 3, 2023
1 parent 8d41efa commit 22b37ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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地址时出现错误的最大重试次数
Expand Down Expand Up @@ -86,7 +86,8 @@ https://api.lance.fun/ip/
IPIP // 推荐在中国大陆使用
https://myip.ipip.net/ip
SB // 全球通用 但效率较慢
https://api-ipv4.ip.sb/ip
```

> 更多IP正在适配中
25 changes: 23 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"reflect"
"strconv"
"strings"
"time"

"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
Expand All @@ -18,7 +19,7 @@ import (
)

var (
version = " Development"
version = "Development"
httpClient = &http.Client{
Timeout: time.Second * 10,
}
Expand Down Expand Up @@ -121,14 +122,15 @@ func getconfig() Config {
if !checkPassing {
os.Exit(1)
}
fmt.Printf("Config load successfully\n")
fmt.Printf("Config loaded\n")
return configData
}

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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 22b37ca

Please sign in to comment.