package handler import ( "bytes" "github.com/gin-gonic/gin" "github.com/vulcand/oxy/forward" "github.com/vulcand/oxy/testutils" "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/service" "io/ioutil" "net/http" "strconv" "strings" ) func Proxy(c *gin.Context) { appId := c.Query("appId") // TODO 根据appId 获取真实地址 realPath, err := service.GetRealPath(appId) if err != nil { c.Error(err) return } f, _ := forward.New(forward.PassHostHeader(true), forward.ResponseModifier(func(resp *http.Response) error { //defer resp.Body.Close() // TODO 是否需要处理数据 ( 是否设置了敏感字段 ) if c.Request.Method == "GET" && service.CheckSensitiveField() { // TODO 过滤敏感字段 respbody, _ := ioutil.ReadAll(resp.Body) respbodyNew, err := service.FilterSensituveField(respbody) if err != nil { return err } resp.Header.Set("X-Log-By", "Apaas") l := strconv.Itoa(len(respbodyNew)) resp.Header.Set("Content-Length", l) resp.Body = ioutil.NopCloser(bytes.NewBuffer(respbodyNew)) } // TODO 计次计费 return nil })) c.Request.URL = testutils.ParseURI(realPath) c.Request.RequestURI = realPath c.Request.Host = getHost(realPath) f.ServeHTTP(c.Writer, c.Request) } // 获取域名 func getHost(url string) (path string) { if strings.Contains(url, "//") { path = strings.Split(url, "//")[1] if strings.Contains(path, "/") { path = strings.Split(path, "/")[0] } } return }