Ejemplo n.º 1
0
        private async Task <Stream> GetStream(Stream sourceStream, ConnectionMode connectionMode, bool isServer, string targetHost)
        {
            switch (connectionMode)
            {
            case ConnectionMode.TCP:
                return(sourceStream);

            case ConnectionMode.SSL:
                var sslStream = new SslStream(sourceStream);
                if (isServer)
                {
                    var cert = ProxyEngine.FindCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, _certificate);
                    Log.DebugFormat("Authenticating as server using certificate: {0}, Thumbprint={1}.", cert.SubjectName.Name, cert.Thumbprint);
                    await sslStream.AuthenticateAsServerAsync(cert, false, SslProtocols.Tls, false);

                    Log.Debug("Completed authentication as server.");
                }
                else
                {
                    await sslStream.AuthenticateAsClientAsync(targetHost);
                }
                return(sslStream);
            }
            return(null);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += ExceptionHandler;

            var configPath = Path.GetFullPath(Path.Combine(AssemblyDirectory, "log4net.config"));

            XmlConfigurator.ConfigureAndWatch(new FileInfo(configPath));

            var options = new Options();

            if (!CommandLine.Parser.Default.ParseArguments(args, options))
            {
                var sb = new StringBuilder();
                sb.AppendLine("Invalid command line:");
                foreach (var error in options.LastParserState.Errors)
                {
                    sb.AppendFormat("\t{0}: ", error.BadOption.LongName);
                    var errorList = new List <string>();
                    if (error.ViolatesFormat)
                    {
                        errorList.Add("invalid format");
                    }
                    if (error.ViolatesMutualExclusiveness)
                    {
                        errorList.Add("violates mutual exclusiveness");
                    }
                    if (error.ViolatesRequired)
                    {
                        errorList.Add("missing required option");
                    }
                    sb.AppendLine(String.Join(", ", errorList));
                }
                Log.Error(sb);
                Console.WriteLine(options.GetUsage());
                return;
            }

            var proxyEngine = new ProxyEngine(options);

            if (options.Console)
            {
                Log.Info("Running in console mode.");
                proxyEngine.Start();
                Console.WriteLine("Press enter to exit...");
                Console.ReadLine();
                proxyEngine.Stop();
            }
            else
            {
                Log.Info("Running in service mode.");
                var services = new ServiceBase[] { new ProxyEngineService(proxyEngine) };
                ServiceBase.Run(services);
            }
        }
Ejemplo n.º 3
0
        public ProxyEngineService(ProxyEngine proxyEngine)
        {
            _proxyEngine = proxyEngine;

            InitializeComponent();
        }