コード例 #1
0
        private static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureWebHost((options) =>
        {
            options.UseStartup <Startup>();
            options.UseKestrel();
            options.ConfigureKestrel(options =>
            {
                PiraeusConfig config = GetPiraeusConfig();
                options.Limits.MaxConcurrentConnections         = config.MaxConnections;
                options.Limits.MaxConcurrentUpgradedConnections = config.MaxConnections;
                options.Limits.MaxRequestBodySize     = config.MaxBufferSize;
                options.Limits.MinRequestBodyDataRate =
                    new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10));
                options.Limits.MinResponseDataRate =
                    new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10));

                if (!string.IsNullOrEmpty(config.ServerCertificateFilename))
                {
                    Console.WriteLine("Port for cert with filename");
                    options.ListenAnyIP(config.GetPorts()[0], (a) => a.UseHttps(config.ServerCertificateFilename, config.ServerCertificatePassword));
                }
                else if (!string.IsNullOrEmpty(config.ServerCertificateStore))
                {
                    Console.WriteLine("Port for cert with store");
                    X509Certificate2 cert = config.GetServerCerticate();
                    options.ListenAnyIP(config.GetPorts()[0], (a) => a.UseHttps(cert));
                }
                else
                {
                    Console.WriteLine("Hard coded port 8081");
                    options.ListenAnyIP(8081);
                }
            });
        });
コード例 #2
0
ファイル: TcpGatewayService.cs プロジェクト: lulzzz/piraeus-1
        public void Init(bool dockerized)
        {
            listeners = new Dictionary <int, TcpServerListener>();
            sources   = new Dictionary <int, CancellationTokenSource>();

            int[] ports = config.GetPorts();

            foreach (var port in ports)
            {
                sources.Add(port, new CancellationTokenSource());
            }

            hostname = !dockerized ? "localhost" : Dns.GetHostName();
            //string hostname = config.Hostname == null ? "localhost" : config.Hostname;

            int index = 0;

            while (index < ports.Length)
            {
                listeners.Add(ports[index], new TcpServerListener(new IPEndPoint(GetIPAddress(hostname), ports[index]), config, this.logger, sources[ports[index]].Token));
                index++;
            }

            KeyValuePair <int, TcpServerListener>[] tcpKvps = listeners.ToArray();

            foreach (var item in tcpKvps)
            {
                item.Value.OnError += Listener_OnError;
                item.Value.StartAsync().LogExceptions();
                logger?.LogInformation($"TCP listener started on port {item.Key}");
            }

            logger?.LogInformation("TCP server started.");
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: lulzzz/piraeus-1
        private static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseStartup <Startup>()
        .UseKestrel(options =>
        {
            PiraeusConfig config = GetPiraeusConfig();
            options.Limits.MaxConcurrentConnections         = config.MaxConnections;
            options.Limits.MaxConcurrentUpgradedConnections = config.MaxConnections;
            options.Limits.MaxRequestBodySize     = config.MaxBufferSize;
            options.Limits.MinRequestBodyDataRate =
                new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10));
            options.Limits.MinResponseDataRate =
                new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10));

            X509Certificate2 cert = config.GetServerCerticate();
            int[] ports           = config.GetPorts();

            foreach (int port in ports)
            {
                if (cert != null)
                {
                    options.ListenAnyIP(port, (a) => a.UseHttps(cert));
                }
                else
                {
                    options.ListenAnyIP(port);
                }
            }
        });
コード例 #4
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            Console.WriteLine("   /$$$$$$$$/$$$$$$ /$$$$$$$         /$$$$$$            /$$");
            Console.WriteLine("  |__  $$__/$$__  $| $$__  $$       /$$__  $$          | $$ ");
            Console.WriteLine(
                "     | $$ | $$  \\__| $$  \\ $$      | $$  \\__/ /$$$$$$ /$$$$$$   /$$$$$$ /$$  /$$  /$$ /$$$$$$ /$$   /$$");
            Console.WriteLine(
                "     | $$ | $$     | $$$$$$$/      | $$ /$$$$|____  $|_  $$_/  /$$__  $| $$ | $$ | $$|____  $| $$  | $$");
            Console.WriteLine(
                "     | $$ | $$     | $$____/       | $$|_  $$ /$$$$$$$ | $$   | $$$$$$$| $$ | $$ | $$ /$$$$$$| $$  | $$");
            Console.WriteLine(
                "     | $$ | $$    $| $$            | $$  \\ $$/$$__  $$ | $$ /$| $$_____| $$ | $$ | $$/$$__  $| $$  | $$");
            Console.WriteLine(
                "     | $$ |  $$$$$$| $$            |  $$$$$$|  $$$$$$$ |  $$$$|  $$$$$$|  $$$$$/$$$$|  $$$$$$|  $$$$$$$");
            Console.WriteLine(
                "     |__/  \\______/|__/             \\______/ \\_______/  \\___/  \\_______/\\_____/\\___/ \\_______/\\____  $$");
            Console.WriteLine(
                "                                                                                              /$$  | $$");
            Console.WriteLine(
                "                                                                                             |  $$$$$$/");
            Console.WriteLine(
                "                                                                                              \\______/ ");

            int[] ports = config.GetPorts();

            foreach (var port in ports)
            {
                sources.Add(port, new CancellationTokenSource());
            }

