Commit efc79c58 authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

init

parents
Pipeline #788 canceled with stages
.idea
*.iml
out
gen
*.sol
*.txt
.DS_Store
*.exe
build
\ No newline at end of file
FROM golang:1.21-alpine AS base
# Set up dependencies
ENV PACKAGES git openssh-client build-base
# Install dependencies
RUN apk add --update $PACKAGES
# Add source files
RUN mkdir -p ./sdk-api
COPY ./ ./sdk-api/
FROM base AS build
RUN cd sdk-api && go mod tidy && go build -v -o /tmp/api ./cmd/api && go build -v -o /tmp/messenger ./cmd/messenger
FROM alpine
WORKDIR /app
COPY ./config.toml /config.toml
COPY --from=build /tmp/api /usr/bin/api
COPY --from=build /tmp/messenger /usr/bin/messenger
EXPOSE 8080
\ No newline at end of file
.PHONY: default all clean dev messenger
GOBIN = $(shell pwd)/build/bin
default: all
all: api messenger
api:
go build $(BUILD_FLAGS) -v -o=${GOBIN}/$@ ./cmd/api
messenger:
go build $(BUILD_FLAGS) -v -o=${GOBIN}/$@ ./cmd/messenger
docker:
docker build -t caduceus/tg-messenger:latest -f Dockerfile .
package main
import (
"flag"
"sdk_api/config"
"sdk_api/dao"
messager "sdk_api/messenger"
"sdk_api/server"
"sdk_api/service"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
}
func main() {
flag.Parse()
cfg, err := config.New()
if err != nil {
panic(err)
}
da, err := dao.New(cfg)
if err != nil {
panic(err)
}
if cfg.Debug {
log.SetLevel(log.DebugLevel)
}
msger := messager.NewMessenger(cfg.TGBot.Token, da)
svs := service.New(cfg, da, msger)
server.StartServer(svs, cfg)
}
package main
import (
"flag"
"sdk_api/config"
"sdk_api/dao"
messager "sdk_api/messenger"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
}
func main() {
flag.Parse()
cfg, err := config.New()
if err != nil {
panic(err)
}
da, err := dao.New(cfg)
if err != nil {
panic(err)
}
if cfg.Debug {
log.SetLevel(log.DebugLevel)
}
msger := messager.NewMessenger(cfg.TGBot.Token, da)
msger.Start()
}
debug = true
[mysql]
#host = "tg-messenger-db"
host = "127.0.0.1"
port = 3306
user = "root"
password = ""
database = "sdk"
max_conn = 10
max_idle_conn = 2
enable_log = false
[server]
listen = "0.0.0.0:8080"
[tg_bot]
token = "6507972032:AAFAjaNz70ibA42kSH7k2gblyIQa1gWJiW0"
package config
import (
"flag"
"github.com/BurntSushi/toml"
)
type Config struct {
Debug bool `toml:"debug"`
MySQL MysqlConfig `toml:"mysql"`
Server ServerConfig `toml:"server"`
TGBot TGBotConfig `toml:"tg_bot"`
}
type MysqlConfig struct {
Host string `toml:"host"`
Port int `toml:"port"`
User string `toml:"user"`
Password string `toml:"password"`
Database string `toml:"database"`
MaxConn int `toml:"max_conn"`
MaxIdleConn int `toml:"max_idle_conn"`
EnableLog bool `toml:"enable_log"`
}
type ServerConfig struct {
Listen string `toml:"listen"`
}
type TGBotConfig struct {
Token string `toml:"token"`
}
var confPath = flag.String("c", "config.toml", "config file path")
func New() (config *Config, err error) {
config = new(Config)
_, err = toml.DecodeFile(*confPath, config)
return
}
package constant
import (
"math/big"
)
const JwtSecret = "VrA1tFnHBhNTPRriHdUQLuFHb4PAFPAa"
const (
InvalidParam = "invalid param"
InternalError = "internal error"
)
var (
ZeroValue = big.NewInt(0)
Gwei = big.NewInt(1000000000)
Ether = big.NewInt(1000000000000000000)
)
package dao
import (
"fmt"
"sdk_api/config"
dbModel "sdk_api/model/db"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
)
type Dao struct {
c *config.Config
db *gorm.DB
}
func New(_c *config.Config) (dao *Dao, err error) {
dao = &Dao{
c: _c,
}
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True",
_c.MySQL.User, _c.MySQL.Password, _c.MySQL.Host, _c.MySQL.Port, _c.MySQL.Database)
lgr := logger.Default
if _c.MySQL.EnableLog {
lgr = logger.Default.LogMode(logger.Info)
}
dao.db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
Logger: lgr,
})
if err != nil {
return
}
sqlDB, err := dao.db.DB()
if err != nil {
return
}
sqlDB.SetMaxOpenConns(_c.MySQL.MaxConn)
sqlDB.SetMaxIdleConns(_c.MySQL.MaxIdleConn)
sqlDB.SetConnMaxIdleTime(time.Hour)
err = dao.db.AutoMigrate(&dbModel.User{}, &dbModel.Active{}, &dbModel.ChatGroup{})
if err != nil {
return
}
err = dao.addTestGroup()
if err != nil {
return
}
return dao, nil
}
package dao
import (
dbModel "sdk_api/model/db"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
func (d *Dao) CreateUser(user *dbModel.User) (err error) {
return d.db.Clauses(clause.OnConflict{
DoUpdates: clause.Assignments(map[string]interface{}{"left_at": gorm.Expr("NULL")}),
}).Create(user).Error
}
func (d *Dao) IncrMessageCount(a *dbModel.Active) (err error) {
a.MsgCount = 1
return d.db.Clauses(clause.OnConflict{
DoUpdates: clause.Assignments(map[string]interface{}{"msg_count": gorm.Expr("msg_count + ?", 1)}),
}).Create(a).Error
}
func (d *Dao) GetUserActiveMsgCount(userId, chatId, unixDay int) (count int, err error) {
a := dbModel.Active{}
err = d.db.Model(&dbModel.Active{}).
Where("`user_id` = ? AND `chat_id` = ? AND `unix_day` = ?", userId, chatId, unixDay).
First(&a).Error
if err == gorm.ErrRecordNotFound {
return 0, nil
}
return a.MsgCount, err
}
func (d *Dao) UserExist(userId, chatId int) (exist bool, err error) {
a := dbModel.User{}
err = d.db.Model(&dbModel.User{}).
Where("`user_id` = ? AND `chat_id` = ? AND `left_at` IS NULL", userId, chatId).
First(&a).Error
if err == gorm.ErrRecordNotFound {
return false, nil
}
return err == nil, err
}
func (d *Dao) UserLeft(userId, chatId int) (err error) {
return d.db.Model(&dbModel.User{}).
Where("`user_id` = ? AND `chat_id` = ? AND `left_at` IS NULL", userId, chatId).
Update("left_at", gorm.Expr("NOW()")).Error
}
func (d *Dao) IsSupportedChat(chatId int) (exist bool, err error) {
a := dbModel.ChatGroup{}
err = d.db.Model(&dbModel.ChatGroup{}).
Where("`chat_id` = ?", chatId).
First(&a).Error
if err == gorm.ErrRecordNotFound {
return false, nil
}
return err == nil, err
}
func (d *Dao) addTestGroup() (err error) {
return d.db.Clauses(clause.OnConflict{DoNothing: true}).Create([]dbModel.ChatGroup{
{
ChatId: -1002174503961,
ChatTitle: "mail_notice",
},
}).Error
}
networks:
default:
name: aon-db
services:
tg-messenger-api:
image: caduceus/tg-messenger:latest
pull_policy: always
container_name: tg-messenger-api
ports:
- "16669:8080"
depends_on:
aon-db:
condition: service_healthy
volumes:
- ./conf/tg_messenger/config.toml:/config.toml
command:
- "/bin/sh"
- "-c"
- "/usr/bin/api -c /config.toml"
restart:
unless-stopped
tg-messenger:
image: caduceus/tg-messenger:latest
pull_policy: always
container_name: tg-messenger
depends_on:
aon-db:
condition: service_healthy
volumes:
- ./conf/tg_messenger/config.toml:/config.toml
command:
- "/bin/sh"
- "-c"
- "/usr/bin/messenger -c /config.toml"
restart:
unless-stopped
\ No newline at end of file
module sdk_api
go 1.21.4
require (
github.com/BurntSushi/toml v0.3.1
github.com/gin-contrib/cors v1.7.2
github.com/gin-gonic/gin v1.10.0
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/sirupsen/logrus v1.9.0
github.com/tidwall/gjson v1.17.1
gorm.io/driver/mysql v1.5.6
gorm.io/gorm v1.25.10
)
require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
This diff is collapsed.
package messager
import (
"sdk_api/dao"
dbModel "sdk_api/model/db"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
log "github.com/sirupsen/logrus"
)
type Messenger struct {
botToken string
bot *tgbotapi.BotAPI
d *dao.Dao
}
func NewMessenger(botToken string, _d *dao.Dao) *Messenger {
bot, err := tgbotapi.NewBotAPI(botToken)
if err != nil {
panic(err)
}
log.WithField("account", bot.Self.UserName).Info("Authorized on account")
return &Messenger{botToken: botToken, bot: bot, d: _d}
}
func (m *Messenger) Start() {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := m.bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil ||
update.Message.IsCommand() {
continue
}
supported, err := m.d.IsSupportedChat(int(update.Message.Chat.ID))
if err != nil {
log.WithError(err).Error("check supported chat error")
continue
}
if !supported {
continue
}
var caught bool
caught = m.handleUserLeft(update.Message)
if caught {
continue
}
caught = m.handleNewUser(update.Message)
if caught {
continue
}
m.handleActive(update.Message)
}
}
func (m *Messenger) ExistGroupChat(userId, chatId int64) (exist bool, err error) {
mem, err := m.bot.GetChatMember(tgbotapi.GetChatMemberConfig{
ChatConfigWithUser: tgbotapi.ChatConfigWithUser{
ChatID: chatId,
UserID: userId,
},
})
if err != nil {
return
}
switch {
case mem.HasLeft(), mem.WasKicked():
return false, nil
default:
return true, nil
}
}
func (m *Messenger) handleNewUser(msg *tgbotapi.Message) (caught bool) {
for _, user := range msg.NewChatMembers {
log.WithFields(log.Fields{
"user_id": user.ID,
"username": user.UserName,
"chat_id": msg.Chat.ID,
"chat_title": msg.Chat.Title,
"is_bot": user.IsBot,
}).Debug("new user")
if user.IsBot {
continue
}
user := &dbModel.User{
UserId: int(user.ID),
ChatId: int(msg.Chat.ID),
Username: user.UserName,
}
if err := m.d.CreateUser(user); err != nil {
log.WithError(err).Error("create user error")
}
if !caught {
caught = true
}
}
return caught
}
func (m *Messenger) handleUserLeft(msg *tgbotapi.Message) (caught bool) {
if msg.LeftChatMember == nil {
return
}
log.WithFields(log.Fields{
"user_id": msg.LeftChatMember.ID,
"username": msg.LeftChatMember.UserName,
"chat_id": msg.Chat.ID,
"chat_title": msg.Chat.Title,
"is_bot": msg.LeftChatMember.IsBot,
}).Debug("user left")
user := msg.LeftChatMember
if user.IsBot {
return
}
err := m.d.UserLeft(int(user.ID), int(msg.Chat.ID))
if err != nil {
log.WithError(err).Error("user left error")
}
return true
}
func (m *Messenger) handleActive(msg *tgbotapi.Message) {
log.WithFields(log.Fields{
"user_id": msg.From.ID,
"username": msg.From.UserName,
"chat_id": msg.Chat.ID,
"chat_title": msg.Chat.Title,
"is_bot": msg.From.IsBot,
}).Debug("new msg")
if msg.From.IsBot {
return
}
exist, err := m.d.UserExist(int(msg.From.ID), int(msg.Chat.ID))
if err != nil {
log.WithError(err).Error("exist user error")
}
if err == nil && !exist {
err = m.d.CreateUser(&dbModel.User{
UserId: int(msg.From.ID),
ChatId: int(msg.Chat.ID),
Username: msg.From.UserName,
})
if err != nil {
log.WithError(err).Error("create user 2 error")
}
}
err = m.d.IncrMessageCount(&dbModel.Active{
UserId: int(msg.From.ID),
ChatId: int(msg.Chat.ID),
UnixDay: msg.Date / 86400,
})
if err != nil {
log.WithError(err).Error("incr message count error")
}
}
func (m *Messenger) SendMessage(chatId int64, msg string) error {
_, err := m.bot.Send(tgbotapi.NewMessage(chatId, msg))
return err
}
package messager
import (
"testing"
)
func TestFunc(t *testing.T) {
token := "6507972032:AAFAjaNz70ibA42kSH7k2gblyIQa1gWJiW0"
m := NewMessenger(token, nil)
m.ExistGroupChat(6689160806, -1002174503961)
m.Start()
}
package middleware
import (
"bytes"
"io"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func PrintRequestResponseBodyMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// 读取请求 body
var requestBody []byte
if c.Request.Body != nil {
requestBody, _ = io.ReadAll(c.Request.Body)
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
}
log.WithFields(log.Fields{"method": c.Request.Method, "uri": c.Request.RequestURI, "body": string(requestBody)}).Debug("request body")
bodyWriter := &responseBodyWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
c.Writer = bodyWriter
c.Next()
responseBody := bodyWriter.body.String()
log.WithFields(log.Fields{"status": c.Writer.Status(), "body": responseBody}).Debug("response body")
}
}
type responseBodyWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (r *responseBodyWriter) Write(b []byte) (int, error) {
r.body.Write(b)
return r.ResponseWriter.Write(b)
}
package middleware
import (
"sdk_api/util"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func JWTMiddleware(c *gin.Context) {
tokenString := c.GetHeader("Authorization")
log.Debugln(tokenString)
if tokenString == "" || len(tokenString) < 7 {
c.JSON(200, gin.H{
"code": 1,
"msg": "invalid token",
"data": "",
})
c.Abort()
return
}
ok, expired, uid, _, _ := util.ParseJWT(tokenString[7:])
if !ok {
c.JSON(200, gin.H{
"code": 1,
"msg": "invalid token",
"data": "",
})
c.Abort()
return
}
if expired {
c.JSON(200, gin.H{
"code": 1,
"msg": "token expired",
"data": "",
})
c.Abort()
return
}
log.WithField("uid", uid).Debug("jwt uid")
c.Set("jwt-uid", uid)
c.Next()
}
package api_model
package db_model
import (
"database/sql"
"gorm.io/gorm"
)
type User struct {
Id int `gorm:"primaryKey"`
UserId int `gorm:"type:int;uniqueIndex:uidx_uid_cid;not null;comment:telegram用户id"`
Username string `gorm:"type:varchar(255);not null;comment:telegram用户名"`
ChatId int `gorm:"type:int;uniqueIndex:uidx_uid_cid;not null;comment:telegram群id"`
LeftAt sql.NullTime `gorm:"index;comment:退出时间"`
gorm.Model
}
type Active struct {
Id int `gorm:"primaryKey"`
UserId int `gorm:"type:int;uniqueIndex:uidx_uid_cid_day;not null;comment:telegram用户id"`
ChatId int `gorm:"type:int;uniqueIndex:uidx_uid_cid_day;not null;comment:telegram群id"`
UnixDay int `gorm:"type:int;not null;uniqueIndex:uidx_uid_cid_day;comment:unix day"`
MsgCount int `gorm:"type:int;not null;comment:消息数"`
gorm.Model
}
type ChatGroup struct {
Id int `gorm:"primaryKey"`
ChatId int `gorm:"type:int;uniqueIndex;not null;comment:telegram群id"`
ChatTitle string `gorm:"type:varchar(255);not null;comment:telegram群名"`
Description string `gorm:"type:varchar(255);comment:群描述"`
gorm.Model
}
# telegram-messenger
统计用户每日发言,是否在某个群组内
\ No newline at end of file
package server
import (
"sdk_api/middleware"
"github.com/gin-gonic/gin"
)
func initRouter(e *gin.Engine) {
e.Use(middleware.PrintRequestResponseBodyMiddleware())
v1 := e.Group("/api/v1")
{
user := v1.Group("/user")
user.GET("/joined", isJoined)
user.GET("/active", active)
}
}
package server
import (
"sdk_api/config"
"sdk_api/service"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
var srv *service.Service
var conf *config.Config
func StartServer(_srv *service.Service, _conf *config.Config) {
srv = _srv
conf = _conf
if !conf.Debug {
gin.SetMode(gin.ReleaseMode)
}
engine := gin.Default()
_cors := cors.DefaultConfig()
_cors.AllowAllOrigins = true
_cors.AllowHeaders = []string{"*"}
engine.Use(cors.New(_cors))
initRouter(engine)
log.Infof("start http server listening %s", conf.Server.Listen)
if err := engine.Run(conf.Server.Listen); err != nil {
log.Error("http server run error: ", err)
}
}
func withSuccess(obj interface{}) interface{} {
return gin.H{
"code": 0,
"msg": "ok",
"data": obj,
}
}
func withError(msg string) interface{} {
return gin.H{
"code": 1,
"error": msg,
"data": "",
}
}
package server
import (
"sdk_api/constant"
"strconv"
"github.com/gin-gonic/gin"
)
func isJoined(c *gin.Context) {
_userId := c.Query("userId")
_chatId := c.Query("chatId")
userId, _ := strconv.Atoi(_userId)
chatId, _ := strconv.Atoi(_chatId)
if userId == 0 || chatId == 0 {
c.JSON(200, withError(constant.InvalidParam))
return
}
joined, err := srv.IsJoined(userId, chatId)
if err != nil {
c.JSON(200, withError(constant.InternalError))
return
}
c.JSON(200, gin.H{"joined": joined})
return
}
func active(c *gin.Context) {
_userId := c.Query("userId")
_chatId := c.Query("chatId")
userId, _ := strconv.Atoi(_userId)
chatId, _ := strconv.Atoi(_chatId)
if userId == 0 || chatId == 0 {
c.JSON(200, withError(constant.InvalidParam))
return
}
msgCount, err := srv.Active(userId, chatId)
if err != nil {
c.JSON(200, withError(constant.InternalError))
return
}
c.JSON(200, gin.H{"msgCount": msgCount})
}
package service
import (
"sdk_api/config"
"sdk_api/dao"
messager "sdk_api/messenger"
)
type Service struct {
d *dao.Dao
cfg *config.Config
msger *messager.Messenger
}
func New(conf *config.Config, da *dao.Dao, m *messager.Messenger) *Service {
return &Service{
d: da,
cfg: conf,
msger: m,
}
}
package service
import (
"time"
log "github.com/sirupsen/logrus"
)
func (s *Service) IsJoined(userId, chatId int) (joined bool, err error) {
joined, err = s.msger.ExistGroupChat(int64(userId), int64(chatId))
if err != nil {
log.WithError(err).Error("exist group chat error")
}
return
}
func (s *Service) Active(userId, chatId int) (msgCount int, err error) {
msgCount, err = s.d.GetUserActiveMsgCount(userId, chatId, int(time.Now().Unix()/86400))
if err != nil {
log.WithError(err).Error("get user active msg count error")
}
return
}
package util
import (
"time"
"github.com/golang-jwt/jwt/v5"
)
const secret = "cxcZa005Y5zWH1wFgXvPGDL02Ey4ZCLAh2XFcfp7HhG3wTg5TbcnhuYhNvN3YLgt"
func GenerateJWT(uid, platform, platformId string) string {
tk := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"uid": uid,
"platform": platform,
"platformId": platformId,
"iat": time.Now().Unix(),
"exp": time.Now().Add(7 * 24 * time.Hour).Unix(),
})
j, _ := tk.SignedString([]byte(secret))
return j
}
func ParseJWT(token string) (ok, expired bool, uid, platform, platformId string) {
claims := jwt.MapClaims{}
tk, err := jwt.ParseWithClaims(token, claims, func(t *jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
if err != nil {
return
}
if !tk.Valid {
return
}
uid = claims["uid"].(string)
platform = claims["platform"].(string)
platformId = claims["platformId"].(string)
exp := claims["exp"].(float64)
if time.Now().Unix() > int64(exp) {
expired = true
return
}
ok = true
return
}
package util
import (
"testing"
)
func TestA(t *testing.T) {
token := GenerateJWT("12345", "telegram", "0x12345")
t.Logf("token: %s", token)
ParseJWT(token)
}
func TestB(t *testing.T) {
t.Log(ParseJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTgwMTU2MTQsImlhdCI6MTcxNzQxMDgxNCwicGxhdGZvcm0iOiJ0ZWxlZ3JhbSIsInBsYXRmb3JtSWQiOiIweDEyMzQ1IiwidWlkIjoiMTIzNDUifQ.yZ1V_cGozBrwK55Y9iZsG4C-B5T96V2E3-AqP6CqkR8"))
}
package util
import (
"crypto/hmac"
"crypto/sha256"
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"time"
"github.com/tidwall/gjson"
)
func VerifyInitData(initData, botToken string) (ok bool, botId, userId string) {
h := hmac.New(sha256.New, []byte("WebAppData"))
h.Write([]byte(botToken))
secret := h.Sum(nil)
h2 := hmac.New(sha256.New, secret)
params, err := url.ParseQuery(initData)
if err != nil {
return
}
var hashval string
var keys []string
for key := range params {
if key == "hash" {
hashval = params.Get(key)
continue
}
if key == "auth_date" {
authDate, _ := strconv.Atoi(params.Get(key))
if int64(authDate) < time.Now().Unix()-3600 || int64(authDate) > time.Now().Unix()+300 {
// todo 可以限制超时时间
return false, "", ""
}
}
if key == "user" {
userId = gjson.Get(params.Get(key), "id").String()
}
keys = append(keys, key)
}
sort.Strings(keys)
var payloads []string
for _, key := range keys {
payloads = append(payloads, fmt.Sprintf("%s=%s", key, params.Get(key)))
}
payload := strings.Join(payloads, "\n")
h2.Write([]byte(payload))
h2sum := h2.Sum(nil)
items := strings.Split(botToken, ":")
if len(items) != 2 {
return
}
ok = fmt.Sprintf("%x", h2sum) == hashval
botId = items[0]
return
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment