增加ip的网址查询

This commit is contained in:
liyp 2024-04-15 14:51:24 +08:00
parent e6a67581d7
commit 2ae8789fef

View file

@ -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 {