diff --git a/README.md b/README.md index ffb0ce4..badb09a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # NPS -![Version](https://img.shields.io/badge/version-v0.26.35-blue) +![Version](https://img.shields.io/badge/version-v0.26.36-blue) ![License](https://img.shields.io/badge/license-GPL--3.0-green) ![Go](https://img.shields.io/badge/Go-%3E%3D1.22-00ADD8?logo=go) diff --git a/cmd/nps/nps.go b/cmd/nps/nps.go index cd30441..0d114f8 100644 --- a/cmd/nps/nps.go +++ b/cmd/nps/nps.go @@ -26,7 +26,6 @@ import ( "ehang.io/nps/lib/common" "ehang.io/nps/lib/crypt" - "ehang.io/nps/web" "github.com/astaxie/beego" "github.com/astaxie/beego/logs" @@ -472,7 +471,6 @@ func initConfig(confDir string) { logs.Info("auth_key:", authKey) logs.Info("auth_crypt_key:", authCryptKey) } - web.ExtractWebFiles(common.GetRunPath()) } const defaultNpsConf = `http_proxy_ip=0.0.0.0 diff --git a/docs/changelog/index.md b/docs/changelog/index.md index 5e15348..0bf32ba 100644 --- a/docs/changelog/index.md +++ b/docs/changelog/index.md @@ -1,4 +1,8 @@ # 更新日志 +- 2026-07-12 v0.26.36 + - 优化: + - Web 管理界面完全从可执行文件内嵌资源加载:启动时不再释放或创建 `web` 目录,即使存在外部 `web` 目录也不再读取;安装/更新流程也不再复制 `web` 静态文件 + - 2026-07-04 v0.26.35 - 新增: - 客户端列表、隧道列表、域名解析列表支持多选批量删除 [#287](https://github.com/yisier/nps/issues/287) diff --git a/lib/goroutine/pool.go b/lib/goroutine/pool.go index b6ff9ea..63b8f7f 100644 --- a/lib/goroutine/pool.go +++ b/lib/goroutine/pool.go @@ -5,12 +5,12 @@ import ( "fmt" "io" "net" - "path/filepath" "strings" "sync" "ehang.io/nps/lib/common" "ehang.io/nps/lib/file" + "ehang.io/nps/web" "github.com/astaxie/beego/logs" "github.com/panjf2000/ants/v2" ) @@ -64,7 +64,7 @@ func CopyBuffer(dst io.Writer, src io.Reader, flow *file.Flow, task *file.Tunnel ip := common.GetIpByAddr(remote) var jsonBytes []byte - errorContent, _ := common.ReadAllFromFile(filepath.Join(common.GetRunPath(), "web", "static", "page", "auth.html")) + errorContent, _ := web.ReadStaticFile("page/auth.html") authHtml := string(errorContent) authHtml = strings.ReplaceAll(authHtml, "${ip}", ip) diff --git a/lib/install/install.go b/lib/install/install.go index 981119e..05bc066 100644 --- a/lib/install/install.go +++ b/lib/install/install.go @@ -294,18 +294,7 @@ func downloadLatest2(bin string, path string) string { return destPath } func copyStaticFile(srcPath, bin string) string { - path := common.GetInstallPath() - if bin == "nps" { - //复制文件到对应目录 - if err := CopyDir(filepath.Join(srcPath, "web", "views"), filepath.Join(path, "web", "views")); err != nil { - log.Fatalln(err) - } - chMod(filepath.Join(path, "web", "views"), 0766) - if err := CopyDir(filepath.Join(srcPath, "web", "static"), filepath.Join(path, "web", "static")); err != nil { - log.Fatalln(err) - } - chMod(filepath.Join(path, "web", "static"), 0766) - } + // nps web UI is embedded in the binary; no web/ files to copy. binPath, _ := filepath.Abs(os.Args[0]) if !common.IsWindows() { if _, err := copyFile(filepath.Join(srcPath, bin), "/usr/bin/"+bin); err != nil { @@ -330,10 +319,7 @@ func copyStaticFile(srcPath, bin string) string { } func copyStaticFileReplaceNps(srcPath, descPath string) string { - //复制文件到对应目录 - os.Rename(filepath.Join(srcPath, "web"), filepath.Join(descPath, "web")) - chMod(filepath.Join(descPath, "web"), 0766) - + // Web UI is embedded in the binary; only replace the executable. binPath, _ := filepath.Abs(os.Args[0]) if !common.IsWindows() { os.Rename(filepath.Join(srcPath, "nps"), filepath.Join(descPath, "nps")) @@ -371,10 +357,8 @@ func InstallNpc() { func InstallNps() string { path := common.GetInstallPath() log.Println("install path:" + path) - if common.FileExists(path) { - MkidrDirAll(path, "web/static", "web/views") - } else { - MkidrDirAll(path, "conf", "web/static", "web/views") + if !common.FileExists(path) { + MkidrDirAll(path, "conf") // not copy config if the config file is exist if err := CopyDir(filepath.Join(common.GetAppPath(), "conf"), filepath.Join(path, "conf")); err != nil { log.Fatalln(err) @@ -383,7 +367,7 @@ func InstallNps() string { } binPath := copyStaticFile(common.GetAppPath(), "nps") log.Println("install ok!") - log.Println("Static files and configuration files in the current directory will be useless") + log.Println("Web UI is embedded in the nps binary; no web/ directory is required") log.Println("The new configuration file is located in", path, "you can edit them") if !common.IsWindows() { log.Println(`You can start with: diff --git a/lib/version/version.go b/lib/version/version.go index 2df5b1f..a6cde10 100644 --- a/lib/version/version.go +++ b/lib/version/version.go @@ -1,6 +1,6 @@ package version -const VERSION = "0.26.35" +const VERSION = "0.26.36" // Compulsory minimum version, Minimum downward compatibility to this version func GetVersion() string { diff --git a/server/proxy/http.go b/server/proxy/http.go index a8a64a2..0cf1963 100644 --- a/server/proxy/http.go +++ b/server/proxy/http.go @@ -10,6 +10,7 @@ import ( "ehang.io/nps/lib/file" "ehang.io/nps/lib/goroutine" "ehang.io/nps/server/connection" + "ehang.io/nps/web" "encoding/json" "fmt" "github.com/astaxie/beego" @@ -58,7 +59,7 @@ func NewHttp(bridge *bridge.Bridge, c *file.Tunnel, httpPort, httpsPort int, use func (s *httpServer) Start() error { var err error - if s.errorContent, err = common.ReadAllFromFile(filepath.Join(common.GetRunPath(), "web", "static", "page", "error.html")); err != nil { + if s.errorContent, err = web.ReadStaticFile("page/error.html"); err != nil { s.errorContent = []byte("nps 404") } if s.httpPort > 0 { @@ -231,7 +232,7 @@ reset: return } - errorContent, _ := common.ReadAllFromFile(filepath.Join(common.GetRunPath(), "web", "static", "page", "auth.html")) + errorContent, _ := web.ReadStaticFile("page/auth.html") authHtml := string(errorContent) authHtml = strings.ReplaceAll(authHtml, "${ip}", common.GetIpByAddr(c.RemoteAddr().String())) s.errorContent, err = []byte(authHtml), err diff --git a/server/proxy/tcp.go b/server/proxy/tcp.go index 68b90e7..d947479 100755 --- a/server/proxy/tcp.go +++ b/server/proxy/tcp.go @@ -7,7 +7,6 @@ import ( "errors" "net" "net/http" - "path/filepath" "strconv" "ehang.io/nps/bridge" @@ -15,6 +14,7 @@ import ( "ehang.io/nps/lib/conn" "ehang.io/nps/lib/file" "ehang.io/nps/server/connection" + "ehang.io/nps/web" "github.com/astaxie/beego" "github.com/astaxie/beego/logs" ) @@ -66,8 +66,8 @@ func (s *WebServer) Start() error { <-stop } beego.BConfig.WebConfig.Session.SessionOn = true - beego.SetStaticPath(beego.AppConfig.String("web_base_url")+"/static", filepath.Join(common.GetRunPath(), "web", "static")) - beego.SetViewsPath(filepath.Join(common.GetRunPath(), "web", "views")) + // Serve management UI from embedded assets only (no disk web/ directory). + web.InitBeegoAssets() err := errors.New("Web management startup failure ") var l net.Listener if l, err = connection.GetWebManagerListener(); err == nil { diff --git a/web/embed.go b/web/embed.go index e41e9c2..455374f 100644 --- a/web/embed.go +++ b/web/embed.go @@ -3,11 +3,11 @@ package web import ( "embed" "io/fs" - "os" - "path/filepath" + "net/http" + "path" + "strings" - "ehang.io/nps/lib/common" - "github.com/astaxie/beego/logs" + "github.com/astaxie/beego" ) //go:embed static @@ -16,37 +16,70 @@ var StaticFS embed.FS //go:embed views var ViewsFS embed.FS -func ExtractWebFiles(runPath string) { - webDir := filepath.Join(runPath, "web") - staticDir := filepath.Join(webDir, "static") - viewsDir := filepath.Join(webDir, "views") - - if !common.FileExists(staticDir) { - extractFS(StaticFS, "static", webDir) - logs.Info("Extracted embedded web/static to", staticDir) - } - if !common.FileExists(viewsDir) { - extractFS(ViewsFS, "views", webDir) - logs.Info("Extracted embedded web/views to", viewsDir) - } +// slashFileSystem normalizes path separators so embed.FS works on Windows +// (beego uses filepath.Join which produces backslashes). +type slashFileSystem struct { + fs http.FileSystem } -func extractFS(efs embed.FS, root string, destDir string) { - fs.WalkDir(efs, ".", func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - targetPath := filepath.Join(destDir, path) - if d.IsDir() { - os.MkdirAll(targetPath, 0755) - return nil - } - data, err := efs.ReadFile(path) - if err != nil { - return err - } - os.MkdirAll(filepath.Dir(targetPath), 0755) - os.WriteFile(targetPath, data, 0644) - return nil +func (s slashFileSystem) Open(name string) (http.File, error) { + name = strings.ReplaceAll(name, "\\", "/") + name = path.Clean("/" + strings.TrimPrefix(name, "/")) + if name == "/" { + name = "." + } else { + name = strings.TrimPrefix(name, "/") + } + return s.fs.Open(name) +} + +// ViewsHTTPFS returns an http.FileSystem for embedded view templates. +// Paths are rooted at "views/..." to match beego SetViewsPath("views"). +func ViewsHTTPFS() http.FileSystem { + return slashFileSystem{fs: http.FS(ViewsFS)} +} + +// StaticHTTPFS returns an http.FileSystem rooted at the embedded static/ directory +// (so Open("css/style.css") maps to static/css/style.css). +func StaticHTTPFS() http.FileSystem { + sub, err := fs.Sub(StaticFS, "static") + if err != nil { + panic("web: embed static sub FS: " + err.Error()) + } + return slashFileSystem{fs: http.FS(sub)} +} + +// ReadStaticFile reads a file from the embedded static FS. +// name is relative to static/, e.g. "page/error.html". +func ReadStaticFile(name string) ([]byte, error) { + name = strings.ReplaceAll(name, "\\", "/") + name = path.Clean("/" + strings.TrimPrefix(name, "/")) + name = strings.TrimPrefix(name, "/") + return StaticFS.ReadFile(path.Join("static", name)) +} + +// InitBeegoAssets configures Beego to serve views and static files exclusively +// from the embedded filesystem. Disk web/ directories are never used. +func InitBeegoAssets() { + beego.SetTemplateFSFunc(func() http.FileSystem { + return ViewsHTTPFS() }) + beego.SetViewsPath("views") + + // Drop default (and any prior) disk-based static mappings. + beego.DelStaticPath("/static") + base := strings.TrimSuffix(beego.AppConfig.String("web_base_url"), "/") + staticPrefix := base + "/static" + if !strings.HasPrefix(staticPrefix, "/") { + staticPrefix = "/" + staticPrefix + } + staticPrefix = path.Clean(staticPrefix) + if base != "" { + // Also clear a bare /static mapping when web_base_url is set. + beego.DelStaticPath(staticPrefix) + } + + fileServer := http.StripPrefix(staticPrefix, http.FileServer(StaticHTTPFS())) + // options[0]=true registers as prefix match: staticPrefix/?:all(.*) + beego.Handler(staticPrefix, fileServer, true) }