You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
2.6 KiB
Go

package web
import (
"net/http"
4 years ago
"sync"
"github.com/gorilla/websocket"
"github.com/lqqyt2423/go-mitmproxy/addon"
4 years ago
"github.com/lqqyt2423/go-mitmproxy/flow"
_log "github.com/sirupsen/logrus"
)
var log = _log.WithField("at", "web addon")
func (web *WebAddon) echo(w http.ResponseWriter, r *http.Request) {
c, err := web.upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
4 years ago
web.addConn(c)
defer func() {
web.removeConn(c)
c.Close()
}()
for {
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
err = c.WriteMessage(mt, message)
if err != nil {
log.Println("write:", err)
break
}
}
}
type concurrentConn struct {
conn *websocket.Conn
mu sync.Mutex
}
type WebAddon struct {
addon.Base
addr string
upgrader *websocket.Upgrader
serverMux *http.ServeMux
server *http.Server
4 years ago
conns []*concurrentConn
4 years ago
connsMu sync.RWMutex
}
func NewWebAddon() *WebAddon {
web := new(WebAddon)
web.addr = ":9081"
web.upgrader = &websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
web.serverMux = new(http.ServeMux)
web.serverMux.HandleFunc("/echo", web.echo)
web.serverMux.Handle("/", http.FileServer(http.Dir("addon/web/client/build")))
web.server = &http.Server{Addr: web.addr, Handler: web.serverMux}
log = log.WithField("in", "WebAddon")
web.conns = make([]*concurrentConn, 0)
go func() {
log.Infof("server start listen at %v\n", web.addr)
err := web.server.ListenAndServe()
log.Error(err)
}()
return web
}
4 years ago
func (web *WebAddon) addConn(c *websocket.Conn) {
web.connsMu.Lock()
web.conns = append(web.conns, &concurrentConn{conn: c})
4 years ago
web.connsMu.Unlock()
}
func (web *WebAddon) removeConn(conn *websocket.Conn) {
web.connsMu.Lock()
defer web.connsMu.Unlock()
index := -1
for i, c := range web.conns {
if conn == c.conn {
4 years ago
index = i
break
}
}
if index == -1 {
return
}
web.conns = append(web.conns[:index], web.conns[index+1:]...)
}
func (web *WebAddon) sendFlow(msgFn func() *message) {
4 years ago
web.connsMu.RLock()
conns := web.conns
web.connsMu.RUnlock()
if len(conns) == 0 {
4 years ago
return
}
msg := msgFn()
4 years ago
for _, c := range conns {
c.mu.Lock()
c.conn.WriteMessage(websocket.BinaryMessage, msg.bytes())
c.mu.Unlock()
4 years ago
}
}
4 years ago
4 years ago
func (web *WebAddon) Request(f *flow.Flow) {
web.sendFlow(func() *message {
return newMessageRequest(f)
})
}
func (web *WebAddon) Responseheaders(f *flow.Flow) {
web.sendFlow(func() *message {
return newMessageResponse(f)
})
4 years ago
}
func (web *WebAddon) Response(f *flow.Flow) {
web.sendFlow(func() *message {
return newMessageResponseBody(f)
})
4 years ago
}