This repository has been archived by the owner on Oct 22, 2019. It is now read-only.
/
stream.go
140 lines (120 loc) · 2.76 KB
/
stream.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
package httap
import (
"bufio"
"bytes"
"io"
"io/ioutil"
"math/rand"
"net"
"net/http"
"os"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/tcpassembly/tcpreader"
)
type Stream struct {
tcpreader.ReaderStream
tap *Wiretap
flow gopacket.Flow
}
func NewStream(tap *Wiretap, netFlow, tcpFlow gopacket.Flow) *Stream {
return &Stream{
ReaderStream: tcpreader.NewReaderStream(),
tap: tap,
flow: netFlow,
}
}
func (st *Stream) Consume() {
buf := bufio.NewReader(st)
for {
req, err := http.ReadRequest(buf)
if err == io.EOF {
return
} else if err != nil {
st.tap.Log("Error: %s", err)
} else {
st.forward(req)
}
}
}
func (st *Stream) forward(req *http.Request) {
req.URL.Scheme = "http"
req.URL.Host = req.Host
if len(st.tap.Methods) > 0 && !st.tap.Methods[req.Method] {
return
}
body := new(bytes.Buffer)
if _, err := io.Copy(body, req.Body); err != nil {
st.tap.Log("Error: %s", err)
}
st.replaceHeaders(req)
for _, dst := range st.tap.Destinations {
n := st.forwardCount()
for i := 0; i < n; i++ {
copy := st.copy(req, body, dst)
repeat := i > 0
time.AfterFunc(time.Duration(i)*st.tap.RepeatDelay, func() {
st.send(copy, req.URL.String(), repeat)
})
}
}
}
func (st *Stream) replaceHeaders(req *http.Request) {
for key, value := range st.tap.Headers {
if value == "" {
req.Header.Del(key)
if key == "user-agent" {
/* "Use the defaultUserAgent unless the Header contains one,
which may be blank to not send the header." */
req.Header.Set(key, "")
}
} else {
req.Header.Set(key, value)
if key == "host" {
req.Host = value
}
}
}
}
func (st *Stream) send(req *http.Request, url string, repeat bool) {
res, err := st.tap.Transport.RoundTrip(req)
if err != nil {
st.tap.Log("Error: %s", err)
} else {
/* "The client must close the response body when finished with it." */
defer res.Body.Close()
var fmt string
if repeat {
fmt = "%s %s %s (%s REPEAT) %d"
} else {
fmt = "%s %s %s (%s) %d"
}
st.tap.Log(fmt, st.flow.Src().String(), req.Method, url, req.URL.Host, res.StatusCode)
if st.tap.Verbose {
req.Body = nil
req.Write(os.Stdout)
}
}
}
func (st *Stream) copy(req *http.Request, body *bytes.Buffer, dst *net.TCPAddr) *http.Request {
host := *dst
/* If the destination IP is unset, use the original destination IP. */
if host.IP == nil {
host.IP = net.ParseIP(st.flow.Dst().String())
}
url := *req.URL
copy := *req
copy.URL = &url
copy.URL.Host = host.String()
copy.Body = ioutil.NopCloser(bytes.NewReader(body.Bytes()))
return ©
}
func (st *Stream) forwardCount() int {
min := int(st.tap.Multiply)
prb := st.tap.Multiply - float32(min)
if rand.Float32() < prb {
return min + 1
} else {
return min
}
}