Ejemplo n.º 1
0
        public static void UpdateRecentMessageType(string recentMessageType, string configKey)
        {
            if (null == recentMessageType || recentMessageType.Length == 0)
            {
                return;
            }

            List <string> recentMessageTypes = FiddlerApp.GetRecentMessageTypes(configKey);

            if (null == recentMessageTypes)
            {
                recentMessageTypes = new List <string>(1);
            }
            recentMessageTypes.Insert(0, recentMessageType);

            for (int idx = 1; idx < recentMessageTypes.Count(); idx++)
            {
                if (recentMessageType.Equals(recentMessageTypes[idx]))
                {
                    recentMessageTypes.RemoveAt(idx);
                    break;
                }
            }

            FiddlerApp.SetRecentMessageTypes(recentMessageTypes, configKey);
        }
Ejemplo n.º 2
0
        // IBaseInspector2.Clear
        public void Clear()
        {
#if DEBUG || OUTPUT_PERF_LOG
            FiddlerApp.LogString("Req Clear");
#endif
            ClearInspector();
        }
Ejemplo n.º 3
0
        static ProtobufInspector()
        {
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            FileVersionInfo            fvi      = FileVersionInfo.GetVersionInfo(assembly.Location);
            string version = fvi.FileVersion;

            FiddlerApp.LogString("ProtobufInspector is loaded, (version:" + fvi.FileVersion + ").");
        }
Ejemplo n.º 4
0
 private void cmbMsgType_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (cmbMessageType.SelectedIndex == cmbMessageType.Items.Count - 1)
     {
         cmbMessageType.Items.Clear();
         FiddlerApp.CleanRecentMessageTypes(inspectorContext.GetName());
         return;
     }
     PostMessage(this.btnReload.Handle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
 }
Ejemplo n.º 5
0
        // Inspector2
        public override void AssignSession(Session oS)
        {
            base.AssignSession(oS);

            if (inspectorContext.AssignSession(oS))
            {
#if DEBUG || OUTPUT_PERF_LOG
                FiddlerApp.LogString("AssignSession:" + inspectorContext.GetName() + " " + oS.url);
#endif
            }
        }
Ejemplo n.º 6
0
        // Inspector2
        public override int ScoreForSession(Session oS)
        {
#if DEBUG || OUTPUT_PERF_LOG
            FiddlerApp.LogString("ScoreForSession:" + inspectorContext.GetName() + " " + oS.url);
#endif
            int score = base.ScoreForSession(oS);

            // inspectorContext.AssignSession(oS);

            return(FiddlerApp.IsProtobufPacket(inspectorContext.GetHeaders(oS)) ? 100 : 0);
        }
Ejemplo n.º 7
0
        public ProtobufInspectorView(InspectorContext inspectorContext)
        {
            this.inspectorContext = inspectorContext;
            InitializeComponent();

#if DEBUG || OUTPUT_PERF_LOG
            FiddlerApp.LogString("New " + inspectorContext.GetName() + " View");
#endif
            this.txtDirectory.Text = FiddlerApp.GetProtoPath(inspectorContext.GetName());

            UpdateMessageTypes(FiddlerApp.GetRecentMessageTypes(inspectorContext.GetName()));
        }
Ejemplo n.º 8
0
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog dialog = new FolderBrowserDialog())
            {
                dialog.ShowNewFolderButton = false;
                dialog.SelectedPath        = this.txtDirectory.Text;

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    FiddlerApp.SetProtoPath(dialog.SelectedPath, inspectorContext.GetName());
                    this.txtDirectory.Text = dialog.SelectedPath;

#if DEBUG || OUTPUT_PERF_LOG
                    UpdateData("ProtoPath Changed");
#else
                    UpdateData();
#endif
                }
            }
        }
