From 2bbff5f7588a6ce7fae53873ca8ec7835e4e21d1 Mon Sep 17 00:00:00 2001 From: gaoshiyao Date: Thu, 23 Apr 2020 18:13:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=90=86=E9=95=9C=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/handler/counthandler.go | 27 ---------- src/handler/proxyhandler.go | 105 ++++++++++++++---------------------- src/main.go | 21 +------- src/router/router.go | 2 - src/service/field.go | 30 +++++++++++ 5 files changed, 71 insertions(+), 114 deletions(-) delete mode 100644 src/handler/counthandler.go create mode 100644 src/service/field.go diff --git a/src/handler/counthandler.go b/src/handler/counthandler.go deleted file mode 100644 index 19a0904..0000000 --- a/src/handler/counthandler.go +++ /dev/null @@ -1,27 +0,0 @@ -package handler - -import ( - "fmt" - "github.com/gin-gonic/gin" - "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/client" - "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/config" -) - -func GetCount(c *gin.Context) { - redis, err := client.GetRedisClient() - if err != nil { - c.Error(err) - } - ic, err := redis.Get("bgproxy" + config.ProxyConf.MeshId) - if err != nil { - c.JSON(500, gin.H{ - "success": 0, - "errMsg": err, - }) - return - } - c.JSON(200, gin.H{ - "Key": fmt.Sprintf("bgproxy" + config.ProxyConf.MeshId), - "Count": ic, - }) -} diff --git a/src/handler/proxyhandler.go b/src/handler/proxyhandler.go index 2681afa..8d892c7 100644 --- a/src/handler/proxyhandler.go +++ b/src/handler/proxyhandler.go @@ -1,83 +1,56 @@ package handler import ( - "fmt" + "bytes" "github.com/gin-gonic/gin" - "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/client" - "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/tools" + "github.com/vulcand/oxy/forward" + "github.com/vulcand/oxy/testutils" + "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/service" + "io/ioutil" "net/http" + "strconv" "strings" - - "github.com/vulcand/oxy/forward" - "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/config" ) -var fwd *forward.Forwarder - -func init() { - fwd, _ = forward.New(forward.PassHostHeader(true)) -} - func Proxy(c *gin.Context) { - if err := count(); err != nil { - fmt.Println(err.Error()) - c.String(200, "缓存计数失败!") - return - } - var result []byte - var err error - switch strings.ToUpper(config.ProxyConf.Method) { - case "GET": - result, err = tools.ProxySend(c.Request, "GET", config.ProxyConf.Url, "", nil) - } + appId := c.Query("appId") + // TODO 根据appId 获取真实地址 + realPath, err := service.GetRealPath(appId) if err != nil { c.Error(err) return } - c.Writer.Write(result) -} - -func getProxyURL(req *http.Request) string { - // 获取转发地址 - result := config.ProxyConf.Url - //path := req.URL.Path - //path = strings.Replace(req.URL.Path, config.Prefix+"/proxy", config.ProxyPath, 1) - //rawQuery := req.URL.RawQuery - //var result = "" - //if rawQuery == "" { - // result = fmt.Sprintf("http://%s%s", config.ProxyHost, path) - //} else { - // result = fmt.Sprintf("http://%s%s?%s", config.ProxyHost, path, rawQuery) - //} - //if strings.Contains(result, "?") { - // if strings.HasSuffix(result, "/") { - // result = strings.TrimRight(result, "/") - // } - //} - return result + 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 getRequestURI(req *http.Request) string { - path := req.URL.Path - rawQuery := req.URL.RawQuery - var result = "" - if rawQuery == "" { - result = path - } else { - result = path + "?" + rawQuery - } - return result -} - -/** -计数 -*/ -func count() error { - redis, err := client.GetRedisClient() - if err != nil { - return err +// 获取域名 +func getHost(url string) (path string) { + if strings.Contains(url, "//") { + path = strings.Split(url, "//")[1] + if strings.Contains(path, "/") { + path = strings.Split(path, "/")[0] + } } - ic, err := redis.Incr("bgproxy" + config.ProxyConf.MeshId) - _, err = ic.Result() - return err + return } diff --git a/src/main.go b/src/main.go index 0aab182..07c2fa4 100644 --- a/src/main.go +++ b/src/main.go @@ -1,9 +1,7 @@ package main import ( - "encoding/json" "fmt" - "io/ioutil" "net/http" "os" "time" @@ -29,7 +27,6 @@ func main() { pflag.Parse() initEnv() initConfig() - getProxyConf() server() } @@ -45,8 +42,8 @@ func server() error { //注册环境变量 func initEnv() { - if len(os.Getenv("AWE_REDIS_CONNECTION")) > 0 { - *redisUrl = os.Getenv("AWE_REDIS_CONNECTION") + if len(os.Getenv("REDIS_CONNECTION")) > 0 { + *redisUrl = os.Getenv("REDIS_CONNECTION") } } @@ -56,17 +53,3 @@ func initConfig() { config.RedisURL = *redisUrl config.Prefix = *argPrefix } - -// 获取代理参数 -func getProxyConf() { - b, err := ioutil.ReadFile(*confPath) - if err != nil { - logrus.Error(err) - return - } - json.Unmarshal(b, &config.ProxyConf) - fmt.Println("代理参数:", config.ProxyConf) - if len(config.ProxyConf.MeshId) > 0 { - *argPrefix += "/" + config.ProxyConf.MeshId - } -} diff --git a/src/router/router.go b/src/router/router.go index ca78d38..6ca90c1 100644 --- a/src/router/router.go +++ b/src/router/router.go @@ -31,9 +31,7 @@ func Load(middleware ...gin.HandlerFunc) http.Handler { e.Use(middleware...) root := e.Group(config.Prefix) { - root.GET("/count", handler.GetCount) root.Any("/proxy", handler.Proxy) } - return e } diff --git a/src/service/field.go b/src/service/field.go new file mode 100644 index 0000000..f76c223 --- /dev/null +++ b/src/service/field.go @@ -0,0 +1,30 @@ +/* +@Time : 2020/4/23 17:44 +@Author : gaoshiyao +@File : field +@Software: GoLand +@Des: +*/ +package service + +import ( + "fmt" +) + +// 判断是否设置了敏感字段,并有敏感词 +func CheckSensitiveField() bool { + return true +} + +// 过滤敏感词 +func FilterSensituveField(respbody []byte) (body []byte, err error) { + body = respbody + fmt.Println("=========> 处理返回体!") + return body, err +} + +// 获取真实地址 +func GetRealPath(appId string) (path string, err error) { + path = "https://www.baidu.com" + return +} -- 2.26.0