|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/gin-gonic/gin" |
| 8 | + "github.com/sirupsen/logrus" |
| 9 | +) |
| 10 | + |
| 11 | +// SetupGuardConfig controls how the setup guard middleware behaves. |
| 12 | +type SetupGuardConfig struct { |
| 13 | + // IsInitialized returns the current initialization status. |
| 14 | + IsInitialized func() (bool, error) |
| 15 | + // SetupPath denotes the setup entry path, defaults to /setup. |
| 16 | + SetupPath string |
| 17 | + // RedirectPath denotes the path to redirect to once initialized, defaults to /. |
| 18 | + RedirectPath string |
| 19 | + // AllowPaths lists exact paths that should remain accessible before initialization. |
| 20 | + AllowPaths []string |
| 21 | + // AllowPrefixes lists path prefixes that should remain accessible before initialization. |
| 22 | + AllowPrefixes []string |
| 23 | +} |
| 24 | + |
| 25 | +// SetupGuard ensures only setup resources are accessible before initialization |
| 26 | +// and blocks setup routes after initialization is complete. |
| 27 | +func SetupGuard(cfg SetupGuardConfig) gin.HandlerFunc { |
| 28 | + setupPath := cfg.SetupPath |
| 29 | + if setupPath == "" { |
| 30 | + setupPath = "/setup" |
| 31 | + } |
| 32 | + redirectPath := cfg.RedirectPath |
| 33 | + if redirectPath == "" { |
| 34 | + redirectPath = "/" |
| 35 | + } |
| 36 | + |
| 37 | + allowPaths := map[string]struct{}{ |
| 38 | + setupPath: {}, |
| 39 | + setupPath + "/": {}, |
| 40 | + } |
| 41 | + |
| 42 | + for _, p := range cfg.AllowPaths { |
| 43 | + allowPaths[p] = struct{}{} |
| 44 | + } |
| 45 | + |
| 46 | + allowPrefixes := []string{setupPath + "/"} |
| 47 | + allowPrefixes = append(allowPrefixes, cfg.AllowPrefixes...) |
| 48 | + |
| 49 | + return func(c *gin.Context) { |
| 50 | + initialized := false |
| 51 | + if cfg.IsInitialized != nil { |
| 52 | + var err error |
| 53 | + initialized, err = cfg.IsInitialized() |
| 54 | + if err != nil { |
| 55 | + logrus.WithError(err).Warn("setup guard: failed to determine initialization state") |
| 56 | + // Fail closed on error so users can still reach setup for recovery. |
| 57 | + initialized = false |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + path := c.Request.URL.Path |
| 62 | + |
| 63 | + if initialized { |
| 64 | + if path == setupPath || strings.HasPrefix(path, setupPath+"/") { |
| 65 | + switch c.Request.Method { |
| 66 | + case http.MethodGet, http.MethodHead: |
| 67 | + c.Redirect(http.StatusFound, redirectPath) |
| 68 | + case http.MethodOptions: |
| 69 | + c.Status(http.StatusNoContent) |
| 70 | + default: |
| 71 | + c.AbortWithStatusJSON(http.StatusForbidden, gin.H{ |
| 72 | + "code": http.StatusForbidden, |
| 73 | + "message": "系统已初始化,禁止重新初始化", |
| 74 | + }) |
| 75 | + } |
| 76 | + c.Abort() |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + c.Next() |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + if _, ok := allowPaths[path]; ok { |
| 85 | + c.Next() |
| 86 | + return |
| 87 | + } |
| 88 | + for _, prefix := range allowPrefixes { |
| 89 | + if strings.HasPrefix(path, prefix) { |
| 90 | + c.Next() |
| 91 | + return |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + switch c.Request.Method { |
| 96 | + case http.MethodGet, http.MethodHead: |
| 97 | + c.Redirect(http.StatusFound, setupPath) |
| 98 | + case http.MethodOptions: |
| 99 | + // Allow CORS preflight to complete without redirect loops. |
| 100 | + c.Status(http.StatusNoContent) |
| 101 | + default: |
| 102 | + c.AbortWithStatusJSON(http.StatusForbidden, gin.H{ |
| 103 | + "code": http.StatusForbidden, |
| 104 | + "message": "系统未初始化,请访问 /setup 完成初始化", |
| 105 | + }) |
| 106 | + } |
| 107 | + c.Abort() |
| 108 | + } |
| 109 | +} |
0 commit comments