Ejemplo n.º 9
0
        public static string ConvertToJson(string protoPath, string[] protoFiles, string descriptorSetUrl, string messageTypeName, bool printEnumAsInteger, bool printPrimitiveFields, bool isReq, byte[] data)
        {
#if DEBUG
            FiddlerApp.LogString("Start Decoding");
#endif
            if (null == messageTypeName)
            {
#if DEBUG
                FiddlerApp.LogString("Decoding:messageTypeName is null");
#endif
                // return null;
            }
            string retval = string.Empty;

            int    ret                = 0;
            string outputString       = "";
            IntPtr outputStringPtr    = IntPtr.Zero;
            UInt32 outputStringLength = 0;

            bool descriptorSetFileExisted = false;
            if (null != descriptorSetUrl)
            {
                string descriptorSetFileName = "";
                descriptorSetFileExisted = DownloadDescriptorSetFile(descriptorSetUrl, out descriptorSetFileName);

                if (descriptorSetFileExisted)
                {
                    PB2JSON_DESCRIPTOR_SET_SRC_INFO src = new PB2JSON_DESCRIPTOR_SET_SRC_INFO();

                    src.options = 4;
                    if (printEnumAsInteger)
                    {
                        src.options |= 1;
                    }
                    if (printPrimitiveFields)
                    {
                        src.options |= 2;
                    }

                    src.messageTypeName       = messageTypeName;
                    src.descriptorSetFileName = descriptorSetFileName;

                    src.messageData         = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * data.Length);
                    src.lengthOfMessageData = (UInt32)data.Length;
                    try
                    {
                        Marshal.Copy(data, 0, src.messageData, data.Length);
                        ret = ConvertMessageWithDescriptorSetToJson(ref src, ref outputStringPtr, ref outputStringLength);
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(src.messageData);
                    }

                    if (File.Exists(descriptorSetFileName))
                    {
                        File.Delete(descriptorSetFileName);
                    }
                }
            }

            if (!descriptorSetFileExisted)
            {
                int totalLength = 0;
                for (int idx = 0; idx < protoFiles.Length; idx++)
                {
                    totalLength += System.Text.Encoding.UTF8.GetByteCount(protoFiles[idx]) + 1;
                }

                byte[] protoFilesPtr = new byte[totalLength + 1];
                protoFilesPtr[totalLength] = 0;
                int offset = 0;
                for (int idx = 0; idx < protoFiles.Length; idx++)
                {
                    byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes(protoFiles[idx]);

                    utf8Bytes.CopyTo(protoFilesPtr, offset);
                    offset += utf8Bytes.Length;
                    protoFilesPtr[offset] = 0;
                    offset++;
                }

                PB2JSON_PROTOS_SRC_INFO src = new PB2JSON_PROTOS_SRC_INFO();

                src.options = 4;
                if (printEnumAsInteger)
                {
                    src.options |= 1;
                }
                if (printPrimitiveFields)
                {
                    src.options |= 2;
                }

                src.protoPath = protoPath;
                // src.protoFileNames = protoFiles;
                src.numberOfProtoFileNames = (UInt32)protoFiles.Length;
                src.lengthOfMessageData    = (UInt32)data.Length;
                src.messageTypeName        = messageTypeName;

                src.protoFileNames = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * protoFilesPtr.Length);
                src.messageData    = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * data.Length);
                try
                {
                    Marshal.Copy(data, 0, src.messageData, data.Length);
                    Marshal.Copy(protoFilesPtr, 0, src.protoFileNames, protoFilesPtr.Length);

                    ret = ConvertMessageWithProtoFilesToJson(ref src, ref outputStringPtr, ref outputStringLength);
                }
                finally
                {
                    Marshal.FreeHGlobal(src.messageData);
                    Marshal.FreeHGlobal(src.protoFileNames);
                }
            }

            if (ret == 1)
            {
                try
                {
                    outputString = convertAndFreeStrPtr(outputStringPtr, outputStringLength);
                }
                catch (Exception ex)
                {
                    FiddlerApp.LogString("Decoding exception: " + ex.Message);
                }
            }
            else
            {
                string errorStr = convertAndFreeStrPtr(outputStringPtr, outputStringLength);
                if (errorStr != null && errorStr.Length > 0)
                {
                    FiddlerApp.LogString("Protobuf Decoding Failed:" + errorStr);
                }
            }

            return(outputString);
        }
Ejemplo n.º 10
0
        protected static bool DownloadDescriptorSetFile(string descriptorSetUrl, out string descriptorSetFileName)
        {
            descriptorSetFileName = "";

            bool       ret = false;
            Stream     stm = null;
            FileStream fs  = null;

            try
            {
                HttpRequestCachePolicy requestCachePolicy = new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAge, TimeSpan.FromSeconds(600));

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(descriptorSetUrl);
                request.Headers.Add("Accept-Encoding", "gzip,deflate");
                request.CachePolicy            = requestCachePolicy;
                request.AllowAutoRedirect      = true;
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                request.Credentials            = CredentialCache.DefaultCredentials;
                WebResponse response = request.GetResponse();

                HttpWebResponse httpWebResponse = (HttpWebResponse)response;
                if (HttpStatusCode.OK == httpWebResponse.StatusCode)
                {
                    stm = httpWebResponse.GetResponseStream();

                    descriptorSetFileName = Path.GetTempFileName();

                    fs = File.OpenWrite(descriptorSetFileName);
                    stm.CopyTo(fs);

                    bool isFromCache = response.IsFromCache;

                    if (!response.IsFromCache)
                    {
                        // Save to cache
                        CacheDescriptorSet(descriptorSetUrl, descriptorSetFileName, 600);
                    }
                    ret = true;
                }
            }
            catch (Exception ex)
            {
                if (File.Exists(descriptorSetFileName))
                {
                    File.Delete(descriptorSetFileName);
                }
                FiddlerApp.LogString("Exception on decoding: " + ex.Message);
            }
            finally
            {
                // File.Delete(descriptorSetFileName);
                if (null != stm)
                {
                    stm.Close();
                }
                if (null != fs)
                {
                    fs.Close();
                }
            }

            if (!File.Exists(descriptorSetFileName))
            {
                ret = false;
            }

            return(ret);
        }
