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
0aa8b3c0
Commit
0aa8b3c0
authored
Nov 17, 2020
by
张磊涛
💬
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dev' into 'master'
Dev See merge request apaas/apaas-meshproxy!3
parents
c03a1f30
7de5c8b5
Changes
9
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
362 additions
and
218 deletions
+362
-218
go.mod
go.mod
+1
-0
go.sum
go.sum
+2
-0
src/dao/service_apply.go
src/dao/service_apply.go
+5
-5
src/dao/service_request_record.go
src/dao/service_request_record.go
+19
-13
src/handler/proxyhandler.go
src/handler/proxyhandler.go
+148
-87
src/main.go
src/main.go
+4
-3
src/model/response.go
src/model/response.go
+26
-11
src/router/router.go
src/router/router.go
+6
-2
src/service/field.go
src/service/field.go
+151
-97
No files found.
go.mod
View file @
0aa8b3c0
...
...
@@ -22,6 +22,7 @@ require (
github.com/onsi/ginkgo v1.10.2 // indirect
github.com/onsi/gomega v1.7.0 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cast v1.3.1
github.com/spf13/pflag v1.0.5
github.com/tealeg/xlsx v1.0.5 // indirect
github.com/vulcand/oxy v1.0.0
...
...
go.sum
View file @
0aa8b3c0
...
...
@@ -143,6 +143,8 @@ github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqn
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
...
...
src/dao/service_apply.go
View file @
0aa8b3c0
...
...
@@ -2,9 +2,9 @@ package dao
type
ServiceApply
struct
{
Id
int
`json:"id" xorm:"id"`
Service
_i
d
string
`json:"service_id" xorm:"service_id"`
User
_i
d
int
`json:"user_id" xorm:"user_id"`
Request
_c
ount
int
`json:"request_count" xorm:"request_count"`
Service
I
d
string
`json:"service_id" xorm:"service_id"`
User
I
d
int
`json:"user_id" xorm:"user_id"`
Request
C
ount
int
`json:"request_count" xorm:"request_count"`
ResFields
string
`json:"res_fields" xorm:"res_fields text"`
}
...
...
src/dao/service_request_record.go
View file @
0aa8b3c0
package
dao
import
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/tools"
import
(
"time"
)
type
Service_request_record
struct
{
Id
int
`json:"id" xorm:"id"`
Service_id
int
`json:"service_id" xorm:"service_id"`
Apply_id
int
`json:"apply_id" xorm:"apply_id"`
User_id
string
`json:"user_id" xorm:"user_id"`
Request_time
tools
.
JsonTime
`json:"request_time" xorm:"request_time created"`
Date_day
int
`json:"date_day" xorm:"date_day"`
Date_month
int
`json:"date_month" xorm:"date_month"`
Date_year
int
`json:"date_year" xorm:"date_year"`
Status
int
`json:"status" xorm:"status"`
Error
string
`json:"error" xorm:"error"`
type
ServiceRequestRecord
struct
{
Id
int64
`json:"id" xorm:"pk default nextval('service_request_record_id_pk'::regclass) autoincr BIGINT"`
ServiceId
int64
`json:"service_id" xorm:"BIGINT"`
ApplyId
int64
`json:"apply_id" xorm:"BIGINT"`
UserId
string
`json:"user_id" xorm:"VARCHAR(50)"`
RequestTime
time
.
Time
`json:"request_time" xorm:"DATETIME"`
DateDay
int
`json:"date_day" xorm:"SMALLINT"`
DateMonth
int
`json:"date_month" xorm:"SMALLINT"`
DateYear
int
`json:"date_year" xorm:"SMALLINT"`
Status
int
`json:"status" xorm:"SMALLINT"`
Error
string
`json:"error" xorm:"TEXT"`
ReqUrl
string
`json:"req_url" xorm:"VARCHAR"`
ApiId
int64
`json:"api_id" xorm:"BIGINT"`
ApplyOid
string
`json:"apply_oid" xorm:"VARCHAR"`
ServiceOid
string
`json:"service_oid" xorm:"VARCHAR"`
}
func
(
s
Service
_request_r
ecord
)
TableName
()
string
{
func
(
s
Service
RequestR
ecord
)
TableName
()
string
{
return
"service_request_record"
}
src/handler/proxyhandler.go
View file @
0aa8b3c0
...
...
@@ -14,7 +14,9 @@ import (
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/service"
"gitlab.wodcloud.com/apaas/apaas-meshproxy/src/tools"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
...
...
@@ -32,58 +34,109 @@ func Proxy(c *gin.Context) {
c
.
JSON
(
200
,
res
)
return
}
apiId
:=
c
.
Param
(
"apiid"
)
//获取服务相关信息
proxyData
,
err
:=
service
.
GetRealPath
(
applyId
)
proxyData
,
err
:=
service
.
GetRealPath
(
applyId
,
apiId
)
if
err
!=
nil
{
fmt
.
Println
(
"err......"
,
err
.
Error
())
c
.
Error
(
err
)
return
}
if
proxyData
.
Second_level
!=
1
{
//proxyData.ReqUrl = "https://apaas3.wodcloud.com/iam/login/#/login"
if
proxyData
.
SecondLevel
!=
1
{
res
:=
model
.
WebRes
{}
res
.
ErrMsg
=
`服务申请未通过审批,请联系组织管理员或超管审批`
c
.
JSON
(
200
,
res
)
return
}
roundTripper
:=
http
.
DefaultTransport
.
(
*
http
.
Transport
)
roundTripper
.
TLSClientConfig
=
&
tls
.
Config
{
InsecureSkipVerify
:
true
}
f
,
_
:=
forward
.
New
(
forward
.
PassHostHeader
(
true
),
forward
.
RoundTripper
(
roundTripper
),
forward
.
ResponseModifier
(
func
(
resp
*
http
.
Response
)
error
{
//Resp = resp
if
proxyData
.
ServiceEndTime
.
Before
(
appT
)
{
res
:=
model
.
WebRes
{}
res
.
ErrMsg
=
`申请的服务已过期`
c
.
JSON
(
200
,
res
)
return
}
//判断是否超过调用次数是否可以调用
callflag
,
sensitiveflag
,
err
:=
service
.
QueryCallsCount
(
proxyData
)
callflag
,
_
,
err
:=
service
.
QueryCallsCount
(
proxyData
)
if
err
!=
nil
{
fmt
.
Println
(
"err......"
,
err
.
Error
())
return
nil
fmt
.
Println
(
"err......"
,
err
.
Error
(),
"无法处理调用次数判断"
)
res
:=
model
.
WebRes
{}
res
.
ErrMsg
=
`无法处理您的请求`
c
.
JSON
(
200
,
res
)
return
}
if
callflag
==
false
{
err
=
errors
.
New
(
`调用达到当日限定次数`
)
Return
(
nil
,
err
,
resp
)
return
nil
if
callflag
{
res
:=
model
.
WebRes
{}
res
.
ErrMsg
=
`已达到最大调用量`
c
.
JSON
(
200
,
res
)
return
}
//如果是静态文件
if
CheckStaticFile
(
c
.
Request
.
URL
.
Path
)
{
host
:=
getHost
(
proxyData
.
ReqUrl
)
//var prefix string
//if apiId == "" {
// prefix= fmt.Sprintf("%s/%s/service/%s/%s/", config.Prefix, config.MeshId,applyId,apiId)
//}else {
// prefix= fmt.Sprintf("%s/%s/service/%s/", config.Prefix, config.MeshId,applyId)
//}
//
//staticPath:= strings.ReplaceAll(c.Request.URL.Path,prefix,"")
fwd
,
_
:=
forward
.
New
(
forward
.
PassHostHeader
(
false
))
c
.
Request
.
URL
=
testutils
.
ParseURI
(
fmt
.
Sprintf
(
"%s%s"
,
host
,
c
.
Request
.
URL
.
Path
))
c
.
Request
.
RequestURI
=
getRequestURI
(
c
.
Request
)
c
.
Request
.
Host
=
host
fwd
.
ServeHTTP
(
c
.
Writer
,
c
.
Request
)
}
else
{
roundTripper
:=
http
.
DefaultTransport
.
(
*
http
.
Transport
)
//roundTripper.ForceAttemptHTTP2 = false
roundTripper
.
TLSClientConfig
=
&
tls
.
Config
{
InsecureSkipVerify
:
true
}
forwarder
,
err
:=
forward
.
New
(
forward
.
PassHostHeader
(
false
),
forward
.
RoundTripper
(
roundTripper
),
forward
.
ResponseModifier
(
func
(
resp
*
http
.
Response
)
error
{
respbody
,
err1
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err1
!=
nil
{
log
.
Println
(
err1
)
return
errors
.
New
(
`数据读取失败`
)
}
bT
:=
time
.
Now
()
fmt
.
Println
(
`开始获取数据:`
,
bT
)
respbody
,
_
:=
ioutil
.
ReadAll
(
resp
.
Body
)
var
res
interface
{}
res
=
respbody
eT
:=
time
.
Since
(
bT
)
fmt
.
Println
(
"获取数据: "
,
eT
)
var
bytesNewBody
[]
byte
//是否需要过滤字段
if
proxyData
.
ResFields
!=
""
{
bT
:=
time
.
Now
()
fmt
.
Println
(
`过滤:`
,
bT
)
res
=
service
.
FiledFilter
(
proxyData
,
respbody
)
eT
:=
time
.
Since
(
bT
)
fmt
.
Println
(
"过滤: "
,
eT
)
if
proxyData
.
DataServiceType1
==
5
&&
proxyData
.
ResFields
!=
""
&&
proxyData
.
ResFields
!=
"[]"
{
//读取数据
bytesNewBody
=
service
.
FiledFilter
(
proxyData
,
respbody
)
}
else
{
bytesNewBody
=
respbody
}
//放回
resp
.
Body
=
ioutil
.
NopCloser
(
bytes
.
NewBuffer
(
bytesNewBody
))
go
func
()
{
defer
func
()
{
err
:=
recover
()
if
err
!=
nil
{
fmt
.
Println
(
err
)
}
//是否需要屏蔽敏感字段
if
sensitiveflag
==
false
{
res
=
service
.
SensitiveFilter
(
proxyData
,
res
)
}()
//调用次数统计
if
err
:=
service
.
CallCounts
(
proxyData
);
err
!=
nil
{
fmt
.
Println
(
err
)
}
if
resp
.
StatusCode
<
400
{
service
.
RecordCall
(
proxyData
,
resp
.
StatusCode
,
nil
)
}
else
{
service
.
RecordCall
(
proxyData
,
resp
.
StatusCode
,
nil
)
}
}()
//是否需要屏蔽敏感字段 仅对数据服务生效 其它服务部进行处理
//if sensitiveflag && proxyData.DataServiceType1 == 5 {
// res = service.SensitiveFilter(proxyData, res)
//}
//计次
//bgn := time.Now()
//fmt.Println(`开始携程:`, bgn)
go
service
.
CallCounts
(
proxyData
)
//
go service.CallCounts(proxyData)
//end := time.Since(bgn)
//fmt.Println("结束携程: ", end)
//err = service.CallCounts(proxyData)
...
...
@@ -98,40 +151,49 @@ func Proxy(c *gin.Context) {
// Return(nil, err, resp)
// return nil
//}
Return
(
res
,
nil
,
resp
)
//
Return(res, nil, resp)
//appeT := time.Since(appT)
//fmt.Println("结束: ", appeT)
return
nil
}))
c
.
Request
.
URL
=
testutils
.
ParseURI
(
getProxyURL
(
proxyData
,
c
.
Request
))
if
err
!=
nil
{
c
.
JSON
(
500
,
errors
.
New
(
"请求失败"
))
return
}
reqURL
:=
testutils
.
ParseURI
(
proxyData
.
ReqUrl
)
if
reqURL
.
RawQuery
==
""
{
reqURL
.
RawQuery
=
c
.
Request
.
URL
.
RawQuery
}
else
{
if
c
.
Request
.
URL
.
RawQuery
!=
""
{
reqURL
.
RawQuery
=
"&"
+
c
.
Request
.
URL
.
RawQuery
}
}
c
.
Request
.
URL
=
reqURL
fmt
.
Println
(
"c.Request.URL-------"
,
c
.
Request
.
URL
)
c
.
Request
.
RequestURI
=
getRequestURI
(
c
.
Request
)
c
.
Request
.
RequestURI
=
reqURL
.
RequestURI
(
)
fmt
.
Println
(
"c.Request-------"
,
c
.
Request
)
c
.
Request
.
Host
=
getHost
(
proxyData
.
RealUrl
)
fmt
.
Println
(
"proxyData.RealUrl-------"
,
proxyData
.
RealUrl
)
f
.
ServeHTTP
(
c
.
Writer
,
c
.
Request
)
c
.
Request
.
Host
=
getHost
(
proxyData
.
ReqUrl
)
fmt
.
Println
(
"proxyData.RealUrl-------"
,
proxyData
.
ReqUrl
)
forwarder
.
ServeHTTP
(
c
.
Writer
,
c
.
Request
)
}
}
func
getProxyURL
(
proxyData
model
.
ProxyData
,
req
*
http
.
Request
)
string
{
realPath
:=
proxyData
.
RealUrl
rawQuery
:=
req
.
URL
.
RawQuery
var
result
=
""
if
rawQuery
==
""
{
if
proxyData
.
Name
==
"时空服务"
{
result
=
realPath
+
"?f=json"
}
else
{
result
=
realPath
}
}
else
{
result
=
realPath
+
"?"
+
rawQuery
if
proxyData
.
Name
==
"时空服务"
&&
!
strings
.
Contains
(
rawQuery
,
"f=json"
)
{
result
=
result
+
"&f=json"
func
CheckStaticFile
(
path
string
)
bool
{
if
strings
.
HasSuffix
(
path
,
".css"
)
||
strings
.
HasSuffix
(
path
,
".js"
)
||
strings
.
HasSuffix
(
path
,
".woff2"
)
||
strings
.
HasSuffix
(
path
,
".jpg"
)
||
strings
.
HasSuffix
(
path
,
".svg"
)
||
strings
.
HasSuffix
(
path
,
".woff"
)
||
strings
.
HasSuffix
(
path
,
".html"
)
||
strings
.
HasSuffix
(
path
,
".ttf"
)
{
return
true
}
}
return
result
return
false
}
func
getRequestURI
(
req
*
http
.
Request
)
string
{
//requestURI, _ := url.ParseRequestURI(req.RequestURI)
//
//requestURI.RawQuery
path
:=
req
.
URL
.
Path
rawQuery
:=
req
.
URL
.
RawQuery
var
result
=
""
...
...
@@ -175,20 +237,19 @@ func gzipCompress(content *[]byte) []byte {
return
compressData
.
Bytes
()
}
// 获取域名
func
getHost
(
url
string
)
(
path
string
)
{
if
strings
.
Contains
(
url
,
"//"
)
{
path
=
strings
.
Split
(
url
,
"//"
)[
1
]
if
strings
.
Contains
(
path
,
"/"
)
{
path
=
strings
.
Split
(
path
,
"/"
)[
0
]
}
// 获取域名 protocol :// hostname[:port] / path / [;parameters][?query]#fragment
func
getHost
(
httpurl
string
)
(
path
string
)
{
parse
,
err
:=
url
.
Parse
(
httpurl
)
if
err
!=
nil
{
return
""
}
return
return
fmt
.
Sprintf
(
"%s://%s"
,
parse
.
Scheme
,
parse
.
Host
)
}
func
HealthCheck
(
c
*
gin
.
Context
)
{
res
:=
model
.
WebRes
{}
proxyData
,
err
:=
service
.
GetReqPath
()
apiId
:=
c
.
Param
(
"apiid"
)
proxyData
,
err
:=
service
.
GetReqPath
(
apiId
)
if
err
!=
nil
{
res
.
Data
=
err
.
Error
()
c
.
JSON
(
500
,
res
)
...
...
src/main.go
View file @
0aa8b3c0
...
...
@@ -18,11 +18,11 @@ import (
var
(
argPort
=
pflag
.
Int
(
"port"
,
8011
,
""
)
argPrefix
=
pflag
.
String
(
"prefix"
,
"/bgmesh/fiddler"
,
""
)
meshId
=
pflag
.
String
(
"meshId"
,
"
149
"
,
""
)
redisUrl
=
pflag
.
String
(
"redisUrl"
,
"redis://
redis.apaas-v3
:6379"
,
""
)
meshId
=
pflag
.
String
(
"meshId"
,
"
461
"
,
""
)
redisUrl
=
pflag
.
String
(
"redisUrl"
,
"redis://
apaas-redis
:6379"
,
""
)
redisTag
=
pflag
.
String
(
"redisTag"
,
"apaas-mesh-proxy"
,
""
)
confPath
=
pflag
.
String
(
"confPath"
,
"/app/config/proxy.json"
,
""
)
dbURL
=
pflag
.
String
(
"dbURL"
,
"host=
stolon-proxy.devops port=5432 user=postgres password=spaceIN511
dbname=apaas sslmode=disable"
,
""
)
dbURL
=
pflag
.
String
(
"dbURL"
,
"host=
apaas-postgis.apaas-v3 port=54321 user=postgres password=passwd123
dbname=apaas sslmode=disable"
,
""
)
dbDriverName
=
pflag
.
String
(
"dbDriverName"
,
"postgres"
,
""
)
)
...
...
@@ -33,6 +33,7 @@ func main() {
server
()
}
//打包
func
server
()
error
{
config
.
Prefix
=
*
argPrefix
handler
:=
router
.
Load
(
ginrus
.
Ginrus
(
logrus
.
StandardLogger
(),
time
.
RFC3339
,
true
))
...
...
src/model/response.go
View file @
0aa8b3c0
package
model
import
"time"
type
ProxyData
struct
{
RealUrl
string
`json:"req_url" xorm:"req_url"`
ResFields
string
`json:"res_fields" xorm:"res_fields text"`
Count
int
`json:"count" xorm:"count"`
Sensituve_word
string
`json:"sensituve_word" xorm:"sensituve_word"`
Sensitive_count
int
`json:"sensitive_count" xorm:"sensitive_count"`
Apply_id
int
`json:"apply_id" xorm:"apply_id"`
Service_id
int
`json:"service_id" xorm:"service_id"`
User_id
string
`json:"user_id" xorm:"user_id"`
Data_service_type1
int
`json:"data_service_type1" xorm:"data_service_type1"`
Name
string
`json:"name"`
Second_level
int
`json:"second_level"xorm:"second_level"`
ApplyId
int64
`json:"apply_id"`
ReqUrl
string
`json:"req_url"`
RequestCount
int64
`json:"request_count"`
Duration
int64
`json:"duration"`
DurationUnit
int64
`json:"duration_unit"`
SpcsType
int
`json:"spcs_type"`
SpcsCount
int64
`json:"spcs_count"`
ResFields
string
`json:"res_fields"`
DataServiceType1
int64
`json:"data_service_type1"`
DataServiceType2
int64
`json:"data_service_type2"`
DataServiceType3
int64
`json:"data_service_type3"`
SecondLevel
int64
`json:"second_level"`
ServiceEndTime
time
.
Time
`json:"service_end_time"`
ReqName
string
`json:"req_name"`
ServiceState
int64
`json:"service_state"`
PayStatus
int64
`json:"pay_status"`
ApplyUserId
string
`json:"apply_user_id"`
ServiceUserId
string
`json:"service_user_id"`
ServiceOid
string
`json:"service_oid"`
ApplyOid
string
`json:"apply_oid"`
SensituveWord
string
`json:"sensituve_word"`
SensitiveCount
int64
`json:"sensitive_count"`
ServiceId
int64
`json:"service_id"`
ApiId
int64
`json:"api_id"`
//接口ID
}
type
WebRes
struct
{
...
...
src/router/router.go
View file @
0aa8b3c0
...
...
@@ -32,8 +32,12 @@ func Load(middleware ...gin.HandlerFunc) http.Handler {
e
.
Use
(
middleware
...
)
root
:=
e
.
Group
(
fmt
.
Sprintf
(
"%s/%s"
,
config
.
Prefix
,
config
.
MeshId
))
{
root
.
GET
(
"/health"
,
handler
.
HealthCheck
)
root
.
Any
(
"/service/:applyId"
,
handler
.
Proxy
)
//root.GET("/health", handler.HealthCheck)
root
.
GET
(
"/health/:apiid/*any"
,
handler
.
HealthCheck
)
//root.Any("/service/:applyId", handler.Proxy)
root
.
Any
(
"/service/:applyId/:apiid"
,
handler
.
Proxy
)
root
.
Any
(
"/service/:applyId/:apiid/*any"
,
handler
.
Proxy
)
//root.GET("/static/*any", handler.StaticProxy)
}
return
e
}
src/service/field.go
View file @
0aa8b3c0
This diff is collapsed.
Click to expand it.
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