From 4a0788729a1e8df33b2461738ea2713361598074 Mon Sep 17 00:00:00 2001 From: hahwu <31872165+hahwu@users.noreply.github.com> Date: Fri, 13 Feb 2026 19:04:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=94=E6=B8=B8notification?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/game_util/GoUtil.go | 76 ++++++++++++++++++++++++++++++++++ src/server/test/fix_test.go | 5 +++ 2 files changed, 81 insertions(+) diff --git a/src/server/game_util/GoUtil.go b/src/server/game_util/GoUtil.go index de953b28..932ba513 100644 --- a/src/server/game_util/GoUtil.go +++ b/src/server/game_util/GoUtil.go @@ -6,11 +6,14 @@ import ( "crypto/cipher" "crypto/md5" crand "crypto/rand" + "crypto/sha256" "encoding/base64" "encoding/gob" + "encoding/hex" "fmt" "io" "math/rand" + "net/http" "reflect" "strconv" "strings" @@ -319,6 +322,11 @@ func Rand6DigitNumber() string { return fmt.Sprintf("%06d", n) } +func Rand8DigitNumber() string { + n := rand.Intn(100000000) + return fmt.Sprintf("%08d", n) +} + func UniqueInts(input []int) []int { seen := make(map[int]struct{}) result := make([]int, 0, len(input)) @@ -545,3 +553,71 @@ func GetISOCodeByIP(ip string) (string, error) { func GetVarKey(Uid int) string { return fmt.Sprintf("var_%d", Uid) } + +const ( + PROJECT_ID = "20659" + ACCESS_ID = "a3d8c1f0e5b72a49" + NOTIFICATION_SECRET_KEY = "K8pYrR6fXcVzWbAqN3mHsD4gJtL9iUvO1" +) + +func NotifyPlayer(uid, pushid int, title, content string) { + + url := "https://tygapi-new.tuyooglobal.com/api/push/ga/push_message/project_trigger" + method := "POST" + executeId := fmt.Sprintf("%s_%s", PROJECT_ID, Rand8DigitNumber()+Rand8DigitNumber()) + payload := strings.NewReader(`[ + { + "projectId": "` + PROJECT_ID + `", + "platform": "auto", + "pushId": "` + PROJECT_ID + `_` + fmt.Sprintf("%08d", pushid) + `", + "executeId": "` + executeId + `", + "environment": "production", + "title": "` + title + `", + "body": "` + content + `", + "userId": [ + "` + strconv.Itoa(uid) + `" + ] + } +]`) + + client := &http.Client{} + req, err := http.NewRequest(method, url, payload) + + if err != nil { + fmt.Println(err) + return + } + d, _ := time.ParseDuration("30m") + timestamp := time.Now().Add(d).Unix() + + var strBuilder strings.Builder + strBuilder.WriteString("Access-Id=" + ACCESS_ID + "&Random=a2dS8Iyak03Ma91JC1xR&Secret-Type=forever&Sign-Type=sha256&Timestamp=") + strBuilder.WriteString(strconv.Itoa(int(timestamp))) + strBuilder.WriteString(":" + NOTIFICATION_SECRET_KEY) + + m := sha256.New() + m.Write([]byte(strBuilder.String())) + signature := hex.EncodeToString(m.Sum(nil)) + + req.Header.Add("Access-Id", ACCESS_ID) + req.Header.Add("Secret-Type", "forever") + req.Header.Add("Sign-Type", "sha256") + req.Header.Add("Random", "a2dS8Iyak03Ma91JC1xR") + req.Header.Add("Timestamp", strconv.Itoa(int(timestamp))) + req.Header.Add("Signature", signature) + req.Header.Add("Content-Type", "application/json") + fmt.Print(req) + res, err := client.Do(req) + if err != nil { + fmt.Println(err) + return + } + defer res.Body.Close() + + body, err := io.ReadAll(res.Body) + if err != nil { + fmt.Println(err) + return + } + fmt.Println(string(body)) +} diff --git a/src/server/test/fix_test.go b/src/server/test/fix_test.go index 4fc8ffb9..4c603547 100644 --- a/src/server/test/fix_test.go +++ b/src/server/test/fix_test.go @@ -5,6 +5,7 @@ import ( decorateCfg "server/conf/decorate" "server/db" "server/game" + GoUtil "server/game_util" "server/pkg/github.com/name5566/leaf/log" "testing" ) @@ -94,3 +95,7 @@ func TestEndless(t *testing.T) { EndlessMod := p1.GetEndlessMod() EndlessMod.ZeroUpdate(ChargeMod.GetMaxCharge(), BaseMod.GetLevel()) } + +func TestNotify(t *testing.T) { + GoUtil.NotifyPlayer(3625212, 1, "Test Notification", "This is a test notification from the server.") +}