From 2ae8789fef167a6f2a5966d3c8ca8b90cfb70c6e Mon Sep 17 00:00:00 2001 From: liyp Date: Mon, 15 Apr 2024 14:51:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ip=E7=9A=84=E7=BD=91=E5=9D=80?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workers/ip.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/workers/ip.go b/workers/ip.go index dfb2b7a..bd272e6 100644 --- a/workers/ip.go +++ b/workers/ip.go @@ -1,8 +1,10 @@ package workers import ( + "bytes" "fmt" "io" + "net" "net/http" "strings" @@ -15,15 +17,56 @@ type Ip struct { func (a *Ip) GetMsg() string { if len(a.Parms) < 2 { - return "您可以使用 /ip ip地址 进行查找,支持ipv4/ipv6" + return "您可以使用 /ip ip地址/网址 进行查找,ip地址支持ipv4/ipv6" } // 去除换行符 raw_msg := strings.TrimRight(a.RawMsg, "\n") fmt.Println("raw_msg:", raw_msg) parms := strings.Fields(raw_msg) - url := "https://api.ip.sb/geoip/" + parms[1] + var ip string + if net.ParseIP(parms[1]) != nil { + ip = parms[1] + } else { + url := "https://api.siterelic.com/ping" + data := map[string]string{ + "url": parms[1], + } + json_data, _ := json.Marshal(data) + req, _ := http.NewRequest("POST", url, bytes.NewBuffer(json_data)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("x-api-key", "d84eae04-0654-4173-bc6a-c457c4fc3f4d") + // 打印请求信息 + // fmt.Println("请求方法:", req.Method) + // fmt.Println("请求URL:", req.URL.String()) + // fmt.Println("请求头:") + // for key, values := range req.Header { + // for _, value := range values { + // fmt.Printf(" %s: %s\n", key, value) + // } + // } + // fmt.Println("请求体:", string(json_data)) - // fmt.Println("url: ", url) + resp, err := http.DefaultClient.Do(req) + if err != nil { + fmt.Println("err:", err) + } else { + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + // println(string(body)) + if err != nil { + fmt.Println("err:", err) + } else { + var resp_data map[string]interface{} + json.Unmarshal(body, &resp_data) + ip = resp_data["data"].(map[string]interface{})["ip"].(string) + // print("ip: ", ip) + } + } + } + + url := "https://api.ip.sb/geoip/" + ip + + fmt.Println("url: ", url) req, err := http.NewRequest("GET", url, nil) if err != nil {