107 lines
2.5 KiB
Go
107 lines
2.5 KiB
Go
package workers
|
||
|
||
import (
|
||
"fmt"
|
||
"net"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/imroc/req/v3"
|
||
)
|
||
|
||
func init() {
|
||
RegisterWorkerFactory("ip", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
||
return &Ip{
|
||
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
||
}
|
||
})
|
||
}
|
||
|
||
type Ip struct {
|
||
*StdAns
|
||
}
|
||
|
||
func (a *Ip) GetMsg() string {
|
||
if len(a.Parms) < 2 {
|
||
return "您可以使用 /ip ip地址/网址 进行查找,ip地址支持ipv4/ipv6"
|
||
}
|
||
// 去除换行符
|
||
raw_msg := strings.TrimRight(a.RawMsg, "\n")
|
||
// fmt.Println("raw_msg:", raw_msg)
|
||
parms := strings.Fields(raw_msg)
|
||
client := req.C().SetTimeout(5 * time.Second)
|
||
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],
|
||
}
|
||
var resp_data map[string]interface{}
|
||
_, err := client.R().
|
||
SetSuccessResult(&resp_data).
|
||
SetBodyJsonMarshal(data).
|
||
SetHeader("Content-Type", "application/json").
|
||
SetHeader("x-api-key", "d84eae04-0654-4173-bc6a-c457c4fc3f4d").
|
||
Post(url)
|
||
if err != nil {
|
||
return "DNS解析失败!"
|
||
}
|
||
if resp_data["data"] != nil {
|
||
ip = resp_data["data"].(map[string]interface{})["ip"].(string)
|
||
// print("ip: ", ip)
|
||
}
|
||
|
||
}
|
||
|
||
url := "https://api.ip.sb/geoip/" + ip
|
||
|
||
fmt.Println("url: ", url)
|
||
var data map[string]interface{}
|
||
resp, err := client.R().
|
||
SetSuccessResult(&data).
|
||
SetHeader("User-Agent", "Mozilla/5.0").
|
||
Get(url)
|
||
if err != nil {
|
||
return "ip查询失败"
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
// fmt.Println("body: ", string(body))
|
||
|
||
// 输出响应数据
|
||
fmt.Println("data: ", data)
|
||
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))
|
||
}
|
||
|
||
if _, ok := data["city"].(string); ok {
|
||
msgs = append(msgs, "城市:"+data["city"].(string))
|
||
}
|
||
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
|
||
}
|