From c3b2e5261b4c699ec8e39544c818f48b99015531 Mon Sep 17 00:00:00 2001 From: liqiang <974923609@qq.com> Date: Thu, 25 Mar 2021 15:37:02 +0800 Subject: [PATCH] chore: web addon add drop message type --- addon/web/conn.go | 24 ++++++++++++++++++------ addon/web/message.go | 31 ++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/addon/web/conn.go b/addon/web/conn.go index 946d3d2..d7b4c86 100644 --- a/addon/web/conn.go +++ b/addon/web/conn.go @@ -130,14 +130,26 @@ func (c *concurrentConn) isIntercpt(f *flow.Flow, after *messageFlow) bool { // 拦截 func (c *concurrentConn) waitIntercept(f *flow.Flow, after *messageFlow) { - log.Infof("waiting Intercept: %s\n", f.Request.URL) ch := c.initWaitChan(f.Id.String()) msg := (<-ch).(*messageEdit) - log.Infof("waited Intercept: %s\n", f.Request.URL) - // f.Request.Method = req.Method - // f.Request.URL = req.URL - // f.Request.Header = req.Header + // drop + if msg.mType == messageTypeDropRequest || msg.mType == messageTypeDropResponse { + f.Response = &flow.Response{ + StatusCode: 502, + } + return + } - log.Infof("waitIntercept: %v", msg) + // change + if msg.mType == messageTypeChangeRequest { + f.Request.Method = msg.request.Method + f.Request.URL = msg.request.URL + f.Request.Header = msg.request.Header + f.Request.Body = msg.request.Body + } else if msg.mType == messageTypeChangeResponse { + // TODO: statusCode + f.Response.Header = msg.response.Header + f.Response.Body = msg.response.Body + } } diff --git a/addon/web/message.go b/addon/web/message.go index 72681ac..a6d2b02 100644 --- a/addon/web/message.go +++ b/addon/web/message.go @@ -16,7 +16,7 @@ import ( // messageFlow // version 1 byte + type 1 byte + id 36 byte + waitIntercept 1 byte + content left bytes -// type: 11/12 +// type: 11/12/13/14 // messageEdit // version 1 byte + type 1 byte + id 36 byte + header len 4 byte + header content bytes + body len 4 byte + [body content bytes] @@ -36,6 +36,8 @@ const ( messageTypeChangeRequest messageType = 11 messageTypeChangeResponse messageType = 12 + messageTypeDropRequest messageType = 13 + messageTypeDropResponse messageType = 14 messageTypeChangeBreakPointRules messageType = 21 ) @@ -47,6 +49,8 @@ var allMessageTypes = []messageType{ messageTypeResponseBody, messageTypeChangeRequest, messageTypeChangeResponse, + messageTypeDropRequest, + messageTypeDropResponse, messageTypeChangeBreakPointRules, } @@ -115,8 +119,8 @@ type messageEdit struct { } func parseMessageEdit(data []byte) *messageEdit { - // 2 + 36 + 4 + 4 - if len(data) < 46 { + // 2 + 36 + if len(data) < 38 { return nil } @@ -127,6 +131,20 @@ func parseMessageEdit(data []byte) *messageEdit { return nil } + msg := &messageEdit{ + mType: mType, + id: id, + } + + if mType == messageTypeDropRequest || mType == messageTypeDropResponse { + return msg + } + + // 2 + 36 + 4 + 4 + if len(data) < 46 { + return nil + } + hl := (int)(binary.BigEndian.Uint32(data[38:42])) if 42+hl+4 > len(data) { return nil @@ -139,11 +157,6 @@ func parseMessageEdit(data []byte) *messageEdit { } bodyContent := data[42+hl+4:] - msg := &messageEdit{ - mType: mType, - id: id, - } - if mType == messageTypeChangeRequest { req := new(flow.Request) err := json.Unmarshal(headerContent, req) @@ -254,7 +267,7 @@ func parseMessage(data []byte) message { mType := (messageType)(data[1]) - if mType == messageTypeChangeRequest || mType == messageTypeChangeResponse { + if mType == messageTypeChangeRequest || mType == messageTypeChangeResponse || mType == messageTypeDropRequest || mType == messageTypeDropResponse { return parseMessageEdit(data) } else if mType == messageTypeChangeBreakPointRules { return parseMessageMeta(data)