/// <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)); } } } }
private void OnWebSocketCaptured(Session session, WebSocketMessage message) { var url = session.fullUrl; if (url != null && url.Contains(Settings.WebSocketURLFilter)) { var payload = message.PayloadAsString(); if (payload != null) { OnPayloadCaptured(payload); } } }
private static void ProcessMessage_(Session session, WebSocketMessage message) { if (message.FrameType == WebSocketFrameTypes.Text && message.PayloadAsString() == "{}") { return; } var uri = new Uri(session.fullUrl); var query = HttpUtility.ParseQueryString(uri.Query); var url = GetUrl_(session, message); var request = new StringBuilder(); request.AppendLine(message.ToString()); request.AppendLine($"IsFinalFrame: {message.IsFinalFrame}"); request.AppendLine($"clientProtocol: {query["clientProtocol"]}"); request.AppendLine($"connectionData: {query["connectionData"]}"); SendRequest_(url, request.ToString()); }
public static void PickSessionParameter(Session oSession, IFiddlerHttpTamper nowFiddlerHttpTamper, Action <string> ShowError, Action <string> ShowMes, bool isRequest, WebSocketMessage webSocketMessage = null) { Func <string, ParameterPick, string> PickFunc = (sourceStr, parameterPick) => { try { return(ParameterPickTypeEngine.dictionaryParameterPickFunc[parameterPick.PickType].ParameterPickFunc(sourceStr, parameterPick.PickTypeExpression, parameterPick.PickTypeAdditional)); } catch (Exception) { return(null); } }; bool isWebSocket = webSocketMessage != null; if (nowFiddlerHttpTamper.ParameterPickList != null) { foreach (ParameterPick parameterPick in nowFiddlerHttpTamper.ParameterPickList) { string pickResult = null; string pickSource = null; switch (parameterPick.PickRange) { case ParameterPickRange.Line: if (isRequest) { pickSource = oSession.fullUrl; if (string.IsNullOrEmpty(pickSource)) { pickResult = null; break; } } else { if (oSession.oResponse.headers == null) { pickResult = null; break; } else { //pickSource = string.Format("{0} {1} {}", oSession.oResponse.headers.HTTPVersion, oSession.oResponse.headers.HTTPResponseCode,oSession.oResponse.headers.StatusDescription); pickSource = string.Format("{0} {1}", oSession.oResponse.headers.HTTPVersion, oSession.oResponse.headers.HTTPResponseStatus); } } pickResult = PickFunc(pickSource, parameterPick); break; case ParameterPickRange.Heads: if (isWebSocket) { ShowError("[ParameterizationPick] can not pick parameter in head when the session is websocket"); break; } IEnumerable <HTTPHeaderItem> headerItems = isRequest ? (IEnumerable <HTTPHeaderItem>)oSession.RequestHeaders : (IEnumerable <HTTPHeaderItem>)oSession.ResponseHeaders; foreach (HTTPHeaderItem tempHead in headerItems) { pickResult = PickFunc(tempHead.ToString(), parameterPick); if (pickResult != null) { break; } } break; case ParameterPickRange.Entity: if (isWebSocket) { if (webSocketMessage.PayloadLength == 0) { pickResult = null; break; } pickSource = webSocketMessage.PayloadAsString(); pickResult = PickFunc(pickSource, parameterPick); } else { if (((oSession.requestBodyBytes == null || oSession.requestBodyBytes.Length == 0) && isRequest) && ((oSession.ResponseBody == null || oSession.ResponseBody.Length == 0) && isRequest)) { pickResult = null; break; } pickSource = isRequest ? oSession.GetRequestBodyAsString() : oSession.GetResponseBodyAsString(); pickResult = PickFunc(pickSource, parameterPick); } break; default: ShowError("[ParameterizationPick] unkonw pick range"); break; } if (pickResult == null) { ShowMes(string.Format("[ParameterizationPick] can not find the parameter with [{0}]", parameterPick.ParameterName)); } else { ShowMes(string.Format("[ParameterizationPick] pick the parameter [{0} = {1}]", parameterPick.ParameterName, pickResult)); if (nowFiddlerHttpTamper.ActuatorStaticDataController.SetActuatorStaticData(parameterPick.ParameterName, pickResult)) { ShowMes(string.Format("[ParameterizationPick] add the parameter [{0}] to ActuatorStaticDataCollection", parameterPick.ParameterName)); } else { ShowError(string.Format("[ParameterizationPick] fail to add the parameter [{0}] to ActuatorStaticDataCollection", parameterPick.ParameterName)); } } } } else { ShowError("[ParameterizationPick] not find ParameterPick to pick"); } }
/// <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); }