Files
bdgrp_go/routes/api/tcpServer/param_userid.go
2026-09-06 21:57:03 +08:00

91 lines
1.9 KiB
Go

package tcpServer
import (
"bdgrp_go/routes"
"crypto/tls"
"fmt"
"io"
"net/http"
"strings"
"github.com/gofiber/fiber/v2"
)
var skipReqHeaders = map[string]bool{
"transfer-encoding": true,
"connection": true,
"keep-alive": true,
"content-encoding": true,
"content-length": true,
"cf-connecting-ip": true,
"cf-ray": true,
"cf-visitor": true,
"cf-ipcountry": true,
"cf-pseudo-ipv4": true,
"x-forwarded-for": true,
"x-forwarded-proto": true,
"x-real-ip": true,
"cdn-loop": true,
}
var skipRespHeaders = map[string]bool{
"transfer-encoding": true,
"connection": true,
"keep-alive": true,
"content-encoding": true,
"content-length": true,
}
var client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
func init() {
path := routes.PathOf()
routes.Register(func(app *fiber.App) {
app.Post(path, handle)
fmt.Println("Loaded:", path)
})
}
func handle(c *fiber.Ctx) error {
userid := c.Params("userid")
url := fmt.Sprintf("https://l3-prod-all-bd.bilibiligame.net/api/tcpServer/%s", userid)
req, err := http.NewRequest("POST", url, strings.NewReader(string(c.Body())))
if err != nil {
return c.Status(500).SendString(err.Error())
}
c.Request().Header.VisitAll(func(k, v []byte) {
key := strings.ToLower(string(k))
if !skipReqHeaders[key] {
req.Header.Set(string(k), string(v))
}
})
req.Header.Set("host", "l3-prod-all-bd.bilibiligame.net")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return c.Status(500).SendString(err.Error())
}
defer resp.Body.Close()
for key, values := range resp.Header {
if !skipRespHeaders[strings.ToLower(key)] {
c.Set(key, values[0])
}
}
c.Response().Header.Del("Content-Length")
body, err := io.ReadAll(resp.Body)
if err != nil {
return c.Status(500).SendString(err.Error())
}
return c.Send(body)
}