-
Notifications
You must be signed in to change notification settings - Fork 1
/
authentication.go
43 lines (39 loc) · 1.11 KB
/
authentication.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
package handletype
import (
"bytes"
"fmt"
. "github.com/TISUnion/MCBot-go/datatype"
)
type Authentication struct {
}
func (*Authentication) Handle(pk *Packet, pl *Player) []byte {
buf := bytes.NewBuffer(*pk.Data)
//获取serverid
serverid := StringInstance.Decode(buf)
//获取rsa参数
publicKey := StringInstance.Decode(buf)
verifyToken := StringInstance.Decode(buf)
fmt.Println(publicKey, verifyToken)
//使用服务器的公钥加密参数
secret_verify_token, ok := RsaEncrypt([]byte(publicKey), []byte(verifyToken))
if !ok {
return []byte{}
}
sharedSecret, ok := RsaEncrypt([]byte(publicKey), []byte(pl.SharedSecret))
if !ok {
return []byte{}
}
//封装数据包
resbuf := bytes.NewBuffer([]byte{0x01})
StringInstance.Encode(string(sharedSecret), resbuf)
StringInstance.Encode(string(secret_verify_token), resbuf)
//发出session请求
sessionTextbuf := bytes.NewBufferString(serverid)
sessionTextbuf.Write([]byte(pl.SharedSecret))
sessionTextbuf.Write([]byte(publicKey))
serverHash := AuthDigest(sessionTextbuf.String())
if !pl.SetSession(serverHash) {
return []byte{}
}
return resbuf.Bytes()
}