From 9259178be2386639b28f1c7f6de23eb1009147cf Mon Sep 17 00:00:00 2001 From: liyp Date: Sun, 14 Apr 2024 21:41:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0ip=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test.go | 2 +- workers/ip.go | 82 ++++++++++++++++++++++++++++++++++++++++++++ workers/newworker.go | 4 +++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 workers/ip.go diff --git a/test/test.go b/test/test.go index 807a0b8..48c9169 100644 --- a/test/test.go +++ b/test/test.go @@ -15,7 +15,7 @@ func main() { fmt.Print("输入指令(不要带/):") raw_msg, _ := reader.ReadString('\n') // 去除末尾的换行符 - raw_msg = strings.TrimRight(raw_msg, "\r\n") + // raw_msg = strings.TrimRight(raw_msg, "\r\n") parms := strings.Split(raw_msg, " ") diff --git a/workers/ip.go b/workers/ip.go new file mode 100644 index 0000000..ec0239f --- /dev/null +++ b/workers/ip.go @@ -0,0 +1,82 @@ +package workers + +import ( + "fmt" + "io" + "net/http" + "strings" + + "github.com/goccy/go-json" +) + +type Ip struct { + *StdAns +} + +func (a *Ip) GetMsg() string { + if len(a.Parms) < 2 { + return "您可以使用 /ip ip地址 进行查找" + } + // 去除换行符 + raw_msg := strings.TrimRight(a.RawMsg, "\n") + fmt.Println("raw_msg:", raw_msg) + parms := strings.Split(raw_msg, " ") + url := "https://api.ip.sb/geoip/" + parms[1] + + // fmt.Println("url: ", url) + + 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() + } + // 输出响应数据 + // 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["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 +} diff --git a/workers/newworker.go b/workers/newworker.go index ea9b0dd..8d14c8c 100644 --- a/workers/newworker.go +++ b/workers/newworker.go @@ -18,6 +18,10 @@ func NewWorker(parms []string, uid, gid, role, mid, rawMsg string) Worker { return &Hhsh{ StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), } + case "ip": + return &Ip{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } default: return &Emm{ StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg)}