public void AssignMessage(WebSocketMessage oWSM) { byte[] data = oWSM.PayloadAsBytes(); string path = string.Format("d:/{0}_sproto.data", oWSM.ID); System.IO.File.WriteAllBytes(path, data); }
/// <summary> /// Modific the websocket message with your rule /// </summary> /// <param name="oSession"></param> /// <param name="webSocketMessage"></param> /// <param name="nowFiddlerRequsetChange"></param> /// <param name="ShowError"></param> /// <param name="ShowMes"></param> public static void ModificWebSocketMessage(Session oSession, WebSocketMessage webSocketMessage, IFiddlerHttpTamper nowFiddlerChange, bool isRequest, Action <string> ShowError, Action <string> ShowMes) { if (nowFiddlerChange.ParameterPickList != null) { PickSessionParameter(oSession, nowFiddlerChange, ShowError, ShowMes, webSocketMessage.IsOutbound, webSocketMessage); } ContentModific payLoadModific = null; if (isRequest) { FiddlerRequestChange nowFiddlerRequsetChange = (FiddlerRequestChange)nowFiddlerChange; payLoadModific = nowFiddlerRequsetChange.BodyModific; } else { FiddlerResponseChange nowFiddlerResponseChange = (FiddlerResponseChange)nowFiddlerChange; payLoadModific = nowFiddlerResponseChange.BodyModific; } //Modific body if (payLoadModific != null && payLoadModific.ModificMode != ContentModificMode.NoChange) { if (payLoadModific.ModificMode == ContentModificMode.HexReplace) { try { webSocketMessage.SetPayload(payLoadModific.GetFinalContent(webSocketMessage.PayloadAsBytes())); } catch (Exception ex) { ShowError(string.Format("error in GetFinalContent in HexReplace with [{0}]", ex.Message)); } } else { string sourcePayload = webSocketMessage.PayloadAsString(); if (payLoadModific.ModificMode == ContentModificMode.ReCode) { try { webSocketMessage.SetPayload(payLoadModific.GetRecodeContent(sourcePayload)); } catch (Exception ex) { ShowError(string.Format("error in GetRecodeContent in ReCode with [{0}]", ex.Message)); } } else { webSocketMessage.SetPayload(payLoadModific.GetFinalContent(sourcePayload)); } } } }
/// <summary> /// Modific the websocket message with your rule /// </summary> /// <param name="oSession"></param> /// <param name="webSocketMessage"></param> /// <param name="nowFiddlerRequsetChange"></param> /// <param name="ShowError"></param> /// <param name="ShowMes"></param> public static void ModificWebSocketMessage(Session oSession, WebSocketMessage webSocketMessage, IFiddlerHttpTamper nowFiddlerChange, bool isRequest, Action <string> ShowError, Action <string> ShowMes) { if (nowFiddlerChange.ParameterPickList != null) { PickSessionParameter(oSession, nowFiddlerChange, ShowError, ShowMes, webSocketMessage.IsOutbound, webSocketMessage); } ParameterContentModific payLoadModific = null; if (isRequest) { FiddlerRequestChange nowFiddlerRequsetChange = (FiddlerRequestChange)nowFiddlerChange; payLoadModific = nowFiddlerRequsetChange.BodyModific; } else { FiddlerResponseChange nowFiddlerResponseChange = (FiddlerResponseChange)nowFiddlerChange; payLoadModific = nowFiddlerResponseChange.BodyModific; } //Modific body if (payLoadModific != null && payLoadModific.ModificMode != ContentModificMode.NoChange) { if (payLoadModific.ModificMode == ContentModificMode.HexReplace) { try { webSocketMessage.SetPayload(payLoadModific.GetFinalContent(webSocketMessage.PayloadAsBytes())); } catch (Exception ex) { ShowError(string.Format("error in GetFinalContent in HexReplace with [{0}]", ex.Message)); } } else { if (webSocketMessage.FrameType == WebSocketFrameTypes.Binary) { ShowError("error in GetFinalContent that WebSocketFrameTypes is Binary ,just use <hex> mode"); } else if (webSocketMessage.FrameType == WebSocketFrameTypes.Ping || webSocketMessage.FrameType == WebSocketFrameTypes.Pong || webSocketMessage.FrameType == WebSocketFrameTypes.Close) { // do nothing } else { string sourcePayload = webSocketMessage.PayloadAsString(); if (payLoadModific.ModificMode == ContentModificMode.ReCode) { try { webSocketMessage.SetPayload(payLoadModific.GetRecodeContent(sourcePayload)); } catch (Exception ex) { ShowError(string.Format("error in GetRecodeContent in ReCode with [{0}]", ex.Message)); } } else { string errMes; NameValueCollection nameValueCollection = new NameValueCollection(); //webSocketMessage.SetPayload(payLoadModific.GetFinalContent(sourcePayload)); string tempPayload = payLoadModific.GetFinalContent(sourcePayload, nameValueCollection, out errMes); if (errMes != null) { ShowError(string.Format("error in GetFinalContent in PayLoadModific that [{0}]", errMes)); } if (tempPayload != sourcePayload) //非标准协议的实现,或没有实现的压缩会导致PayloadAsString()使数据不可逆 { webSocketMessage.SetPayload(tempPayload); } if (nameValueCollection != null && nameValueCollection.Count > 0) { ShowMes(string.Format("[ParameterizationContent]:{0}", nameValueCollection.MyToFormatString())); } } } } } }
public bool Match(Session oSession, bool isRequest, WebSocketMessage webSocketMessage = null) { bool isWebSocket = webSocketMessage != null;// oSession.BitFlags.HasFlag(SessionFlags.IsWebSocketTunnel); bool isMatch = true; if (isWebSocket) { if (!oSession.BitFlags.HasFlag(SessionFlags.IsWebSocketTunnel)) { return(false); } if (!((isRequest && webSocketMessage.IsOutbound) || (!isRequest && !webSocketMessage.IsOutbound))) { return(false); } if (!UriMatch.Match(oSession.fullUrl)) { return(false); } if (BodyMatch != null) { if (webSocketMessage.FrameType == WebSocketFrameTypes.Binary && BodyMatch.IsHexMatch) { if (!BodyMatch.Match(webSocketMessage.PayloadAsBytes())) { return(false); } } else if (webSocketMessage.FrameType == WebSocketFrameTypes.Text && !BodyMatch.IsHexMatch) { if (!BodyMatch.Match(webSocketMessage.PayloadAsString())) { return(false); } } else if (webSocketMessage.FrameType == WebSocketFrameTypes.Continuation) { //延续帧 return(false); } else { return(false); } } } else { if (UriMatch != null) { if (!UriMatch.Match(oSession.fullUrl)) { return(false); } } if (HeadMatch != null) { if (!HeadMatch.Match(true ? (HTTPHeaders)oSession.RequestHeaders : (HTTPHeaders)oSession.ResponseHeaders)) { return(false); } } if (BodyMatch != null) { if (BodyMatch.IsHexMatch) { if (!BodyMatch.Match(true ? oSession.requestBodyBytes : oSession.responseBodyBytes)) { return(false); } } else { if (!BodyMatch.Match(true ? oSession.GetRequestBodyAsString() : oSession.GetResponseBodyAsString())) { return(false); } } } } return(isMatch); }