Exemplo n.º 1
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

            var service = new TDSProxyService();

            service.Start(args);
            Console.Write("Press ESC to end...");
            while (Console.ReadKey(false).Key != ConsoleKey.Escape)
            {
            }
            service.Stop();
        }
Exemplo n.º 2
0
        public TDSListener(TDSProxyService service, Configuration.ListenerElement configuration)
        {
            var insideAddresses = Dns.GetHostAddresses(configuration.ForwardToHost);

            if (0 == insideAddresses.Length)
            {
                log.ErrorFormat("Unable to resolve forwardToHost=\"{0}\" for listener {1}", configuration.ForwardToHost, configuration.Name);
                _stopped = true;
                return;
            }
            ForwardTo = new IPEndPoint(insideAddresses.First(), configuration.ForwardToPort);

            _service = service;

            var bindToEP = new IPEndPoint(configuration.BindToAddress ?? IPAddress.Any, configuration.ListenOnPort);

            try
            {
                var catalog = new AssemblyCatalog(configuration.AuthenticatorDll);
                _mefContainer = new CompositionContainer(catalog);
                var exports = _mefContainer.GetExports <IAuthenticator>().ToList();
                var export  = exports.FirstOrDefault(a => a.Value.GetType().FullName == configuration.AuthenticatorClass);
                if (null == export)
                {
                    log.ErrorFormat(
                        "Found dll {0} but not authenticator implementation {1} (DLL exported: {2})",
                        configuration.AuthenticatorDll,
                        configuration.AuthenticatorClass,
                        string.Join("; ", exports.Select(exp => exp.Value.GetType().FullName)));
                    Dispose();
                    return;
                }
                _export       = export;
                Authenticator = _export.Value;
                _mefContainer.ReleaseExports(exports.Where(e => e != _export));
            }
            catch (CompositionException ce)
            {
                log.Error(
                    "Failed to find an authenticator. Composition errors:\r\n\t" +
                    string.Join("\r\n\t", ce.Errors.Select(err => "Element: " + err.Element.DisplayName + ", Error: " + err.Description)),
                    ce);
                Dispose();
                return;
            }
            catch (Exception e)
            {
                log.Error("Failed to find an authenticator", e);
                Dispose();
                return;
            }

            try
            {
                log.DebugFormat("Opening SSL certificate store {0}.{1}", configuration.SslCertStoreLocation, configuration.SslCertStoreName);
                var store = new X509Store(configuration.SslCertStoreName, configuration.SslCertStoreLocation);
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                var matching = store.Certificates.Find(X509FindType.FindByThumbprint, configuration.SslCertSubjectThumbprint, false);
                if (0 == matching.Count)
                {
                    log.ErrorFormat(
                        "Failed to find SSL certification with thumbprint '{0}' in location {1}, store {2}.",
                        configuration.SslCertSubjectThumbprint,
                        configuration.SslCertStoreLocation,
                        configuration.SslCertStoreName);
                    Dispose();
                    return;
                }
                Certificate = matching[0];
            }
            catch (Exception e)
            {
                log.Error("Failed to load SSL certificate", e);
                Dispose();
                return;
            }

            _tcpListener = new TcpListener(bindToEP);
            _tcpListener.Start();
            _tcpListener.BeginAcceptTcpClient(AcceptConnection, _tcpListener);

            _service.AddListener(this);

            log.InfoFormat(
                "Listening on {0} and forwarding to {1} (SSL cert DN {2}; serial {3}; authenticator {4})",
                bindToEP,
                ForwardTo,
                Certificate.Subject,
                Certificate.GetSerialNumberString(),
                Authenticator.GetType().FullName);
        }
Exemplo n.º 3
0
        public TDSListener(TDSProxyService service, ListenerElement configuration)
        {
            var insideAddresses = Dns.GetHostAddresses(configuration.ForwardToHost);

            if (0 == insideAddresses.Length)
            {
                log.ErrorFormat("Unable to resolve forwardToHost=\"{0}\" for listener {1}", configuration.ForwardToHost, configuration.Name);
                _stopped = true;
                return;
            }
            ForwardTo = new IPEndPoint(insideAddresses.First(), configuration.ForwardToPort);

            _service = service;

            var bindToEP = new IPEndPoint(configuration.BindToAddress ?? IPAddress.Any, configuration.ListenOnPort);

            try
            {
                var catalog = new AggregateCatalog(from AuthenticatorElement a in configuration.Authenticators
                                                   select new AssemblyCatalog(a.Dll));
                _mefContainer = new CompositionContainer(catalog);

                _authenticators = _mefContainer.GetExports <IAuthenticator>().ToArray();
                if (!_authenticators.Any())
                {
                    throw new InvalidOperationException("No authenticators");
                }
            }
            catch (CompositionException ce)
            {
                log.Error(
                    "Failed to find an authenticator. Composition errors:\r\n\t" +
                    string.Join("\r\n\t", ce.Errors.Select(err => "Element: " + err.Element.DisplayName + ", Error: " + err.Description)),
                    ce);
                Dispose();
                return;
            }
            catch (Exception e)
            {
                log.Error("Failed to find an authenticator", e);
                Dispose();
                return;
            }

            try
            {
                log.DebugFormat("Opening SSL certificate store {0}.{1}", configuration.SslCertStoreLocation, configuration.SslCertStoreName);
                var store = new X509Store(configuration.SslCertStoreName, configuration.SslCertStoreLocation);
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                var matching = store.Certificates.Find(X509FindType.FindByThumbprint, configuration.SslCertSubjectThumbprint, false);
                if (0 == matching.Count)
                {
                    log.ErrorFormat(
                        "Failed to find SSL certification with thumbprint '{0}' in location {1}, store {2}.",
                        configuration.SslCertSubjectThumbprint,
                        configuration.SslCertStoreLocation,
                        configuration.SslCertStoreName);
                    Dispose();
                    return;
                }
                Certificate = matching[0];
            }
            catch (Exception e)
            {
                log.Error("Failed to load SSL certificate", e);
                Dispose();
                return;
            }

            _tcpListener = new TcpListener(bindToEP);
            _tcpListener.Start();
            _tcpListener.BeginAcceptTcpClient(AcceptConnection, _tcpListener);

            _service.AddListener(this);

            log.InfoFormat(
                "Listening on {0} and forwarding to {1} (SSL cert DN {2}; expires {5} serial {3}; authenticators {4})",
                bindToEP,
                ForwardTo,
                Certificate.Subject,
                Certificate.GetSerialNumberString(),
                string.Join(", ", from a in Authenticators select a.GetType().FullName),
                Certificate.GetExpirationDateString());
        }
