forked from hidu/pproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
req_rewrite.go
246 lines (213 loc) · 5.91 KB
/
req_rewrite.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package serve
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
func (ser *ProxyServe) reqRewriteByjs(req *http.Request, reqCtx *requestCtx) int {
if !ser.reqMod.CanMod() {
return 304
}
schema := req.URL.Scheme
origin_url := req.URL.String()
origin_get_query := req.URL.Query()
///================================================================
headerKv := make(map[string]string)
headerKv["method"] = req.Method
headerKv["schema"] = schema
headerKv["path"] = req.URL.Path
_host, port_int, _ := getHostPortFromReq(req)
headerKv["host"] = _host
headerKv["port"] = fmt.Sprintf("%d", port_int)
username := ""
psw := ""
if req.URL.User != nil {
username = req.URL.User.Username()
psw, _ = req.URL.User.Password()
}
headerKv["username"] = username
headerKv["proxy_user"] = reqCtx.User.Name
headerKv["password"] = psw
//===================================================================
rewriteData := make(map[string]interface{})
rewriteData["header"] = headerKv
rewriteData["get"] = origin_get_query
rewriteData["post"] = *reqCtx.FormPost
reqObjNew, rErr := ser.reqMod.rewrite(rewriteData)
if rErr != nil {
log.Println("rewrite failed:", rErr)
}
headerKvNew := make(map[string]string)
isHeaderChange := false
for k, v := range headerKv {
_newVal := getMapValStr(reqObjNew, k)
headerKvNew[k] = _newVal
if _newVal != v {
isHeaderChange = true
}
}
//-------------------------------------------------------
var get_new url.Values
isGetChange := false
if _get, has := reqObjNew["get"]; has {
get_new = _req_mapToUrlValue(_get)
isGetChange = checkUrlValuesChange(origin_get_query, get_new)
}
//-------------------------------------------------------
var post_new url.Values
isPostChange := false
if schema == "http" {
if _post, has := reqObjNew["post"]; has {
post_new = _req_mapToUrlValue(_post)
isPostChange = checkUrlValuesChange(*reqCtx.FormPost, post_new)
}
}
host_addr := getMapValStr(reqObjNew, "host_addr")
isHostAddrChange := host_addr != ""
if ser.Debug {
fmt.Println("rewriteChange:", "is_get_change:", isGetChange, "new_get:", get_new,
"isPostChange:", isPostChange, "new_post:", post_new,
"isHostAddrChange:", isHostAddrChange, "new_host_addr:", host_addr,
)
}
///===============================================================================
if !isHeaderChange && !isGetChange && !isPostChange && !isHostAddrChange {
return 304
}
///===============================================================================
var url_base string
if isHeaderChange {
// schema := headerKvNew["schema"]
url_base = schema + "://"
if headerKvNew["username"] != "" {
url_base += fmt.Sprintf("%s:%s@", headerKvNew["username"], headerKvNew["password"])
}
url_base += headerKvNew["host"]
if headerKvNew["port"] != "" && headerKvNew["port"] != "80" {
url_base += ":" + headerKvNew["port"]
}
url_base += headerKvNew["path"]
} else {
if req.URL.RawQuery == "" {
url_base = origin_url
} else {
url_base = origin_url[:len(origin_url)-len(req.URL.RawQuery)-1]
}
}
if isGetChange {
url_base += "?" + get_new.Encode()
} else {
url_base += "?" + req.URL.RawQuery
}
if isHeaderChange || isGetChange {
var url_err error
req.URL, url_err = url.Parse(url_base)
if ser.Debug {
log.Println("DEBUG req_rewrite,url_new:", url_base, "req_new:", req.URL)
}
if url_err != nil {
return 502
}
req.Host = req.URL.Host
}
//////////////////////////////////////////////////////////////////////////////
if isPostChange {
buf := bytes.NewBuffer([]byte{})
_post_body := post_new.Encode()
req.Header.Del("Content-Length")
if req.Header.Get(Content_Encoding) == "gzip" {
tmp := gzipEncode([]byte(_post_body)).Bytes()
buf.Write(tmp)
} else {
buf.WriteString(_post_body)
}
req.ContentLength = int64(buf.Len())
req.Body = ioutil.NopCloser(buf).(io.ReadCloser)
}
////////////////////////////////////////////////////////////////////////////
if isHostAddrChange {
req.URL.Host = host_addr
if ser.Debug {
log.Println("rewrite host addr:", req.URL.Host, "==>", host_addr)
}
}
return 200
}
func (ser *ProxyServe) reqRewrite(req *http.Request, reqCtx *requestCtx) int {
origin_host := req.Host + "#" + req.URL.Host
statusCode1 := ser.reqRewriteByjs(req, reqCtx)
new_host := req.Host + "#" + req.URL.Host
if ser.Debug {
log.Println("rewrte_debug:\n", "origin_host:", origin_host, "\nnew_host:", new_host, "\n")
}
statusCode2 := 304
if origin_host == new_host {
statusCode2 = ser.reqRewriteByHosts(req)
}
if statusCode1 == 200 || statusCode2 == 200 {
return 200
}
if statusCode1 >= 500 || statusCode2 >= 500 {
return 502
}
return 304
}
func (ser *ProxyServe) reqRewriteByHosts(req *http.Request) int {
if ser.hosts == nil {
return 304
}
if host, has := ser.hosts[req.URL.Host]; has {
log.Println("rewrite host:", req.URL.Host, "==>", host)
req.URL.Host = host
return 200
}
host_info := strings.Split(req.URL.Host, ":")
if len(host_info) == 1 {
if req.URL.Scheme == "http" {
host_info = append(host_info, "80")
}
}
req_host := strings.Join(host_info, ":")
if host, has := ser.hosts[req_host]; has {
log.Println("rewrite host:", req.Host, "==>", host)
req.URL.Host = host
return 200
}
if host, has := ser.hosts[host_info[0]]; has {
log.Println("rewrite host:", req.Host, "==>", host)
req.URL.Host = host
if !strings.Contains(host, ":") {
req.URL.Host += ":" + host_info[1]
}
return 200
}
return 304
}
/**
*
*/
func _req_mapToUrlValue(values interface{}) url.Values {
uValues := make(url.Values)
if values == nil {
return uValues
}
vs := values.(map[string]interface{})
for k, arr := range vs {
switch value := arr.(type) {
case []interface{}:
for _, v := range value {
uValues.Add(k, fmt.Sprintf("%v", v))
}
case interface{}:
uValues.Set(k, fmt.Sprintf("%v", value))
default:
log.Println("unkonw type:", value)
}
}
return uValues
}