示例#1
0
        private static void Main()
        {
            XmlConfigurator.Configure();
            try
            {
                var settings = SimpleSettings.Create("settings");

                CryptoConfig.AddAlgorithm(typeof(RHHE), RHHE.Name);

                var sleepPeriod = int.Parse(settings.GetValue("sleep"));
                var ttl         = int.Parse(settings.GetValue("ttl"));

                var server   = PrepareServer(settings);
                var wsServer = PrepareWsServer(settings);

                SecretHolder.Init(settings.GetValue("secret"));
                CredentialsHolder.Init(settings.GetValue("credentials"), sleepPeriod);
                PointHolder.Init(settings.GetValue("points"), sleepPeriod, ttl, (point, msg) => wsServer.BroadcastAsync(point, msg, CancellationToken.None));

                Task
                .WhenAll(
                    server.AcceptLoopAsync(CancellationToken.None),
                    wsServer.AcceptLoopAsync(CancellationToken.None)
                    )
                .Wait();
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
                Log.Fatal("Unexpected exception", ex);
                Environment.Exit(ex.HResult == 0 ? ex.HResult : -1);
            }
        }
        public override async Task Handle(HttpListenerContext context)
        {
            var credentials = await context.GetCredentialsAsync().ConfigureAwait(false);

            if (credentials == null)
            {
                return;
            }

            var user = CredentialsHolder.GetUser(credentials.Login);
            var hash = CredentialsHolder.GetPassHashed(credentials.Login, credentials.Password);

            if (user == null)
            {
                if (!CredentialsHolder.AddUser(credentials.Login, hash))
                {
                    context.Response.StatusCode = (int)HttpStatusCode.Conflict;
                    return;
                }
                context.SetLoginCookie(credentials.Login);
                return;
            }

            if (user.Hash != hash)
            {
                context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return;
            }

            context.SetLoginCookie(user.Login);
        }