完善添加评论

This commit is contained in:
xing 2023-04-10 22:28:16 +08:00
parent e505ee2e03
commit 0d8ee89580
3 changed files with 27 additions and 12 deletions

View File

@ -69,12 +69,12 @@ maxRequestSleepNum: 100
maxRequestNum: 500 maxRequestNum: 500
# 单ip同时最大搜索请求数 # 单ip同时最大搜索请求数
singleIpSearchNum: 10 singleIpSearchNum: 10
# 错误日志输出路径 # 错误日志输出路径 stdout|stderr|file path 默认为stderr
logOutput: err.log logOutput: err.log
# Gzip # Gzip
gzip: false gzip: false
# 提交评论url # 提交评论url host需为ip形式
postCommentUrl: http://wp.test/wp-comments-post.php postCommentUrl: http://127.0.0.1/wp-comments-post.php
# TrustIps # TrustIps
trustIps: [] trustIps: []
# 分页器间隔数 # 分页器间隔数

View File

@ -2,6 +2,7 @@ package actions
import ( import (
"bytes" "bytes"
"compress/gzip"
"errors" "errors"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
@ -31,11 +32,12 @@ func PostComment(c *gin.Context) {
if err != nil { if err != nil {
c.Writer.WriteHeader(http.StatusConflict) c.Writer.WriteHeader(http.StatusConflict)
c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8") c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
c.Writer.WriteString(err.Error()) c.Writer.WriteString("评论出错,请联系管理员或稍后再度")
} }
}() }()
conf := config.GetConfig() conf := config.GetConfig()
if err != nil { if err != nil {
logs.Error(err, "获取评论数据错误")
return return
} }
c.Request.Body = io.NopCloser(bytes.NewBuffer(data)) 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)) c.Request.Body = io.NopCloser(bytes.NewBuffer(data))
req, err := http.NewRequest("POST", conf.PostCommentUrl, strings.NewReader(c.Request.PostForm.Encode())) req, err := http.NewRequest("POST", conf.PostCommentUrl, strings.NewReader(c.Request.PostForm.Encode()))
if err != nil { if err != nil {
logs.Error(err, "创建评论请求错误")
return return
} }
defer req.Body.Close() defer req.Body.Close()
req.Header = c.Request.Header.Clone() req.Header = c.Request.Header.Clone()
home, err := url.Parse(wpconfig.GetOption("siteurl")) home, err := url.Parse(wpconfig.GetOption("siteurl"))
if err != nil { if err != nil {
logs.Error(err, "解析评论接口错误")
return return
} }
req.Host = home.Host req.Host = home.Host
res, err := cli.Do(req) res, err := cli.Do(req)
if err != nil && err != http.ErrUseLastResponse { if err != nil && err != http.ErrUseLastResponse {
logs.Error(err, "请求评论接口错误")
return return
} }
if res.StatusCode == http.StatusFound { if res.StatusCode == http.StatusFound {
@ -76,11 +81,7 @@ func PostComment(c *gin.Context) {
} }
up.Host = cu.Host up.Host = cu.Host
up.Scheme = "http" up.Scheme = "http"
newReq, er := http.NewRequest("GET", up.String(), nil) newReq, _ := http.NewRequest("GET", up.String(), nil)
if er != nil {
err = er
return
}
newReq.Host = home.Host newReq.Host = home.Host
newReq.Header.Set("Cookie", strings.Join(slice.Map(c.Request.Cookies(), func(t *http.Cookie) string { 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) 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")) c.Redirect(http.StatusFound, res.Header.Get("Location"))
return 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 { if err != nil {
logs.Error(err, "读取结果错误")
return 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)
} }

View File

@ -61,7 +61,7 @@ func Errs(err error, depth int, desc string, args ...any) {
} }
func Error(err error, 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) { func IfError(err error, desc string, args ...any) {