示例#1
0
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            //  var script = (string)textEditor1.Invoke(new Func<string>(() => textEditor1.Text));
            string script = GetStatus();

            // string script = textEditor1.Text.ToString();
            IsInNetDelegate             IsInNet             = PacExtensions.IsInNetAction;
            localHostOrDomainIsDelegate localHostOrDomainIs = PacExtensions.localHostOrDomainIs;
            myIpAddressDelegate         myIpAddress         = PacExtensions.myIpAddress;
            isResolvableDelegate        isResolvable        = PacExtensions.isResolvable;
            dateRangeDelegate           dateRange           = PacExtensions.dateRange;
            weekdayRangeDelegate        weekdayRange        = PacExtensions.weekdayRange;
            timeRangeDelegate           timeRange           = PacExtensions.timeRange;
            isPlainHostNameDelegate     isPlainHostName     = PacExtensions.isPlainHostName;
            dnsDomainIsDelegate         dnsDomainIs         = PacExtensions.dnsDomainIs;
            dnsResolveDelegate          dnsResolve          = PacExtensions.dnsResolve;
            alertDelegate           alert           = PacExtensions.alert;
            dnsDomainLevelsDelegate dnsDomainLevels = PacExtensions.dnsDomainLevels;
            shExpMatchDelegate      shExpMatch      = PacExtensions.shExpMatch;

            JintEngine jint = new JintEngine()
                              .SetDebugMode(true)
                              .SetFunction("isInNet", IsInNet)
                              .SetFunction("localHostOrDomainIs", localHostOrDomainIs)
                              .SetFunction("myIpAddress", myIpAddress)
                              .SetFunction("isResolvable", isResolvable)
                              .SetFunction("dateRange", dateRange)
                              .SetFunction("weekdayRange", weekdayRange)
                              .SetFunction("timeRange", timeRange)
                              .SetFunction("isPlainHostName", isPlainHostName)
                              .SetFunction("dnsDomainIs", dnsDomainIs)
                              .SetFunction("dnsResolve", dnsResolve)
                              .SetFunction("alert", alert)
                              .SetFunction("dnsDomainLevels", dnsDomainLevels)
                              .SetFunction("shExpMatch", shExpMatch);

            try
            {
                jint.Step += jint_Step;
                // Not sure why replacing text ??
                // textEditor1.Text = script;

                var result = jint.Run(script);

                executionHistory.Clear();
                listBox1.Invoke(new Action(delegate
                {
                    listBox1.Items.Clear();
                }));

                Uri uri;

                //if (!textBoxURL.Text.Contains("://"))
                //{
                //    Invoke(new Action(delegate
                //        {
                //            textBoxURL.Text = "http://" + textBoxURL.Text;
                //        }));
                //}


                if (testURLS.Count > 0)
                {
                    // have url list..
                }
                else
                {
                    testURLS.Add(textBoxURL.Text);
                }

                foreach (string strUrl in testURLS)
                {
                    if (!Uri.TryCreate(strUrl, UriKind.Absolute, out uri))
                    {
                        listView1.Invoke(new Action(delegate
                        {
                            listView1.Items.Add(string.Format("'{0}' is not a valid URL", textBoxURL.Text), 2);
                        }));
                    }
                    else
                    {
                        PacExtensions.CounterReset();
                        result = jint.Run(string.Format("FindProxyForURL(\"{0}\",\"{1}\")", uri.ToString(), uri.Host));

                        Trace.WriteLine(result);
                        listProxyResults1.Invoke(new Action(delegate
                        {
                            listView1.Items.Add(string.Format("Testing URL: {0}", strUrl), 0);
                            listView1.Items.Add(string.Format("IsInNet Count: {0} Total Duration: {1} ms", PacExtensions.IsInNetCount, PacExtensions.IsInNetDuration.Milliseconds), 0);
                            listView1.Items.Add(string.Format("DnsDomainIs Count: {0} Total Duration: {1} ms", PacExtensions.DnsDomainIsCount, PacExtensions.DnsDomainIsDuration.Milliseconds), 0);

                            string[] arr = new string[4];
                            ListViewItem itm;
                            //add items to ListView
                            arr[0] = listProxyResults1.Items.Count.ToString();
                            arr[1] = strUrl;
                            arr[2] = result.ToString();
                            arr[3] = DateTime.Now.ToString("yyyyMMddHHmmssfff");

                            // arr[2] = ((line - 1).ToString() + " : " + textEditor1.Lines[line - 1]);
                            itm = new ListViewItem(arr);
                            listProxyResults1.Items.Add(itm);
                            listProxyResults1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                            listProxyResults1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                            foreach (string s in PacExtensions.EvaluationHistory)
                            {
                                listView1.Items.Add(s, 0);
                            }
                        }));
                    }
                } // end of foreach url...
            }

            catch (Jint.Native.JsException ex)
            {
                listBox1.Invoke(new Action(delegate
                {
                    string msg = ex.Message.Replace("An unexpected error occurred while parsing the script. Jint.JintException: ", "");
                    listView1.Items.Add(msg, 2);
                }));
            }
            catch (System.NullReferenceException)
            {
                listBox1.Invoke(new Action(delegate
                {
                    string msg = "Null reference. Probably variable/function not defined, remember functions and variables are case sensitive.";
                    listView1.Items.Add(msg, 2);
                }));
            }
            catch (JintException ex)
            {
                listBox1.Invoke(new Action(delegate
                {
                    int i = ex.InnerException.ToString().IndexOf(":");

                    string msg = ex.InnerException.ToString();

                    if (msg.Contains("line"))
                    {
                        int x = msg.IndexOf("line");
                        int y = msg.IndexOf(":", x);
                        if (y > 0)
                        {
                            string lineNumber = msg.Substring(x + 5, y - x - 5);
                            int line;
                            if (Int32.TryParse(lineNumber, out line))
                            {
                                textEditor1.SelectAll();
                                textEditor1.SelectionBackColor = textEditor1.BackColor;
                                textEditor1.Select(textEditor1.GetFirstCharIndexFromLine(line - 1), textEditor1.Lines[line - 1].Length);
                                textEditor1.SelectionBackColor = Color.Yellow;
                                //        textEditor1.HighlightActiveLine = true;
                                //        textEditor1.GotoLine(line - 1);
                            }
                        }
                    }

                    if (i > 0)
                    {
                        msg = msg.Substring(i + 1);
                    }
                    msg = msg.Substring(0, msg.IndexOf("  at Jint."));

                    if (msg.Contains("Object reference not set to an instance of an object."))
                    {
                        msg = "Variable/Function not defined. Remember variables/functions are case sensitive.";
                    }

                    //.Replace("An unexpected error occurred while parsing the script. Jint.JintException: ", "");
                    listView1.Items.Add(msg, 2);

                    if (!msg.Contains("Variable/Function not defined."))
                    {
                        listBox1.Items.Add(string.Format("Fatal Error: {0}. {1}", ex.Message, ex.InnerException));
                    }
                }));
            }
            catch (Exception ex)
            {
                listBox1.Invoke(new Action(delegate
                {
                    listBox1.Items.Add(string.Format("Fatal Error: {0}", ex.Message));
                }));
            }
        }
