mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-09 23:22:27 +08:00
删除冗余代码
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
@@ -25,8 +24,6 @@ type Server struct {
|
|||||||
|
|
||||||
skipValidate bool
|
skipValidate bool
|
||||||
|
|
||||||
openID string
|
|
||||||
|
|
||||||
messageHandler func(*message.MixMessage) *message.Reply
|
messageHandler func(*message.MixMessage) *message.Reply
|
||||||
|
|
||||||
RequestRawXMLMsg []byte
|
RequestRawXMLMsg []byte
|
||||||
@@ -34,10 +31,9 @@ type Server struct {
|
|||||||
ResponseRawXMLMsg []byte
|
ResponseRawXMLMsg []byte
|
||||||
ResponseMsg interface{}
|
ResponseMsg interface{}
|
||||||
|
|
||||||
isSafeMode bool
|
random []byte
|
||||||
random []byte
|
nonce string
|
||||||
nonce string
|
timestamp int64
|
||||||
timestamp int64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewServer init
|
//NewServer init
|
||||||
@@ -52,9 +48,6 @@ func (srv *Server) VerifyURL() (string, error) {
|
|||||||
nonce := srv.Query("nonce")
|
nonce := srv.Query("nonce")
|
||||||
signature := srv.Query("msg_signature")
|
signature := srv.Query("msg_signature")
|
||||||
echoStr := srv.Query("echostr")
|
echoStr := srv.Query("echostr")
|
||||||
log.Info("Signature----", util.CalSignature(srv.Token, timestamp, nonce))
|
|
||||||
log.Info("Signature----", util.Signature(srv.Token, timestamp, nonce, echoStr))
|
|
||||||
log.Info("srv.Token---", srv.Token)
|
|
||||||
if signature != util.Signature(srv.Token, timestamp, nonce, echoStr) {
|
if signature != util.Signature(srv.Token, timestamp, nonce, echoStr) {
|
||||||
return "", NewSDKErr(40001)
|
return "", NewSDKErr(40001)
|
||||||
}
|
}
|
||||||
@@ -73,17 +66,6 @@ func (srv *Server) SkipValidate(skip bool) {
|
|||||||
|
|
||||||
//Serve 处理微信的请求消息
|
//Serve 处理微信的请求消息
|
||||||
func (srv *Server) Serve() error {
|
func (srv *Server) Serve() error {
|
||||||
if !srv.Validate() {
|
|
||||||
log.Error("Validate Signature Failed.")
|
|
||||||
return fmt.Errorf("请求校验失败")
|
|
||||||
}
|
|
||||||
|
|
||||||
echostr, exists := srv.GetQuery("echostr")
|
|
||||||
if exists {
|
|
||||||
srv.String(echostr)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
response, err := srv.handleRequest()
|
response, err := srv.handleRequest()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -91,7 +73,6 @@ func (srv *Server) Serve() error {
|
|||||||
|
|
||||||
//debug print request msg
|
//debug print request msg
|
||||||
log.Debugf("request msg =%s", string(srv.RequestRawXMLMsg))
|
log.Debugf("request msg =%s", string(srv.RequestRawXMLMsg))
|
||||||
|
|
||||||
return srv.buildResponse(response)
|
return srv.buildResponse(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,15 +90,6 @@ func (srv *Server) Validate() bool {
|
|||||||
|
|
||||||
//HandleRequest 处理微信的请求
|
//HandleRequest 处理微信的请求
|
||||||
func (srv *Server) handleRequest() (reply *message.Reply, err error) {
|
func (srv *Server) handleRequest() (reply *message.Reply, err error) {
|
||||||
//set isSafeMode
|
|
||||||
srv.isSafeMode = false
|
|
||||||
encryptType := srv.Query("encrypt_type")
|
|
||||||
if encryptType == "aes" {
|
|
||||||
srv.isSafeMode = true
|
|
||||||
}
|
|
||||||
|
|
||||||
//set openID
|
|
||||||
srv.openID = srv.Query("openid")
|
|
||||||
|
|
||||||
var msg interface{}
|
var msg interface{}
|
||||||
msg, err = srv.getMessage()
|
msg, err = srv.getMessage()
|
||||||
@@ -133,45 +105,34 @@ func (srv *Server) handleRequest() (reply *message.Reply, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetOpenID return openID
|
|
||||||
func (srv *Server) GetOpenID() string {
|
|
||||||
return srv.openID
|
|
||||||
}
|
|
||||||
|
|
||||||
//getMessage 解析微信返回的消息
|
//getMessage 解析微信返回的消息
|
||||||
func (srv *Server) getMessage() (interface{}, error) {
|
func (srv *Server) getMessage() (interface{}, error) {
|
||||||
var rawXMLMsgBytes []byte
|
var rawXMLMsgBytes []byte
|
||||||
var err error
|
var err error
|
||||||
if srv.isSafeMode {
|
|
||||||
var encryptedXMLMsg message.EncryptedXMLMsg
|
|
||||||
if err := xml.NewDecoder(srv.Request.Body).Decode(&encryptedXMLMsg); err != nil {
|
|
||||||
return nil, fmt.Errorf("从body中解析xml失败,err=%v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//验证消息签名
|
var encryptedXMLMsg message.EncryptedXMLMsg
|
||||||
timestamp := srv.Query("timestamp")
|
if err := xml.NewDecoder(srv.Request.Body).Decode(&encryptedXMLMsg); err != nil {
|
||||||
srv.timestamp, err = strconv.ParseInt(timestamp, 10, 32)
|
return nil, fmt.Errorf("从body中解析xml失败,err=%v", err)
|
||||||
if err != nil {
|
}
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
nonce := srv.Query("nonce")
|
|
||||||
srv.nonce = nonce
|
|
||||||
msgSignature := srv.Query("msg_signature")
|
|
||||||
msgSignatureGen := util.Signature(srv.Token, timestamp, nonce, encryptedXMLMsg.EncryptedMsg)
|
|
||||||
if msgSignature != msgSignatureGen {
|
|
||||||
return nil, fmt.Errorf("消息不合法,验证签名失败")
|
|
||||||
}
|
|
||||||
|
|
||||||
//解密
|
//验证消息签名
|
||||||
srv.random, rawXMLMsgBytes, err = util.DecryptMsg(srv.CorpID, encryptedXMLMsg.EncryptedMsg, srv.EncodingAESKey)
|
timestamp := srv.Query("timestamp")
|
||||||
if err != nil {
|
srv.timestamp, err = strconv.ParseInt(timestamp, 10, 32)
|
||||||
return nil, fmt.Errorf("消息解密失败, err=%v", err)
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
} else {
|
}
|
||||||
rawXMLMsgBytes, err = ioutil.ReadAll(srv.Request.Body)
|
nonce := srv.Query("nonce")
|
||||||
if err != nil {
|
srv.nonce = nonce
|
||||||
return nil, fmt.Errorf("从body中解析xml失败, err=%v", err)
|
msgSignature := srv.Query("msg_signature")
|
||||||
}
|
msgSignatureGen := util.Signature(srv.Token, timestamp, nonce, encryptedXMLMsg.EncryptedMsg)
|
||||||
|
if msgSignature != msgSignatureGen {
|
||||||
|
return nil, fmt.Errorf("消息不合法,验证签名失败")
|
||||||
|
}
|
||||||
|
|
||||||
|
//解密
|
||||||
|
srv.random, rawXMLMsgBytes, err = util.DecryptMsg(srv.CorpID, encryptedXMLMsg.EncryptedMsg, srv.EncodingAESKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("消息解密失败, err=%v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
srv.RequestRawXMLMsg = rawXMLMsgBytes
|
srv.RequestRawXMLMsg = rawXMLMsgBytes
|
||||||
@@ -244,24 +205,24 @@ func (srv *Server) buildResponse(reply *message.Reply) (err error) {
|
|||||||
func (srv *Server) Send() (err error) {
|
func (srv *Server) Send() (err error) {
|
||||||
replyMsg := srv.ResponseMsg
|
replyMsg := srv.ResponseMsg
|
||||||
log.Debugf("response msg =%+v", replyMsg)
|
log.Debugf("response msg =%+v", replyMsg)
|
||||||
if srv.isSafeMode {
|
|
||||||
//安全模式下对消息进行加密
|
//安全模式下对消息进行加密
|
||||||
var encryptedMsg []byte
|
var encryptedMsg []byte
|
||||||
encryptedMsg, err = util.EncryptMsg(srv.random, srv.ResponseRawXMLMsg, srv.CorpID, srv.EncodingAESKey)
|
encryptedMsg, err = util.EncryptMsg(srv.random, srv.ResponseRawXMLMsg, srv.CorpID, srv.EncodingAESKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
|
||||||
//TODO 如果获取不到timestamp nonce 则自己生成
|
|
||||||
timestamp := srv.timestamp
|
|
||||||
timestampStr := strconv.FormatInt(timestamp, 10)
|
|
||||||
msgSignature := util.Signature(srv.Token, timestampStr, srv.nonce, string(encryptedMsg))
|
|
||||||
replyMsg = message.ResponseEncryptedXMLMsg{
|
|
||||||
EncryptedMsg: string(encryptedMsg),
|
|
||||||
MsgSignature: msgSignature,
|
|
||||||
Timestamp: timestamp,
|
|
||||||
Nonce: srv.nonce,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
//TODO 如果获取不到timestamp nonce 则自己生成
|
||||||
|
timestamp := srv.timestamp
|
||||||
|
timestampStr := strconv.FormatInt(timestamp, 10)
|
||||||
|
msgSignature := util.Signature(srv.Token, timestampStr, srv.nonce, string(encryptedMsg))
|
||||||
|
replyMsg = message.ResponseEncryptedXMLMsg{
|
||||||
|
EncryptedMsg: string(encryptedMsg),
|
||||||
|
MsgSignature: msgSignature,
|
||||||
|
Timestamp: timestamp,
|
||||||
|
Nonce: srv.nonce,
|
||||||
|
}
|
||||||
|
|
||||||
if replyMsg != nil {
|
if replyMsg != nil {
|
||||||
srv.XML(replyMsg)
|
srv.XML(replyMsg)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user