podterminal/web/router.go

86 lines
1.7 KiB
Go
Raw Permalink Normal View History

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"
"github.com/google/uuid"
"github.com/spf13/viper"
2024-01-27 11:20:40 +01:00
)
var proxies = make(map[string]*httputil.ReverseProxy)
func Run() error {
2024-01-27 11:20:40 +01:00
router := setupRouter()
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 {
log.Panic("Could not start Webserver: ", err)
2024-01-27 11:20:40 +01:00
return err
}
return nil
}
func setupRouter() *gin.Engine {
log.Println("Setting up router")
2024-01-27 11:20:40 +01:00
gin.ForceConsoleColor()
gin.SetMode("release")
router := gin.New()
store := cookie.NewStore([]byte(uuid.NewString()))
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))
//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
}
}