Commit 2bbff5f7 authored by gaoshiyao's avatar gaoshiyao

代理镜像

parent e24b3a7b
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,
})
}
package handler package handler
import ( import (
"fmt" "bytes"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/client" "github.com/vulcand/oxy/forward"
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/tools" "github.com/vulcand/oxy/testutils"
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/service"
"io/ioutil"
"net/http" "net/http"
"strconv"
"strings" "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) { func Proxy(c *gin.Context) {
if err := count(); err != nil { appId := c.Query("appId")
fmt.Println(err.Error()) // TODO 根据appId 获取真实地址
c.String(200, "缓存计数失败!") realPath, err := service.GetRealPath(appId)
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)
}
if err != nil { if err != nil {
c.Error(err) c.Error(err)
return return
} }
c.Writer.Write(result) f, _ := forward.New(forward.PassHostHeader(true), forward.ResponseModifier(func(resp *http.Response) error {
} //defer resp.Body.Close()
// TODO 是否需要处理数据 ( 是否设置了敏感字段 )
func getProxyURL(req *http.Request) string { if c.Request.Method == "GET" && service.CheckSensitiveField() {
// 获取转发地址 // TODO 过滤敏感字段
result := config.ProxyConf.Url respbody, _ := ioutil.ReadAll(resp.Body)
//path := req.URL.Path respbodyNew, err := service.FilterSensituveField(respbody)
//path = strings.Replace(req.URL.Path, config.Prefix+"/proxy", config.ProxyPath, 1) if err != nil {
//rawQuery := req.URL.RawQuery return err
//var result = "" }
//if rawQuery == "" { resp.Header.Set("X-Log-By", "Apaas")
// result = fmt.Sprintf("http://%s%s", config.ProxyHost, path) l := strconv.Itoa(len(respbodyNew))
//} else { resp.Header.Set("Content-Length", l)
// result = fmt.Sprintf("http://%s%s?%s", config.ProxyHost, path, rawQuery) resp.Body = ioutil.NopCloser(bytes.NewBuffer(respbodyNew))
//} }
//if strings.Contains(result, "?") { // TODO 计次计费
// if strings.HasSuffix(result, "/") { return nil
// result = strings.TrimRight(result, "/") }))
// } c.Request.URL = testutils.ParseURI(realPath)
//} c.Request.RequestURI = realPath
return result c.Request.Host = getHost(realPath)
f.ServeHTTP(c.Writer, c.Request)
} }
func getRequestURI(req *http.Request) string { // 获取域名
path := req.URL.Path func getHost(url string) (path string) {
rawQuery := req.URL.RawQuery if strings.Contains(url, "//") {
var result = "" path = strings.Split(url, "//")[1]
if rawQuery == "" { if strings.Contains(path, "/") {
result = path path = strings.Split(path, "/")[0]
} else { }
result = path + "?" + rawQuery
}
return result
}
/**
计数
*/
func count() error {
redis, err := client.GetRedisClient()
if err != nil {
return err
} }
ic, err := redis.Incr("bgproxy" + config.ProxyConf.MeshId) return
_, err = ic.Result()
return err
} }
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"time" "time"
...@@ -29,7 +27,6 @@ func main() { ...@@ -29,7 +27,6 @@ func main() {
pflag.Parse() pflag.Parse()
initEnv() initEnv()
initConfig() initConfig()
getProxyConf()
server() server()
} }
...@@ -45,8 +42,8 @@ func server() error { ...@@ -45,8 +42,8 @@ func server() error {
//注册环境变量 //注册环境变量
func initEnv() { func initEnv() {
if len(os.Getenv("AWE_REDIS_CONNECTION")) > 0 { if len(os.Getenv("REDIS_CONNECTION")) > 0 {
*redisUrl = os.Getenv("AWE_REDIS_CONNECTION") *redisUrl = os.Getenv("REDIS_CONNECTION")
} }
} }
...@@ -56,17 +53,3 @@ func initConfig() { ...@@ -56,17 +53,3 @@ func initConfig() {
config.RedisURL = *redisUrl config.RedisURL = *redisUrl
config.Prefix = *argPrefix 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
}
}
...@@ -31,9 +31,7 @@ func Load(middleware ...gin.HandlerFunc) http.Handler { ...@@ -31,9 +31,7 @@ func Load(middleware ...gin.HandlerFunc) http.Handler {
e.Use(middleware...) e.Use(middleware...)
root := e.Group(config.Prefix) root := e.Group(config.Prefix)
{ {
root.GET("/count", handler.GetCount)
root.Any("/proxy", handler.Proxy) root.Any("/proxy", handler.Proxy)
} }
return e return e
} }
/*
@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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment