addon/web.go -> addon/web/web.go

addon-dailer
lqqyt2423 4 years ago
parent 68a5284163
commit 16a0445afc

@ -1,4 +1,4 @@
package addon package web
import ( import (
"encoding/json" "encoding/json"
@ -7,14 +7,17 @@ import (
"text/template" "text/template"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/lqqyt2423/go-mitmproxy/addon"
"github.com/lqqyt2423/go-mitmproxy/flow" "github.com/lqqyt2423/go-mitmproxy/flow"
"github.com/sirupsen/logrus" _log "github.com/sirupsen/logrus"
) )
var log = _log.WithField("at", "web addon")
func (web *WebAddon) echo(w http.ResponseWriter, r *http.Request) { func (web *WebAddon) echo(w http.ResponseWriter, r *http.Request) {
c, err := web.upgrader.Upgrade(w, r, nil) c, err := web.upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
web.log.Print("upgrade:", err) log.Print("upgrade:", err)
return return
} }
@ -27,13 +30,13 @@ func (web *WebAddon) echo(w http.ResponseWriter, r *http.Request) {
for { for {
mt, message, err := c.ReadMessage() mt, message, err := c.ReadMessage()
if err != nil { if err != nil {
web.log.Println("read:", err) log.Println("read:", err)
break break
} }
web.log.Printf("recv: %s", message) log.Printf("recv: %s", message)
err = c.WriteMessage(mt, message) err = c.WriteMessage(mt, message)
if err != nil { if err != nil {
web.log.Println("write:", err) log.Println("write:", err)
break break
} }
} }
@ -44,12 +47,11 @@ func home(w http.ResponseWriter, r *http.Request) {
} }
type WebAddon struct { type WebAddon struct {
Base addon.Base
addr string addr string
upgrader *websocket.Upgrader upgrader *websocket.Upgrader
serverMux *http.ServeMux serverMux *http.ServeMux
server *http.Server server *http.Server
log *logrus.Entry
conns []*websocket.Conn conns []*websocket.Conn
connsMu sync.RWMutex connsMu sync.RWMutex
@ -77,13 +79,13 @@ func NewWebAddon() *WebAddon {
web.serverMux.HandleFunc("/", home) web.serverMux.HandleFunc("/", home)
web.server = &http.Server{Addr: web.addr, Handler: web.serverMux} web.server = &http.Server{Addr: web.addr, Handler: web.serverMux}
web.log = log.WithField("in", "WebAddon") log = log.WithField("in", "WebAddon")
web.conns = make([]*websocket.Conn, 0) web.conns = make([]*websocket.Conn, 0)
go func() { go func() {
web.log.Infof("server start listen at %v\n", web.addr) log.Infof("server start listen at %v\n", web.addr)
err := web.server.ListenAndServe() err := web.server.ListenAndServe()
web.log.Error(err) log.Error(err)
}() }()
return web return web
@ -125,7 +127,7 @@ func (web *WebAddon) sendFlow(on string, f *flow.Flow) {
msg := newMessage(on, f) msg := newMessage(on, f)
b, err := json.Marshal(msg) b, err := json.Marshal(msg)
if err != nil { if err != nil {
web.log.Error(err) log.Error(err)
return return
} }
for _, c := range conns { for _, c := range conns {

@ -5,6 +5,7 @@ import (
"os" "os"
"github.com/lqqyt2423/go-mitmproxy/addon" "github.com/lqqyt2423/go-mitmproxy/addon"
"github.com/lqqyt2423/go-mitmproxy/addon/web"
"github.com/lqqyt2423/go-mitmproxy/proxy" "github.com/lqqyt2423/go-mitmproxy/proxy"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -51,7 +52,7 @@ func main() {
p.AddAddon(dumper) p.AddAddon(dumper)
} }
p.AddAddon(addon.NewWebAddon()) p.AddAddon(web.NewWebAddon())
log.Fatal(p.Start()) log.Fatal(p.Start())
} }

Loading…
Cancel
Save