public void TestIsBannedFalseWhenNoBannedPlayers() { IBanManager banManager = CreateBanManager(); bool isBanned = banManager.IsBanned(IPAddress.Parse("127.0.0.1")); Assert.IsFalse(isBanned); }
private void OnClientConnected(Tcp con, IPEndPoint endpointConfig, Loop loop) { try { var remoteEndPoint = con.GetPeerEndPoint(); // get rid of banned clients as early as possible if (banManager?.IsBanned(remoteEndPoint.Address) == true) { logger.Debug(() => $"[{LogCat}] Disconnecting banned ip {remoteEndPoint.Address}"); con.Dispose(); return; } var connectionId = CorrelationIdGenerator.GetNextId(); logger.Debug(() => $"[{LogCat}] Accepting connection [{connectionId}] from {remoteEndPoint.Address}:{remoteEndPoint.Port}"); // setup client connection con.KeepAlive(true, 1); // setup client var client = new StratumClient(); client.Init(loop, con, ctx, clock, endpointConfig, connectionId, data => OnReceive(client, data), () => OnReceiveComplete(client), ex => OnReceiveError(client, ex)); // register client lock (clients) { clients[connectionId] = client; } OnConnect(client); } catch (Exception ex) { logger.Error(ex, () => nameof(OnClientConnected)); } }
public override bool IsBanned(Socket socket) { if (socket == null) // we should have a valid socket data. { return(true); // else just behave the client as banned. } var endpoint = (IPEndPoint)socket.RemoteEndPoint; // get the remote endpoint for socket. if (endpoint == null || endpoint.Address == null) // if we don't have an endpoint information, basically we can't determine the ip miner { return(false); // in case, we just allow him to get connected as we can ban him later based on his behaviours. } return(_banManager.IsBanned(endpoint.Address)); }
/// <summary> /// Called after an inbound message has been received but before the message is dispatched to the intended operation. /// </summary> /// <param name="request">The request message.</param> /// <param name="channel">The incoming channel.</param> /// <param name="instanceContext">The current service instance.</param> /// <returns> /// The object used to correlate state. This object is passed back in the <see cref="M:System.ServiceModel.Dispatcher.IDispatchMessageInspector.BeforeSendReply(System.ServiceModel.Channels.Message@,System.Object)"/> method. /// </returns> public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { // RemoteEndpointMessageProperty new in 3.5 allows us to get the remote endpoint address. RemoteEndpointMessageProperty remoteEndpoint = request.Properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; if (remoteEndpoint != null) { // The address is a string so we have to parse to get as a number IPAddress address = IPAddress.Parse(remoteEndpoint.Address); // If ip address is denied clear the request mesage so service method does not get execute if (_verifier.IsBanned(address)) { Log.Default.WriteLine(LogLevels.Warning, "Banned player {0} tried to connect", address); request = null; object result = (channel.LocalAddress.Uri.Scheme.Equals(Uri.UriSchemeHttp) || channel.LocalAddress.Uri.Scheme.Equals(Uri.UriSchemeHttps)) ? HttpAccessDenied : AccessDenied; return(result); } // TODO //else //{ // // Check SPAM // ITetriNETCallback callback = OperationContext.Current.GetCallbackChannel<ITetriNETCallback>(); // IPlayer player = _playerManager[callback]; // if (player != null) // { // TimeSpan timeSpan = DateTime.Now - player.LastActionFromClient; // //Log.Default.WriteLine(LogLevels.Debug, "DELAY BETWEEN LAST MSG AND NOW:{0} | {1}", timeSpan.TotalMilliseconds, player.LastActionFromClient); // } //} } return(null); }
private void OnClientConnected(Tcp con, IPEndPoint endpointConfig, Loop loop) { try { var remoteEndPoint = con.GetPeerEndPoint(); // get rid of banned clients as early as possible if (banManager?.IsBanned(remoteEndPoint.Address) == true) { logger.Debug(() => $"[{LogCat}] Disconnecting banned ip {remoteEndPoint.Address}"); con.Dispose(); return; } var connectionId = CorrelationIdGenerator.GetNextId(); logger.Debug(() => $"[{LogCat}] Accepting connection [{connectionId}] from {remoteEndPoint.Address}:{remoteEndPoint.Port}"); // setup client var client = new StratumClient <TClientContext>(); client.Init(loop, con, ctx, endpointConfig, connectionId); // request subscription var sub = client.Received .Select(data => Observable.FromAsync(() => Task.Run(() => // get off of LibUV event-loop-thread immediately { // boot pre-connected clients if (banManager?.IsBanned(client.RemoteEndpoint.Address) == true) { logger.Info(() => $"[{LogCat}] [{connectionId}] Disconnecting banned client @ {remoteEndPoint.Address}"); DisconnectClient(client); data.Dispose(); return(Unit.Default); } logger.Trace(() => $"[{LogCat}] [{client.ConnectionId}] Received request data: {StratumClient<TClientContext>.Encoding.GetString(data.Array, 0, data.Size)}"); // parse request var request = ParseRequest(data); logger.Debug(() => $"[{LogCat}] [{client.ConnectionId}] Dispatching request '{request.Method}' [{request.Id}]"); try { // dispatch request OnRequestAsync(client, new Timestamped <JsonRpcRequest>(request, clock.UtcNow)).Wait(); } catch (Exception ex) { logger.Error(ex, () => $"[{LogCat}] [{client.ConnectionId}] Error processing request {request.Method} [{request.Id}]"); } return(Unit.Default); }))) .Concat() .Subscribe(_ => { }, ex => OnReceiveError(client, ex), () => OnReceiveComplete(client)); // ensure subscription is disposed on loop thread var disposer = loop.CreateAsync((handle) => { sub.Dispose(); handle.Dispose(); }); client.Subscription = Disposable.Create(() => { disposer.Send(); }); // register client lock (clients) { clients[connectionId] = client; } OnConnect(client); } catch (Exception ex) { logger.Error(ex, () => nameof(OnClientConnected)); } }
private void OnClientConnected(Tcp con, IPEndPoint endpointConfig, Loop loop) { try { var remoteEndPoint = con.GetPeerEndPoint(); // get rid of banned clients as early as possible if (banManager?.IsBanned(remoteEndPoint.Address) == true) { logger.Debug(() => $"[{LogCat}] Disconnecting banned ip {remoteEndPoint.Address}"); con.Dispose(); return; } var connectionId = CorrelationIdGenerator.GetNextId(); logger.Debug(() => $"[{LogCat}] Accepting connection [{connectionId}] from {remoteEndPoint.Address}:{remoteEndPoint.Port}"); // setup client var client = new StratumClient <TClientContext>(); client.Init(loop, con, ctx, endpointConfig, connectionId); var sub = client.Received.Subscribe(data => { // get off of LibUV event-loop-thread immediately Task.Run(() => { using (data) { JsonRpcRequest request = null; try { // boot pre-connected clients if (banManager?.IsBanned(client.RemoteEndpoint.Address) == true) { logger.Info(() => $"[{LogCat}] [{connectionId}] Disconnecting banned client @ {remoteEndPoint.Address}"); DisconnectClient(client); return; } // de-serialize logger.Trace(() => $"[{LogCat}] [{client.ConnectionId}] Received request data: {StratumClient<TClientContext>.Encoding.GetString(data.Array, 0, data.Size)}"); request = DeserializeRequest(data); if (request != null) { // dispatch logger.Debug(() => $"[{LogCat}] [{client.ConnectionId}] Dispatching request '{request.Method}' [{request.Id}]"); OnRequestAsync(client, new Timestamped <JsonRpcRequest>(request, clock.UtcNow)).Wait(); } else { logger.Trace(() => $"[{LogCat}] [{client.ConnectionId}] Unable to deserialize request"); } } catch (JsonReaderException jsonEx) { // junk received (no valid json) logger.Error(() => $"[{LogCat}] [{client.ConnectionId}] Connection json error state: {jsonEx.Message}"); if (clusterConfig.Banning?.BanOnJunkReceive.HasValue == false || clusterConfig.Banning?.BanOnJunkReceive == true) { logger.Info(() => $"[{LogCat}] [{client.ConnectionId}] Banning client for sending junk"); banManager?.Ban(client.RemoteEndpoint.Address, TimeSpan.FromMinutes(30)); } } catch (Exception ex) { if (request != null) { logger.Error(ex, () => $"[{LogCat}] [{client.ConnectionId}] Error processing request {request.Method} [{request.Id}]"); } } } }); }, ex => OnReceiveError(client, ex), () => OnReceiveComplete(client)); // ensure subscription is disposed on loop thread var disposer = loop.CreateAsync((handle) => { sub.Dispose(); handle.Dispose(); }); client.Subscription = Disposable.Create(() => { disposer.Send(); }); // register client lock (clients) { clients[connectionId] = client; } OnConnect(client); } catch (Exception ex) { logger.Error(ex, () => nameof(OnClientConnected)); } }
private void OnClientConnected(Tcp con, IPEndPoint endpointConfig, Loop loop) { try { var remoteEndPoint = con.GetPeerEndPoint(); // get rid of banned clients as early as possible if (banManager?.IsBanned(remoteEndPoint.Address) == true) { logger.Trace(() => $"[{LogCat}] Disconnecting banned ip {remoteEndPoint.Address}"); con.Dispose(); return; } var connectionId = CorrelationIdGenerator.GetNextId(); logger.Trace(() => $"[{LogCat}] Accepting connection [{connectionId}] from {remoteEndPoint.Address}:{remoteEndPoint.Port}"); // setup client var client = new StratumClient <TClientContext>(); client.Init(con, ctx, endpointConfig, connectionId); // request subscription var sub = client.Requests .ObserveOn(TaskPoolScheduler.Default) // WARN: never add .SubscribeOn here (must sub/unsub on UV event-loop thread) .Subscribe(async tsRequest => { var request = tsRequest.Value; logger.Debug(() => $"[{LogCat}] [{client.ConnectionId}] Received request {request.Method} [{request.Id}]"); try { // boot pre-connected clients if (banManager?.IsBanned(client.RemoteEndpoint.Address) == true) { logger.Trace(() => $"[{LogCat}] [{connectionId}] Disconnecting banned client @ {remoteEndPoint.Address}"); DisconnectClient(client); return; } await OnRequestAsync(client, tsRequest); } catch (Exception ex) { logger.Error(ex, () => $"Error handling request: {request.Method}"); } }, ex => OnReceiveError(client, ex), () => OnReceiveComplete(client)); // ensure subscription is disposed on loop thread var disposer = loop.CreateAsync((handle) => { sub.Dispose(); handle.Dispose(); }); client.Subscription = Disposable.Create(() => { disposer.Send(); }); // register client lock (clients) { clients[connectionId] = client; } OnConnect(client); } catch (Exception ex) { logger.Error(ex, () => nameof(OnClientConnected)); } }
private void OnClientConnected(Tcp con, IPEndPoint endpointConfig, Loop loop) { try { var remoteEndPoint = con.GetPeerEndPoint(); // get rid of banned clients as early as possible if (banManager?.IsBanned(remoteEndPoint.Address) == true) { logger.Trace(() => $"[{LogCat}] Disconnecting banned ip {remoteEndPoint.Address}"); con.Dispose(); return; } var connectionId = CorrelationIdGenerator.GetNextId(); logger.Trace(() => $"[{LogCat}] Accepting connection [{connectionId}] from {remoteEndPoint.Address}:{remoteEndPoint.Port}"); // setup client var client = new StratumClient <TClientContext>(); client.Init(loop, con, ctx, endpointConfig, connectionId); // request subscription var sub = client.Requests .Do(x => logger.Trace(() => $"[{LogCat}] [{client.ConnectionId}] Received request {x.Value.Method} [{x.Value.Id}]")) .Select(tsRequest => Observable.FromAsync(() => Task.Run(() => // get off of LibUV event-loop-thread immediately { var request = tsRequest.Value; logger.Trace(() => $"[{LogCat}] [{client.ConnectionId}] Dispatching request {request.Method} [{request.Id}]"); try { // boot pre-connected clients if (banManager?.IsBanned(client.RemoteEndpoint.Address) == true) { logger.Trace(() => $"[{LogCat}] [{connectionId}] Disconnecting banned client @ {remoteEndPoint.Address}"); DisconnectClient(client); return; } OnRequestAsync(client, tsRequest).Wait(); } catch (Exception ex) { logger.Error(ex, () => $"Error handling request: {request.Method}"); } }))) .Concat() .Subscribe(_ => { }, ex => OnReceiveError(client, ex), () => OnReceiveComplete(client)); // ensure subscription is disposed on loop thread var disposer = loop.CreateAsync((handle) => { sub.Dispose(); handle.Dispose(); }); client.Subscription = Disposable.Create(() => { disposer.Send(); }); // register client lock (clients) { clients[connectionId] = client; } OnConnect(client); } catch (Exception ex) { logger.Error(ex, () => nameof(OnClientConnected)); } }