-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
72 lines (61 loc) · 1.69 KB
/
mux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package debug
import (
"net/http"
"os"
"strings"
"sync"
"github.com/gofiber/fiber/v2"
"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/config"
"github.com/pubgo/funk/errors"
"github.com/pubgo/funk/log"
"github.com/pubgo/funk/recovery"
"github.com/pubgo/funk/running"
"github.com/pubgo/funk/strutil"
"github.com/pubgo/lava/core/debug"
"github.com/valyala/fasthttp"
"gopkg.in/yaml.v3"
)
var (
passwd = running.InstanceID
once sync.Once
)
func init() {
log.Info().Str("password", running.InstanceID).Msg("debug password")
debug.App().Use(func(c *fiber.Ctx) (gErr error) {
defer recovery.Recovery(func(err error) {
err = errors.WrapTag(err,
errors.T("headers", c.GetReqHeaders()),
errors.T("url", c.Request().URI().String()),
)
gErr = c.JSON(err)
})
token := strutil.FirstFnNotEmpty(
func() string { return c.Query("token") },
func() string { return string(c.Request().Header.Peek("token")) },
func() string { return c.Cookies("token") },
)
once.Do(func() {
configBytes := assert.Must1(os.ReadFile(config.GetConfigPath()))
var cfg debug.Config
assert.Must(yaml.Unmarshal(configBytes, &cfg))
passwd = cfg.Debug.Password
})
host := strings.Split(c.Hostname(), ":")[0]
if host != "localhost" && host != "127.0.0.1" {
if token != passwd {
err := errors.New("token 不存在或者密码不对")
c.WriteString(err.Error())
c.SendStatus(http.StatusInternalServerError)
return err
}
}
cc := fasthttp.AcquireCookie()
defer fasthttp.ReleaseCookie(cc)
cc.SetKey("token")
cc.SetValue(token)
c.Response().Header.SetCookie(cc)
log.Info().Str("path", c.Request().URI().String()).Msg("request")
return c.Next()
})
}