代码高亮显示,布局优化,修复评论

This commit is contained in:
xing 2022-10-31 19:47:29 +08:00
parent 7c75b3f5ab
commit 1cf6327385
13 changed files with 414 additions and 41 deletions

View File

@ -1,12 +1,18 @@
package actions package actions
import ( import (
"bytes"
"errors" "errors"
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/actions/common"
"github/fthvgb1/wp-go/logs"
"github/fthvgb1/wp-go/mail"
"github/fthvgb1/wp-go/vars" "github/fthvgb1/wp-go/vars"
"io/ioutil" "io"
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -17,7 +23,7 @@ func PostComment(c *gin.Context) {
Jar: jar, Jar: jar,
Timeout: time.Second * 3, Timeout: time.Second * 3,
} }
body, err := ioutil.ReadAll(c.Request.Body) data, err := c.GetRawData()
defer func() { defer func() {
if err != nil { if err != nil {
c.String(http.StatusConflict, err.Error()) c.String(http.StatusConflict, err.Error())
@ -26,6 +32,16 @@ func PostComment(c *gin.Context) {
if err != nil { if err != nil {
return return
} }
c.Request.Body = io.NopCloser(bytes.NewBuffer(data))
i := c.PostForm("comment_post_ID")
author := c.PostForm("author")
m := c.PostForm("email")
comment := c.PostForm("comment")
c.Request.Body = io.NopCloser(bytes.NewBuffer(data))
body, err := io.ReadAll(c.Request.Body)
if err != nil {
return
}
req, err := http.NewRequest("POST", vars.Conf.PostCommentUrl, strings.NewReader(string(body))) req, err := http.NewRequest("POST", vars.Conf.PostCommentUrl, strings.NewReader(string(body)))
if err != nil { if err != nil {
return return
@ -38,14 +54,31 @@ func PostComment(c *gin.Context) {
if err != nil { if err != nil {
return return
} }
if res.StatusCode == http.StatusOK && res.Request.Response.StatusCode == http.StatusFound { //todo 待优化
if res.Request.Response != nil && res.Request.Response.StatusCode == http.StatusFound {
for _, cookie := range res.Request.Response.Cookies() { for _, cookie := range res.Request.Response.Cookies() {
c.SetCookie(cookie.Name, cookie.Value, cookie.MaxAge, cookie.Path, cookie.Domain, cookie.Secure, cookie.HttpOnly) c.SetCookie(cookie.Name, cookie.Value, cookie.MaxAge, cookie.Path, cookie.Domain, cookie.Secure, cookie.HttpOnly)
} }
c.Redirect(http.StatusFound, res.Request.Response.Header.Get("Location")) c.Redirect(http.StatusFound, res.Request.Response.Header.Get("Location"))
cc := c.Copy()
go func() {
id, err := strconv.ParseUint(i, 10, 64)
if err != nil {
logs.ErrPrintln(err, "获取文档id", i)
return return
} }
s, err := ioutil.ReadAll(res.Body) post, err := common.GetPostById(cc, id)
if err != nil {
logs.ErrPrintln(err, "获取文档", id)
return
}
su := fmt.Sprintf("%s[%s]发表了评论对文档[%v]的评论", author, m, post.PostTitle)
err = mail.SendMail([]string{vars.Conf.Mail.User}, su, comment)
logs.ErrPrintln(err, "发送邮件")
}()
return
}
s, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return return
} }

View File

@ -1,8 +1,12 @@
package actions package actions
import ( import (
"fmt"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/helper"
"github/fthvgb1/wp-go/models"
"github/fthvgb1/wp-go/phpass"
"net/http" "net/http"
"strings" "strings"
) )
@ -24,5 +28,13 @@ func Login(c *gin.Context) {
c.Error(err) c.Error(err)
return return
} }
pass, err := phpass.NewPasswordHash(8, true).HashPassword(password)
if err != nil {
c.Error(err)
return
}
cohash := fmt.Sprintf("wp-postpass_%s", helper.StringMd5(models.Options["siteurl"]))
c.SetCookie(cohash, pass, 24*3600, "/", "", false, false)
c.Redirect(http.StatusFound, ref) c.Redirect(http.StatusFound, ref)
} }

253
phpass/phpass.go Normal file
View File

@ -0,0 +1,253 @@
package phpass
import (
"crypto/md5"
"fmt"
"github/fthvgb1/wp-go/helper"
"golang.org/x/crypto/bcrypt"
"io"
"os"
"strconv"
"strings"
"time"
"unicode/utf8"
)
type PasswordHash struct {
itoa64 string
iterationCountLog2 int
portableHashes bool
randomState string
}
func NewPasswordHash(iterationCountLog2 int, portableHashes bool) *PasswordHash {
if iterationCountLog2 < 4 || iterationCountLog2 > 31 {
iterationCountLog2 = 8
}
return &PasswordHash{
iterationCountLog2: iterationCountLog2,
portableHashes: portableHashes,
itoa64: "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
randomState: strconv.Itoa(os.Getgid()),
}
}
func (p *PasswordHash) getRandomBytes(count int) (r string, err error) {
urand := "/dev/urandom"
f, err := os.OpenFile(urand, os.O_RDWR, 0644)
if err != nil {
return "", err
}
defer f.Close()
buf := make([]byte, count)
_, err = f.Read(buf)
if err != nil {
return "", err
}
r = string(buf)
if len(buf) < count {
r = ""
for i := 0; i < count; i = i + 16 {
p.randomState = helper.StringMd5(fmt.Sprintf("%d%s", time.Now().UnixMilli(), p.randomState))
n, err := md5Raw(p.randomState)
if err != nil {
return "", err
}
r = fmt.Sprintf("%s%s", r, n)
}
r = r[0:count]
}
return
}
func (p *PasswordHash) Encode64(input string, count int) (out string) {
i := 0
s := strings.Builder{}
for {
if i >= count {
break
}
v := 0
if i < len(input) {
v = int(input[i])
}
s.WriteString(string(p.itoa64[v&0x3f]))
i++
if i < count {
if i < len(input) {
v |= int(input[i]) << 8
} else {
v = 0
}
}
s.WriteString(string(p.itoa64[(v>>6)&0x3f]))
if i >= count {
break
}
i++
if i < count {
if i < len(input) {
v |= int(input[i]) << 16
} else {
v = 0
}
}
s.WriteString(string(p.itoa64[(v>>12)&0x3f]))
if i >= count {
break
}
i++
s.WriteString(string(p.itoa64[(v>>18)&0x3f]))
}
out = s.String()
return
}
func (p *PasswordHash) CryptPrivate(password, set string) (rr string, err error) {
rr = "*0"
r := []rune(rr)
setting := []rune(set)
if string(r) == string(setting[0:2]) {
rr = "*1"
}
id := setting[0:3]
idx := string(id)
if idx != "$P$" && idx != "$H$" {
return
}
log2 := strings.Index(p.itoa64, string(setting[3]))
if log2 < 7 || log2 > 30 {
return
}
count := 1 << log2
l := 12
if len(setting) < 12 {
l = len(setting)
}
salt := setting[4:l]
if len(salt) != 8 {
return
}
hash, err := md5Raw(fmt.Sprintf("%s%s", string(salt), password))
if err != nil {
return
}
for i := 0; i < count; i++ {
hash, err = md5Raw(fmt.Sprintf("%s%s", hash, password))
if err != nil {
return
}
}
rr = string(setting[0:l])
rr = fmt.Sprintf("%s%s", rr, p.Encode64(hash, 16))
return
}
func (p *PasswordHash) genSaltBlowFish(input string) (out string, err error) {
itoa64 := "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
s := strings.Builder{}
s.WriteString("$2a$")
s.WriteString(fmt.Sprintf("%c", '0'+p.iterationCountLog2/10))
s.WriteString(fmt.Sprintf("%c", '0'+p.iterationCountLog2%10))
s.WriteString("$")
i := 0
for {
c1 := int(input[i])
i++
s.WriteString(string(itoa64[c1>>2]))
c1 = (c1 & 0x03) << 4
if i >= 16 {
s.WriteString(string(itoa64[c1]))
break
}
c2 := int(input[i])
i++
c1 |= c2 >> 4
s.WriteString(string(input[c1]))
c1 = (c2 & 0x0f) << 2
c2 = int(input[i])
i++
c1 |= c2 >> 6
s.WriteString(string(itoa64[c1]))
s.WriteString(string(itoa64[c2]))
}
out = s.String()
return
}
func (p *PasswordHash) HashPassword(pass string) (r string, err error) {
if utf8.RuneCountInString(pass) > 4096 {
r = "*"
return
}
random := ""
hash := ""
if !p.portableHashes {
random, err = p.getRandomBytes(16)
if err != nil {
return
}
h, er := bcrypt.GenerateFromPassword([]byte(pass), 16)
if er != nil {
err = er
return
}
hash = string(h)
if len(hash) == 60 {
r = hash
return
}
}
if len(random) < 6 {
random, err = p.getRandomBytes(6)
if err != nil {
return
}
salt := p.genSaltPrivate(random)
hash, err = p.CryptPrivate(pass, salt)
if err != nil {
return
}
if len(hash) == 34 {
r = hash
return
}
}
r = "*"
return
}
func (p *PasswordHash) CheckPassword(pass, hash string) bool {
if utf8.RuneCountInString(pass) > 4096 {
return false
}
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(pass))
if err != nil {
return false
}
return true
}
func (p *PasswordHash) genSaltPrivate(input string) string {
s := strings.Builder{}
s.WriteString("$P$")
min := 30
if p.iterationCountLog2+5 < 30 {
min = p.iterationCountLog2 + 5
}
s.WriteString(string(p.itoa64[min]))
s.WriteString(p.Encode64(input, 6))
return s.String()
}
func md5Raw(s string) (string, error) {
h := md5.New()
_, err := io.WriteString(h, s)
if err != nil {
return "", err
}
return string(h.Sum(nil)), err
}

View File

@ -71,7 +71,7 @@ func SetupRouter() *gin.Engine {
r.GET("/p/:id/feed", actions.PostFeed) r.GET("/p/:id/feed", actions.PostFeed)
r.GET("/feed", actions.Feed) r.GET("/feed", actions.Feed)
r.GET("/comments/feed", actions.CommentsFeed) r.GET("/comments/feed", actions.CommentsFeed)
r.POST("/comment", actions.PostComment) r.POST("/comment", middleware.FlowLimit(vars.Conf.MaxRequestSleepNum, 5, vars.Conf.SleepTime), actions.PostComment)
if helper.IsContainInArr(gin.Mode(), []string{gin.DebugMode, gin.TestMode}) { if helper.IsContainInArr(gin.Mode(), []string{gin.DebugMode, gin.TestMode}) {
pprof.Register(r, "dev/pprof") pprof.Register(r, "dev/pprof")
} }

View File

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -34,12 +34,13 @@
{{end}} {{end}}
</div> </div>
<footer id="colophon" class="site-footer"> <footer id="colophon" class="site-footer">
{{template "layout/footer" .}} <div class="site-info">
<a href="https://cn.wordpress.org/" class="imprint">自豪地采用WordPress</a>
</div>
</footer> </footer>
</div> </div>
{{ block "footer" .}} {{template "layout/footer" .}}
{{end}}
</body> </body>
</html> </html>
{{ end }} {{ end }}

View File

@ -1,7 +1,17 @@
{{define "layout/footer"}} {{define "layout/footer"}}
<style>.wp-container-1 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-1 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-1 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<div class="site-info"> <style>.wp-container-2 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-2 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-2 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<a href="https://cn.wordpress.org/" class="imprint">自豪地采用WordPress</a> <style>.wp-container-3 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-3 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-3 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
</div> <style>.wp-container-4 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-4 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-4 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<script src='/wp-content/themes/twentyfifteen/js/skip-link-focus-fix.js?ver=20141028' id='twentyfifteen-skip-link-focus-fix-js'></script>
<script id='twentyfifteen-script-js-extra'>
var screenReaderText = {"expand":"<span class=\"screen-reader-text\">\u5c55\u5f00\u5b50\u83dc\u5355<\/span>","collapse":"<span class=\"screen-reader-text\">\u6298\u53e0\u5b50\u83dc\u5355<\/span>"};
</script>
<script src='/wp-content/themes/twentyfifteen/js/functions.js?ver=20220524' id='twentyfifteen-script-js'></script>
<script src='/wp-content/plugins/enlighter/cache/enlighterjs.min.js?ver=0A0B0C' id='enlighterjs-js'></script>
<script id='enlighterjs-js-after'>
!function(e,n){if("undefined"!=typeof EnlighterJS){var o={"selectors":{"block":"pre.EnlighterJSRAW","inline":"code.EnlighterJSRAW"},"options":{"indent":4,"ampersandCleanup":true,"linehover":true,"rawcodeDbclick":false,"textOverflow":"break","linenumbers":true,"theme":"enlighter","language":"generic","retainCssClasses":false,"collapse":false,"toolbarOuter":"","toolbarTop":"{BTN_RAW}{BTN_COPY}{BTN_WINDOW}{BTN_WEBSITE}","toolbarBottom":""}};(e.EnlighterJSINIT=function(){EnlighterJS.init(o.selectors.block,o.selectors.inline,o.options)})()}else{(n&&(n.error||n.log)||function(){})("Error: EnlighterJS resources not loaded yet!")}}(window,console);
</script>
{{ block "footer" .}}
{{end}}
{{end}} {{end}}

View File

@ -14,6 +14,8 @@
<link href='https://fonts.gstatic.com' crossorigin rel='preconnect' /> <link href='https://fonts.gstatic.com' crossorigin rel='preconnect' />
<link rel="alternate" type="application/rss+xml" title="{{ .title }} &raquo; Feed" href="/feed" /> <link rel="alternate" type="application/rss+xml" title="{{ .title }} &raquo; Feed" href="/feed" />
<link rel="alternate" type="application/rss+xml" title="{{ .title }} &raquo; 评论Feed" href="/comments/feed" /> <link rel="alternate" type="application/rss+xml" title="{{ .title }} &raquo; 评论Feed" href="/comments/feed" />
<link rel='stylesheet' id='enlighterjs-css' href='/wp-content/plugins/enlighter/cache/enlighterjs.min.css?ver=0A0B0C' media='all' />
<script> <script>
window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/svg\/","svgExt":".svg","source":{"concatemoji":"\/wp-includes\/js\/wp-emoji-release.min.js?ver=6.0.2"}}; window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/svg\/","svgExt":".svg","source":{"concatemoji":"\/wp-includes\/js\/wp-emoji-release.min.js?ver=6.0.2"}};
/*! This file is auto-generated */ /*! This file is auto-generated */

View File

@ -124,17 +124,6 @@
{{template "layout/empty"}} {{template "layout/empty"}}
{{end }} {{end }}
{{end}} {{end}}
{{ define "footer"}} {{ define "footer"}}
<style>.wp-container-1 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-1 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-1 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<style>.wp-container-2 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-2 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-2 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<style>.wp-container-3 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-3 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-3 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<style>.wp-container-4 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-4 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-4 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<script src='/wp-content/themes/twentyfifteen/js/skip-link-focus-fix.js?ver=20141028' id='twentyfifteen-skip-link-focus-fix-js'></script>
<script src='/wp-includes/js/comment-reply.min.js?ver=6.0.2' id='comment-reply-js'></script> <script src='/wp-includes/js/comment-reply.min.js?ver=6.0.2' id='comment-reply-js'></script>
<script id='twentyfifteen-script-js-extra'>
var screenReaderText = {"expand":"<span class=\"screen-reader-text\">\u5c55\u5f00\u5b50\u83dc\u5355<\/span>","collapse":"<span class=\"screen-reader-text\">\u6298\u53e0\u5b50\u83dc\u5355<\/span>"};
</script>
<script src='/wp-content/themes/twentyfifteen/js/functions.js?ver=20220524' id='twentyfifteen-script-js'></script>
{{end}} {{end}}

View File

@ -1,7 +1,5 @@
{{template "layout/base" .}} {{template "layout/base" .}}
{{define "head"}}
{{end}}
{{define "content" }} {{define "content" }}
{{if .posts}} {{if .posts}}
<div id="primary" class="content-area"> <div id="primary" class="content-area">
@ -73,16 +71,3 @@
{{end}} {{end}}
{{end}} {{end}}
{{define "footer"}}
<style>.wp-container-1 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-1 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-1 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<style>.wp-container-2 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-2 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-2 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<style>.wp-container-3 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-3 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-3 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<style>.wp-container-4 > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }.wp-container-4 > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }.wp-container-4 > .aligncenter { margin-left: auto !important; margin-right: auto !important; }</style>
<script src='/wp-content/themes/twentyfifteen/js/skip-link-focus-fix.js?ver=20141028' id='twentyfifteen-skip-link-focus-fix-js'></script>
<script id='twentyfifteen-script-js-extra'>
var screenReaderText = {"expand":"<span class=\"screen-reader-text\">\u5c55\u5f00\u5b50\u83dc\u5355<\/span>","collapse":"<span class=\"screen-reader-text\">\u6298\u53e0\u5b50\u83dc\u5355<\/span>"};
</script>
<script src='/wp-content/themes/twentyfifteen/js/functions.js?ver=20220524' id='twentyfifteen-script-js'></script>
{{end}}