Пример #1
0
        public override ThenResponse Perform(EventInfo eventInfo)
        {
            ThenResponse thenResponse = ThenResponse.Continue;

            string destinationHost = DestinationHost?.GetText(eventInfo.Variables) ?? eventInfo.ProxyConnection.ProxyInfo.DestinationHost;
            int?   destinationPort = DestinationPort?.GetInt(eventInfo.Variables) ?? eventInfo.ProxyConnection.ProxyInfo.DestinationPort;

            if (eventInfo.Direction == Messages.DataDirection.Target)
            {
                if (!String.IsNullOrEmpty(destinationHost) && destinationPort != null && (OverrideCurrentConnection || !eventInfo.ProxyConnection.HasTargetConnection))
                {
                    if (!eventInfo.ProxyConnection.InitTargetConnection(destinationHost, destinationPort.Value))
                    {
                        thenResponse = ThenResponse.BreakRules;
                    }
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(destinationHost) && destinationPort != null && (OverrideCurrentConnection || !eventInfo.ProxyConnection.HasOriginConnection))
                {
                    if (!eventInfo.ProxyConnection.InitOriginConnection(destinationHost, destinationPort.Value))
                    {
                        thenResponse = ThenResponse.BreakRules;
                    }
                }
            }

            return(thenResponse);
        }
Пример #2
0
        public override ThenResponse Perform(EventInfo eventInfo)
        {
            ThenResponse thenResponse = ThenResponse.BreakRules;

            if (eventInfo.Type == EventType.Message && (OverrideCurrentConnection || !eventInfo.ProxyConnection.HasConnection(eventInfo.Direction)))
            {
                HttpMessage httpMessage = eventInfo.Message as HttpMessage;
                if (httpMessage != null)
                {
                    string[] host = httpMessage.Headers.GetOrDefault("Host")?.Split(':');
                    if (host != null)
                    {
                        string hostname = host[0];
                        int    port;
                        if (host.Length <= 1 || !int.TryParse(host[1], out port))
                        {
                            port = 80;
                        }
                        if (eventInfo.ProxyConnection.InitConnection(eventInfo.Direction, hostname, port))
                        {
                            thenResponse = ThenResponse.Continue;
                        }
                    }
                }
            }
            else
            {
                thenResponse = ThenResponse.Continue;
            }
            return(thenResponse);
        }
Пример #3
0
		public override ThenResponse Perform(EventInfo eventInfo)
		{
			ThenResponse response = ThenResponse.Continue;
			try
			{
				IScriptHandler scriptHandler = Container.GetExportedValue<IScriptHandler>();
				if (scriptHandler != null)
				{
					if (UseNamedScript)
					{
						if (!Enum.TryParse(scriptHandler.RunNamedScript(eventInfo, ScriptName), out response))
						{
							response = ThenResponse.Continue;
						}
					}
					else
					{
						if (!Enum.TryParse(scriptHandler.RunScript(eventInfo, ScriptText), out response))
						{
							response = ThenResponse.Continue;
						}
					}
				}
			}
			catch (Exception e)
			{
				Log.LogError(e, "Unknown Script Error: " + e.Message);
			}
			return response;
		}
Пример #4
0
        private ThenResponse PerformThens(IEnumerable <Then> thens, EventInfo eventInfo)
        {
            ThenResponse ThenResult = ThenResponse.Continue;

            foreach (Then then in thens)
            {
                ThenResponse result = then.Perform(eventInfo);
                ThenResult |= result;
                if (result.HasFlag(ThenResponse.BreakThens) || result.HasFlag(ThenResponse.BreakRules))
                {
                    break;
                }
            }
            return(ThenResult);
        }
