From 5cda4e0c8e24d5fd86487b1010d068c4d2d76684 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Thu, 1 Feb 2024 21:00:34 -0700 Subject: [PATCH] fix a lot of issue --- cmds/liter-server/api_v1.go | 15 +++---- cmds/liter-server/configs.go | 51 +++++++++++++---------- cmds/liter-server/dashboard | 2 +- cmds/liter-server/dashboard_assets_dev.go | 9 +++- cmds/liter-server/handler.go | 1 - cmds/liter-server/main.go | 2 +- cmds/liter-server/utils.go | 2 + 7 files changed, 49 insertions(+), 33 deletions(-) diff --git a/cmds/liter-server/api_v1.go b/cmds/liter-server/api_v1.go index 3c2dd97..2c64360 100644 --- a/cmds/liter-server/api_v1.go +++ b/cmds/liter-server/api_v1.go @@ -514,6 +514,14 @@ func registerPlayerAPI(s *Server, g *gin.RouterGroup) { // server status APIs func registerStatus(s *Server, g *gin.RouterGroup) { g.GET("/conns", func(ctx *gin.Context) { + ctx.Header("Cache-Control", "no-cache") + lm := s.conns.LastModified().Format(time.RFC1123) + ctx.Header("Last-Modified", lm) + if lm2 := ctx.GetHeader("If-Modified-Since"); len(lm2) != 0 && lm2 == lm { + ctx.Status(http.StatusNotModified) + return + } + type resT struct { Id int `json:"id"` Addr string `json:"addr"` @@ -522,8 +530,6 @@ func registerStatus(s *Server, g *gin.RouterGroup) { Server string `json:"server"` Player *liter.PlayerInfo `json:"player,omitempty"` } - ctx.Header("Cache-Control", "no-cache") - lm := s.conns.LastModified().Format(time.RFC1123) data := make([]resT, 0, s.conns.Count()) s.conns.ForEach(func(i int, conn *Conn) { local, svr, ok := conn.LocalServer() @@ -539,11 +545,6 @@ func registerStatus(s *Server, g *gin.RouterGroup) { Server: svr, }) }) - ctx.Header("Last-Modified", lm) - if lm2 := ctx.GetHeader("Last-Modified"); len(lm2) != 0 && lm2 == lm { - ctx.Status(http.StatusNotModified) - return - } ctx.JSON(http.StatusOK, gin.H{ "status": "ok", "data": data, diff --git a/cmds/liter-server/configs.go b/cmds/liter-server/configs.go index 3807e8c..2dc31a8 100644 --- a/cmds/liter-server/configs.go +++ b/cmds/liter-server/configs.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "errors" "fmt" "net" "os" @@ -199,16 +200,18 @@ func (p *PlayerInfo) update() (err error) { return } -func (p *PlayerInfo) UnmarshalJSON(buf []byte) (err error) { +var errUnknownPlayerInfo = errors.New("Unknown player info") + +func (p *PlayerInfo) UnmarshalJSON(buf []byte) error { var v any - if err = json.Unmarshal(buf, &v); err != nil { - return + if err := json.Unmarshal(buf, &v); err != nil { + return err } switch v := v.(type) { case string: - var err error - if p.Id, err = uuid.Parse(v); err == nil { - profile, err := AuthClient.GetPlayerProfile(p.Id) + if uid, err := uuid.Parse(v); err == nil { + p.Id = uid + profile, err := AuthClient.GetPlayerProfile(uid) if err != nil { return nil } @@ -226,12 +229,12 @@ func (p *PlayerInfo) UnmarshalJSON(buf []byte) (err error) { var ok bool var id string p.Name, _ = v["name"].(string) - if p.onlineMode { - if id, ok = v["id"].(string); ok { - var e error - if p.Id, e = uuid.Parse(id); e != nil { - ok = false - } + if id, ok = v["id"].(string); ok { + var e error + if p.Id, e = uuid.Parse(id); e != nil { + ok = false + } + if p.onlineMode { profile, err := AuthClient.GetPlayerProfile(p.Id) if err != nil { return nil @@ -240,20 +243,26 @@ func (p *PlayerInfo) UnmarshalJSON(buf []byte) (err error) { return nil } } - if p.Name == "" { - return fmt.Errorf("Unknown player info") - } else if p.onlineMode { - info, err := AuthClient.GetPlayerInfo(p.Name) - if err != nil { - return nil + if !ok { + if p.Name != "" && p.onlineMode { + info, err := AuthClient.GetPlayerInfo(p.Name) + if err != nil { + return err + } + p.Name = info.Name + p.Id = info.Id + } else { + return errUnknownPlayerInfo } - p.Name = info.Name - p.Id = info.Id } default: return fmt.Errorf("Unexpected player info (%T)%v", v, v) } - return + return nil +} + +func (p *PlayerInfo) MarshalJSON() (buf []byte, err error) { + return json.Marshal(&p.PlayerInfo) } type Whitelist struct { diff --git a/cmds/liter-server/dashboard b/cmds/liter-server/dashboard index 6e544ee..a9cdaec 160000 --- a/cmds/liter-server/dashboard +++ b/cmds/liter-server/dashboard @@ -1 +1 @@ -Subproject commit 6e544ee27ac6f3c28e97653385da2166e928b90b +Subproject commit a9cdaec6b1e77a28a86d7ae32f428c84de911261 diff --git a/cmds/liter-server/dashboard_assets_dev.go b/cmds/liter-server/dashboard_assets_dev.go index 9db8ccd..82372ff 100644 --- a/cmds/liter-server/dashboard_assets_dev.go +++ b/cmds/liter-server/dashboard_assets_dev.go @@ -12,7 +12,7 @@ import ( "github.com/gin-gonic/gin" ) -var DashboardAssets http.FileSystem = gin.Dir("dashboard/dist", false) +var DashboardAssets http.FileSystem func runDashResources() (cleaner func(), err error) { basedir := "." @@ -21,7 +21,12 @@ func runDashResources() (cleaner func(), err error) { } loger.Infof("Starting frontend debug server") - cmd := exec.Command("npm", "-C", filepath.Join(basedir, "dashboard"), "run", "build-dev") + npmDir := filepath.Join(basedir, "dashboard") + distDir := filepath.Join(npmDir, "dist") + + DashboardAssets = gin.Dir(distDir, false) + + cmd := exec.Command("npm", "-C", npmDir, "run", "build-dev") pout, err := cmd.StdoutPipe() if err != nil { diff --git a/cmds/liter-server/handler.go b/cmds/liter-server/handler.go index 64fe8db..c099817 100644 --- a/cmds/liter-server/handler.go +++ b/cmds/liter-server/handler.go @@ -375,7 +375,6 @@ func proxyLoginPackets(s *Server, svr, cli *liter.Conn) (res *liter.LoginSuccess loginDisconnectByErr(cli, err) return } - return } func parseAndForward(dst, src *script.WrappedConn) <-chan error { diff --git a/cmds/liter-server/main.go b/cmds/liter-server/main.go index a8d0c12..0adec8a 100644 --- a/cmds/liter-server/main.go +++ b/cmds/liter-server/main.go @@ -158,7 +158,6 @@ func (r *Runner) Run() { var err error loger.Infof("Liter Server %s", version) - r.manager.SetLogger(loger) if r.configDir, err = os.Getwd(); err != nil { loger.Panicf("Cannot get working dir", err) @@ -166,6 +165,7 @@ func (r *Runner) Run() { r.LoadConfigs() r.manager = script.NewManager() + r.manager.SetLogger(loger) r.server = NewServer(r.configDir, r.manager) r.server.configLock = &r.configLock r.server.cfgHash = &r.cfgHash diff --git a/cmds/liter-server/utils.go b/cmds/liter-server/utils.go index 5018226..b1a1257 100644 --- a/cmds/liter-server/utils.go +++ b/cmds/liter-server/utils.go @@ -79,6 +79,8 @@ func (m *FlatMemory[T]) Count() int { } func (m *FlatMemory[T]) LastModified() time.Time { + m.mux.RLock() + defer m.mux.RUnlock() return m.lastEdit }