/
genproxy.go
172 lines (140 loc) · 4.26 KB
/
genproxy.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
package main
import (
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/negroni"
"github.com/elazarl/goproxy"
"github.com/meatballhat/negroni-logrus"
"bufio"
"flag"
"fmt"
"net"
"net/http"
"os"
"regexp"
)
const DefaultPort = ":8500"
// orPanic - wrapper for logging errors
func orPanic(err error) {
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Panic("Got error.")
}
}
func main() {
// getting proxy configuration
verbose := flag.Bool("v", false, "should every proxy request be logged to stdout")
record := flag.Bool("record", false, "should proxy record")
destination := flag.String("destination", ".", "destination URI to catch")
flag.Parse()
// getting settings
initSettings()
// overriding default settings
AppConfig.recordState = *record
// overriding destination
AppConfig.destination = *destination
// getting default database
port := os.Getenv("ProxyPort")
if port == "" {
port = DefaultPort
} else {
port = fmt.Sprintf(":%s", port)
}
// Output to stderr instead of stdout, could also be a file.
log.SetOutput(os.Stderr)
log.SetFormatter(&log.TextFormatter{})
redisPool := getRedisPool()
defer redisPool.Close()
cache := Cache{pool: redisPool,
prefix: AppConfig.cachePrefix}
// getting connections
d := DBClient{
cache: cache,
http: &http.Client{},
}
// creating proxy
proxy := goproxy.NewProxyHttpServer()
proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("^.*$"))).
HandleConnect(goproxy.AlwaysMitm)
// enable curl -p for all hosts on port 80
proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("^.*:80$"))).
HijackConnect(func(req *http.Request, client net.Conn, ctx *goproxy.ProxyCtx) {
defer func() {
if e := recover(); e != nil {
ctx.Logf("error connecting to remote: %v", e)
client.Write([]byte("HTTP/1.1 500 Cannot reach destination\r\n\r\n"))
}
client.Close()
}()
clientBuf := bufio.NewReadWriter(bufio.NewReader(client), bufio.NewWriter(client))
remote, err := net.Dial("tcp", req.URL.Host)
orPanic(err)
remoteBuf := bufio.NewReadWriter(bufio.NewReader(remote), bufio.NewWriter(remote))
for {
req, err := http.ReadRequest(clientBuf.Reader)
orPanic(err)
orPanic(req.Write(remoteBuf))
orPanic(remoteBuf.Flush())
resp, err := http.ReadResponse(remoteBuf.Reader, req)
orPanic(err)
orPanic(resp.Write(clientBuf.Writer))
orPanic(clientBuf.Flush())
}
})
// just helper handler to know where request hits proxy or no
proxy.OnRequest().DoFunc(
func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
log.WithFields(log.Fields{
"destination": r.URL.Host,
}).Info("Got request")
return r, nil
})
// hijacking plain connections
proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile(*destination))).DoFunc(
func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
log.Info("connection found......")
log.Info(fmt.Sprintf("Url path: %s", r.URL.Path))
return d.processRequest(r)
})
go d.startAdminInterface()
proxy.Verbose = *verbose
// proxy starting message
log.WithFields(log.Fields{
"RedisAddress": AppConfig.redisAddress,
"Destination": *destination,
"ProxyPort": port,
}).Info("Proxy is starting...")
log.Warn(http.ListenAndServe(port, proxy))
}
// processRequest - processes incoming requests and based on proxy state (record/playback)
// returns HTTP response.
func (d *DBClient) processRequest(req *http.Request) (*http.Request, *http.Response) {
if AppConfig.recordState {
log.Info("*** RECORD ***")
newResponse, err := d.recordRequest(req)
if err != nil {
// something bad happened, passing through
return req, nil
} else {
// discarding original requests and returns supplied response
return req, newResponse
}
} else {
log.Info("*** PLAYBACK ***")
newResponse := d.getResponse(req)
return req, newResponse
}
}
func (d *DBClient) startAdminInterface() {
// starting admin interface
mux := getBoneRouter(*d)
n := negroni.Classic()
n.Use(negronilogrus.NewMiddleware())
n.UseHandler(mux)
// admin interface starting message
log.WithFields(log.Fields{
"RedisAddress": AppConfig.redisAddress,
"AdminPort": AppConfig.adminInterface,
}).Info("Admin interface is starting...")
n.Run(AppConfig.adminInterface)
}