waflogcontroller.go 876 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
package controller

import (
	"net/http"
	"net/http/httputil"
	"net/url"
	"strings"

	"github.com/gin-gonic/gin"
)

type WafLogController struct {
	remote       string
	regionUrlMap map[string]string
}

func NewWafLogController(regionUrlMap map[string]string) *WafLogController {
	return &WafLogController{
		regionUrlMap: regionUrlMap,
	}
}

func (c *WafLogController) WafLogProxy(ctx *gin.Context) {
	region := ctx.Query("region")
	remoteUrl, err := url.Parse(c.regionUrlMap[region])
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	proxy := httputil.NewSingleHostReverseProxy(remoteUrl)
	proxy.Director = func(req *http.Request) {
		req.URL.Path = strings.Replace(req.URL.Path, "/api/v2/containerSec/waf", "/api/v2/waf", 1)
		req.URL.RawQuery = ctx.Request.URL.RawQuery
	}

	proxy.ServeHTTP(ctx.Writer, ctx.Request)

}