forked from hidu/api-front
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_handler.go
338 lines (290 loc) · 9.14 KB
/
server_handler.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package proxy
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strings"
"sync"
"time"
)
func (apiServer *APIServer) newHandler(api *apiStruct) func(http.ResponseWriter, *http.Request) {
bindPath := api.Path
log.Println(apiServer.ServerConf.Port, api.Name, "bind path [", bindPath, "]")
return func(rw http.ResponseWriter, req *http.Request) {
id := api.pvInc()
uniqID := apiServer.uniqReqID(id)
broadData := apiServer.initBroadCastData(req)
broadData.ID = uniqID
start := time.Now()
defer func() {
used := float64(time.Now().Sub(start).Nanoseconds()) / 1e6
broadData.setData("used", used)
go apiServer.broadcastAPIReq(api, broadData)
}()
rw.Header().Set("Api-Proxy-Version", APIProxyVersion)
log.Println("[access]", req.URL.String())
relPath := req.URL.Path[len(bindPath):]
req.Header.Set("Connection", "close")
logData := make(map[string]interface{})
var logRw sync.RWMutex
body, err := ioutil.ReadAll(req.Body)
logData["body_len"] = len(body)
if err != nil {
rw.WriteHeader(http.StatusBadGateway)
rw.Write([]byte("read body failed"))
broadData.setError(err.Error())
return
}
//get body must by before parse callerPref
hosts, masterHost, cpf := api.getAPIHostsByReq(req)
broadData.setData("master", masterHost)
broadData.setData("remote", cpf.GetIP())
_uri := req.URL.Path
if req.URL.RawQuery != "" {
_uri += "?" + req.URL.RawQuery
}
mainLogStr := fmt.Sprintf("uniqid=%s port=%d remote=%s method=%s uri=%s master=%s hostsTotal=%d refer=%s", uniqID, apiServer.ServerConf.Port, req.RemoteAddr, req.Method, _uri, masterHost, len(hosts), req.Referer())
var printLog = func(logIndex int) {
logRw.RLock()
defer logRw.RUnlock()
totalUsed := fmt.Sprintf("%.3fms", float64(time.Now().Sub(start).Nanoseconds())/1e6)
log.Println(fmt.Sprintf("[access]logindex=%d/%d", logIndex, len(hosts)), mainLogStr, fmt.Sprintf("totalUsed=%s", totalUsed), logData)
}
defer (func() {
printLog(1)
})()
rw.Header().Set("Api-Proxy-Master", masterHost)
addrInfo := strings.Split(req.RemoteAddr, ":")
if len(hosts) == 0 {
logData["hostTotal"] = 0
rw.WriteHeader(http.StatusBadGateway)
rw.Write([]byte("no backend hosts"))
broadData.setError("no backend hosts")
return
}
bodyLen := int64(len(body))
var reqs []*apiHostRequest
//build request
for _, apiHost := range hosts {
isMaster := apiHost.Name == masterHost
urlNew := ""
serverURL := apiHost.URLStr
if api.HostAsProxy {
serverURL = "http://" + req.Host + api.Path
}
if strings.HasSuffix(urlNew, "/") {
urlNew += strings.TrimLeft(relPath, "/")
} else {
urlNew += relPath
}
if req.URL.RawQuery != "" {
urlNew += "?" + req.URL.RawQuery
}
rawURL := apiHost.URLStr + urlNew
if isMaster {
rw.Header().Set("Api-Proxy-Raw-Url", rawURL)
}
urlNew = serverURL + urlNew
broadData.setData("raw_url", rawURL)
reqNew, err := http.NewRequest(req.Method, urlNew, ioutil.NopCloser(bytes.NewReader(body)))
if err != nil {
log.Println("[error]build req failed:", err)
if isMaster {
rw.WriteHeader(http.StatusBadGateway)
rw.Write([]byte("error:" + err.Error() + "\nraw_url:" + rawURL))
}
broadData.setError(err.Error())
return
}
copyHeaders(reqNew.Header, req.Header)
//only accept gzip encode
acceptEncoding := reqNew.Header.Get("Accept-Encoding")
if acceptEncoding != "" && (InStringSlice("gzip", reqNew.Header["Accept-Encoding"]) || strings.Contains(acceptEncoding, "gzip")) {
reqNew.Header.Set("Accept-Encoding", "gzip")
}
if bodyLen > 0 {
reqNew.ContentLength = bodyLen
reqNew.Header.Set("Content-Length", fmt.Sprintf("%d", bodyLen))
}
reqNew.Header.Set("HTTP_X_FORWARDED_FOR", addrInfo[0])
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: time.Duration(api.TimeoutMs) * time.Millisecond,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
}
if api.HostAsProxy {
transport.Proxy = (func(u string) func(*http.Request) (*url.URL, error) {
return func(req *http.Request) (*url.URL, error) {
return url.Parse(u)
}
})(apiHost.URLStr)
}
apiReq := &apiHostRequest{
req: reqNew,
transport: transport,
apiHost: apiHost,
isMaster: isMaster,
urlNew: urlNew,
urlRaw: rawURL,
}
reqs = append(reqs, apiReq)
}
//call master at first sync
for index, apiReq := range reqs {
if !apiReq.isMaster {
continue
}
backLog := make(map[string]interface{})
defer (func() {
logRw.Lock()
logData[fmt.Sprintf("host_%s_%d", apiReq.apiHost.Name, index)] = backLog
logRw.Unlock()
})()
hostStart := time.Now()
backLog["isMaster"] = apiReq.isMaster
backLog["start"] = fmt.Sprintf("%.4f", float64(hostStart.UnixNano())/1e9)
resp, err := apiReq.transport.RoundTrip(apiReq.req)
if err != nil {
log.Println("[error]call_master_sync "+apiReq.urlNew, err)
rw.WriteHeader(http.StatusBadGateway)
rw.Write([]byte("fetch_error:" + err.Error() + "\nraw_url:" + apiReq.urlRaw + "\nnew_url:" + apiReq.urlNew))
return
}
defer resp.Body.Close()
apiServer.addBroadCastDataResponse(broadData, resp)
for k, vs := range resp.Header {
for _, v := range vs {
rw.Header().Add(k, v)
}
}
rw.Header().Set("Connection", "close")
rw.WriteHeader(resp.StatusCode)
backLog["status"] = resp.StatusCode
n, err := io.Copy(rw, resp.Body)
if err != nil {
log.Println(apiReq.urlNew, "io.copy:", n, err)
}
hostEnd := time.Now()
used := hostEnd.Sub(hostStart)
backLog["end"] = fmt.Sprintf("%.4f", float64(hostEnd.UnixNano())/1e9)
backLog["used"] = fmt.Sprintf("%.3fms", float64(used.Nanoseconds())/1e6)
}
if len(reqs) > 1 {
//call other hosts async
go (func(reqs []*apiHostRequest) {
defer (func() {
printLog(len(reqs))
})()
var wgOther sync.WaitGroup
for index, apiReq := range reqs {
if apiReq.isMaster {
continue
}
wgOther.Add(1)
go (func(index int, apiReq *apiHostRequest) {
backLog := make(map[string]interface{})
defer (func() {
logRw.Lock()
logData[fmt.Sprintf("host_%s_%d", apiReq.apiHost.Name, index)] = backLog
logRw.Unlock()
wgOther.Done()
})()
hostStart := time.Now()
backLog["isMaster"] = apiReq.isMaster
backLog["start"] = fmt.Sprintf("%.4f", float64(hostStart.UnixNano())/1e9)
backLog["isMaster"] = apiReq.isMaster
resp, err := apiReq.transport.RoundTrip(apiReq.req)
if err != nil {
log.Println("[error]call_other_async,fetch "+apiReq.urlNew, err)
return
}
backLog["status"] = resp.StatusCode
defer resp.Body.Close()
hostEnd := time.Now()
used := hostEnd.Sub(hostStart)
backLog["end"] = fmt.Sprintf("%.4f", float64(hostEnd.UnixNano())/1e9)
backLog["used"] = fmt.Sprintf("%.3fms", float64(used.Nanoseconds())/1e6)
})(index, apiReq)
}
wgOther.Wait()
})(reqs)
}
}
}
type apiHostRequest struct {
req *http.Request
urlRaw string
urlNew string
transport *http.Transport
apiHost *Host
isMaster bool
}
var reqCookieDumpLine = regexp.MustCompile(`Cookie: .+\r\n`)
func (apiServer *APIServer) initBroadCastData(req *http.Request) *BroadCastData {
data := newReqBroadCastData(req)
dumpBody := IsRequestDumpBody(req)
dump, _ := httputil.DumpRequest(req, dumpBody)
reqDetail := string(dump)
if apiServer.ServerConf.HiddenCookie {
reqDetail = reqCookieDumpLine.ReplaceAllStringFunc(reqDetail, ReqCookieHidden)
}
data.setData("req_detail", base64.StdEncoding.EncodeToString([]byte(reqDetail)))
return data
}
var resCookieDumpLine = regexp.MustCompile(`Set-Cookie: .+\r\n`)
func (apiServer *APIServer) addBroadCastDataResponse(broadData *BroadCastData, resp *http.Response) {
dumpBody := true
if resp.StatusCode == http.StatusOK {
dumpBody = IsContentTypeText(resp.Header.Get("Content-Type"))
if dumpBody && resp.ContentLength > 0 && resp.ContentLength > 1e6 {
dumpBody = false
}
}
switch resp.StatusCode {
case http.StatusBadRequest:
case http.StatusNotFound:
case http.StatusSeeOther:
case http.StatusForbidden:
case http.StatusGone:
case http.StatusBadGateway:
case http.StatusInternalServerError:
case http.StatusServiceUnavailable:
case http.StatusGatewayTimeout:
dumpBody = true
break
default:
break
}
broadData.setData("resp_status", resp.StatusCode)
dump, _ := httputil.DumpResponse(resp, false)
resDetail := string(dump)
if apiServer.ServerConf.HiddenCookie {
resDetail = resCookieDumpLine.ReplaceAllStringFunc(resDetail, ResCookieSetHidden)
}
if !dumpBody {
resDetail += "---body skipped---"
} else {
bd := forgetRead(&resp.Body)
if resp.Header.Get("Content-Encoding") == "gzip" {
resDetail += gzipDocode(bd)
} else {
resDetail += bd.String()
}
}
// fmt.Println(res_detail)
broadData.setData("res_detail", base64.StdEncoding.EncodeToString([]byte(resDetail)))
}
func (apiServer *APIServer) broadcastAPIReq(api *apiStruct, data *BroadCastData) {
apiServer.web.broadcastAPI(api, "req", data)
}