规范Go变量
This commit is contained in:
BIN
IP_region_query/ipquery
Normal file
BIN
IP_region_query/ipquery
Normal file
Binary file not shown.
@@ -11,12 +11,24 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type IPInfo struct {
|
||||
Msg string `json:"msg"`
|
||||
Data struct {
|
||||
Continent string `json:"continent"`
|
||||
Country string `json:"country"`
|
||||
} `json:"data"`
|
||||
// Response 结构体用于存储整个JSON响应
|
||||
type JSON struct {
|
||||
Code string `json:"code"`
|
||||
Data Data `json:"data"`
|
||||
IP string `json:"ip"`
|
||||
}
|
||||
|
||||
// Data 结构体用于存储"data"字段下的详细信息
|
||||
type Data struct {
|
||||
Continent string `json:"continent"`
|
||||
Country string `json:"country"`
|
||||
Zipcode string `json:"zipcode"`
|
||||
Owner string `json:"owner"`
|
||||
Isp string `json:"isp"`
|
||||
Adcode string `json:"adcode"`
|
||||
Prov string `json:"prov"`
|
||||
City string `json:"city"`
|
||||
District string `json:"district"`
|
||||
}
|
||||
|
||||
func isValidIP(ip string) bool {
|
||||
@@ -24,20 +36,32 @@ func isValidIP(ip string) bool {
|
||||
}
|
||||
|
||||
func main() {
|
||||
var (
|
||||
IP_ADDR string
|
||||
URL_ADDR string
|
||||
err error
|
||||
|
||||
HTTP_RESP *http.Response
|
||||
HTTP_BODY []byte
|
||||
HTTP_CLIENT *http.Client
|
||||
|
||||
HTPP_JSON JSON
|
||||
)
|
||||
|
||||
if len(os.Args) < 2 {
|
||||
log.Fatalf("用法: %s <IP>", os.Args[0])
|
||||
}
|
||||
|
||||
ip := os.Args[1]
|
||||
if !isValidIP(ip) {
|
||||
log.Fatalf("无效的 IP 地址: %s", ip)
|
||||
IP_ADDR = os.Args[1]
|
||||
if !isValidIP(IP_ADDR) {
|
||||
log.Fatalf("无效的 IP 地址: %s", IP_ADDR)
|
||||
}
|
||||
|
||||
// 目标 URL
|
||||
url := "https://qifu.baidu.com/ip/geo/v1/district?ip=" + ip
|
||||
URL_ADDR = "https://qifu.baidu.com/ip/geo/v1/district?ip=" + IP_ADDR
|
||||
|
||||
// 创建 HTTP 请求
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
req, err := http.NewRequest("GET", URL_ADDR, nil)
|
||||
if err != nil {
|
||||
log.Fatalf("创建请求时出错: %v", err)
|
||||
}
|
||||
@@ -61,31 +85,36 @@ func main() {
|
||||
req.Header.Set("Upgrade-Insecure-Requests", "1")
|
||||
|
||||
// 创建 HTTP 客户端并设置超时时间
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
HTTP_CLIENT = &http.Client{Timeout: 10 * time.Second}
|
||||
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
HTTP_RESP, err = HTTP_CLIENT.Do(req)
|
||||
if err != nil {
|
||||
log.Fatalf("发送请求时出错: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer HTTP_RESP.Body.Close()
|
||||
|
||||
// 检查 HTTP 响应状态码
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Fatalf("HTTP 请求失败,状态码: %d", resp.StatusCode)
|
||||
if HTTP_RESP.StatusCode != http.StatusOK {
|
||||
log.Fatalf("HTTP 请求失败,状态码: %d", HTTP_RESP.StatusCode)
|
||||
}
|
||||
|
||||
// 读取响应体
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
HTTP_BODY, err = io.ReadAll(HTTP_RESP.Body)
|
||||
if err != nil {
|
||||
log.Fatalf("读取响应体时出错: %v", err)
|
||||
}
|
||||
|
||||
// 解析 JSON 数据
|
||||
var ipInfo IPInfo
|
||||
if err := json.Unmarshal(body, &ipInfo); err != nil {
|
||||
log.Fatalf("解析 JSON 时出错: %v", err)
|
||||
err = json.Unmarshal(HTTP_BODY, &HTPP_JSON)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing JSON:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("%s%s\n", ipInfo.Data.Continent, ipInfo.Data.Country)
|
||||
if HTPP_JSON.Code == "Success" {
|
||||
fmt.Printf("%s%s\n", HTPP_JSON.Data.Continent, HTPP_JSON.Data.Country)
|
||||
} else {
|
||||
fmt.Printf("Error!\n")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user