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