-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.go
102 lines (88 loc) · 2.9 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// @description wechat2 是腾讯微信公众平台 api 的 golang 语言封装
// @link https://github.com/philsong/wechat2 for the canonical source repository
// @license https://github.com/philsong/wechat2/blob/master/LICENSE
// @authors chanxuehong(chanxuehong@gmail.com)
package mp
import (
"errors"
"sync"
)
// 公众号服务端接口, 处理单个公众号的消息(事件)请求.
type WechatServer interface {
WechatId() string // 获取公众号的原始ID, 等于后台中的 公众号设置-->帐号详情-->原始ID
Token() string // 获取公众号的Token, 和后台中的设置相等
AppId() string // 获取公众号的 AppId
CurrentAESKey() [32]byte // 获取当前有效的 AES 加密 Key
LastAESKey() [32]byte // 获取最后一个有效的 AES 加密 Key
MessageHandler() MessageHandler // 获取 MessageHandler
}
var _ WechatServer = new(DefaultWechatServer)
type DefaultWechatServer struct {
wechatId string
token string
appId string
rwmutex sync.RWMutex
currentAESKey [32]byte // 当前的 AES Key
lastAESKey [32]byte // 最后一个 AES Key
isLastAESKeyValid bool // lastAESKey 是否有效, 如果 lastAESKey 是 zero 则无效
messageHandler MessageHandler
}
// NewDefaultWechatServer 创建一个新的 DefaultWechatServer.
// 如果不知道自己的 AppId 是多少, 可以先随便填入一个字符串,
// 这样正常情况下会出现 AppId mismatch 错误, 错误的 have 后面的就是正确的 AppId.
func NewDefaultWechatServer(wechatId, token, appId string, AESKey []byte,
messageHandler MessageHandler) (srv *DefaultWechatServer) {
if len(AESKey) != 32 {
panic("mp: the length of AESKey must equal to 32")
}
if messageHandler == nil {
panic("mp: nil messageHandler")
}
srv = &DefaultWechatServer{
wechatId: wechatId,
token: token,
appId: appId,
messageHandler: messageHandler,
}
copy(srv.currentAESKey[:], AESKey)
return
}
func (srv *DefaultWechatServer) WechatId() string {
return srv.wechatId
}
func (srv *DefaultWechatServer) Token() string {
return srv.token
}
func (srv *DefaultWechatServer) AppId() string {
return srv.appId
}
func (srv *DefaultWechatServer) MessageHandler() MessageHandler {
return srv.messageHandler
}
func (srv *DefaultWechatServer) CurrentAESKey() (key [32]byte) {
srv.rwmutex.RLock()
key = srv.currentAESKey
srv.rwmutex.RUnlock()
return
}
func (srv *DefaultWechatServer) LastAESKey() (key [32]byte) {
srv.rwmutex.RLock()
if srv.isLastAESKeyValid {
key = srv.lastAESKey
} else {
key = srv.currentAESKey
}
srv.rwmutex.RUnlock()
return
}
func (srv *DefaultWechatServer) UpdateAESKey(AESKey []byte) (err error) {
if len(AESKey) != 32 {
return errors.New("the length of AESKey must equal to 32")
}
srv.rwmutex.Lock()
srv.lastAESKey = srv.currentAESKey
srv.isLastAESKeyValid = true
copy(srv.currentAESKey[:], AESKey)
srv.rwmutex.Unlock()
return
}