forked from FloatTech/ZeroBot-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gist.go
44 lines (39 loc) · 1.19 KB
/
gist.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
package manager
import (
"crypto/md5"
"encoding/hex"
"fmt"
"strconv"
"time"
"github.com/sirupsen/logrus"
"github.com/wdvxdr1123/ZeroBot/utils/helper"
"github.com/FloatTech/floatbox/math"
"github.com/FloatTech/floatbox/web"
)
// user hash file
const gistraw = "https://gist.githubusercontent.com/%s/%s/raw/%s"
func checkNewUser(qq, gid int64, ghun, hash string) (bool, string) {
if db.CanFind("member", "where ghun="+ghun) {
return false, "该github用户已入群"
}
gidsum := md5.Sum(helper.StringToBytes(strconv.FormatInt(gid, 10)))
gidhex := hex.EncodeToString(gidsum[:])
u := fmt.Sprintf(gistraw, ghun, hash, gidhex)
logrus.Debugln("[gist]visit url:", u)
data, err := web.GetData(u)
if err == nil {
logrus.Debugln("[gist]get data:", helper.BytesToString(data))
st, err := strconv.ParseInt(helper.BytesToString(data), 10, 64)
if err == nil {
// 600s 内验证成功
ok := math.Abs(int(time.Now().Unix()-st)) < 600
if ok {
_ = db.Insert("member", &member{QQ: qq, Ghun: ghun})
return true, ""
}
return false, "时间戳超时"
}
return false, "时间戳格式错误: " + helper.BytesToString(data)
}
return false, "无法连接到gist: " + err.Error()
}