/// <summary> /// Start an individual POP3 proxy on its own thread. /// </summary> /// <param name="parameters">Pop3ProxyArguments object containing all parameters for this connection.</param> private static void StartProxy(object parameters) { Pop3ProxyArguments arguments = (Pop3ProxyArguments)parameters; // Start the proxy using passed-in settings. arguments.Proxy.Start(arguments.AcceptedIPs, arguments.LocalIpAddress, arguments.LocalPort, arguments.LocalEnableSsl, arguments.RemoteServerHostName, arguments.RemoteServerPort, arguments.RemoteServerEnableSsl, arguments.RemoteServerCredential, arguments.ExportDirectory, arguments.LogFile, arguments.LogLevel, arguments.InstanceId, arguments.DebugMode); }
/// <summary> /// Start all POP3 proxy instances from the specified settings file. /// </summary> /// <param name="fileName">File containing the POP3 proxy settings.</param> public static List <Pop3Proxy> StartProxiesFromSettingsFile(string fileName) { List <Pop3Proxy> pop3Proxies = new List <Pop3Proxy>(); try { if (File.Exists(fileName)) { XPathDocument document = new XPathDocument(fileName); XPathNavigator navigator = document.CreateNavigator(); int pop3ServiceCount = ProxyFunctions.GetXmlIntValue(navigator, "/Settings/IMAP/ServiceCount"); for (int i = 1; i <= pop3ServiceCount; i++) { Pop3ProxyArguments arguments = new Pop3ProxyArguments(); arguments.AcceptedIPs = ProxyFunctions.GetXmlStringValue(navigator, "/Settings/SMTP/Service" + i + "/AcceptedIPs"); string localIpAddress = ProxyFunctions.GetXmlStringValue(navigator, "/Settings/POP3/Service" + i + "/LocalIPAddress").ToUpper(); switch (localIpAddress) { // Treat blank values as "Any". case "": case "ANY": arguments.LocalIpAddress = IPAddress.Any; break; case "BROADCAST": arguments.LocalIpAddress = IPAddress.Broadcast; break; case "IPV6ANY": arguments.LocalIpAddress = IPAddress.IPv6Any; break; case "IPV6LOOPBACK": arguments.LocalIpAddress = IPAddress.IPv6Loopback; break; case "LOOPBACK": arguments.LocalIpAddress = IPAddress.Loopback; break; default: // Try to parse the local IP address. If unable to, proceed to the next service instance. if (!IPAddress.TryParse(localIpAddress, out arguments.LocalIpAddress)) { continue; } break; } arguments.LocalPort = ProxyFunctions.GetXmlIntValue(navigator, "/Settings/POP3/Service" + i + "/LocalPort"); // If the port is invalid, proceed to the next service instance. if (arguments.LocalPort < 1) { continue; } arguments.LocalEnableSsl = ProxyFunctions.GetXmlBoolValue(navigator, "/Settings/POP3/Service" + i + "/LocalEnableSSL"); arguments.RemoteServerHostName = ProxyFunctions.GetXmlStringValue(navigator, "/Settings/POP3/Service" + i + "/RemoteServerHostName"); // If the host name is invalid, proceed to the next service instance. if (string.IsNullOrEmpty(arguments.RemoteServerHostName)) { continue; } arguments.RemoteServerPort = ProxyFunctions.GetXmlIntValue(navigator, "/Settings/POP3/Service" + i + "/RemoteServerPort"); // If the port is invalid, proceed to the next service instance. if (arguments.RemoteServerPort < 1) { continue; } arguments.RemoteServerEnableSsl = ProxyFunctions.GetXmlBoolValue(navigator, "/Settings/POP3/Service" + i + "/RemoteServerEnableSSL"); string remoteServerUsername = ProxyFunctions.GetXmlStringValue(navigator, "/Settings/POP3/Service" + i + "/RemoteServerUsername"); if (!string.IsNullOrEmpty(remoteServerUsername)) { arguments.RemoteServerCredential = new NetworkCredential(); arguments.RemoteServerCredential.UserName = remoteServerUsername; arguments.RemoteServerCredential.Password = ProxyFunctions.GetXmlStringValue(navigator, "/Settings/POP3/Service" + i + "/RemoteServerPassword"); } string certificateLocationValue = ProxyFunctions.GetXmlStringValue(navigator, "/Settings/POP3/Service" + i + "/Certificate/Location"); StoreLocation certificateLocation = StoreLocation.LocalMachine; if (certificateLocationValue.ToUpper() == "CURRENTUSER") { certificateLocation = StoreLocation.CurrentUser; } // Try to load the signing certificate based on its serial number first, then fallback to its subject name. string certificateValue = ProxyFunctions.GetXmlStringValue(navigator, "/Settings/POP3/Service" + i + "/Certificate/SerialNumber"); if (!string.IsNullOrEmpty(certificateValue)) { arguments.Certificate = CertHelper.GetCertificateBySerialNumber(certificateLocation, certificateValue); } else { certificateValue = ProxyFunctions.GetXmlStringValue(navigator, "/Settings/POP3/Service" + i + "/Certificate/SubjectName"); if (!string.IsNullOrEmpty(certificateValue)) { arguments.Certificate = CertHelper.GetCertificateBySubjectName(certificateLocation, certificateValue); } } arguments.ExportDirectory = ProxyFunctions.GetXmlStringValue(navigator, "Settings/POP3/Service" + i + "/ExportDirectory"); arguments.LogFile = ProxyFunctions.GetXmlStringValue(navigator, "Settings/POP3/Service" + i + "/LogFile"); string logLevel = ProxyFunctions.GetXmlStringValue(navigator, "Settings/POP3/Service" + i + "/LogLevel"); switch (logLevel.ToUpper()) { case "NONE": arguments.LogLevel = LogLevel.None; break; case "CRITICAL": arguments.LogLevel = LogLevel.Critical; break; case "ERROR": arguments.LogLevel = LogLevel.Error; break; case "RAW": arguments.LogLevel = LogLevel.Raw; break; case "VERBOSE": arguments.LogLevel = LogLevel.Verbose; break; case "WARNING": arguments.LogLevel = LogLevel.Warning; break; case "INFORMATION": default: arguments.LogLevel = LogLevel.Information; break; } arguments.InstanceId = i; arguments.DebugMode = ProxyFunctions.GetXmlBoolValue(navigator, "Settings/POP3/Service" + i + "/Debug"); // Remember the proxy in order to close it when the service stops. arguments.Proxy = new Pop3Proxy(); pop3Proxies.Add(arguments.Proxy); Thread proxyThread = new Thread(new ParameterizedThreadStart(StartProxy)); proxyThread.Name = "OpaqueMail POP3 Proxy"; proxyThread.Start(arguments); } } } catch { // Ignore errors if the XML settings file is malformed. } return(pop3Proxies); }