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
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
}
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
}
}
......@@ -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
}
/*
@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