waflogcontroller.go 1.26 KB
Newer Older
1 2 3 4 5 6 7 8 9
package controller

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

	"github.com/gin-gonic/gin"
10
	"github.com/rs/zerolog/log"
11 12 13 14 15 16 17 18 19 20 21 22 23 24
)

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) {
25
	region := ctx.Query("region_code")
26 27 28 29 30
	remoteUrl, err := url.Parse(c.regionUrlMap[region])
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
31
	log.Info().Msgf("remoteUrl: %s", remoteUrl.String())
32

33 34 35 36 37 38 39
	// proxy := httputil.NewSingleHostReverseProxy(remoteUrl)
	// proxy.Rewrite = func(req *httputil.ProxyRequest) {
	// 	req.Out.URL.Path = strings.Replace(req.In.URL.Path, "/api/v2/containerSec/waf", "/api/v2/waf", 1)
	// 	req.Out.Host = remoteUrl.Host
	// }
	proxy := &httputil.ReverseProxy{
		Rewrite: func(req *httputil.ProxyRequest) {
40 41
			req.Out.URL.Scheme = remoteUrl.Scheme
			req.Out.URL.RawQuery = req.In.URL.RawQuery
42 43 44
			req.Out.URL.Host = remoteUrl.Host
			req.Out.URL.Path = strings.Replace(req.In.URL.Path, "/api/v2/containerSec/waf", "/api/v2/waf", 1)
		},
45 46 47 48 49
	}

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

}