Example #1
0
        static void Main(string[] args)
        {
            // Packets from 127.0.0.1 are forwarded to 127.0.0.1:1007
            var newRule = new ProxyRule(IPAddress.Parse("127.0.0.1"), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1007));

            configuration.AddRule(newRule);
            var proxy = new TcpProxy(configuration);

            Console.ReadKey();
        }
        public static bool RuleIsTrue(ProxyRule rule, IEnumerable <Assertion> worldState)
        {
            var factGroup = new FactGroup()
            {
                Junction = rule.Junction,
                Facts    = rule.IfTrue
            };

            return(FactGroupIsTrue(factGroup, worldState));
            //return rule.FactGroups.All(fg => FactGroupIsTrue(fg, worldState));
        }
        /// <summary>
        /// Refreshes the rules.
        /// </summary>
        /// <param name="reader">The reader.</param>
        public void RefreshRules(TextReader reader)
        {
            // put a lock on the refresh process so that only one refresh can happen at a time
            lock (_refreshLock)
            {
                Manager.LogEnabled = false;
                Manager.LogPath    = null;

                string tempLogPath       = null;
                int    tempLogLevel      = 0;
                bool   tempEngineEnabled = false;

                string            line;
                IList <ProxyRule> rules        = new List <ProxyRule>();
                IList <string>    unknownLines = new List <string>();

                while (reader.Peek() >= 0)
                {
                    line = reader.ReadLine().Trim();

                    if (String.IsNullOrEmpty(line))
                    {
                        // just plain old ignore empty lines no logging or anything
                        continue;
                    }
                    else if (line[0] == '#')
                    {
                        Manager.LogIf(tempLogLevel >= 4, "Comment: " + line, "Rule Processing");
                    }
                    else if (ProxyRequestsLine.IsMatch(line))
                    {
                        #region ProxyRequests

                        Match  match       = ProxyRequestsLine.Match(line);
                        string engineState = match.Groups["state"].Value;

                        // by default the engine is turned off
                        if (String.IsNullOrEmpty(engineState) || String.Equals(engineState, "off", StringComparison.OrdinalIgnoreCase))
                        {
                            rules.Clear();
                            tempEngineEnabled = false;

                            // don't bother processing any other rules if the engine is disabled
                            break;
                        }
                        else
                        {
                            tempEngineEnabled = true;
                        }

                        Manager.LogIf(tempLogLevel >= 3, "ProxyRequests: " + (tempEngineEnabled ? "Enabled" : "Disabled"), "Rule Processing");

                        #endregion
                    }
                    else if (ProxyLogLine.IsMatch(line))
                    {
                        #region ProxyLog

                        Match match = ProxyLogLine.Match(line);
                        tempLogPath = match.Groups["location"].Value;
                        tempLogPath = NormalizeLogLocation(tempLogPath);

                        Manager.LogIf(tempLogLevel >= 3, "ProxyLog: " + tempLogPath, "Rule Processing");

                        #endregion
                    }
                    else if (ProxyLogLevelLine.IsMatch(line))
                    {
                        #region ProxyLogLevel

                        Match match    = ProxyLogLevelLine.Match(line);
                        int   logLevel = 1;

                        if (!Int32.TryParse(match.Groups["level"].Value, out logLevel))
                        {
                            tempLogLevel = 0;
                            Manager.LogIf(tempLogLevel >= 3, "ProxyLogLevel: " + match.Groups["level"].Value + " not understood.", "Rule Processing");
                        }
                        else
                        {
                            tempLogLevel = logLevel;
                        }

                        Manager.LogIf(tempLogLevel >= 3, "ProxyLogLevel: " + logLevel, "Rule Processing");

                        #endregion
                    }
                    else if (ProxyPassLine.IsMatch(line))
                    {
                        #region ProxyPass

                        Match match = ProxyPassLine.Match(line);

                        try
                        {
                            // initialize the proxy
                            var rule = new ProxyRule {
                                Path          = match.Groups["path"].Value,
                                DownstreamUri = new Uri(match.Groups["url"].Value)
                            };

                            // add condition to next rule that shows up
                            rules.Add(rule);
                        }
                        catch (Exception exc)
                        {
                            if (tempLogLevel >= 3)
                            {
                                Manager.Log("ProxyPass: "******"Error");
                            }
                            else
                            {
                                Manager.Log("ProxyPass: "******"Error");
                            }
                        }
                        finally
                        {
                            Manager.LogIf(tempLogLevel >= 3, "ProxyPass: "******"pattern"].Value + " " + match.Groups["substitution"].Value + " [" + match.Groups["flags"].Value + "]", "Rule Processing");
                        }

                        #endregion
                    }
                    else
                    {
                        unknownLines.Add(line);
                    }
                }

                Manager.LogIf(tempLogLevel > 0, "Managed Fusion Rewriter Version: " + Manager.RewriterVersion, "Rule Processing");

                // clear and add new rules
                ClearRules();
                AddRules(rules);

                // try to process any unknown lines
                if (unknownLines.Count > 0)
                {
                    RefreshUnknownLines(ref unknownLines);

                    foreach (var unknownLine in unknownLines)
                    {
                        Manager.LogIf(tempLogLevel >= 4, "Not Understood: " + unknownLine, "Unknown");
                    }
                }


                // set the ruleset defining properties
                LogLocation        = tempLogPath;
                LogLevel           = tempLogLevel;
                EngineEnabled      = tempEngineEnabled;
                Manager.LogPath    = tempLogPath;
                Manager.LogEnabled = tempLogLevel > 0;
            }
        }