Exemplo n.º 4
0
        public TDSListener(TDSProxyService service, ListenerElement configuration)
        {
            var insideAddresses = Dns.GetHostAddresses(configuration.ForwardToHost);

            if (0 == insideAddresses.Length)
            {
                log.ErrorFormat("Unable to resolve forwardToHost=\"{0}\" for listener {1}", configuration.ForwardToHost, configuration.Name);
                _stopped = true;
                return;
            }
            ForwardTo = new IPEndPoint(insideAddresses.First(), configuration.ForwardToPort);

            _service = service;

            var bindToEP = new IPEndPoint(configuration.BindToAddress ?? IPAddress.Any, configuration.ListenOnPort);

            try
            {
                var catalog = new AggregateCatalog(from AuthenticatorElement a in configuration.Authenticators
                                                   select new AssemblyCatalog(a.Dll));
                _mefContainer = new CompositionContainer(catalog);

                var  allExports     = _mefContainer.GetExports <IAuthenticator>().ToDictionary(a => a.GetType().GetGenericArguments()[0].FullName);
                var  authenticators = new Lazy <IAuthenticator> [configuration.Authenticators.Count];
                bool die            = false;
                var  used           = new List <Lazy <IAuthenticator> >();
                for (int i = 0; i < configuration.Authenticators.Count; i++)
                {
                    var a = configuration.Authenticators[i];
                    if (!allExports.TryGetValue(a.Class, out var export))
                    {
                        log.ErrorFormat(
                            "For authenticator {0} found dll {1} but not class {2} (exports in catalog: {3})",
                            a.Name,
                            a.Dll,
                            a.Class,
                            string.Join("; ", allExports.Keys));
                        die = true;
                    }

                    used.Add(export);
                    authenticators[i] = export;
                }

                if (die)
                {
                    Dispose();
                    return;
                }

                _authenticators = authenticators;
                _mefContainer.ReleaseExports(allExports.Values.Except(used));
            }
            catch (CompositionException ce)
            {
                log.Error(
                    "Failed to find an authenticator. Composition errors:\r\n\t" +
                    string.Join("\r\n\t", ce.Errors.Select(err => "Element: " + err.Element.DisplayName + ", Error: " + err.Description)),
                    ce);
                Dispose();
                return;
            }
            catch (Exception e)
            {
                log.Error("Failed to find an authenticator", e);
                Dispose();
                return;
            }

            try
            {
                log.DebugFormat("Opening SSL certificate store {0}.{1}", configuration.SslCertStoreLocation, configuration.SslCertStoreName);
                var store = new X509Store(configuration.SslCertStoreName, configuration.SslCertStoreLocation);
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                var matching = store.Certificates.Find(X509FindType.FindByThumbprint, configuration.SslCertSubjectThumbprint, false);
                if (0 == matching.Count)
                {
                    log.ErrorFormat(
                        "Failed to find SSL certification with thumbprint '{0}' in location {1}, store {2}.",
                        configuration.SslCertSubjectThumbprint,
                        configuration.SslCertStoreLocation,
                        configuration.SslCertStoreName);
                    Dispose();
                    return;
                }
                Certificate = matching[0];
            }
            catch (Exception e)
            {
                log.Error("Failed to load SSL certificate", e);
                Dispose();
                return;
            }

            _tcpListener = new TcpListener(bindToEP);
            _tcpListener.Start();
            _tcpListener.BeginAcceptTcpClient(AcceptConnection, _tcpListener);

            _service.AddListener(this);

            log.InfoFormat(
                "Listening on {0} and forwarding to {1} (SSL cert DN {2}; expires {5} serial {3}; authenticators {4})",
                bindToEP,
                ForwardTo,
                Certificate.Subject,
                Certificate.GetSerialNumberString(),
                string.Join(", ", from a in Authenticators select a.GetType().FullName),
                Certificate.GetExpirationDateString());
        }