Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
apaas-meshproxy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gzga-jzapi
apaas-meshproxy
Commits
2bbff5f7
Commit
2bbff5f7
authored
Apr 23, 2020
by
gaoshiyao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
代理镜像
parent
e24b3a7b
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
71 additions
and
114 deletions
+71
-114
src/handler/counthandler.go
src/handler/counthandler.go
+0
-27
src/handler/proxyhandler.go
src/handler/proxyhandler.go
+39
-66
src/main.go
src/main.go
+2
-19
src/router/router.go
src/router/router.go
+0
-2
src/service/field.go
src/service/field.go
+30
-0
No files found.
src/handler/counthandler.go
deleted
100644 → 0
View file @
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
,
})
}
src/handler/proxyhandler.go
View file @
2bbff5f7
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
}
src/main.go
View file @
2bbff5f7
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
}
}
src/router/router.go
View file @
2bbff5f7
...
...
@@ -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
}
src/service/field.go
0 → 100644
View file @
2bbff5f7
/*
@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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment