From 0df98345d5936c19c2d7808e87903326662b5aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E9=BB=84=E6=9E=97?= Date: Mon, 11 Sep 2023 22:59:23 +0800 Subject: [PATCH] add new login (not complete) --- assets/templates/login.html | 62 +++++++++++++++++++++++++++++ pkg/server/controller/controller.go | 17 +++++++- pkg/server/controller/op.go | 33 ++++++++++++--- 3 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 assets/templates/login.html diff --git a/assets/templates/login.html b/assets/templates/login.html new file mode 100644 index 0000000..74b091d --- /dev/null +++ b/assets/templates/login.html @@ -0,0 +1,62 @@ + + + + Login + + + + + + + + + +
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+ +
+
+ + + + \ No newline at end of file diff --git a/pkg/server/controller/controller.go b/pkg/server/controller/controller.go index 265c8a0..007765e 100644 --- a/pkg/server/controller/controller.go +++ b/pkg/server/controller/controller.go @@ -175,7 +175,22 @@ func (c *HandleController) MakeHandlerFunc() gin.HandlerFunc { } } -func (c *HandleController) MakeManagerFunc() func(context *gin.Context) { +func (c *HandleController) MakeLoginFunc() func(context *gin.Context) { + return func(context *gin.Context) { + method := context.Request.Method + if method == "GET" { + context.HTML(http.StatusOK, "login.html", gin.H{ + "version": c.Version, + }) + } else { + context.JSON(http.StatusOK, gin.H{ + "Success": true, + }) + } + } +} + +func (c *HandleController) MakeIndexFunc() func(context *gin.Context) { return func(context *gin.Context) { context.HTML(http.StatusOK, "index.html", gin.H{ "version": c.Version, diff --git a/pkg/server/controller/op.go b/pkg/server/controller/op.go index acfe839..a471fe9 100644 --- a/pkg/server/controller/op.go +++ b/pkg/server/controller/op.go @@ -1,11 +1,13 @@ package controller import ( + "encoding/base64" "fmt" plugin "github.com/fatedier/frp/pkg/plugin/server" "github.com/gin-gonic/gin" "gopkg.in/ini.v1" "log" + "net/http" "os" "path/filepath" "strconv" @@ -37,17 +39,21 @@ func (c *HandleController) Register(rootDir string, engine *gin.Engine) { engine.Delims("${", "}") engine.LoadHTMLGlob(filepath.Join(assets, "templates/*")) engine.POST("/handler", c.MakeHandlerFunc()) + engine.Static("/static", filepath.Join(assets, "static")) + engine.GET("/login", c.MakeLoginFunc()) var group *gin.RouterGroup if len(c.CommonInfo.User) != 0 { - group = engine.Group("/", gin.BasicAuthForRealm(gin.Accounts{ - c.CommonInfo.User: c.CommonInfo.Pwd, - }, "Restricted")) + //group = engine.Group("/", gin.BasicAuthForRealm(gin.Accounts{ + // c.CommonInfo.User: c.CommonInfo.Pwd, + //}, "Restricted")) + + group = engine.Group("/", c.Authorize()) } else { group = engine.Group("/") } - group.Static("/static", filepath.Join(assets, "static")) - group.GET("/", c.MakeManagerFunc()) + group.POST("/login", c.MakeLoginFunc()) + group.GET("/", c.MakeIndexFunc()) group.GET("/lang.json", c.MakeLangFunc()) group.GET("/tokens", c.MakeQueryTokensFunc()) group.POST("/add", c.MakeAddTokenFunc()) @@ -58,6 +64,23 @@ func (c *HandleController) Register(rootDir string, engine *gin.Engine) { group.GET("/proxy/*serverApi", c.MakeProxyFunc()) } +func (c *HandleController) Authorize() gin.HandlerFunc { + return func(context *gin.Context) { + authorizationFromUser := context.Request.Header.Get("Authorization") + + userAndPwd := []byte(c.CommonInfo.User + ":" + c.CommonInfo.Pwd) + authorizationFromConfig := "Basic " + base64.StdEncoding.EncodeToString(userAndPwd) + + if authorizationFromUser == authorizationFromConfig { + context.Next() + } else { + context.Abort() + context.Redirect(http.StatusTemporaryRedirect, "/login") + return + } + } +} + func (c *HandleController) HandleLogin(content *plugin.LoginContent) plugin.Response { token := content.Metas["token"] user := content.User