web addon

addon-dailer
lqqyt2423 4 years ago
parent d352d25316
commit 68a5284163

@ -8,7 +8,6 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/lqqyt2423/go-mitmproxy/flow" "github.com/lqqyt2423/go-mitmproxy/flow"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -56,6 +55,18 @@ type WebAddon struct {
connsMu sync.RWMutex connsMu sync.RWMutex
} }
type message struct {
On string `json:"on"`
Flow *flow.Flow `json:"flow"`
}
func newMessage(on string, f *flow.Flow) *message {
return &message{
On: on,
Flow: f,
}
}
func NewWebAddon() *WebAddon { func NewWebAddon() *WebAddon {
web := new(WebAddon) web := new(WebAddon)
web.addr = ":9081" web.addr = ":9081"
@ -102,27 +113,32 @@ func (web *WebAddon) removeConn(conn *websocket.Conn) {
web.conns = append(web.conns[:index], web.conns[index+1:]...) web.conns = append(web.conns[:index], web.conns[index+1:]...)
} }
func (web *WebAddon) Request(f *flow.Flow) { func (web *WebAddon) sendFlow(on string, f *flow.Flow) {
b, err := json.Marshal(f) web.connsMu.RLock()
if err != nil { conns := web.conns
web.log.Error(err) web.connsMu.RUnlock()
if len(conns) == 0 {
return return
} }
id := uuid.NewV4() msg := newMessage(on, f)
f.State["id"] = id b, err := json.Marshal(msg)
web.log.Infof("id: %s, request: %s\n", id, b)
}
func (web *WebAddon) Response(f *flow.Flow) {
b, err := json.Marshal(f)
if err != nil { if err != nil {
web.log.Error(err) web.log.Error(err)
return return
} }
for _, c := range conns {
c.WriteMessage(websocket.TextMessage, b)
}
}
web.log.Infof("id: %s, response: %s\n", f.State["id"], b) func (web *WebAddon) Request(f *flow.Flow) {
web.sendFlow("request", f)
}
func (web *WebAddon) Response(f *flow.Flow) {
web.sendFlow("response", f)
} }
var homeTemplate = template.Must(template.New("").Parse(` var homeTemplate = template.Must(template.New("").Parse(`

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
uuid "github.com/satori/go.uuid"
_log "github.com/sirupsen/logrus" _log "github.com/sirupsen/logrus"
) )
@ -63,18 +64,24 @@ type Flow struct {
Stream bool Stream bool
done chan struct{} done chan struct{}
Id uuid.UUID
State map[string]interface{} // Can add value by addon State map[string]interface{} // Can add value by addon
} }
func (f *Flow) MarshalJSON() ([]byte, error) { func (f *Flow) MarshalJSON() ([]byte, error) {
j := make(map[string]interface{}) j := make(map[string]interface{})
j["id"] = f.Id
j["request"] = f.Request j["request"] = f.Request
j["response"] = f.Response j["response"] = f.Response
return json.Marshal(j) return json.Marshal(j)
} }
func NewFlow() *Flow { func NewFlow() *Flow {
return &Flow{done: make(chan struct{}), State: make(map[string]interface{})} return &Flow{
done: make(chan struct{}),
Id: uuid.NewV4(),
State: make(map[string]interface{}),
}
} }
func (f *Flow) Done() <-chan struct{} { func (f *Flow) Done() <-chan struct{} {

Loading…
Cancel
Save