2024-08-17 16:31:09 +08:00
|
|
|
|
package workers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"compress/gzip"
|
|
|
|
|
"fmt"
|
2024-10-25 23:15:20 +08:00
|
|
|
|
"go-bot/config"
|
2024-08-17 16:31:09 +08:00
|
|
|
|
"io"
|
2024-10-25 23:15:20 +08:00
|
|
|
|
"slices"
|
2024-09-08 16:46:05 +08:00
|
|
|
|
"time"
|
2024-08-17 16:31:09 +08:00
|
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
|
"github.com/andybalholm/brotli"
|
|
|
|
|
|
2024-09-08 16:46:05 +08:00
|
|
|
|
"github.com/imroc/req/v3"
|
2024-08-17 16:31:09 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
2024-10-25 23:15:20 +08:00
|
|
|
|
plugins := config.GetConfig()["PLUGINS"].([]interface{})
|
|
|
|
|
if slices.Contains(plugins, "httpcat") {
|
|
|
|
|
RegisterWorkerFactory("httpcat", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
|
|
|
|
return &HttpCat{
|
|
|
|
|
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-08-17 16:31:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type HttpCat struct {
|
|
|
|
|
*StdAns
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *HttpCat) GetMsg() string {
|
|
|
|
|
if len(a.Parms) < 2 {
|
|
|
|
|
return "您可以使用 !httpcat 状态码 进行查找,状态码支持100-599"
|
|
|
|
|
}
|
|
|
|
|
baseUrl := "https://http.cat/"
|
|
|
|
|
picUrl := baseUrl + "images/" + a.Parms[1] + ".jpg"
|
2024-09-08 16:46:05 +08:00
|
|
|
|
// 使用 req 发起 HEAD 请求检查资源是否存在
|
|
|
|
|
client := req.C().SetTimeout(5 * time.Second)
|
|
|
|
|
resp, err := client.R().Head(picUrl)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "请求错误: " + err.Error()
|
2024-08-17 16:31:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判断资源是否存在
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
|
return fmt.Sprintf("资源不存在,状态码: %d", resp.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
respContent := "[CQ:image,file=" + picUrl + "]"
|
|
|
|
|
docUrl := "https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status/" + a.Parms[1]
|
2024-09-08 16:46:05 +08:00
|
|
|
|
resp, err = client.R().
|
|
|
|
|
SetHeader("Host", "developer.mozilla.org").
|
|
|
|
|
SetHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)").
|
|
|
|
|
SetHeader("Accept", "*/*").
|
|
|
|
|
SetHeader("Content-Type", "text/plain").
|
|
|
|
|
Get(docUrl)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "请求错误: " + err.Error()
|
2024-08-17 18:04:24 +08:00
|
|
|
|
}
|
2024-09-08 16:46:05 +08:00
|
|
|
|
if resp != nil {
|
|
|
|
|
defer resp.Body.Close()
|
2024-08-17 18:04:24 +08:00
|
|
|
|
}
|
2024-09-08 16:46:05 +08:00
|
|
|
|
if resp != nil && resp.StatusCode == 200 {
|
2024-08-17 16:31:09 +08:00
|
|
|
|
// 解压缩响应体
|
|
|
|
|
var reader io.ReadCloser
|
2024-09-08 16:46:05 +08:00
|
|
|
|
switch resp.Header.Get("Content-Encoding") {
|
2024-08-17 16:31:09 +08:00
|
|
|
|
case "br":
|
2024-09-08 16:46:05 +08:00
|
|
|
|
reader = io.NopCloser(brotli.NewReader(resp.Body))
|
2024-08-17 16:31:09 +08:00
|
|
|
|
case "gzip":
|
|
|
|
|
var err error
|
2024-09-08 16:46:05 +08:00
|
|
|
|
reader, err = gzip.NewReader(resp.Body)
|
2024-08-17 16:31:09 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("gzip 解压错误:", err)
|
|
|
|
|
return respContent
|
|
|
|
|
}
|
|
|
|
|
defer reader.Close()
|
|
|
|
|
default:
|
2024-09-08 16:46:05 +08:00
|
|
|
|
reader = resp.Body
|
2024-08-17 16:31:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用 goquery 解析 HTML
|
|
|
|
|
doc, err := goquery.NewDocumentFromReader(reader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("HTML 解析错误:", err)
|
|
|
|
|
return respContent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找 <meta name="description"> 标签并获取 content 属性的值
|
|
|
|
|
description, exists := doc.Find(`meta[name="description"]`).Attr("content")
|
|
|
|
|
if exists {
|
|
|
|
|
// fmt.Println("Meta Description Content:", description)
|
|
|
|
|
respContent += description
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println("没有找到 meta description 标签")
|
|
|
|
|
// 将解析后的 HTML 保存到文件
|
|
|
|
|
// file, err := os.Create("output.html")
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// fmt.Println("文件创建错误:", err)
|
|
|
|
|
// return respContent
|
|
|
|
|
// }
|
|
|
|
|
// defer file.Close()
|
|
|
|
|
|
|
|
|
|
// // 输出 HTML 内容到文件
|
|
|
|
|
// html, err := doc.Html()
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// fmt.Println("获取 HTML 错误:", err)
|
|
|
|
|
// return respContent
|
|
|
|
|
// }
|
|
|
|
|
// _, err = file.WriteString(html)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// fmt.Println("写入文件错误:", err)
|
|
|
|
|
// return respContent
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// fmt.Println("HTML 内容已保存到 output.html 文件")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return respContent
|
|
|
|
|
|
|
|
|
|
}
|