#if DEBUG
            hostname = "localhost";
#else
            hostname = Dns.GetHostName();
#endif

            int index = 0;
            while (index < ports.Length)
            {
                listeners.Add(ports[index],
                              new TcpServerListener(new IPEndPoint(GetIPAddress(hostname), ports[index]), config, orleansConfig,
                                                    logger, sources[ports[index]].Token));
                logger?.LogInformation($"TCP listener added to port {ports[index]}");
                index++;
            }

            KeyValuePair <int, TcpServerListener>[] tcpKvps = listeners.ToArray();

            foreach (var item in tcpKvps)
            {
                item.Value.OnError += Listener_OnError;
                item.Value.StartAsync().LogExceptions(logger);
                logger?.LogInformation($"TCP listener started on port {item.Key}");
            }

            logger?.LogInformation("TCP server started.");
            return(Task.CompletedTask);
        }
コード例 #5
0
        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return(Host.CreateDefaultBuilder(args)
                   .ConfigureServices(services => services.AddPiraeusConfiguration())
                   .ConfigureWebHostDefaults(webBuilder =>
            {
                PiraeusConfig config = WebApiHelpers.GetPiraeusConfig();
                webBuilder
                .ConfigureKestrel(options =>
                {
                    options.Limits.MaxConcurrentConnections = config.MaxConnections;
                    options.Limits.MaxConcurrentUpgradedConnections = config.MaxConnections;
                    options.Limits.MaxRequestBodySize = config.MaxBufferSize;
                    options.Limits.MinRequestBodyDataRate =
                        new MinDataRate(100, TimeSpan.FromSeconds(10));
                    options.Limits.MinResponseDataRate =
                        new MinDataRate(100, TimeSpan.FromSeconds(10));
                    X509Certificate2 cert = config.GetServerCerticate();
                    int[] ports = config.GetPorts();

                    foreach (int port in ports)
                    {
                        if (cert != null)
                        {
                            options.ListenAnyIP(port, a => a.UseHttps(cert));
                        }
                        else
                        {
                            IPAddress address = GetIPAddress(Dns.GetHostName());
                            options.Listen(address, port);
                        }
                    }

                    if (!string.IsNullOrEmpty(config.ServerCertificateFilename))
                    {
                        string[] portStrings = config.Ports.Split(";", StringSplitOptions.RemoveEmptyEntries);

                        foreach (string portString in portStrings)
                        {
                            options.ListenAnyIP(Convert.ToInt32(portString),
                                                a => a.UseHttps(config.ServerCertificateFilename,
                                                                config.ServerCertificatePassword));
                        }
                    }
                });
                webBuilder.UseStartup <Startup>();
            }));
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: lulzzz/piraeus-1
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseStartup <Startup>()
        .UseKestrel(options =>
        {
            PiraeusConfig config = GetPiraeusConfig();
            options.Limits.MaxConcurrentConnections         = config.MaxConnections;
            options.Limits.MaxConcurrentUpgradedConnections = config.MaxConnections;
            options.Limits.MaxRequestBodySize     = config.MaxBufferSize;
            options.Limits.MinRequestBodyDataRate =
                new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10));
            options.Limits.MinResponseDataRate =
                new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10));

            X509Certificate2 cert = config.GetServerCerticate();
            int[] ports           = config.GetPorts();

            foreach (int port in ports)
            {
                if (cert != null)
                {
                    options.ListenAnyIP(port, (a) => a.UseHttps(cert));
                }
                else
                {
                    IPAddress address = GetIPAddress(Dns.GetHostName());
                    options.Listen(address, port);
                    //options.Listen(IPAddress.Loopback, port);
                    //options.ListenAnyIP(port);
                }
            }


            if (!string.IsNullOrEmpty(config.ServerCertificateFilename))
            {
                string[] portStrings = config.Ports.Split(";", StringSplitOptions.RemoveEmptyEntries);

                foreach (var portString in portStrings)
                {
                    options.ListenAnyIP(Convert.ToInt32(portString), (a) => a.UseHttps(config.ServerCertificateFilename, config.ServerCertificatePassword));
                }
            }
            //else
            //{
            //    options.ListenAnyIP(config.Channels.Http.ListenPort, (a) => a.UseHttps(".\\localhost.pfx", "pass@word1"));
            //}
        });
