forked from sipt/shuttle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.go
225 lines (212 loc) · 4.55 KB
/
dump.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
package shuttle
import (
"os"
"fmt"
"io/ioutil"
"sync"
"bytes"
"github.com/sipt/shuttle/pool"
"bufio"
"net/http"
"compress/gzip"
"compress/zlib"
"io"
"encoding/base64"
)
var dump IDump
func init() {
if dump == nil {
dump = &FileDump{
Actives: make(map[int64]chan *fileDumpEntity),
}
}
err := dump.Clear()
if err != nil {
os.Exit(1)
}
}
func SetDump(d IDump) {
dump = d
}
func GetDump() IDump {
return dump
}
const (
DumpOrderWrite = iota
DumpOrderClose
DumpRequestEntity
DumpResponseEntity
)
type IDump interface {
InitDump(int64) error
WriteRequest(int64, []byte) (n int, err error)
WriteResponse(int64, []byte) (n int, err error)
Dump(int64) ([]byte, error)
Complete(int64) error
Clear() error
}
type FileDump struct {
sync.RWMutex
Actives map[int64]chan *fileDumpEntity
completeList []string
cancel chan bool
}
type fileDumpEntity struct {
data []byte
order int
entityType int
}
func (f *FileDump) InitDump(id int64) error {
reqBuf := bytes.NewBuffer(pool.GetBuf()[:0])
respBuf := bytes.NewBuffer(pool.GetBuf()[:0])
dataChan := make(chan *fileDumpEntity, 8)
f.Lock()
f.Actives[id] = dataChan
f.Unlock()
go func() {
var data *fileDumpEntity
for {
data = <-dataChan
switch data.order {
case DumpOrderWrite:
switch data.entityType {
case DumpRequestEntity:
reqBuf.Write(data.data)
case DumpResponseEntity:
respBuf.Write(data.data)
}
case DumpOrderClose:
file, err := os.OpenFile(fmt.Sprintf("./temp/%d_data.txt", id), os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
Logger.Errorf("[%d] create data file failed: %v", id, err)
return
}
file.WriteString(`{"req":"`)
file.WriteString(base64.StdEncoding.EncodeToString(reqBuf.Bytes()))
file.WriteString(`"`)
//解析返回值
b := bufio.NewReader(reqBuf)
req, err := http.ReadRequest(b)
if err != nil {
Logger.Errorf("[%d] parse http request failed: %v", id, err)
return
}
b = bufio.NewReader(respBuf)
resp, err := http.ReadResponse(b, req)
if err != nil {
Logger.Errorf("[%d] parse http response failed: %v", id, err)
return
}
var r io.Reader
if resp.Header.Get("Content-Encoding") == "gzip" {
r, err = gzip.NewReader(resp.Body)
if err != nil {
Logger.Errorf("[%d] gzip init for response failed: %v", id, err)
return
}
} else if resp.Header.Get("Content-Encoding") == "deflate" {
r, err = zlib.NewReader(resp.Body)
if err != nil {
Logger.Errorf("[%d] deflate init for response failed: %v", id, err)
return
}
} else {
r = resp.Body
}
file.WriteString(`,"resp_body":"`)
reqBuf.Reset()
reqBuf.ReadFrom(r)
file.WriteString(base64.StdEncoding.EncodeToString(reqBuf.Bytes()))
resp.Body.Close()
file.WriteString(`","resp_header":"`)
reqBuf.Reset()
resp.Write(reqBuf)
file.WriteString(base64.StdEncoding.EncodeToString(reqBuf.Bytes()))
file.WriteString(`"}`)
return
}
}
}()
return nil
}
func (f *FileDump) WriteRequest(id int64, data []byte) (n int, err error) {
f.RLock()
c, ok := f.Actives[id]
if ok {
c <- &fileDumpEntity{
data: data,
order: DumpOrderWrite,
entityType: DumpRequestEntity,
}
}
f.RUnlock()
return len(data), nil
}
func (f *FileDump) WriteResponse(id int64, data []byte) (n int, err error) {
f.RLock()
c, ok := f.Actives[id]
if ok {
c <- &fileDumpEntity{
data: data,
order: DumpOrderWrite,
entityType: DumpResponseEntity,
}
}
f.RUnlock()
return len(data), nil
}
func (f *FileDump) Dump(id int64) ([]byte, error) {
file := fmt.Sprintf("./temp/%d_data.txt", id)
_, err := os.Stat(file)
if os.IsNotExist(err) {
return []byte{}, nil
}
return ioutil.ReadFile(file)
}
func (f *FileDump) Complete(id int64) error {
f.RLock()
_, ok := f.Actives[id]
f.RUnlock()
if ok {
f.Lock()
c, ok := f.Actives[id]
if ok {
delete(f.Actives, id)
}
f.Unlock()
if ok {
c <- &fileDumpEntity{
order: DumpOrderClose,
}
}
}
return nil
}
func (f *FileDump) Clear() error {
f.Lock()
for k := range f.Actives {
c, ok := f.Actives[k]
if ok {
c <- &fileDumpEntity{
order: DumpOrderClose,
}
}
}
f.Actives = make(map[int64]chan *fileDumpEntity)
// Clear files
_, err := os.Stat("temp/")
if !os.IsNotExist(err) {
err := os.RemoveAll("temp")
if err != nil {
Logger.Errorf("delete dir error: %v", err)
return err
}
}
err = os.Mkdir("temp", os.ModePerm)
if err != nil {
Logger.Errorf("mkdir failed![%v]\n", err)
return err
}
f.Unlock()
return nil
}