Ejemplo n.º 11
0
        public void UpdateData()
#endif
        {
#if DEBUG || OUTPUT_PERF_LOG
            FiddlerApplication.Log.LogString(inspectorContext.GetName() + " UpdateData: " + reason);
#else
            FiddlerApp.LogString(inspectorContext.GetName() + " UpdateData");
#endif

            if (inspectorContext.IsInvalidSession())
            {
#if DEBUG || OUTPUT_PERF_LOG
                FiddlerApp.LogString("UpdateData exits for invalidated session");
#endif
                ClearView();
                return;
            }

            HTTPHeaders headers = inspectorContext.Headers;
            if (!FiddlerApp.IsProtobufPacket(headers))
            {
#if DEBUG || OUTPUT_PERF_LOG
                FiddlerApp.LogString("UpdateData exits for non-protobuf session");
#endif
                ClearView();
                return;
            }

            ClearView(false);
            string messageTypeName  = "";
            string descriptorSetUrl = "";
            if (null != headers && FiddlerApp.ParseMessageTypeNameAndDescriptorSetUrl(headers, out messageTypeName, out descriptorSetUrl))
            {
                this.cmbMessageType.Text    = messageTypeName == null ? "" : messageTypeName;
                this.cmbMessageType.Enabled = (messageTypeName == null || messageTypeName.Length == 0);
            }
            else
            {
                this.cmbMessageType.Enabled = true;
            }

            string protoPath            = this.txtDirectory.Text;
            bool   printEnumAsInteger   = this.chkboxEnumValue.Checked;
            bool   printPrimitiveFields = this.chkboxPrintPrimitiveFields.Checked;

            try
            {
                string jsonString = null;
                byte[] body       = FiddlerApp.DecodeContent(inspectorContext.RawBody, headers);

                if (null != body)
                {
                    string[] protoFiles = FiddlerApp.LoadProtos(protoPath);

                    jsonString = Protobuf2Json.ConvertToJson(protoPath, protoFiles, descriptorSetUrl, messageTypeName, printEnumAsInteger, printPrimitiveFields, false, body);

                    object jsonObject = Fiddler.WebFormats.JSON.JsonDecode(jsonString);
                    Fiddler.WebFormats.JSON.JSONParseResult jsonResult = null;
                    if (!(jsonObject is Fiddler.WebFormats.JSON.JSONParseResult))
                    {
                        tvJson.Nodes.Clear();
                        return;
                    }

                    jsonResult = jsonObject as Fiddler.WebFormats.JSON.JSONParseResult;
                    tvJson.Tag = jsonString;
#if DEBUG || OUTPUT_PERF_LOG
                    FiddlerApplication.Log.LogString(inspectorContext.GetName() + " beginUpdate");
#endif
                    TreeNode rootNode = new TreeNode("Protobuf");

                    Queue <KeyValuePair <object, TreeNode> > queue = new Queue <KeyValuePair <object, TreeNode> >();
                    object   jsonItem   = jsonResult.JSONObject;
                    TreeNode parentNode = rootNode;

                    while (true)
                    {
                        AddNode(jsonItem, parentNode, queue);
                        if (queue.Count == 0)
                        {
                            break;
                        }

                        KeyValuePair <object, TreeNode> kv = queue.Dequeue();
                        jsonItem   = kv.Key;
                        parentNode = kv.Value;
                    }

                    rootNode.ExpandAll();

                    tvJson.BeginUpdate();
                    try
                    {
                        if (tvJson.Nodes.Count > 0)
                        {
                            tvJson.Nodes.Clear();
                        }

                        tvJson.Nodes.Add(rootNode);

                        // tvJson.ExpandAll();
                        // rootNode.EnsureVisible();
                    }
                    finally
                    {
                        tvJson.EndUpdate();
#if DEBUG || OUTPUT_PERF_LOG
                        FiddlerApp.LogString(inspectorContext.GetName() + " EndUpdate: " + tvJson.GetNodeCount(true).ToString());
#endif
                    }
                }
            }
            catch (Exception ex)
            {
                FiddlerApp.LogString(ex.Message);
            }
        }