|
|
|
@ -2,6 +2,7 @@ package web
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
@ -14,6 +15,8 @@ type concurrentConn struct {
|
|
|
|
|
|
|
|
|
|
waitChans map[string]chan interface{}
|
|
|
|
|
waitChansMu sync.Mutex
|
|
|
|
|
|
|
|
|
|
interceptUri string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newConn(c *websocket.Conn) *concurrentConn {
|
|
|
|
@ -24,6 +27,10 @@ func newConn(c *websocket.Conn) *concurrentConn {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *concurrentConn) writeMessage(msg *message, f *flow.Flow) {
|
|
|
|
|
if c.isIntercpt(f, msg) {
|
|
|
|
|
msg.waitIntercept = 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
|
err := c.conn.WriteMessage(websocket.BinaryMessage, msg.bytes())
|
|
|
|
|
c.mu.Unlock()
|
|
|
|
@ -32,8 +39,10 @@ func (c *concurrentConn) writeMessage(msg *message, f *flow.Flow) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if msg.waitIntercept == 1 {
|
|
|
|
|
c.waitIntercept(f, msg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *concurrentConn) readloop() {
|
|
|
|
|
for {
|
|
|
|
@ -54,6 +63,15 @@ func (c *concurrentConn) readloop() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if msg.mType == messageTypeChangeInterceptUri {
|
|
|
|
|
interceptUri := ""
|
|
|
|
|
if len(msg.content) > 0 {
|
|
|
|
|
interceptUri = string(msg.content)
|
|
|
|
|
}
|
|
|
|
|
c.interceptUri = interceptUri
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if msg.mType == messageTypeChangeRequest {
|
|
|
|
|
req := new(flow.Request)
|
|
|
|
|
err := json.Unmarshal(msg.content, req)
|
|
|
|
@ -84,15 +102,21 @@ func (c *concurrentConn) initWaitChan(key string) chan interface{} {
|
|
|
|
|
|
|
|
|
|
// 是否拦截
|
|
|
|
|
func (c *concurrentConn) isIntercpt(f *flow.Flow, after *message) bool {
|
|
|
|
|
if after.mType != messageTypeRequest {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 拦截
|
|
|
|
|
func (c *concurrentConn) waitIntercept(f *flow.Flow, after *message) {
|
|
|
|
|
if !c.isIntercpt(f, after) {
|
|
|
|
|
return
|
|
|
|
|
if c.interceptUri == "" {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(f.Request.URL.String(), c.interceptUri) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 拦截
|
|
|
|
|
func (c *concurrentConn) waitIntercept(f *flow.Flow, after *message) {
|
|
|
|
|
log.Infof("waiting Intercept: %s\n", f.Request.URL)
|
|
|
|
|
ch := c.initWaitChan(f.Id.String())
|
|
|
|
|
req := (<-ch).(*flow.Request)
|
|
|
|
|