Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Oct 21, 2021
1 parent dedfd13 commit 9dccfef
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ var (
host string
port int
ssl bool
Timeout int
timeout int
)

// webCmd represents the web command
var webCmd = &cobra.Command{
Use: "web",
Short: "以web方式启动",
Run: func(cmd *cobra.Command, args []string) {
web.Start(host, port, ssl)
web.Start(host, port, timeout, ssl)
},
}

func init() {
webCmd.Flags().StringVarP(&host, "host", "", "0.0.0.0", "web服务监听地址")
webCmd.Flags().IntVarP(&port, "port", "p", 80, "web服务启动端口")
webCmd.Flags().BoolVarP(&ssl, "ssl", "", false, "web服务是否以https方式运行")
webCmd.Flags().IntVarP(&Timeout, "timeout", "t", 120, "登录超时时间(min)")
webCmd.Flags().IntVarP(&timeout, "timeout", "t", 120, "登录超时时间(min)")
rootCmd.AddCommand(webCmd)
}
11 changes: 6 additions & 5 deletions web/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/appleboy/gin-jwt/v2"
"github.com/gin-gonic/gin"
"time"
"trojan/cmd"
"trojan/core"
"trojan/web/controller"
)
Expand All @@ -22,12 +21,12 @@ type Login struct {
Password string `form:"password" json:"password" binding:"required"`
}

func init() {
func jwtInit(timeout int) {
authMiddleware, err = jwt.New(&jwt.GinJWTMiddleware{
Realm: "k8s-manager",
Key: []byte("secret key"),
Timeout: time.Minute * time.Duration(cmd.Timeout),
MaxRefresh: time.Minute * time.Duration(cmd.Timeout),
Timeout: time.Minute * time.Duration(timeout),
MaxRefresh: time.Minute * time.Duration(timeout),
IdentityKey: identityKey,
SendCookie: true,
PayloadFunc: func(data interface{}) jwt.MapClaims {
Expand Down Expand Up @@ -115,7 +114,9 @@ func RequestUsername(c *gin.Context) string {
}

// Auth 权限router
func Auth(r *gin.Engine) *jwt.GinJWTMiddleware {
func Auth(r *gin.Engine, timeout int) *jwt.GinJWTMiddleware {
jwtInit(timeout)

newInstall := gin.H{"code": 201, "message": "No administrator account found inside the database", "data": nil}
r.NoRoute(authMiddleware.MiddlewareFunc(), func(c *gin.Context) {
claims := jwt.ExtractClaims(c)
Expand Down
4 changes: 2 additions & 2 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ func staticRouter(router *gin.Engine) {
}

// Start web启动入口
func Start(host string, port int, isSSL bool) {
func Start(host string, port, timeout int, isSSL bool) {
router := gin.Default()
router.Use(gzip.Gzip(gzip.DefaultCompression))
staticRouter(router)
router.Use(Auth(router).MiddlewareFunc())
router.Use(Auth(router, timeout).MiddlewareFunc())
trojanRouter(router)
userRouter(router)
dataRouter(router)
Expand Down

0 comments on commit 9dccfef

Please sign in to comment.