add http auth

This commit is contained in:
YouROK
2021-03-03 12:29:41 +03:00
parent 4b912c90ae
commit f108b7c718
5 changed files with 16 additions and 4 deletions

View File

@@ -21,6 +21,7 @@ type args struct {
Path string `arg:"-d" help:"database path"` Path string `arg:"-d" help:"database path"`
LogPath string `arg:"-l" help:"log path"` LogPath string `arg:"-l" help:"log path"`
RDB bool `arg:"-r" help:"start in read-only DB mode"` RDB bool `arg:"-r" help:"start in read-only DB mode"`
HttpAuth bool `arg:"-a" help:"Http auth on all requests"`
DontKill bool `arg:"-k" help:"dont kill program on signal"` DontKill bool `arg:"-k" help:"dont kill program on signal"`
UI bool `arg:"-u" help:"run page torrserver in browser"` UI bool `arg:"-u" help:"run page torrserver in browser"`
} }
@@ -43,6 +44,7 @@ func main() {
} }
settings.Path = params.Path settings.Path = params.Path
settings.HttpAuth = params.HttpAuth
log.Init(params.LogPath) log.Init(params.LogPath)
dnsResolve() dnsResolve()

View File

@@ -4,6 +4,7 @@ var (
tdb *TDB tdb *TDB
Path string Path string
ReadOnly bool ReadOnly bool
HttpAuth bool
) )
func InitSets(readOnly bool) { func InitSets(readOnly bool) {

View File

@@ -14,7 +14,7 @@ type requestI struct {
Action string `json:"action,omitempty"` Action string `json:"action,omitempty"`
} }
func SetupRoute(route *gin.Engine) { func SetupRoute(route *gin.RouterGroup) {
route.GET("/echo", echo) route.GET("/echo", echo)
route.GET("/shutdown", shutdown) route.GET("/shutdown", shutdown)

View File

@@ -6,7 +6,7 @@ import (
"server/web/pages/template" "server/web/pages/template"
) )
func SetupRoute(route *gin.Engine) { func SetupRoute(route *gin.RouterGroup) {
route.GET("/", mainPage) route.GET("/", mainPage)
route.GET("/stat", statPage) route.GET("/stat", statPage)
} }

View File

@@ -3,10 +3,12 @@ package web
import ( import (
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"server/log" "server/log"
"server/torr" "server/torr"
"server/version" "server/version"
"server/web/api" "server/web/api"
"server/web/auth"
"server/web/pages" "server/web/pages"
) )
@@ -26,8 +28,15 @@ func Start(port string) {
route := gin.New() route := gin.New()
route.Use(gin.Recovery(), cors.Default()) route.Use(gin.Recovery(), cors.Default())
api.SetupRoute(route)
pages.SetupRoute(route) routeAuth := auth.SetupAuth(route)
if routeAuth != nil {
api.SetupRoute(routeAuth)
pages.SetupRoute(routeAuth)
} else {
api.SetupRoute(&route.RouterGroup)
pages.SetupRoute(&route.RouterGroup)
}
log.TLogln("Start web", port) log.TLogln("Start web", port)
waitChan <- route.Run(":" + port) waitChan <- route.Run(":" + port)
} }