add some routes
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
SERVER=TW
|
||||||
21
gen/gen.go
21
gen/gen.go
@@ -24,18 +24,23 @@ func main() {
|
|||||||
moduleName := "bdgrp_go"
|
moduleName := "bdgrp_go"
|
||||||
|
|
||||||
filepath.WalkDir("routes", func(path string, d os.DirEntry, err error) error {
|
filepath.WalkDir("routes", func(path string, d os.DirEntry, err error) error {
|
||||||
if err != nil || !d.IsDir() || path == "routes" {
|
if err != nil || d.IsDir() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
entries, _ := os.ReadDir(path)
|
if !strings.HasSuffix(d.Name(), ".go") {
|
||||||
for _, e := range entries {
|
return nil
|
||||||
if !e.IsDir() && strings.HasSuffix(e.Name(), ".go") {
|
}
|
||||||
pkg := moduleName + "/" + filepath.ToSlash(path)
|
|
||||||
|
pkg := moduleName + "/" + filepath.ToSlash(filepath.Dir(path))
|
||||||
|
|
||||||
|
for _, p := range packages {
|
||||||
|
if p == pkg {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
packages = append(packages, pkg)
|
packages = append(packages, pkg)
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -6,6 +6,7 @@ require (
|
|||||||
github.com/gofiber/fiber/v2 v2.52.15
|
github.com/gofiber/fiber/v2 v2.52.15
|
||||||
github.com/gookit/color v1.6.1
|
github.com/gookit/color v1.6.1
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
|
google.golang.org/protobuf v1.36.12
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -2,6 +2,8 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1
|
|||||||
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
|
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
|
||||||
github.com/gofiber/fiber/v2 v2.52.15 h1:Cov1uKeVPyu9q0jSrN60W+A8XNX+/WK8J7cy5osHLIk=
|
github.com/gofiber/fiber/v2 v2.52.15 h1:Cov1uKeVPyu9q0jSrN60W+A8XNX+/WK8J7cy5osHLIk=
|
||||||
github.com/gofiber/fiber/v2 v2.52.15/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw=
|
github.com/gofiber/fiber/v2 v2.52.15/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw=
|
||||||
|
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||||
|
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/gookit/assert v0.1.1 h1:lh3GcawXe/p+cU7ESTZ5Ui3Sm/x8JWpIis4/1aF0mY0=
|
github.com/gookit/assert v0.1.1 h1:lh3GcawXe/p+cU7ESTZ5Ui3Sm/x8JWpIis4/1aF0mY0=
|
||||||
@@ -35,3 +37,5 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
|
|||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
|
||||||
|
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||||
|
|||||||
54
pbutil/pbutil.go
Normal file
54
pbutil/pbutil.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package pbutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
_ "bdgrp_go/proto/cn"
|
||||||
|
_ "bdgrp_go/proto/gl"
|
||||||
|
_ "bdgrp_go/proto/jp"
|
||||||
|
_ "bdgrp_go/proto/tw"
|
||||||
|
|
||||||
|
"google.golang.org/protobuf/encoding/protojson"
|
||||||
|
"google.golang.org/protobuf/proto"
|
||||||
|
"google.golang.org/protobuf/reflect/protoreflect"
|
||||||
|
"google.golang.org/protobuf/reflect/protoregistry"
|
||||||
|
)
|
||||||
|
|
||||||
|
func fullName(message string) protoreflect.FullName {
|
||||||
|
server := strings.ToLower(os.Getenv("SERVER"))
|
||||||
|
// twdori.proto.ce.UserLogin
|
||||||
|
return protoreflect.FullName(fmt.Sprintf("%sdori.proto.ce.%s", server, message))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Decode(message string, data []byte) (proto.Message, error) {
|
||||||
|
mt, err := protoregistry.GlobalTypes.FindMessageByName(fullName(message))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("message %s not found", message)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := mt.New().Interface()
|
||||||
|
if err := proto.Unmarshal(data, msg); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return msg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Encode(message string, data map[string]any) ([]byte, error) {
|
||||||
|
mt, err := protoregistry.GlobalTypes.FindMessageByName(fullName(message))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("message %s not found", message)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := mt.New().Interface()
|
||||||
|
// map → json → proto
|
||||||
|
jsonBytes, _ := json.Marshal(data)
|
||||||
|
if err := protojson.Unmarshal(jsonBytes, msg); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return proto.Marshal(msg)
|
||||||
|
}
|
||||||
135797
proto/cn/cn.go
Normal file
135797
proto/cn/cn.go
Normal file
File diff suppressed because it is too large
Load Diff
125786
proto/gl/gl.go
Normal file
125786
proto/gl/gl.go
Normal file
File diff suppressed because it is too large
Load Diff
129339
proto/jp/jp.go
Normal file
129339
proto/jp/jp.go
Normal file
File diff suppressed because it is too large
Load Diff
129280
proto/tw/tw.go
Normal file
129280
proto/tw/tw.go
Normal file
File diff suppressed because it is too large
Load Diff
20
routes/api/acdm/userid.go
Normal file
20
routes/api/acdm/userid.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package acdm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"bdgrp_go/routes"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
routes.Register(func(app *fiber.App) {
|
||||||
|
app.Get("/api/acdm/:userid", acdm)
|
||||||
|
fmt.Println("Loaded: /api/acdm/:userid")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func acdm(c *fiber.Ctx) error {
|
||||||
|
return c.SendString("")
|
||||||
|
}
|
||||||
54
routes/api/application.go
Normal file
54
routes/api/application.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"bdgrp_go/routes"
|
||||||
|
"bdgrp_go/routes/upstream"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
path := routes.PathOf()
|
||||||
|
routes.Register(func(app *fiber.App) {
|
||||||
|
app.Get(path, application)
|
||||||
|
fmt.Println("Loaded:", path)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func application(c *fiber.Ctx) error {
|
||||||
|
server := os.Getenv("SERVER")
|
||||||
|
cachePath := filepath.Join("resp", server, "application.binpb")
|
||||||
|
|
||||||
|
headers := map[string]string{
|
||||||
|
"User-Agent": c.Get("User-Agent"),
|
||||||
|
"Content-Type": c.Get("Content-Type"),
|
||||||
|
"Accept": c.Get("Accept"),
|
||||||
|
"Accept-Encoding": c.Get("Accept-Encoding"),
|
||||||
|
"x-clientversion": c.Get("x-clientversion"),
|
||||||
|
}
|
||||||
|
|
||||||
|
if server == "CN" {
|
||||||
|
headers["x-platformid"] = c.Get("x-platformid")
|
||||||
|
headers["x-deviceid"] = c.Get("x-deviceid")
|
||||||
|
headers["x-channelid"] = c.Get("x-channelid")
|
||||||
|
headers["x-clientplatform"] = c.Get("x-clientplatform")
|
||||||
|
}
|
||||||
|
|
||||||
|
url := fmt.Sprintf("%s/api/application", upstream.BaseUrl())
|
||||||
|
data, err := upstream.Fetch(url, headers)
|
||||||
|
if err != nil {
|
||||||
|
// fallback 到 cache
|
||||||
|
data, err = os.ReadFile(cachePath)
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(500).SendString("no cache available")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
os.WriteFile(cachePath, data, 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Send(data)
|
||||||
|
}
|
||||||
54
routes/api/cleanupinfo.go
Normal file
54
routes/api/cleanupinfo.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bdgrp_go/routes/upstream"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"bdgrp_go/routes"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
path := routes.PathOf()
|
||||||
|
routes.Register(func(app *fiber.App) {
|
||||||
|
app.Get(path, cleanupinfo)
|
||||||
|
fmt.Println("Loaded:", path)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanupinfo(c *fiber.Ctx) error {
|
||||||
|
server := os.Getenv("SERVER")
|
||||||
|
cachePath := filepath.Join("resp", server, "cleanupinfo.binpb")
|
||||||
|
|
||||||
|
headers := map[string]string{
|
||||||
|
"User-Agent": c.Get("User-Agent"),
|
||||||
|
"Content-Type": c.Get("Content-Type"),
|
||||||
|
"Accept": c.Get("Accept"),
|
||||||
|
"Accept-Encoding": c.Get("Accept-Encoding"),
|
||||||
|
"x-clientversion": c.Get("x-clientversion"),
|
||||||
|
}
|
||||||
|
|
||||||
|
if server == "CN" {
|
||||||
|
headers["x-platformid"] = c.Get("x-platformid")
|
||||||
|
headers["x-deviceid"] = c.Get("x-deviceid")
|
||||||
|
headers["x-channelid"] = c.Get("x-channelid")
|
||||||
|
headers["x-clientplatform"] = c.Get("x-clientplatform")
|
||||||
|
}
|
||||||
|
|
||||||
|
url := fmt.Sprintf("%s/api/application", upstream.BaseUrl())
|
||||||
|
data, err := upstream.Fetch(url, headers)
|
||||||
|
if err != nil {
|
||||||
|
// fallback 到 cache
|
||||||
|
data, err = os.ReadFile(cachePath)
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(500).SendString("no cache available")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
os.WriteFile(cachePath, data, 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Send(data)
|
||||||
|
}
|
||||||
54
routes/api/information/overview.go
Normal file
54
routes/api/information/overview.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package information
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"bdgrp_go/routes"
|
||||||
|
"bdgrp_go/routes/upstream"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
path := routes.PathOf()
|
||||||
|
routes.Register(func(app *fiber.App) {
|
||||||
|
app.Get(path, application)
|
||||||
|
fmt.Println("Loaded:", path)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func application(c *fiber.Ctx) error {
|
||||||
|
server := os.Getenv("SERVER")
|
||||||
|
cachePath := filepath.Join("resp", server, "overview.binpb")
|
||||||
|
|
||||||
|
headers := map[string]string{
|
||||||
|
"User-Agent": c.Get("User-Agent"),
|
||||||
|
"Content-Type": c.Get("Content-Type"),
|
||||||
|
"Accept": c.Get("Accept"),
|
||||||
|
"Accept-Encoding": c.Get("Accept-Encoding"),
|
||||||
|
"x-clientversion": c.Get("x-clientversion"),
|
||||||
|
}
|
||||||
|
|
||||||
|
if server == "CN" {
|
||||||
|
headers["x-platformid"] = c.Get("x-platformid")
|
||||||
|
headers["x-deviceid"] = c.Get("x-deviceid")
|
||||||
|
headers["x-channelid"] = c.Get("x-channelid")
|
||||||
|
headers["x-clientplatform"] = c.Get("x-clientplatform")
|
||||||
|
}
|
||||||
|
|
||||||
|
url := fmt.Sprintf("%s/api/information/overview", upstream.BaseUrl())
|
||||||
|
data, err := upstream.Fetch(url, headers)
|
||||||
|
if err != nil {
|
||||||
|
// fallback 到 cache
|
||||||
|
data, err = os.ReadFile(cachePath)
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(500).SendString("no cache available")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
os.WriteFile(cachePath, data, 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Send(data)
|
||||||
|
}
|
||||||
57
routes/api/system/application.go
Normal file
57
routes/api/system/application.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bdgrp_go/pbutil"
|
||||||
|
"bdgrp_go/routes"
|
||||||
|
"bdgrp_go/utils"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
path := routes.PathOf()
|
||||||
|
routes.Register(func(app *fiber.App) {
|
||||||
|
app.Get(path, application)
|
||||||
|
fmt.Println("Loaded:", path)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func application(c *fiber.Ctx) error {
|
||||||
|
var offset int
|
||||||
|
server := os.Getenv("SERVER")
|
||||||
|
|
||||||
|
switch server {
|
||||||
|
case "TW", "CN":
|
||||||
|
offset = 28800000
|
||||||
|
break
|
||||||
|
case "JP":
|
||||||
|
offset = 32400000
|
||||||
|
break
|
||||||
|
case "GL":
|
||||||
|
offset = 0
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
data := map[string]any{
|
||||||
|
"serverDate": strconv.FormatInt(time.Now().Add(27*time.Hour).UnixMilli(), 10),
|
||||||
|
"timeZoneRawOffset": offset,
|
||||||
|
}
|
||||||
|
|
||||||
|
encoded, err := pbutil.Encode("ServerSystem", data)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return c.Status(500).SendString("")
|
||||||
|
}
|
||||||
|
|
||||||
|
encrypted, err := utils.Encrypt(encoded)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Send(encrypted)
|
||||||
|
}
|
||||||
50
routes/api/system/config.go
Normal file
50
routes/api/system/config.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bdgrp_go/routes/upstream"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"bdgrp_go/routes"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
path := routes.PathOf()
|
||||||
|
routes.Register(func(app *fiber.App) {
|
||||||
|
app.Get(path, config)
|
||||||
|
fmt.Println("Loaded:", path)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func config(c *fiber.Ctx) error {
|
||||||
|
server := os.Getenv("SERVER")
|
||||||
|
cachePath := filepath.Join("resp", server, "config.binpb")
|
||||||
|
|
||||||
|
headers := map[string]string{
|
||||||
|
"User-Agent": c.Get("User-Agent"),
|
||||||
|
"Content-Type": c.Get("Content-Type"),
|
||||||
|
"Accept": c.Get("Accept"),
|
||||||
|
"Accept-Encoding": c.Get("Accept-Encoding"),
|
||||||
|
"x-clientversion": c.Get("x-clientversion"),
|
||||||
|
"x-platformid": c.Get("x-platformid"),
|
||||||
|
"x-deviceid": c.Get("x-deviceid"),
|
||||||
|
"x-channelid": c.Get("x-channelid"),
|
||||||
|
"x-clientplatform": c.Get("x-clientplatform"),
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := upstream.Fetch("https://l3-prod-all-bd.bilibiligame.net/api/system/config", headers)
|
||||||
|
if err != nil {
|
||||||
|
// fallback 到 cache
|
||||||
|
data, err = os.ReadFile(cachePath)
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(500).SendString("no cache available")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
os.WriteFile(cachePath, data, 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Send(data)
|
||||||
|
}
|
||||||
22
routes/helper.go
Normal file
22
routes/helper.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PathOf() string {
|
||||||
|
_, file, _, _ := runtime.Caller(1)
|
||||||
|
|
||||||
|
// 取 routes/ 之後的部分
|
||||||
|
idx := strings.Index(file, "routes/")
|
||||||
|
if idx == -1 {
|
||||||
|
return "/"
|
||||||
|
}
|
||||||
|
path := file[idx+len("routes"):]
|
||||||
|
|
||||||
|
// 去掉 .go
|
||||||
|
path = strings.TrimSuffix(path, ".go")
|
||||||
|
|
||||||
|
return path
|
||||||
|
}
|
||||||
31
routes/upstream/upstream.go
Normal file
31
routes/upstream/upstream.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package upstream
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var BaseUrls = map[string]string{
|
||||||
|
"TW": "https://v1010-bd.mobimon.com.tw",
|
||||||
|
"JP": "https://api.garupa.jp",
|
||||||
|
"GL": "https://api.app-bang-dream-gbp.com",
|
||||||
|
"CN": "https://l3-prod-all-bd.bilibiligame.net",
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fetch(url string, headers map[string]string) ([]byte, error) {
|
||||||
|
req, _ := http.NewRequest("GET", url, nil)
|
||||||
|
for k, v := range headers {
|
||||||
|
req.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
return io.ReadAll(resp.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BaseUrl() string {
|
||||||
|
return BaseUrls[os.Getenv("SERVER")]
|
||||||
|
}
|
||||||
81
utils/crypto.go
Normal file
81
utils/crypto.go
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var keys = map[string]string{
|
||||||
|
"TW": "mikumikulukaluka",
|
||||||
|
"JP": "mikumikulukaluka",
|
||||||
|
"GL": "RXzFAC87WR28eNcP",
|
||||||
|
"CN": "wakakabiliwakaka",
|
||||||
|
}
|
||||||
|
|
||||||
|
var ivs = map[string]string{
|
||||||
|
"TW": "lukalukamikumiku",
|
||||||
|
"JP": "lukalukamikumiku",
|
||||||
|
"GL": "hrk0k7knXlit3S_y",
|
||||||
|
"CN": "biliwakakawohaha",
|
||||||
|
}
|
||||||
|
|
||||||
|
func getKeyIV() ([]byte, []byte, error) {
|
||||||
|
server := os.Getenv("SERVER")
|
||||||
|
key, ok := keys[server]
|
||||||
|
if !ok {
|
||||||
|
return nil, nil, fmt.Errorf("unknown server: %s", server)
|
||||||
|
}
|
||||||
|
return []byte(key), []byte(ivs[server]), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Decrypt(buf []byte) ([]byte, error) {
|
||||||
|
key, iv, err := getKeyIV()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
block, err := aes.NewCipher(key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(buf)%aes.BlockSize != 0 {
|
||||||
|
return nil, fmt.Errorf("invalid block size")
|
||||||
|
}
|
||||||
|
|
||||||
|
decrypted := make([]byte, len(buf))
|
||||||
|
cipher.NewCBCDecrypter(block, iv).CryptBlocks(decrypted, buf)
|
||||||
|
|
||||||
|
// 手動移除 padding(setAutoPadding(false) 對應)
|
||||||
|
padLen := int(decrypted[len(decrypted)-1])
|
||||||
|
if padLen > 0 && padLen <= 16 && padLen <= len(decrypted) {
|
||||||
|
decrypted = decrypted[:len(decrypted)-padLen]
|
||||||
|
}
|
||||||
|
|
||||||
|
return decrypted, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Encrypt(buf []byte) ([]byte, error) {
|
||||||
|
key, iv, err := getKeyIV()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
block, err := aes.NewCipher(key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 手動加 PKCS7 padding(setAutoPadding(true) 對應)
|
||||||
|
blockSize := block.BlockSize()
|
||||||
|
padding := blockSize - len(buf)%blockSize
|
||||||
|
padded := append(buf, bytes.Repeat([]byte{byte(padding)}, padding)...)
|
||||||
|
|
||||||
|
encrypted := make([]byte, len(padded))
|
||||||
|
cipher.NewCBCEncrypter(block, iv).CryptBlocks(encrypted, padded)
|
||||||
|
|
||||||
|
return encrypted, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user