Пример #5
0
        private ThenResponse Run(EventInfo eventInfo)
        {
            IReadOnlyList <Rule> rules = null;

            switch (eventInfo.ProxyConnection.ProxyInfo.DataType)
            {
            case ProxyDataType.Http:
                rules = HttpRulesRegistry.GetRules();
                break;

            case ProxyDataType.Text:
                rules = TextRulesRegistry.GetRules();
                break;

            default:
                rules = new List <Rule>();
                break;
            }


            ThenResponse thenResult = ThenResponse.Continue;

            foreach (Rule rule in rules)
            {
                try
                {
                    if (rule.Enabled && MatchWhens(rule.Whens, eventInfo))
                    {
                        thenResult |= PerformThens(rule.Thens, eventInfo);
                        if (thenResult.HasFlag(ThenResponse.BreakRules))
                        {
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.LogError(e, "Failure running rule", rule.Name);
                }
            }
            return(thenResult);
        }
Пример #6
0
        public void ThenBroadcast_PerformTest()
        {
            var testCases = new[]
            {
                new
                {
                    InputEventInfo = new EventInfo()
                    {
                        Type = EventType.Connected
                    },
                    WillBroadcast = false,
                    HitCount      = Times.Never()
                },
                new
                {
                    InputEventInfo = new EventInfo()
                    {
                        Type = EventType.Disconnected
                    },
                    WillBroadcast = false,
                    HitCount      = Times.Never()
                },
                new
                {
                    InputEventInfo = new EventInfo()
                    {
                        Type = EventType.Message
                    },
                    WillBroadcast = true,
                    HitCount      = Times.Once()
                }
            };
            Mock <Self> selfMock = new Mock <Self>()
            {
                CallBase = true
            };
            Self mockedSelf = selfMock.Object;

            Singleton <ISelf> .Instance = mockedSelf;
            ThenBroadcast then            = new ThenBroadcast();
            EventInfo     resultEventInfo = null;

            mockedSelf.NewEventBroadcasted += (EventInfo eventInfo) => resultEventInfo = eventInfo;


            foreach (var testCase in testCases)
            {
                ThenResponse response = then.Perform(testCase.InputEventInfo);

                if (testCase.WillBroadcast)
                {
                    Assert.AreEqual(testCase.InputEventInfo, resultEventInfo);
                }
                else
                {
                    Assert.AreEqual(null, resultEventInfo);
                }
                Assert.AreEqual(ThenResponse.Continue, response);
                resultEventInfo = null;
            }
        }
Пример #7
0
 public void ShouldReturnMessage()
 {
     WhenHandleIsCalledWithException(new Exception());
     ThenResponse.Should().Be(Constants.ServerError);
 }
Пример #8
0
 public void ShouldSave()
 {
     WhenSaveIsCalled();
     ThenResponse.Should().Be(true);
 }
Пример #9
0
        public override ThenResponse Perform(EventInfo eventInfo)
        {
            if (eventInfo.Type == EventType.Message)
            {
                ThenResponse thenResult = ThenResponse.BreakRules;
                if (!eventInfo.Message.CheckedEntity(HttpMessage.EntityFlag))
                {
                    IVariable <byte[]> carryOverVar   = eventInfo.Variables.GetOrDefault <byte[]>($"ThenDelimitHttp_CarryOverBytes") ?? eventInfo.Variables.Add <byte[]>($"ThenDelimitHttp_CarryOverBytes");
                    byte[]             carryOverBytes = carryOverVar.Value ?? new byte[0];
                    carryOverVar.Value = new byte[0];

                    byte[] fullBytes = carryOverBytes.Combine(eventInfo.Message.RawBytes);

                    List <EventInfo> childEvents = new List <EventInfo>();

                    Tuple <int, HttpMessage> messageInfo = null;
                    IVariable <int>          syncId      = eventInfo.Variables.GetOrDefault <int>("ThenDelimitHttp_SyncId") ?? eventInfo.Variables.Add <int>("ThenDelimitHttp_SyncId");

                    do
                    {
                        HttpMessageParser parser = new HttpMessageParser()
                        {
                            TextEncoding = eventInfo.ProxyConnection.ProxyInfo.DefaultEncoding
                        };
                        messageInfo = parser.Parse(fullBytes);
                        if (messageInfo.Item2 != null)
                        {
                            carryOverBytes           = new byte[0];
                            messageInfo.Item2.SyncId = syncId.Value++;
                            childEvents.Add(eventInfo.Clone(message: messageInfo.Item2));
                            if (messageInfo.Item1 <= fullBytes.Length)
                            {
                                fullBytes = new Buffer <byte>(fullBytes, messageInfo.Item1, fullBytes.Length - messageInfo.Item1).GetBytes();
                            }
                            else
                            {
                                fullBytes = new byte[0];
                            }
                        }
                        else
                        {
                            carryOverBytes = fullBytes;
                        }
                    }while (fullBytes.Length > 0 && messageInfo.Item2 != null);

                    carryOverVar.Value = carryOverBytes;

                    eventInfo.Engine.Queue.AddFirst(childEvents);

                    eventInfo.Message.SetEntityFlag(HttpMessage.EntityFlag, false);
                }
                else if (eventInfo.Message is HttpMessage)
                {
                    thenResult = ThenResponse.Continue;
                }
                return(thenResult);
            }
            else
            {
                return(ThenResponse.Continue);
            }
        }
Пример #10
0
        public override ThenResponse Perform(EventInfo eventInfo)
        {
            ThenResponse thenResult = ThenResponse.Continue;

            if (eventInfo.Type == EventType.Message)
            {
                if (!eventInfo.Message.Complete && eventInfo.Type == EventType.Message)
                {
                    IVariable <string>             carryOverVar  = eventInfo.Variables.GetOrDefault <string>($"ThenDelimitText_CarryOverText_{eventInfo.Direction}") ?? eventInfo.Variables.Add <string>($"ThenDelimitText_CarryOverText_{eventInfo.Direction}");
                    string                         carryOverText = carryOverVar.Value ?? string.Empty;
                    List <Tuple <string, string> > sections      = null;

                    if (eventInfo.ProxyConnection.ProxyInfo.UseDelimiter)
                    {
                        sections = eventInfo.Message.RawText.SplitWithDelimiters(eventInfo.ProxyConnection.ProxyInfo.Delimiters);
                    }
                    else
                    {
                        sections = new List <Tuple <string, string> > {
                            new Tuple <string, string>(eventInfo.Message.RawText, string.Empty)
                        };
                    }

                    List <EventInfo> childEvents = new List <EventInfo>();

                    carryOverVar.Value = String.Empty;
                    string foundDelimiter = null;
                    if (eventInfo.Message.RawText.StartsWith(eventInfo.ProxyConnection.ProxyInfo.Delimiters, out foundDelimiter))
                    {
                        childEvents.Add(eventInfo.Clone(message: new Message()
                        {
                            TextEncoding = eventInfo.Message.TextEncoding,
                            RawText      = carryOverText + foundDelimiter,
                            Delimiter    = foundDelimiter,
                            Complete     = true
                        }));
                    }
                    if (sections.Count > 0)
                    {
                        for (int sectionIndex = 0; sectionIndex < sections.Count - 1; sectionIndex++)
                        {
                            childEvents.Add(eventInfo.Clone(message: new Message()
                            {
                                TextEncoding = eventInfo.Message.TextEncoding,
                                RawText      = carryOverText + sections[sectionIndex].Item1 + sections[sectionIndex].Item2,
                                Delimiter    = sections[sectionIndex].Item2,
                                Complete     = true
                            }));
                            carryOverText = string.Empty;
                        }
                        int lastIndex = sections.Count - 1;
                        if (sections[lastIndex].Item2 != null)
                        {
                            childEvents.Add(eventInfo.Clone(message: new Message()
                            {
                                TextEncoding = eventInfo.Message.TextEncoding,
                                RawText      = carryOverText + sections[lastIndex].Item1 + sections[lastIndex].Item2,
                                Delimiter    = sections[lastIndex].Item2,
                                Complete     = true
                            }));
                            carryOverText = string.Empty;
                        }
                        else
                        {
                            carryOverVar.Value = carryOverText + sections[lastIndex].Item1;
                        }
                    }
                    else
                    {
                        carryOverVar.Value = carryOverText;
                    }
                    thenResult = ThenResponse.BreakRules;
                    eventInfo.Engine.Queue.AddFirst(childEvents);
                }
            }
            return(thenResult);
        }