2024-01-27 11:20:40 +01:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http/httputil"
|
|
|
|
|
|
|
|
"github.com/gin-contrib/sessions"
|
|
|
|
"github.com/gin-contrib/sessions/cookie"
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-01-28 15:31:58 +01:00
|
|
|
"github.com/spf13/viper"
|
2024-01-27 11:20:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var proxies = make(map[string]*httputil.ReverseProxy)
|
|
|
|
|
2024-01-29 16:45:51 +01:00
|
|
|
func Run() error {
|
2024-01-27 11:20:40 +01:00
|
|
|
router := setupRouter()
|
2024-01-29 16:45:51 +01:00
|
|
|
address := fmt.Sprintf("%s:%d", viper.GetString("ip_addr"), viper.GetInt("port"))
|
|
|
|
log.Println("Listening on address", address)
|
|
|
|
var err error
|
|
|
|
if viper.GetBool("ssl") == true {
|
|
|
|
err = router.RunTLS(
|
|
|
|
address,
|
|
|
|
viper.GetString("ssl_cert"),
|
|
|
|
viper.GetString("ssl_cert_key"),
|
|
|
|
)
|
|
|
|
log.Println("Using SSL")
|
|
|
|
} else {
|
|
|
|
err = router.Run(address)
|
|
|
|
}
|
|
|
|
log.Println("Router is ready")
|
2024-01-27 11:20:40 +01:00
|
|
|
if err != nil {
|
2024-01-31 08:21:43 +01:00
|
|
|
log.Panic("Could not start Webserver: ", err)
|
2024-01-27 11:20:40 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupRouter() *gin.Engine {
|
2024-01-28 15:31:58 +01:00
|
|
|
log.Println("Setting up router")
|
2024-01-27 11:20:40 +01:00
|
|
|
gin.ForceConsoleColor()
|
|
|
|
gin.SetMode("release")
|
|
|
|
router := gin.New()
|
2024-02-02 18:47:45 +01:00
|
|
|
store := cookie.NewStore([]byte(viper.GetString("session_key")))
|
2024-01-28 15:31:58 +01:00
|
|
|
store.Options(sessions.Options{
|
|
|
|
MaxAge: viper.GetInt("maxAge"),
|
|
|
|
})
|
2024-01-27 11:20:40 +01:00
|
|
|
router.Use(gin.Recovery())
|
|
|
|
router.Use(sessions.Sessions("session", store))
|
2024-01-28 15:31:58 +01:00
|
|
|
//router.Use(urlLog())
|
2024-01-30 16:45:03 +01:00
|
|
|
if viper.GetBool("block_filebrowser") == true {
|
|
|
|
router.Use(blockFilebrowser)
|
|
|
|
}
|
2024-01-28 11:24:18 +01:00
|
|
|
router.Use(containerProxy)
|
|
|
|
// router.Any("/", containerProxy)
|
2024-01-27 11:20:40 +01:00
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
func urlLog() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
|
|
|
log.Printf(
|
|
|
|
"[INFO] %s: %s: %s",
|
|
|
|
c.Request.RemoteAddr,
|
|
|
|
c.Request.Method,
|
|
|
|
c.Request.URL,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 16:45:03 +01:00
|
|
|
// blockFilebrowser blocks the URLs used by the Kclient File browser
|
|
|
|
func blockFilebrowser(c *gin.Context) {
|
|
|
|
switch c.Request.URL.RawPath {
|
|
|
|
case "/files":
|
|
|
|
c.Abort()
|
|
|
|
case "/filebrowser.js":
|
|
|
|
c.Abort()
|
|
|
|
case "/files/socket.io":
|
|
|
|
c.Abort()
|
|
|
|
|
|
|
|
default:
|
|
|
|
c.Next()
|
2024-01-27 11:20:40 +01:00
|
|
|
}
|
|
|
|
}
|