move socket code to the main file
This commit is contained in:
parent
477af61528
commit
e6c9e34f85
21
capture.go
Normal file
21
capture.go
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Capture struct {
|
||||||
|
Url string `json:"url"`
|
||||||
|
Method string `json:"method"`
|
||||||
|
Status int `json:"status"`
|
||||||
|
Request string `json:"request"`
|
||||||
|
Response string `json:"response"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Captures struct {
|
||||||
|
items []Capture
|
||||||
|
max int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Captures) Add(capture Capture) {
|
||||||
|
c.items = append([]Capture{capture}, c.items...)
|
||||||
|
if len(c.items) > c.max {
|
||||||
|
c.items = c.items[:len(c.items)-1]
|
||||||
|
}
|
||||||
|
}
|
49
main.go
49
main.go
|
@ -7,22 +7,23 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
|
|
||||||
|
"github.com/googollee/go-socket.io"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Capture map[string]interface{}
|
var captures Captures
|
||||||
|
var socket socketio.Socket
|
||||||
var captures []Capture
|
|
||||||
var maxCaptures int
|
|
||||||
|
|
||||||
type Transport struct {
|
type Transport struct {
|
||||||
http.RoundTripper
|
http.RoundTripper
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
targetURL, proxyPort, dashboard, maxCaptrs := parseFlags()
|
targetURL, proxyPort, dashboard, maxCaptures := parseFlags()
|
||||||
maxCaptures = maxCaptrs
|
captures.max = maxCaptures
|
||||||
|
|
||||||
proxy := httputil.NewSingleHostReverseProxy(targetURL)
|
proxy := httputil.NewSingleHostReverseProxy(targetURL)
|
||||||
proxy.Transport = Transport{http.DefaultTransport}
|
proxy.Transport = Transport{http.DefaultTransport}
|
||||||
|
@ -43,6 +44,21 @@ func getProxyHandler(handler http.Handler) http.Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getSocketHandler() http.Handler {
|
||||||
|
server, err := socketio.NewServer(nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
server.On("connection", func(so socketio.Socket) {
|
||||||
|
socket = so
|
||||||
|
emit()
|
||||||
|
})
|
||||||
|
server.On("error", func(so socketio.Socket, err error) {
|
||||||
|
log.Println("socket error:", err)
|
||||||
|
})
|
||||||
|
return server
|
||||||
|
}
|
||||||
|
|
||||||
func (t Transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
func (t Transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
reqDump, err := httputil.DumpRequest(req, true)
|
reqDump, err := httputil.DumpRequest(req, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -59,15 +75,13 @@ func (t Transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
capture := Capture{
|
capture := Capture{req.URL.Path, req.Method, res.StatusCode,
|
||||||
"url": req.URL.Path,
|
string(reqDump),
|
||||||
"method": req.Method,
|
string(resDump),
|
||||||
"status": res.StatusCode,
|
|
||||||
"request": string(reqDump),
|
|
||||||
"response": string(resDump),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
save(capture)
|
captures.Add(capture)
|
||||||
|
emit()
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
@ -83,10 +97,9 @@ func DumpResponse(res *http.Response) ([]byte, error) {
|
||||||
return resDump, err
|
return resDump, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func save(capture Capture) {
|
func emit() {
|
||||||
captures = append([]Capture{capture}, captures...)
|
if socket == nil {
|
||||||
if len(captures) > maxCaptures {
|
return
|
||||||
captures = captures[:len(captures)-1]
|
|
||||||
}
|
}
|
||||||
emit(captures)
|
socket.Emit("captures", captures.items)
|
||||||
}
|
}
|
||||||
|
|
32
socket.go
32
socket.go
|
@ -1,32 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/googollee/go-socket.io"
|
|
||||||
)
|
|
||||||
|
|
||||||
var socket socketio.Socket
|
|
||||||
|
|
||||||
func getSocketHandler() http.Handler {
|
|
||||||
server, err := socketio.NewServer(nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
server.On("connection", func(so socketio.Socket) {
|
|
||||||
socket = so
|
|
||||||
emit(captures)
|
|
||||||
})
|
|
||||||
server.On("error", func(so socketio.Socket, err error) {
|
|
||||||
log.Println("socket error:", err)
|
|
||||||
})
|
|
||||||
return server
|
|
||||||
}
|
|
||||||
|
|
||||||
func emit(captures []Capture) {
|
|
||||||
if socket == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
socket.Emit("captures", captures)
|
|
||||||
}
|
|
Loading…
Reference in a new issue