示例#2
0
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            string script = textEditor1.Text;

            IsInNetDelegate             IsInNet             = PacExtensions.IsInNetAction;
            localHostOrDomainIsDelegate localHostOrDomainIs = PacExtensions.localHostOrDomainIs;
            myIpAddressDelegate         myIpAddress         = PacExtensions.myIpAddress;
            isResolvableDelegate        isResolvable        = PacExtensions.isResolvable;
            dateRangeDelegate           dateRange           = PacExtensions.dateRange;
            weekdayRangeDelegate        weekdayRange        = PacExtensions.weekdayRange;
            timeRangeDelegate           timeRange           = PacExtensions.timeRange;
            isPlainHostNameDelegate     isPlainHostName     = PacExtensions.isPlainHostName;
            dnsDomainIsDelegate         dnsDomainIs         = PacExtensions.dnsDomainIs;
            dnsResolveDelegate          dnsResolve          = PacExtensions.dnsResolve;
            alertDelegate           alert           = PacExtensions.alert;
            dnsDomainLevelsDelegate dnsDomainLevels = PacExtensions.dnsDomainLevels;
            shExpMatchDelegate      shExpMatch      = PacExtensions.shExpMatch;

            JintEngine jint = new JintEngine()
                              .SetDebugMode(true)
                              .SetFunction("isInNet", IsInNet)
                              .SetFunction("localHostOrDomainIs", localHostOrDomainIs)
                              .SetFunction("myIpAddress", myIpAddress)
                              .SetFunction("isResolvable", isResolvable)
                              .SetFunction("dateRange", dateRange)
                              .SetFunction("weekdayRange", weekdayRange)
                              .SetFunction("timeRange", timeRange)
                              .SetFunction("isPlainHostName", isPlainHostName)
                              .SetFunction("dnsDomainIs", dnsDomainIs)
                              .SetFunction("dnsResolve", dnsResolve)
                              .SetFunction("alert", alert)
                              .SetFunction("dnsDomainLevels", dnsDomainLevels)
                              .SetFunction("shExpMatch", shExpMatch);

            try
            {
                jint.Step       += jint_Step;
                textEditor1.Text = script;

                var result = jint.Run(script);

                executionHistory.Clear();
                listBox1.Invoke(new Action(delegate
                {
                    listBox1.Items.Clear();
                }));

                Uri uri;

                if (!textBoxURL.Text.Contains("://"))
                {
                    this.Invoke(new Action(delegate
                    {
                        textBoxURL.Text = "http://" + textBoxURL.Text;
                    }));
                }
                if (!Uri.TryCreate(textBoxURL.Text, UriKind.Absolute, out uri))
                {
                    listView1.Invoke(new Action(delegate
                    {
                        listView1.Items.Add(string.Format("'{0}' is not a valid URL", textBoxURL.Text), 2);
                    }));
                }
                else
                {
                    PacExtensions.CounterReset();
                    result = jint.Run(string.Format("FindProxyForURL(\"{0}\",\"{1}\")", uri.ToString(), uri.Host));

                    Trace.WriteLine(result);
                    textBoxProxy.Invoke(new Action(delegate
                    {
                        listView1.Items.Add(string.Format("IsInNet Count: {0} Total Duration: {1} ms", PacExtensions.IsInNetCount, PacExtensions.IsInNetDuration.Milliseconds));
                        listView1.Items.Add(string.Format("DnsDomainIs Count: {0} Total Duration: {1} ms", PacExtensions.DnsDomainIsCount, PacExtensions.DnsDomainIsDuration.Milliseconds));

                        textBoxProxy.Text = result.ToString();
                        foreach (string s in PacExtensions.EvaluationHistory)
                        {
                            listView1.Items.Add(s, 0);
                        }
                    }));
                }
            }

            catch (Jint.Native.JsException ex)
            {
                listBox1.Invoke(new Action(delegate
                {
                    string msg = ex.Message.Replace("An unexpected error occurred while parsing the script. Jint.JintException: ", "");
                    listView1.Items.Add(msg, 2);
                }));
            }
            catch (System.NullReferenceException)
            {
                listBox1.Invoke(new Action(delegate
                {
                    string msg = "Null reference. Probably variable/function not defined, remember functions and variables are case sensitive.";
                    listView1.Items.Add(msg, 2);
                }));
            }
            catch (JintException ex)
            {
                listBox1.Invoke(new Action(delegate
                {
                    int i = ex.InnerException.ToString().IndexOf(":");

                    string msg = ex.InnerException.ToString();

                    if (msg.Contains("line"))
                    {
                        int x = msg.IndexOf("line");
                        int y = msg.IndexOf(":", x);
                        if (y > 0)
                        {
                            string lineNumber = msg.Substring(x + 5, y - x - 5);
                            int line;
                            if (Int32.TryParse(lineNumber, out line))
                            {
                                textEditor1.HighlightActiveLine = true;
                                textEditor1.GotoLine(line - 1);
                            }
                        }
                    }

                    if (i > 0)
                    {
                        msg = msg.Substring(i + 1);
                    }
                    msg = msg.Substring(0, msg.IndexOf("  at Jint."));

                    if (msg.Contains("Object reference not set to an instance of an object."))
                    {
                        msg = "Variable/Function not defined. Remember variables/functions are case sensitive.";
                    }

                    //.Replace("An unexpected error occurred while parsing the script. Jint.JintException: ", "");
                    listView1.Items.Add(msg, 2);

                    if (!msg.Contains("Variable/Function not defined."))
                    {
                        listBox1.Items.Add(string.Format("Fatal Error: {0}. {1}", ex.Message, ex.InnerException));
                    }
                }));
            }
            catch (Exception ex)
            {
                listBox1.Invoke(new Action(delegate
                {
                    listBox1.Items.Add(string.Format("Fatal Error: {0}", ex.Message));
                }));
            }
        }