2017-11-08 00:10:54 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-11-15 15:37:54 +01:00
|
|
|
"compress/gzip"
|
2018-07-21 19:50:53 +02:00
|
|
|
"encoding/json"
|
2017-11-18 13:23:36 +01:00
|
|
|
"fmt"
|
2017-11-08 00:10:54 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
2018-08-04 01:53:54 +02:00
|
|
|
"net/url"
|
2018-07-21 19:50:53 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-11-17 10:49:18 +01:00
|
|
|
"sync"
|
2017-11-08 00:10:54 +01:00
|
|
|
|
2017-11-21 22:36:40 +01:00
|
|
|
"github.com/googollee/go-socket.io"
|
|
|
|
)
|
2017-11-15 15:37:54 +01:00
|
|
|
|
2018-07-21 19:50:53 +02:00
|
|
|
var captures Captures
|
|
|
|
|
2018-08-04 01:53:54 +02:00
|
|
|
var dashboardSocket socketio.Socket
|
2018-08-02 11:09:00 +02:00
|
|
|
|
2017-11-08 00:10:54 +01:00
|
|
|
func main() {
|
2018-09-16 16:21:36 +02:00
|
|
|
config := ReadConfig()
|
2018-11-11 17:54:35 +01:00
|
|
|
startCapture(config)
|
|
|
|
}
|
2017-11-08 00:10:54 +01:00
|
|
|
|
2018-11-11 17:54:35 +01:00
|
|
|
func startCapture(config Config) {
|
|
|
|
http.Handle("/", proxyHandler(config))
|
|
|
|
http.Handle("/socket.io/", dashboardSocketHandler(config))
|
|
|
|
http.Handle(config.DashboardPath, dashboardHandler())
|
|
|
|
http.Handle(config.DashboardClearPath, dashboardClearHandler())
|
|
|
|
http.Handle(config.DashboardItemInfoPath, dashboardItemInfoHandler())
|
2018-09-16 16:18:39 +02:00
|
|
|
|
2018-11-16 22:39:53 +01:00
|
|
|
captureHost := fmt.Sprintf("http://localhost:%s", config.ProxyPort)
|
2017-11-18 13:23:36 +01:00
|
|
|
|
2018-11-16 22:39:53 +01:00
|
|
|
fmt.Printf("\nListening on %s", captureHost)
|
|
|
|
fmt.Printf("\n %s/%s\n\n", captureHost, config.Dashboard)
|
2017-11-18 13:23:36 +01:00
|
|
|
|
2018-09-16 16:21:36 +02:00
|
|
|
fmt.Println(http.ListenAndServe(":"+config.ProxyPort, nil))
|
2017-11-08 00:10:54 +01:00
|
|
|
}
|
|
|
|
|
2018-11-11 17:54:35 +01:00
|
|
|
func dashboardSocketHandler(config Config) http.Handler {
|
2017-11-21 22:36:40 +01:00
|
|
|
server, err := socketio.NewServer(nil)
|
|
|
|
if err != nil {
|
2018-11-16 22:39:53 +01:00
|
|
|
fmt.Printf("socket server error: %v\n", err)
|
2017-11-21 22:36:40 +01:00
|
|
|
}
|
|
|
|
server.On("connection", func(so socketio.Socket) {
|
2018-08-04 01:53:54 +02:00
|
|
|
dashboardSocket = so
|
2018-09-16 16:21:36 +02:00
|
|
|
dashboardSocket.Emit("config", config)
|
2018-09-07 16:45:02 +02:00
|
|
|
emitToDashboard(captures)
|
2017-11-21 22:36:40 +01:00
|
|
|
})
|
|
|
|
server.On("error", func(so socketio.Socket, err error) {
|
2018-11-16 22:39:53 +01:00
|
|
|
fmt.Printf("socket error: %v\n", err)
|
2017-11-21 22:36:40 +01:00
|
|
|
})
|
|
|
|
return server
|
|
|
|
}
|
|
|
|
|
2018-11-11 17:54:35 +01:00
|
|
|
func dashboardClearHandler() http.Handler {
|
2018-11-16 22:39:53 +01:00
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2018-09-07 16:45:02 +02:00
|
|
|
captures = nil
|
|
|
|
emitToDashboard(captures)
|
2018-11-16 22:39:53 +01:00
|
|
|
rw.WriteHeader(http.StatusOK)
|
2018-09-07 16:45:02 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-11 17:54:35 +01:00
|
|
|
func dashboardHandler() http.Handler {
|
2018-11-16 22:39:53 +01:00
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.Header().Add("Content-Type", "text/html")
|
|
|
|
fmt.Fprint(rw, dashboardHTML)
|
2018-08-04 01:53:54 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-11 17:54:35 +01:00
|
|
|
func dashboardItemInfoHandler() http.Handler {
|
2018-11-16 22:39:53 +01:00
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2018-09-16 16:18:39 +02:00
|
|
|
idStr := req.URL.Path[strings.LastIndex(req.URL.Path, "/")+1:]
|
|
|
|
idInt, _ := strconv.Atoi(idStr)
|
|
|
|
for _, c := range captures {
|
|
|
|
if c.ID == idInt {
|
2018-11-16 22:39:53 +01:00
|
|
|
rw.Header().Add("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(rw).Encode(c)
|
2018-09-26 01:00:02 +02:00
|
|
|
break
|
2018-09-16 16:18:39 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-02 00:36:23 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-11 17:54:35 +01:00
|
|
|
func proxyHandler(config Config) http.Handler {
|
|
|
|
url, _ := url.Parse(config.TargetURL)
|
|
|
|
captureID := 0
|
2018-11-17 10:49:18 +01:00
|
|
|
mux := sync.Mutex{}
|
2018-11-11 17:54:35 +01:00
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
req.Host = url.Host
|
|
|
|
req.URL.Host = url.Host
|
|
|
|
req.URL.Scheme = url.Scheme
|
|
|
|
|
|
|
|
reqDump, err := dumpRequest(req)
|
|
|
|
if err != nil {
|
2018-11-16 22:39:53 +01:00
|
|
|
fmt.Printf("could not dump request: %v\n", err)
|
2018-11-11 17:54:35 +01:00
|
|
|
}
|
2017-11-08 00:10:54 +01:00
|
|
|
|
2018-11-11 17:54:35 +01:00
|
|
|
proxy := httputil.NewSingleHostReverseProxy(url)
|
|
|
|
proxy.ModifyResponse = func(res *http.Response) error {
|
|
|
|
resDump, err := dumpResponse(res)
|
|
|
|
if err != nil {
|
2018-11-16 22:39:53 +01:00
|
|
|
return fmt.Errorf("could not dump response: %v", err)
|
2018-11-11 17:54:35 +01:00
|
|
|
}
|
2018-11-17 10:49:18 +01:00
|
|
|
mux.Lock()
|
2018-11-11 17:54:35 +01:00
|
|
|
captureID++
|
|
|
|
capture := Capture{
|
|
|
|
ID: captureID,
|
|
|
|
Path: req.URL.Path,
|
|
|
|
Method: req.Method,
|
|
|
|
Status: res.StatusCode,
|
|
|
|
Request: string(reqDump),
|
|
|
|
Response: string(resDump),
|
|
|
|
}
|
|
|
|
captures.Add(capture)
|
|
|
|
captures.RemoveLastAfterReaching(config.MaxCaptures)
|
2018-11-17 10:49:18 +01:00
|
|
|
mux.Unlock()
|
2018-11-11 17:54:35 +01:00
|
|
|
emitToDashboard(captures)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
proxy.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) {
|
|
|
|
fmt.Printf("uh oh | %v | %s\n", err, req.URL)
|
|
|
|
}
|
|
|
|
proxy.ServeHTTP(rw, req)
|
|
|
|
})
|
2018-08-04 01:53:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func dumpRequest(req *http.Request) ([]byte, error) {
|
2018-11-11 17:54:35 +01:00
|
|
|
if req.Header.Get("Content-Encoding") == "gzip" {
|
|
|
|
var originalBody bytes.Buffer
|
|
|
|
tee := io.TeeReader(req.Body, &originalBody)
|
|
|
|
reader, _ := gzip.NewReader(tee)
|
|
|
|
req.Body = ioutil.NopCloser(reader)
|
|
|
|
reqDump, err := httputil.DumpRequest(req, true)
|
|
|
|
req.Body = ioutil.NopCloser(&originalBody)
|
|
|
|
return reqDump, err
|
|
|
|
}
|
2018-07-21 19:50:53 +02:00
|
|
|
return httputil.DumpRequest(req, true)
|
|
|
|
}
|
|
|
|
|
2018-08-04 01:53:54 +02:00
|
|
|
func dumpResponse(res *http.Response) ([]byte, error) {
|
2017-11-15 15:37:54 +01:00
|
|
|
if res.Header.Get("Content-Encoding") == "gzip" {
|
2018-11-11 17:54:35 +01:00
|
|
|
var originalBody bytes.Buffer
|
|
|
|
tee := io.TeeReader(res.Body, &originalBody)
|
|
|
|
reader, _ := gzip.NewReader(tee)
|
|
|
|
res.Body = ioutil.NopCloser(reader)
|
|
|
|
resDump, err := httputil.DumpResponse(res, true)
|
|
|
|
res.Body = ioutil.NopCloser(&originalBody)
|
|
|
|
return resDump, err
|
2017-11-15 15:37:54 +01:00
|
|
|
}
|
2018-11-11 17:54:35 +01:00
|
|
|
return httputil.DumpResponse(res, true)
|
2017-11-15 15:37:54 +01:00
|
|
|
}
|
2018-09-07 16:45:02 +02:00
|
|
|
|
|
|
|
func emitToDashboard(captures Captures) {
|
|
|
|
if dashboardSocket != nil {
|
|
|
|
dashboardSocket.Emit("captures", captures.MetadataOnly())
|
|
|
|
}
|
|
|
|
}
|