-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgorc.go
More file actions
215 lines (185 loc) · 4.78 KB
/
Copy pathgorc.go
File metadata and controls
215 lines (185 loc) · 4.78 KB
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
package main
import (
"bufio"
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
"syscall"
"time"
)
const (
RequestPayload = '1'
ResponsePayload = '2'
ReplayedResponsePayload = '3'
)
type Request struct {
StartAt time.Duration `json:"time"`
Latency time.Duration `json:"latency"`
Header http.Header `json:"header"`
Method string `json:"method"`
URI string `json:"uri"`
Proto string `json:"proto"`
Body string `json:"body"`
}
type Response struct {
StartAt time.Duration `json:"time"`
Latency time.Duration `json:"latency"`
Header http.Header `json:"header"`
Status string `json:"status"`
StatusCode int `json:"status_code"`
Proto string `json:"proto"`
Body string `json:"body"`
}
type Capture struct {
ReqID string `json:"req_id"`
Latency time.Duration `json:"latency"`
Request Request `json:"request"`
OriginalResponse Response `json:"original_response"`
ReplayedResponse Response `json:"replayed_response"`
}
type Script struct {
Stdin io.Writer
Stdout io.Reader
}
var reqs = map[string]*Capture{}
var spt *Script
func main() {
// exec script command
execScript()
// scan stdin
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
encoded := scanner.Bytes()
buf := make([]byte, len(encoded)/2)
_, _ = hex.Decode(buf, encoded)
process(buf)
}
}
func process(buf []byte) {
// First byte indicate payload type, possible values:
// 1 - Request
// 2 - Response
// 3 - ReplayedResponse
payloadType := buf[0]
headerSize := bytes.IndexByte(buf, '\n') + 1
header := buf[:headerSize-1]
// Header contains space separated values of: request type, request id, and request start time (or round-trip time for responses)
meta := bytes.Split(header, []byte(" "))
// For each request you should receive 3 payloads (request, response, replayed response) with same request id
reqID := string(meta[1])
payload := buf[headerSize:]
data, ok := reqs[reqID]
if !ok {
data = &Capture{
ReqID: reqID,
}
reqs[reqID] = data
}
pt, _ := time.ParseDuration(string(meta[2]) + "ns")
latency, _ := time.ParseDuration(string(meta[3]) + "ns")
Debug("Received payload:", string(buf))
switch payloadType {
case RequestPayload: // Request
req := parseReq(payload)
req.StartAt = pt
req.Latency = latency
data.Request = req
// Emitting data back
_, _ = os.Stdout.Write(encode(buf))
case ResponsePayload: // Original response
res := parseRes(payload)
res.StartAt = pt
res.Latency = latency
data.OriginalResponse = res
case ReplayedResponsePayload: // Replayed response
res := parseRes(payload)
res.StartAt = pt
res.Latency = latency
data.ReplayedResponse = res
data.Latency = data.ReplayedResponse.StartAt - data.Request.StartAt
// script callback
v, _ := json.Marshal(data)
v = append(v, '\n')
_, _ = spt.Stdin.Write(v)
// del data
delete(reqs, reqID)
}
}
func execScript() {
if len(os.Args) < 2 {
Log("[ERROR] missing callback script")
}
cmd := exec.CommandContext(context.Background(), os.Args[1], os.Args[2:]...)
cmd.Stderr = os.Stderr
spt = new(Script)
spt.Stdin, _ = cmd.StdinPipe()
spt.Stdout, _ = cmd.StdoutPipe()
go func() {
var err error
if err = cmd.Start(); err == nil {
err = cmd.Wait()
}
if err != nil {
if e, ok := err.(*exec.ExitError); ok {
status := e.Sys().(syscall.WaitStatus)
if status.Signal() == syscall.SIGKILL /*killed or context canceled */ {
return
}
}
Debug(fmt.Sprintf("[SCRIPT] command[%q] error: %q", strings.Join(os.Args[1:], " "), err.Error()))
}
}()
}
func encode(buf []byte) []byte {
dst := make([]byte, len(buf)*2+1)
hex.Encode(dst, buf)
dst[len(dst)-1] = '\n'
return dst
}
func Log(args ...interface{}) {
_, _ = fmt.Fprintf(os.Stderr, "%s ", time.Now().Format("2006-01-02 15:04:05.000000"))
_, _ = fmt.Fprintln(os.Stderr, args...)
}
func Debug(args ...interface{}) {
if os.Getenv("DEBUG") != "" {
Log(args...)
}
}
func parseReq(buf []byte) Request {
req, _ := http.ReadRequest(bufio.NewReader(bytes.NewReader(buf)))
var p []byte
if req.Body != nil {
defer func() { _ = req.Body.Close() }()
p, _ = ioutil.ReadAll(req.Body)
}
return Request{
Header: req.Header,
Method: req.Method,
URI: req.RequestURI,
Proto: req.Proto,
Body: string(p),
}
}
func parseRes(buf []byte) Response {
res, _ := http.ReadResponse(bufio.NewReader(bytes.NewReader(buf)), nil)
var p []byte
if res.Body != nil {
defer func() { _ = res.Body.Close() }()
p, _ = ioutil.ReadAll(res.Body)
}
return Response{
Header: res.Header,
Status: res.Status,
StatusCode: res.StatusCode,
Proto: res.Proto,
Body: string(p),
}
}