private void HeyListen(HttpAuthenticationService httpAuth)
 {
     using (HttpListener listener = new HttpListener())
     {
         listener.Prefixes.Add(Config.HttpPrefix);
         listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
         listener.Start();
         while (true)
         {
             HttpListenerContext       context  = listener.GetContext();
             HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity;
             if (identity == null || !httpAuth.Authenticate(identity.Name, identity.Password))
             {
                 HttpListenerResponse authResponse = context.Response;
                 authResponse.AddHeader("WWW-Authenticate", "Basic");
                 authResponse.StatusCode = (int)HttpStatusCode.Unauthorized;
                 using (StreamWriter writer = new StreamWriter(authResponse.OutputStream))
                 {
                     writer.Write("NO!");
                     writer.Flush();
                 }
                 continue;
             }
             var userId  = Helper.GetUserId(identity.Name);
             var guildId = Helper.GetGuildId(identity.Name);
             HttpListenerRequest request = context.Request;
             string command;
             using (StreamReader reader = new StreamReader(request.InputStream))
             {
                 command = reader.ReadToEnd();
             }
             HttpListenerResponse response = context.Response;
             string responseString;
             try
             {
                 responseString = HandleHttpCommand(command, userId, guildId);
             }
             catch (Exception ex)
             {
                 responseString      = ex.ToString();
                 response.StatusCode = (int)HttpStatusCode.InternalServerError;
             }
             using (StreamWriter writer = new StreamWriter(response.OutputStream))
             {
                 writer.Write(responseString);
                 writer.Flush();
             }
         }
     }
 }
Exemple #2
0
        static void Main()
        {
            IAuthenticationService authService = new HttpAuthenticationService(
                "http://localhost:8888/async/auth/AuthenticateUserAsync");
            IStockPortfolioService portfolioService = new HttpPortfolioService(
                "http://localhost:8888/async/portfolio/GetPortfolioAsync");
            IStockPriceService priceService = new HttpPriceService(
                "http://localhost:8888/async/price/LookupPriceAsync");

            //IAuthenticationService authService = new SimpleAuthenticationService();
            //IStockPortfolioService portfolioService = new SimpleStockPortfolioService();
            //IStockPriceService priceService = new SimpleStockPriceService();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new StockViewerForm(authService, portfolioService, priceService));
        }
        private async Task MainAsync()
        {
            _client.Log += Logger;

            var httpAuth = new HttpAuthenticationService(Helper.GetAppDataPath("auth.json"));

            await Init(httpAuth);

            await _client.LoginAsync(TokenType.Bot, token);

            await _client.StartAsync();

            HeyListen(httpAuth);

            // Wait infinitely so your bot actually stays connected.
            await Task.Delay(-1);
        }
        private async Task Init(HttpAuthenticationService httpAuth)
        {
            Cache = new CacheService();
            _map.AddSingleton(Cache);
            AudioClients = new AudioClientService();
            _map.AddSingleton(AudioClients);
            var gcmService = new GuildConfigManagerService();

            _map.AddSingleton(gcmService);
            _map.AddSingleton(httpAuth);

            Config = new ConfigService(Helper.GetAppDataPath("config.json"));
            Cleanup(Config.FileCachePath);
            token = Config.BotToken;
            _map.AddSingleton(Config);
            Queues = new QueueManagerService(Config, gcmService);
            _map.AddSingleton(Queues);

            _services = _map.BuildServiceProvider();

            await _commands.AddModulesAsync(Assembly.GetEntryAssembly());

            _client.MessageReceived += HandleCommandAsync;
        }