fix: 修复权限边界与安全问题
- JWT 密钥改为环境变量/配置文件/随机生成注入,移除硬编码 - AuthMiddleware 用数据库 role 覆盖 claims,降级/禁用即时生效 - 改 role/status 时主动失效目标用户会话 - /ws 支持 JWT 认证(?token=),兼容密码认证,CheckOrigin 改同源校验 - 默认管理员改用随机密码,stdout 与文件双交付 - 登录与 WS 密码认证加限流(5次/分钟) - role 字段白名单校验(仅 admin/user) - socket/目录权限可配置(sock_mode/sock_group/sock_dir_mode),默认兼容现状 - 配置文件权限收紧 0644→0600,目录 0755→0700 - 静态文件改用 http.Dir.Open + NoRoute,移除手动路径拼接 - 隧道加 SetReadLimit(1MB) 与单用户连接数上限(3) - README 补充安全配置与生产部署说明
This commit is contained in:
+12
-13
@@ -2,7 +2,6 @@ package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"lmvpn/internal/handler"
|
||||
@@ -15,7 +14,7 @@ import (
|
||||
func Setup(r *gin.Engine) {
|
||||
r.GET("/ws", vpn.HandleWS)
|
||||
|
||||
r.POST("/api/login", handler.Login)
|
||||
r.POST("/api/login", middleware.LoginRateLimit(), handler.Login)
|
||||
|
||||
auth := r.Group("/api")
|
||||
auth.Use(middleware.AuthMiddleware())
|
||||
@@ -37,21 +36,21 @@ func Setup(r *gin.Engine) {
|
||||
admin.DELETE("/users/:id/sessions", handler.AdminRevokeUserSessions)
|
||||
}
|
||||
|
||||
fs := http.FileServer(http.Dir("./dist"))
|
||||
r.Use(func(c *gin.Context) {
|
||||
if strings.HasPrefix(c.Request.URL.Path, "/ws") {
|
||||
distDir := http.Dir("./dist")
|
||||
fs := http.FileServer(distDir)
|
||||
r.NoRoute(func(c *gin.Context) {
|
||||
path := c.Request.URL.Path
|
||||
if strings.HasPrefix(path, "/api") || strings.HasPrefix(path, "/ws") {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(c.Request.URL.Path, "/api") {
|
||||
c.Next()
|
||||
f, err := distDir.Open(path)
|
||||
if err != nil {
|
||||
c.Header("Content-Type", "text/html")
|
||||
c.File("./dist/index.html")
|
||||
return
|
||||
}
|
||||
|
||||
path := "./dist" + c.Request.URL.Path
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
c.Request.URL.Path = "/"
|
||||
}
|
||||
f.Close()
|
||||
fs.ServeHTTP(c.Writer, c.Request)
|
||||
c.Abort()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user