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); } }
public ConfigController( IConfigService configService, IAppService appService, IModifyLogService modifyLogService, IRemoteServerNodeProxy remoteServerNodeProxy, IServerNodeService serverNodeService, IAppBasicAuthService appBasicAuthService) { _configService = configService; _appService = appService; _modifyLogService = modifyLogService; _remoteServerNodeProxy = remoteServerNodeProxy; _serverNodeService = serverNodeService; _appBasicAuthService = appBasicAuthService; }
public AppBasicAuthenticationAttribute(IAppBasicAuthService appBasicAuthService) { _appBasicAuthService = appBasicAuthService; }
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); } }