/// <summary> /// Does the actual forwarding of the events. /// </summary> private void ForwardEvents() { _IsRunning = true; try { // Keep looping as long as the MTA is running. while (!_IsStopping) { IList <MantaEvent> events = null; // Get events for forwarding. try { events = EventDB.GetEventsForForwarding(10); } catch (SqlNullValueException) { events = new List <MantaEvent>(); } if (events.Count == 0) { // No events to forward sleep for a second and look again. Thread.Sleep(1000); continue; } else { // Found events to forward, create and run Tasks to forward. for (var i = 0; i < events.Count; i++) { ForwardEventAsync(events[i]).Wait(); } } } } catch (Exception ex) { // Something went wrong. Logging.Error("EventHttpForwarder encountered an error.", ex); MantaCoreEvents.InvokeMantaCoreStopping(); Environment.Exit(-1); } _IsRunning = false; }
/// <summary> /// Gets the MxPatternID that matches the MX Record, Outbound IP Address combo. /// </summary> /// <param name="record"></param> /// <param name="ipAddress"></param> /// <returns></returns> private static int GetMxPatternID(MXRecord record, VirtualMTA ipAddress) { if (_matchedPatterns == null) { _matchedPatterns = new ConcurrentDictionary <string, MatchedMxPatternCollection>(); } MatchedMxPatternCollection matchedPatterns = null; if (!_matchedPatterns.TryGetValue(record.Host, out matchedPatterns)) { matchedPatterns = new MatchedMxPatternCollection(); _matchedPatterns.AddOrUpdate(record.Host, matchedPatterns, (string s, MatchedMxPatternCollection p) => matchedPatterns); } MatchedMxPattern matchedPattern = matchedPatterns.GetMatchedMxPattern(ipAddress); if (matchedPattern != null && matchedPattern.MatchedUtc.AddMinutes(MtaParameters.MTA_CACHE_MINUTES) > DateTime.UtcNow) { // Found a valid cached pattern ID so return it. return(matchedPattern.MxPatternID); } // Loop through all of the patterns for (int i = 0; i < _MXPatterns.Count; i++) { // The current pattern we're working with. OutboundMxPattern pattern = _MXPatterns[i]; // If the pattern applies only to a specified IP address then // only check for a match if getting rules for that IP. if (pattern.LimitedToOutboundIpAddressID.HasValue && pattern.LimitedToOutboundIpAddressID.Value != ipAddress.ID) { continue; } if (pattern.Type == OutboundMxPatternType.CommaDelimited) { // Pattern is a comma delimited list, so split the values string[] strings = pattern.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // Loop though the values in the split string array. for (int c = 0; c < strings.Length; c++) { try { // If they are a match return the rules. if (strings[c].Equals(record.Host, StringComparison.OrdinalIgnoreCase)) { if (pattern.LimitedToOutboundIpAddressID.HasValue) { matchedPatterns.Add(pattern.ID, ipAddress); } else { matchedPatterns.Add(pattern.ID, null); } return(pattern.ID); } } catch (Exception) { } } continue; } else if (pattern.Type == OutboundMxPatternType.Regex) { // Pattern is Regex so just need to do an IsMatch if (Regex.IsMatch(record.Host, pattern.Value, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture)) { // Found pattern match. if (pattern.LimitedToOutboundIpAddressID.HasValue) { matchedPatterns.Add(pattern.ID, ipAddress); } else { matchedPatterns.Add(pattern.ID, null); } return(pattern.ID); } else { continue; } } else { // Don't know what to do with this pattern so move on to the next. Logging.Error("Unknown OutboundMxPatternType : " + pattern.Type.ToString()); continue; } } // Should have been found by default at least, but hasn't. Logging.Fatal("No MX Pattern Rules! Default Deleted?"); MantaCoreEvents.InvokeMantaCoreStopping(); Environment.Exit(0); return(-1); }