From 0d8ee89580b01e203ce97a539839b796210f2bdd Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 10 Apr 2023 22:28:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B7=BB=E5=8A=A0=E8=AF=84?= =?UTF-8?q?=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.example.yaml | 6 +++--- internal/actions/comment.go | 31 +++++++++++++++++++++++-------- internal/pkg/logs/log.go | 2 +- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/config.example.yaml b/config.example.yaml index 7560db7..a8ed53c 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -69,12 +69,12 @@ maxRequestSleepNum: 100 maxRequestNum: 500 # 单ip同时最大搜索请求数 singleIpSearchNum: 10 -# 错误日志输出路径 +# 错误日志输出路径 stdout|stderr|file path 默认为stderr logOutput: err.log # Gzip gzip: false -# 提交评论url -postCommentUrl: http://wp.test/wp-comments-post.php +# 提交评论url host需为ip形式 +postCommentUrl: http://127.0.0.1/wp-comments-post.php # TrustIps trustIps: [] # 分页器间隔数 diff --git a/internal/actions/comment.go b/internal/actions/comment.go index bc7886d..1cf7b33 100644 --- a/internal/actions/comment.go +++ b/internal/actions/comment.go @@ -2,6 +2,7 @@ package actions import ( "bytes" + "compress/gzip" "errors" "fmt" "github.com/fthvgb1/wp-go/helper/slice" @@ -31,11 +32,12 @@ func PostComment(c *gin.Context) { if err != nil { c.Writer.WriteHeader(http.StatusConflict) c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8") - c.Writer.WriteString(err.Error()) + c.Writer.WriteString("评论出错,请联系管理员或稍后再度") } }() conf := config.GetConfig() if err != nil { + logs.Error(err, "获取评论数据错误") return } c.Request.Body = io.NopCloser(bytes.NewBuffer(data)) @@ -46,17 +48,20 @@ func PostComment(c *gin.Context) { c.Request.Body = io.NopCloser(bytes.NewBuffer(data)) req, err := http.NewRequest("POST", conf.PostCommentUrl, strings.NewReader(c.Request.PostForm.Encode())) if err != nil { + logs.Error(err, "创建评论请求错误") return } defer req.Body.Close() req.Header = c.Request.Header.Clone() home, err := url.Parse(wpconfig.GetOption("siteurl")) if err != nil { + logs.Error(err, "解析评论接口错误") return } req.Host = home.Host res, err := cli.Do(req) if err != nil && err != http.ErrUseLastResponse { + logs.Error(err, "请求评论接口错误") return } if res.StatusCode == http.StatusFound { @@ -76,11 +81,7 @@ func PostComment(c *gin.Context) { } up.Host = cu.Host up.Scheme = "http" - newReq, er := http.NewRequest("GET", up.String(), nil) - if er != nil { - err = er - return - } + newReq, _ := http.NewRequest("GET", up.String(), nil) newReq.Host = home.Host newReq.Header.Set("Cookie", strings.Join(slice.Map(c.Request.Cookies(), func(t *http.Cookie) string { return fmt.Sprintf("%s=%s", t.Name, t.Value) @@ -116,9 +117,23 @@ func PostComment(c *gin.Context) { c.Redirect(http.StatusFound, res.Header.Get("Location")) return } - s, err := io.ReadAll(res.Body) + var r io.Reader + if res.Header.Get("Content-Encoding") == "gzip" { + r, err = gzip.NewReader(res.Body) + if err != nil { + logs.Error(err, "gzip解压错误") + return + } + } else { + r = res.Body + } + s, err := io.ReadAll(r) if err != nil { + logs.Error(err, "读取结果错误") return } - err = errors.New(string(s)) + c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8") + c.Writer.WriteHeader(res.StatusCode) + _, _ = c.Writer.Write(s) + } diff --git a/internal/pkg/logs/log.go b/internal/pkg/logs/log.go index 6899613..deffc18 100644 --- a/internal/pkg/logs/log.go +++ b/internal/pkg/logs/log.go @@ -61,7 +61,7 @@ func Errs(err error, depth int, desc string, args ...any) { } func Error(err error, desc string, args ...any) { - Errs(err, 3, desc, args) + Errs(err, 3, desc, args...) } func IfError(err error, desc string, args ...any) {