159 lines
4.1 KiB
Go
159 lines
4.1 KiB
Go
package controllers
|
||
|
||
import (
|
||
"io"
|
||
"strings"
|
||
"time"
|
||
|
||
"server/services/wechatmini"
|
||
"server/services/wechatmp"
|
||
|
||
"github.com/beego/beego/v2/core/logs"
|
||
beego "github.com/beego/beego/v2/server/web"
|
||
)
|
||
|
||
type WechatMiniCallbackController struct {
|
||
beego.Controller
|
||
}
|
||
|
||
func (c *WechatMiniCallbackController) Callback() {
|
||
cfgs, err := wechatmini.LoadPushConfigs()
|
||
if err != nil || len(cfgs) == 0 {
|
||
c.Ctx.Output.SetStatus(403)
|
||
c.Ctx.Output.Body([]byte("wechat mini push not configured"))
|
||
return
|
||
}
|
||
|
||
q := c.Ctx.Request.URL.Query()
|
||
timestamp := q.Get("timestamp")
|
||
nonce := q.Get("nonce")
|
||
|
||
if strings.EqualFold(c.Ctx.Input.Method(), "GET") {
|
||
echostr := q.Get("echostr")
|
||
if reply, ok := verifyPushEchostr(cfgs, timestamp, nonce, q.Get("signature"), q.Get("msg_signature"), echostr); ok {
|
||
c.Ctx.Output.Body([]byte(reply))
|
||
return
|
||
}
|
||
c.Ctx.Output.SetStatus(403)
|
||
c.Ctx.Output.Body([]byte("invalid signature"))
|
||
return
|
||
}
|
||
|
||
body, _ := io.ReadAll(c.Ctx.Request.Body)
|
||
raw := strings.TrimSpace(string(body))
|
||
if raw == "" {
|
||
c.Ctx.Output.Body([]byte(""))
|
||
return
|
||
}
|
||
|
||
format := wechatmini.DetectPushFormat(raw)
|
||
msg, perr := parsePushBody(raw, format)
|
||
if perr != nil {
|
||
logs.Info("[wechat-mini] 推送消息解析失败: %v", perr)
|
||
c.Ctx.Output.Body([]byte(""))
|
||
return
|
||
}
|
||
|
||
var cfg *wechatmini.PushConfig
|
||
encrypted := false
|
||
|
||
if msg.Encrypt != "" {
|
||
msgSig := q.Get("msg_signature")
|
||
if msgSig == "" {
|
||
msgSig = msg.MsgSignature
|
||
}
|
||
if c2, ok := wechatmini.MatchPushConfigByMsgSignature(cfgs, timestamp, nonce, msg.Encrypt, msgSig); ok &&
|
||
c2.EncryptMode == wechatmp.EncryptModeSafe {
|
||
plain, derr := wechatmini.DecryptPushMessage(c2, msg.Encrypt)
|
||
if derr != nil {
|
||
logs.Info("[wechat-mini] 推送消息解密失败: %v", derr)
|
||
c.Ctx.Output.Body([]byte(""))
|
||
return
|
||
}
|
||
inner, ierr := parsePushBody(plain, format)
|
||
if ierr != nil {
|
||
logs.Info("[wechat-mini] 解密后消息解析失败: %v", ierr)
|
||
c.Ctx.Output.Body([]byte(""))
|
||
return
|
||
}
|
||
cfg = c2
|
||
msg = inner
|
||
encrypted = true
|
||
}
|
||
}
|
||
|
||
if cfg == nil {
|
||
if c3, ok := wechatmini.MatchPushConfigBySignature(cfgs, timestamp, nonce, q.Get("signature")); ok {
|
||
cfg = c3
|
||
}
|
||
}
|
||
|
||
if cfg == nil {
|
||
logs.Info("[wechat-mini] 推送消息签名校验失败(timestamp=%s nonce=%s)", timestamp, nonce)
|
||
c.Ctx.Output.Body([]byte(""))
|
||
return
|
||
}
|
||
|
||
logs.Info("[wechat-mini] 收到推送: appid=%s type=%s event=%s from=%s key=%s",
|
||
cfg.AppID, msg.MsgType, msg.Event, msg.FromUserName, msg.EventKey)
|
||
|
||
reply := wechatmini.HandlePushMessage(cfg, msg)
|
||
if reply == "" {
|
||
c.Ctx.Output.Body([]byte(""))
|
||
return
|
||
}
|
||
|
||
now := time.Now().Unix()
|
||
replyBody, berr := wechatmini.BuildTextReply(cfg.DataFormat, msg.FromUserName, msg.ToUserName, reply, now)
|
||
if berr != nil {
|
||
c.Ctx.Output.Body([]byte(""))
|
||
return
|
||
}
|
||
if encrypted {
|
||
ts := q.Get("timestamp")
|
||
if ts == "" {
|
||
ts = msg.TimeStamp
|
||
}
|
||
replyNonce := nonce
|
||
if replyNonce == "" {
|
||
replyNonce = msg.Nonce
|
||
}
|
||
enc, eerr := wechatmini.BuildEncryptedReply(cfg, replyBody, ts, replyNonce, cfg.DataFormat)
|
||
if eerr != nil {
|
||
c.Ctx.Output.Body([]byte(""))
|
||
return
|
||
}
|
||
replyBody = enc
|
||
}
|
||
|
||
if cfg.DataFormat == wechatmini.DataFormatJSON {
|
||
c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
|
||
} else {
|
||
c.Ctx.Output.Header("Content-Type", "application/xml; charset=utf-8")
|
||
}
|
||
c.Ctx.Output.Body([]byte(replyBody))
|
||
}
|
||
|
||
func parsePushBody(raw, format string) (*wechatmini.PushMessage, error) {
|
||
if format == wechatmini.DataFormatJSON {
|
||
return wechatmini.ParsePushJSON(raw)
|
||
}
|
||
return wechatmini.ParsePushXML(raw)
|
||
}
|
||
|
||
func verifyPushEchostr(cfgs []wechatmini.PushConfig, timestamp, nonce, signature, msgSignature, echostr string) (string, bool) {
|
||
if echostr == "" {
|
||
return "", false
|
||
}
|
||
if cfg, ok := wechatmini.MatchPushConfigByMsgSignature(cfgs, timestamp, nonce, echostr, msgSignature); ok &&
|
||
cfg.EncryptMode == wechatmp.EncryptModeSafe {
|
||
if plain, derr := wechatmini.DecryptPushMessage(cfg, echostr); derr == nil {
|
||
return plain, true
|
||
}
|
||
}
|
||
if _, ok := wechatmini.MatchPushConfigBySignature(cfgs, timestamp, nonce, signature); ok {
|
||
return echostr, true
|
||
}
|
||
return "", false
|
||
}
|