-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddle_data.go
95 lines (86 loc) · 2.07 KB
/
middle_data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package rproxy
import (
"bytes"
"compress/flate"
"compress/gzip"
"io"
"net/http"
"strings"
"github.com/andybalholm/brotli"
"gopkg.in/elazarl/goproxy.v1"
)
// DataMiddle 数据中间代理接口
type DataMiddle interface {
// Name 中间件名称
Name() string
// Scope 指定代理范围
Scope(req *http.Request, ctx *goproxy.ProxyCtx) bool
// Handle 处理请求
Handle(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response
}
// MiniMiddle 实现微型中间件
type MiniDataMiddle struct {
Method string
Host string
Path string
HandleF func(res *http.Response, body []byte)
}
var _ DataMiddle = &MiniDataMiddle{}
// NewMiniMiddle 实例化微型中间件
func NewMiniDataMiddle(method, host, path string, handleF func(res *http.Response, body []byte)) *MiniDataMiddle {
return &MiniDataMiddle{
Method: method,
Host: host,
Path: path,
HandleF: handleF,
}
}
// Name 中间件名称
func (m *MiniDataMiddle) Name() string {
return "mini_" + m.Host + m.Path
}
// Scope 指定代理范围
func (m *MiniDataMiddle) Scope(req *http.Request, ctx *goproxy.ProxyCtx) bool {
if m.Method != "" {
if req.Method != strings.ToUpper(m.Method) {
return false
}
}
if m.Host != "" && req.Host != m.Host {
return false
}
if m.Path != "" && req.URL.Path != m.Path {
return false
}
return true
}
// Handle 处理请求
func (m *MiniDataMiddle) Handle(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
if m.HandleF != nil {
var (
reader io.Reader
oldBody, _ = io.ReadAll(resp.Body)
)
switch resp.Header.Get("Content-Encoding") {
case "gzip":
reader, _ = gzip.NewReader(bytes.NewReader(oldBody))
case "deflate":
reader = flate.NewReader(bytes.NewReader(oldBody))
case "br":
reader = brotli.NewReader(bytes.NewReader(oldBody))
default:
reader = bytes.NewReader(oldBody)
}
body, err := io.ReadAll(reader)
if err != nil {
return resp
}
m.HandleF(resp, body)
if v, ok := resp.Body.(io.ReadSeeker); ok {
v.Seek(0, 0)
} else {
resp.Body = io.NopCloser(bytes.NewReader(oldBody))
}
}
return resp
}