59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package suite
|
|
|
|
import (
|
|
mastermanager "bdgrp_go/master"
|
|
"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, master)
|
|
fmt.Println("Loaded:", path)
|
|
})
|
|
}
|
|
|
|
func master(c *fiber.Ctx) error {
|
|
server := os.Getenv("SERVER")
|
|
cachePath := filepath.Join("resp", server, "suitemaster.bz2")
|
|
|
|
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/suite/master", upstream.BaseUrl())
|
|
data, err := upstream.Fetch(url, headers)
|
|
if err != nil {
|
|
data, err = os.ReadFile(cachePath)
|
|
if err != nil {
|
|
return c.Status(500).SendString("no cache available")
|
|
}
|
|
} else {
|
|
os.WriteFile(cachePath, data, 0644)
|
|
go mastermanager.ReloadMaster()
|
|
}
|
|
|
|
c.Set("Content-Length", fmt.Sprintf("%d", len(data)))
|
|
c.Set("Content-Type", "application/octet-stream")
|
|
c.Set("X-Encoding", "bzip2")
|
|
return c.Send(data)
|
|
}
|