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-07-21 19:50:53 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
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-08-02 00:36:23 +02:00
|
|
|
type transport struct {
|
2017-11-15 15:37:54 +01:00
|
|
|
http.RoundTripper
|
|
|
|
}
|
|
|
|
|
2018-07-27 00:49:12 +02:00
|
|
|
var captureID = 0
|
2018-07-21 19:50:53 +02:00
|
|
|
var captures Captures
|
|
|
|
|
|
|
|
var socket socketio.Socket
|
|
|
|
|
|
|
|
var host string
|
|
|
|
var dashboardPath string
|
2018-08-02 00:36:23 +02:00
|
|
|
var dashboardItemPath string
|
2018-07-21 19:50:53 +02:00
|
|
|
|
2018-08-02 11:09:00 +02:00
|
|
|
var args Args
|
|
|
|
|
2017-11-08 00:10:54 +01:00
|
|
|
func main() {
|
2018-08-02 11:09:00 +02:00
|
|
|
args = args.Parse()
|
2017-11-08 00:10:54 +01:00
|
|
|
|
2018-08-02 11:09:00 +02:00
|
|
|
dashboardPath = fmt.Sprintf("/%s/", args.dashboard)
|
|
|
|
dashboardItemPath = fmt.Sprintf("/%s/items/", args.dashboard)
|
2018-07-21 19:50:53 +02:00
|
|
|
|
2018-08-02 00:36:23 +02:00
|
|
|
http.Handle("/", getProxyHandler())
|
2017-11-08 00:10:54 +01:00
|
|
|
http.Handle("/socket.io/", getSocketHandler())
|
2018-08-02 00:36:23 +02:00
|
|
|
http.Handle(dashboardItemPath, getDashboardItemHandler())
|
2018-07-21 19:50:53 +02:00
|
|
|
http.Handle(dashboardPath, getDashboardHandler())
|
|
|
|
|
2018-08-02 11:09:00 +02:00
|
|
|
host = fmt.Sprintf("http://localhost:%s", args.port)
|
2017-11-18 13:23:36 +01:00
|
|
|
|
2018-07-21 19:50:53 +02:00
|
|
|
fmt.Printf("\nListening on %s", host)
|
|
|
|
fmt.Printf("\n %s/%s\n\n", host, args.dashboard)
|
2017-11-18 13:23:36 +01:00
|
|
|
|
2018-08-02 00:36:23 +02:00
|
|
|
if err := http.ListenAndServe(":"+args.port, nil); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2017-11-08 00:10:54 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 00:36:23 +02:00
|
|
|
func getProxyHandler() http.Handler {
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(args.url)
|
|
|
|
proxy.Transport = transport{http.DefaultTransport}
|
2017-11-08 00:10:54 +01:00
|
|
|
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
|
|
request.Host = request.URL.Host
|
2018-08-02 00:36:23 +02:00
|
|
|
proxy.ServeHTTP(response, request)
|
2017-11-15 15:37:54 +01:00
|
|
|
})
|
|
|
|
}
|
2017-11-08 00:10:54 +01:00
|
|
|
|
2017-11-21 22:36:40 +01:00
|
|
|
func getSocketHandler() http.Handler {
|
|
|
|
server, err := socketio.NewServer(nil)
|
|
|
|
if err != nil {
|
2018-08-02 00:36:23 +02:00
|
|
|
panic(err)
|
2017-11-21 22:36:40 +01:00
|
|
|
}
|
|
|
|
server.On("connection", func(so socketio.Socket) {
|
|
|
|
socket = so
|
|
|
|
emit()
|
|
|
|
})
|
|
|
|
server.On("error", func(so socketio.Socket, err error) {
|
2018-08-02 00:36:23 +02:00
|
|
|
fmt.Println("socket error:", err)
|
2017-11-21 22:36:40 +01:00
|
|
|
})
|
|
|
|
return server
|
|
|
|
}
|
|
|
|
|
2018-08-02 00:36:23 +02:00
|
|
|
func getDashboardItemHandler() http.Handler {
|
|
|
|
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
id := strings.TrimPrefix(req.URL.Path, dashboardItemPath)
|
|
|
|
i, _ := strconv.Atoi(id)
|
|
|
|
json, _ := json.Marshal(captures[i])
|
|
|
|
res.Header().Add("Content-Type", "application/json")
|
|
|
|
res.Write([]byte(json))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDashboardHandler() http.Handler {
|
|
|
|
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
res.Header().Add("Content-Type", "text/html")
|
|
|
|
res.Write([]byte(dashboardHTML))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
2017-11-08 00:10:54 +01:00
|
|
|
|
2018-07-21 19:50:53 +02:00
|
|
|
reqDump, err := DumpRequest(req)
|
2017-11-19 16:51:26 +01:00
|
|
|
if err != nil {
|
2018-07-21 19:50:53 +02:00
|
|
|
return nil, err
|
2017-11-19 16:51:26 +01:00
|
|
|
}
|
2017-11-08 00:10:54 +01:00
|
|
|
|
2018-07-22 20:14:27 +02:00
|
|
|
res, err := t.RoundTripper.RoundTrip(req)
|
|
|
|
if err != nil {
|
2018-08-02 11:09:00 +02:00
|
|
|
return nil, fmt.Errorf("uh oh | %v | %s", err, req.URL)
|
2018-07-22 20:14:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-19 16:51:26 +01:00
|
|
|
resDump, err := DumpResponse(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-15 15:37:54 +01:00
|
|
|
|
2018-07-21 19:50:53 +02:00
|
|
|
capture := Capture{
|
2018-07-27 00:49:12 +02:00
|
|
|
ID: captureID,
|
2018-07-21 19:50:53 +02:00
|
|
|
Path: req.URL.Path,
|
|
|
|
Method: req.Method,
|
|
|
|
Status: res.StatusCode,
|
|
|
|
Request: string(reqDump),
|
|
|
|
Response: string(resDump),
|
2017-11-15 15:37:54 +01:00
|
|
|
}
|
2018-07-27 00:49:12 +02:00
|
|
|
captureID++
|
2017-11-15 15:37:54 +01:00
|
|
|
|
2017-11-21 22:36:40 +01:00
|
|
|
captures.Add(capture)
|
|
|
|
emit()
|
2017-11-15 15:37:54 +01:00
|
|
|
|
2017-11-19 16:51:26 +01:00
|
|
|
return res, nil
|
2017-11-08 00:10:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-21 19:50:53 +02:00
|
|
|
func DumpRequest(req *http.Request) ([]byte, error) {
|
|
|
|
return httputil.DumpRequest(req, true)
|
|
|
|
}
|
|
|
|
|
2017-11-15 15:37:54 +01:00
|
|
|
func DumpResponse(res *http.Response) ([]byte, error) {
|
|
|
|
var originalBody bytes.Buffer
|
2018-07-22 20:14:27 +02:00
|
|
|
reader := io.TeeReader(res.Body, &originalBody)
|
2017-11-15 15:37:54 +01:00
|
|
|
if res.Header.Get("Content-Encoding") == "gzip" {
|
2018-07-22 20:14:27 +02:00
|
|
|
reader, _ = gzip.NewReader(reader)
|
2017-11-15 15:37:54 +01:00
|
|
|
}
|
2018-07-22 20:14:27 +02:00
|
|
|
res.Body = ioutil.NopCloser(reader)
|
2017-11-18 12:42:53 +01:00
|
|
|
resDump, err := httputil.DumpResponse(res, true)
|
2017-11-15 15:37:54 +01:00
|
|
|
res.Body = ioutil.NopCloser(&originalBody)
|
2017-11-18 12:42:53 +01:00
|
|
|
return resDump, err
|
2017-11-15 15:37:54 +01:00
|
|
|
}
|
2017-11-08 00:10:54 +01:00
|
|
|
|
2017-11-21 22:36:40 +01:00
|
|
|
func emit() {
|
|
|
|
if socket == nil {
|
|
|
|
return
|
2017-11-08 00:10:54 +01:00
|
|
|
}
|
2018-08-02 00:36:23 +02:00
|
|
|
socket.Emit("captures", captures.ToReferences(host+dashboardItemPath))
|
2017-11-08 00:10:54 +01:00
|
|
|
}
|