go-bot/workers/ip.go

130 lines
3.1 KiB
Go
Raw Normal View History

2024-04-14 21:41:50 +08:00
package workers
import (
2024-04-15 14:51:24 +08:00
"bytes"
2024-04-14 21:41:50 +08:00
"fmt"
"io"
2024-04-15 14:51:24 +08:00
"net"
2024-04-14 21:41:50 +08:00
"net/http"
"strings"
"github.com/goccy/go-json"
)
type Ip struct {
*StdAns
}
func (a *Ip) GetMsg() string {
if len(a.Parms) < 2 {
2024-04-15 14:51:24 +08:00
return "您可以使用 /ip ip地址/网址 进行查找ip地址支持ipv4/ipv6"
2024-04-14 21:41:50 +08:00
}
// 去除换行符
raw_msg := strings.TrimRight(a.RawMsg, "\n")
fmt.Println("raw_msg:", raw_msg)
2024-04-14 22:08:50 +08:00
parms := strings.Fields(raw_msg)
2024-04-15 14:51:24 +08:00
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))
2024-04-14 21:41:50 +08:00
2024-04-15 14:51:24 +08:00
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)
2024-04-14 21:41:50 +08:00
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("创建请求失败:", err)
}
req.Header.Set("User-Agent", "Mozilla/5.0")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "ip查询失败"
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "ip查询失败"
}
// fmt.Println("body: ", string(body))
var data map[string]interface{}
err = json.Unmarshal(body, &data)
if err != nil {
fmt.Println("data: ", data)
return err.Error()
}
// 输出响应数据
2024-04-14 22:08:50 +08:00
fmt.Println("data: ", data)
2024-04-14 21:41:50 +08:00
if _, ok := data["code"].(float64); ok {
code := data["code"].(float64)
if code == 401 {
return "ip查询失败"
}
}
var msgs []string
if _, ok := data["organization"].(string); ok {
msgs = append(msgs, "组织:"+data["organization"].(string))
}
2024-04-14 21:44:57 +08:00
if _, ok := data["city"].(string); ok {
msgs = append(msgs, "城市:"+data["city"].(string))
}
2024-04-14 21:41:50 +08:00
if _, ok := data["isp"].(string); ok {
msgs = append(msgs, "ISP"+data["isp"].(string))
}
if _, ok := data["asn"].(string); ok {
msgs = append(msgs, "ASN"+data["asn"].(string))
}
if _, ok := data["asn_organization"].(string); ok {
msgs = append(msgs, "ASN组织"+data["asn_organization"].(string))
}
if _, ok := data["country"].(string); ok {
msgs = append(msgs, "国家:"+data["country"].(string))
}
msgs = append(msgs, "IP"+data["ip"].(string))
msg := strings.Join(msgs, "\n")
return msg
}