public async Task Invoke(HttpContext context, IAppBasicAuthService appBasicAuth, IConfigService configService)
        {
            if (context.Request.Path == "/ws")
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    if (!await appBasicAuth.ValidAsync(context.Request))
                    {
                        await context.Response.WriteAsync("basic auth failed .");

                        return;
                    }
                    var appId = context.Request.Headers["appid"];
                    if (string.IsNullOrEmpty(appId))
                    {
                        var appIdSecret = appBasicAuth.GetAppIdSecret(context.Request);
                        appId = appIdSecret.Item1;
                    }
                    WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();

                    var client = new WebsocketClient()
                    {
                        Client            = webSocket,
                        Id                = Guid.NewGuid().ToString(),
                        AppId             = appId,
                        LastHeartbeatTime = DateTime.Now
                    };
                    _websocketCollection.AddClient(client);
                    _logger.LogInformation("Websocket client {0} Added ", client.Id);
                    try
                    {
                        await Handle(context, client, configService);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Handle websocket client {0} err .", client.Id);
                        await _websocketCollection.RemoveClient(client, WebSocketCloseStatus.Empty, ex.Message);

                        await context.Response.WriteAsync("closed");
                    }
                }
                else
                {
                    context.Response.StatusCode = 400;
                }
            }
            else
            {
                await _next(context);
            }
        }
Exemplo n.º 2
0
        public async Task Invoke(HttpContext context, IAppService appService, IConfigService configService)
        {
            if (context.Request.Path == "/ws")
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    var basicAuth = new BasicAuthenticationAttribute(appService);
                    if (!await basicAuth.Valid(context.Request))
                    {
                        await context.Response.WriteAsync("closed");

                        return;
                    }
                    var       appId     = context.Request.Headers["appid"];
                    WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();

                    var client = new WebsocketClient()
                    {
                        Client = webSocket,
                        Id     = Guid.NewGuid().ToString(),
                        AppId  = appId
                    };
                    _websocketCollection.AddClient(client);
                    _logger.LogInformation("Websocket client {0} Added ", client.Id);
                    try
                    {
                        await Handle(context, client, configService);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Handle websocket client {0} err .", client.Id);
                        await _websocketCollection.RemoveClient(client, WebSocketCloseStatus.Empty, ex.Message);

                        await context.Response.WriteAsync("closed");
                    }
                }
                else
                {
                    context.Response.StatusCode = 400;
                }
            }
            else
            {
                await _next(context);
            }
        }
Exemplo n.º 3
0
        public async Task Invoke(HttpContext context, IAppBasicAuthService appBasicAuth, IConfigService configService)
        {
            if (context.Request.Path == "/ws")
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    if (!await appBasicAuth.ValidAsync(context.Request))
                    {
                        context.Response.StatusCode = 401;
                        await context.Response.WriteAsync("basic auth failed .");

                        return;
                    }
                    var appId = context.Request.Headers["appid"];
                    if (string.IsNullOrEmpty(appId))
                    {
                        var appIdSecret = appBasicAuth.GetAppIdSecret(context.Request);
                        appId = appIdSecret.Item1;
                    }
                    context.Request.Query.TryGetValue("client_name", out StringValues name);
                    if (!string.IsNullOrEmpty(name))
                    {
                        name = HttpUtility.UrlDecode(name);
                    }
                    else
                    {
                        _logger.LogInformation("Websocket client request No Name property ");
                    }
                    context.Request.Query.TryGetValue("client_tag", out StringValues tag);
                    if (!string.IsNullOrEmpty(tag))
                    {
                        tag = HttpUtility.UrlDecode(tag);
                    }
                    else
                    {
                        _logger.LogInformation("Websocket client request No TAG property ");
                    }
                    WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();

                    var clientIp = GetRemoteIp(context.Request);
                    var client   = new WebsocketClient()
                    {
                        Client            = webSocket,
                        Id                = Guid.NewGuid().ToString(),
                        AppId             = appId,
                        LastHeartbeatTime = DateTime.Now,
                        Name              = name,
                        Tag               = tag,
                        Ip                = clientIp.ToString()
                    };
                    _websocketCollection.AddClient(client);
                    _logger.LogInformation("Websocket client {0} Added ", client.Id);

                    try
                    {
                        await Handle(context, client, configService);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Handle websocket client {0} err .", client.Id);
                        await _websocketCollection.RemoveClient(client, WebSocketCloseStatus.Empty, ex.Message);

                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("closed");
                    }
                }
                else
                {
                    context.Response.StatusCode = 400;
                }
            }
            else
            {
                await _next(context);
            }
        }