コード例 #7
0
ファイル: Program.cs プロジェクト: lulzzz/piraeus-2
        private static IHostBuilder CreateHostBuilder(string[] args)
        {
            return(Host.CreateDefaultBuilder(args)
                   .ConfigureServices((hostContext, services) =>
            {
                services.AddPiraeusConfiguration(out config);
            })
                   .ConfigureWebHost(options =>
            {
                options.UseStartup <Startup>();
                options.UseKestrel();
                options.ConfigureKestrel(options =>
                {
                    options.Limits.MaxConcurrentConnections = config.MaxConnections;
                    options.Limits.MaxConcurrentUpgradedConnections = config.MaxConnections;
                    options.Limits.MaxRequestBodySize = config.MaxBufferSize;
                    options.Limits.MinRequestBodyDataRate =
                        new MinDataRate(100, TimeSpan.FromSeconds(10));
                    options.Limits.MinResponseDataRate =
                        new MinDataRate(100, TimeSpan.FromSeconds(10));

                    X509Certificate2 cert = config.GetServerCerticate();
                    int[] ports = config.GetPorts();

                    foreach (int port in ports)
                    {
                        if (cert != null)
                        {
                            Console.WriteLine($"Listening on {port} using certificate.");
                            options.ListenAnyIP(port, a => a.UseHttps(cert));
                        }
                        else
                        {
                            Console.WriteLine($"Listening on {port}.");
                            options.ListenAnyIP(port);
                        }
                    }
                });
            }));
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: lulzzz/piraeus-1
        static void Main(string[] args)
        {
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
            listeners = new Dictionary <int, UdpServerListener>();
            sources   = new Dictionary <int, CancellationTokenSource>();

            OrleansConfig orleansConfig = GetOrleansConfig();

            config = GetPiraeusConfig();

            if (!orleansConfig.Dockerized)
            {
                var client = new ClientBuilder();
                client.ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(IPiSystem).Assembly));
                client.UseLocalhostClustering();
                clusterClient = client.Build();
                hostname      = "localhost";
            }
            else
            {
                clusterClient = GetClient(orleansConfig);
                hostname      = Dns.GetHostName();
            }

            Task connectTask = ConnectAsync();

            Task.WhenAll(connectTask);

            int[] ports = config.GetPorts();

            foreach (var port in ports)
            {
                sources.Add(port, new CancellationTokenSource());
            }

            //string hostname = config.Hostname == null ? "localhost" : config.Hostname;

            int index = 0;

            while (index < ports.Length)
            {
                listeners.Add(ports[index], new UdpServerListener(config, new IPEndPoint(GetIPAddress(hostname), ports[index]), sources[ports[index]].Token));
                index++;
            }

            KeyValuePair <int, UdpServerListener>[] udpKvps = listeners.ToArray();

            foreach (var item in udpKvps)
            {
                item.Value.OnError += Listener_OnError;
                Task task = item.Value.StartAsync();
                Task.WhenAll(task);
            }

            done = new ManualResetEventSlim(false);

            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                done.Set();
                eventArgs.Cancel = true;
            };

            Console.WriteLine("UDP Gateway is ready...");
            done.Wait();
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: lulzzz/piraeus-2
        private static IHostBuilder CreateHostBuilder(string[] args)
        {
            return(Host.CreateDefaultBuilder(args)
                   .ConfigureLogging(builder =>
            {
                PiraeusConfig pconfig = GetPiraeusConfig();
                LogLevel logLevel = Enum.Parse <LogLevel>(pconfig.LogLevel);
                var loggers = pconfig.GetLoggerTypes();

                if (loggers.HasFlag(LoggerType.Console))
                {
                    builder.AddConsole();
                }

                if (loggers.HasFlag(LoggerType.Debug))
                {
                    builder.AddDebug();
                }

                if (loggers.HasFlag(LoggerType.AppInsights) && !string.IsNullOrEmpty(pconfig.InstrumentationKey))
                {
                    builder.AddApplicationInsights(pconfig.InstrumentationKey);
                }

                builder.SetMinimumLevel(logLevel);
                builder.Services.AddSingleton <ILog, Logger>();
            })
                   .ConfigureWebHost(options =>
            {
                options.UseStartup <Startup>();
                options.UseKestrel();
                options.ConfigureKestrel(options =>
                {
                    PiraeusConfig config = GetPiraeusConfig();
                    options.Limits.MaxConcurrentConnections = config.MaxConnections;
                    options.Limits.MaxConcurrentUpgradedConnections = config.MaxConnections;
                    options.Limits.MaxRequestBodySize = config.MaxBufferSize;
                    options.Limits.MinRequestBodyDataRate =
                        new MinDataRate(100, TimeSpan.FromSeconds(10));
                    options.Limits.MinResponseDataRate =
                        new MinDataRate(100, TimeSpan.FromSeconds(10));

                    if (!string.IsNullOrEmpty(config.ServerCertificateFilename))
                    {
                        Console.WriteLine("Port for cert with filename");
                        options.ListenAnyIP(config.GetPorts()[0],
                                            a => a.UseHttps(config.ServerCertificateFilename, config.ServerCertificatePassword));
                    }
                    else if (!string.IsNullOrEmpty(config.ServerCertificateStore))
                    {
                        Console.WriteLine("Port for cert with store");
                        X509Certificate2 cert = config.GetServerCerticate();
                        options.ListenAnyIP(config.GetPorts()[0], a => a.UseHttps(cert));
                    }
                    else
                    {
                        Console.WriteLine("Hard coded port 8081");
                        options.ListenAnyIP(8081);
                    }
                });
            }));
        }