forked from chanxuehong/wechat
/
agent_server.go
101 lines (85 loc) · 2.75 KB
/
agent_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
// @description wechat 是腾讯微信公众平台 api 的 golang 语言封装
// @link https://github.com/chanxuehong/wechat for the canonical source repository
// @license https://github.com/chanxuehong/wechat/blob/master/LICENSE
// @authors chanxuehong(chanxuehong@gmail.com)
package corp
import (
"bytes"
"errors"
"sync"
)
type AgentServer interface {
Token() string // 获取应用的Token, 用于校验签名.
CurrentAESKey() [32]byte // 获取当前有效的 AES 加密 Key
LastAESKey() (key [32]byte, valid bool) // 获取上一个有效的 AES 加密 Key
CorpId() string // 企业号Id, 用于约束消息的 CorpId, 如果为空表示不约束, 同时忽略 AgentId 的约束
AgentId() int64 // 应用Id, 用于约束消息的 AgentId, 如果为 -1 表示不约束
MessageHandler() MessageHandler // 获取 MessageHandler
}
var _ AgentServer = (*DefaultAgentServer)(nil)
type DefaultAgentServer struct {
corpId string
agentId int64
token string
rwmutex sync.RWMutex
currentAESKey [32]byte // 当前的 AES Key
lastAESKey [32]byte // 最后一个 AES Key
isLastAESKeyValid bool // lastAESKey 是否有效, 如果 lastAESKey 是 zero 则无效
messageHandler MessageHandler
}
// NewDefaultAgentServer 创建一个新的 DefaultAgentServer.
func NewDefaultAgentServer(corpId string, agentId int64, token string, aesKey []byte, handler MessageHandler) (srv *DefaultAgentServer) {
if len(aesKey) != 32 {
panic("the length of aesKey must equal to 32")
}
if handler == nil {
panic("nil MessageHandler")
}
srv = &DefaultAgentServer{
corpId: corpId,
agentId: agentId,
token: token,
messageHandler: handler,
}
copy(srv.currentAESKey[:], aesKey)
return
}
func (srv *DefaultAgentServer) CorpId() string {
return srv.corpId
}
func (srv *DefaultAgentServer) AgentId() int64 {
return srv.agentId
}
func (srv *DefaultAgentServer) Token() string {
return srv.token
}
func (srv *DefaultAgentServer) MessageHandler() MessageHandler {
return srv.messageHandler
}
func (srv *DefaultAgentServer) CurrentAESKey() (key [32]byte) {
srv.rwmutex.RLock()
key = srv.currentAESKey
srv.rwmutex.RUnlock()
return
}
func (srv *DefaultAgentServer) LastAESKey() (key [32]byte, valid bool) {
srv.rwmutex.RLock()
key = srv.lastAESKey
valid = srv.isLastAESKeyValid
srv.rwmutex.RUnlock()
return
}
func (srv *DefaultAgentServer) UpdateAESKey(aesKey []byte) (err error) {
if len(aesKey) != 32 {
return errors.New("the length of aesKey must equal to 32")
}
srv.rwmutex.Lock()
defer srv.rwmutex.Unlock()
if bytes.Equal(aesKey, srv.currentAESKey[:]) {
return
}
srv.isLastAESKeyValid = true
srv.lastAESKey = srv.currentAESKey
copy(srv.currentAESKey[:], aesKey)
return
}