Exemplo n.º 1
0
        protected override void OnStop()
        {
            Logging.Info("Stopping Manta MTA Service");

            // Need to wait while servers & client shutdown.
            MantaCoreEvents.InvokeMantaCoreStopping();
            for (int i = 0; i < _SmtpServers.Count; i++)
            {
                (_SmtpServers[i] as SmtpServer).Dispose();
            }

            Logging.Info("Manta MTA Service has stopped.");
        }
Exemplo n.º 2
0
        /// <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)
                {
                    MantaEventCollection events = null;
                    // Get events for forwarding.
                    try
                    {
                        events = Core.DAL.EventDB.GetEventsForForwarding(10);
                    }
                    catch (SqlNullValueException)
                    {
                        events = new MantaEventCollection();
                    }


                    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.
                        var eventTasks = new Task[events.Count];
                        for (var i = 0; i < events.Count; i++)
                        {
                            ForwardEventAsync(events[i]).Wait();
                        }
                        // eventTasks[0] = Task.Factory.StartNew(async (evt) => await ForwardEventAsync((MantaEvent)evt), events[i]);

                        //Task.WaitAll(eventTasks);
                    }
                }
            }
            catch (Exception ex)
            {
                // Something went wrong.
                Logging.Error("EventHttpForwarder encountered an error.", ex);
                MantaCoreEvents.InvokeMantaCoreStopping();
                Environment.Exit(-1);
            }

            _IsRunning = false;
        }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
        private SmtpOutboundClientCollection() : base()
        {
            // Thread responsable for removing inactive connections.
            Thread t = new Thread(new ThreadStart(delegate()
            {
                // Loop forever
                while (true)
                {
                    try
                    {
                        int removedCount = 0;

                        // Loop through all of the connections.
                        for (int i = 0; i < _Instance.Count; i++)
                        {
                            SmtpOutboundClient client = _Instance[i];
                            if (client != null &&
                                client.IsActive == false &&
                                client.LastActive.AddSeconds(MtaParameters.Client.ConnectionIdleTimeoutInterval) <= DateTime.UtcNow)
                            {
                                // The connection is not null and appears to have been inactive for the idle timeout interval.

                                try
                                {
                                    if (!client.ExecQuitAsync().Result)
                                    {
                                        if (client.Connected)
                                        {
                                            client.GetStream().Close();
                                        }
                                        client.Close();
                                    }
                                }
                                catch (Exception)
                                {
                                    client.Close();
                                }
                                finally
                                {
                                    client.Dispose();
                                }
                                i--;                                 // ExecQuitAsync will remove from the list.
                                removedCount++;
                            }
                        }

                        if (removedCount > 0)
                        {
                            Logging.Debug("Removed " + removedCount + " idle connections");
                        }
                    }
                    catch (Exception ex)
                    {
                        Logging.Fatal("SmtpOutboundClient idle handler failed", ex);
                        MantaCoreEvents.InvokeMantaCoreStopping();
                        Environment.Exit(-1);
                    }

                    Thread.Sleep(1000);
                }
            }));

            t.IsBackground = true;
            t.Start();
        }