Пример #1
0
 public ProxyAsync(IProxyConnection proxyConnection, Uri url)
 {
     this.asyncinterval  = ProxySettings.GetAsyncInterval();
     this.timeout        = ProxySettings.GetTimeout();
     this.pc             = proxyConnection;
     this.cancelTokenSrc = new CancellationTokenSource();
     this.url            = url;
 }
Пример #2
0
        /// <summary>
        /// Prunes the collections list and initializes new connections
        /// </summary>
        private void PruneConnections(object state)
        {
            try
            {
                if (!_listening)
                {
                    return;
                }

                Queue <IProxyConnection> connectionsToStart = new Queue <IProxyConnection>();

                lock (_lockConnections)
                {
                    IProxyConnection conn;
                    int counter = 0;

                    //first clear dead connections
                    while (counter < _connections.Count)
                    {
                        conn = _connections.Dequeue();
                        if (conn.Closed && _connections.Count >= _connectionLimit)
                        {
                            //call connection stop just in case we have a lingering thread
                            //this should set the _stop flag to true and end any async operations
                            conn.Stop();
                        }
                        else //if the connection is not closed put it back at the end of the queue
                        {
                            _connections.Enqueue(conn);
                            counter++;                             //update the counter to show this connection was processed
                        }
                    }

                    //if the number is still high clear connections that are not busy
                    //find connections that idle and drop them starting with the oldest in the list
                    counter = 0;
                    while (_connections.Count >= _connectionLimit && counter < _connections.Count)
                    {
                        conn = _connections.Dequeue();
                        if (!conn.IsBusy)
                        {
                            conn.Stop();
                            HttpServerConsole.Instance.WriteLine(LogMessageType.Warning,
                                                                 "Max clients exceeded. Idle connection was dropped");
                        }
                        else
                        {
                            //put the connection back to the end of the queue
                            _connections.Enqueue(conn);
                            counter++;
                        }
                    }


                    //lastly if the number is still high forcefully kill the oldest connections until we get below the limit
                    //this should be a rare situation and something may be wrong with the connection, a very slow client
                    while (_connections.Count >= _connectionLimit)
                    {
                        conn = _connections.Dequeue();
                        conn.Stop();
                        HttpServerConsole.Instance.WriteLine(LogMessageType.Error,
                                                             "Max clients exceeded. Older connections were dropped");
                    }

                    //now that we ensured there's room dequeue any pending connections and start them
                    while (_connections.Count < _connectionLimit && _clientQueue.Count > 0)
                    {
                        TcpClientInfo clientInfo = _clientQueue.Dequeue();

                        if (clientInfo.Client.Connected)
                        {
                            // Create the new connection
                            conn = GetConnection(clientInfo);

                            // Add the connection to list of connections
                            _connections.Enqueue(conn);
                            //adding the connection to a separate collection in order to
                            //start it outside the lock
                            connectionsToStart.Enqueue(conn);
                        }
                    }
                }                //end critical section

                //start connections
                while (connectionsToStart.Count > 0 && _listening)
                {
                    IProxyConnection conn = connectionsToStart.Dequeue();
                    conn.Start();
                }
            }
            catch (Exception ex)
            {
                SdkSettings.Instance.Logger.Log(TraceLevel.Error, "BaseProxy.PruneConnections: Caught exception: {0}", ex.ToString());
            }
        }
 public bool TryRemoveConnection(IProxyConnection connection)
 {
     Tuple<int, int> result;
     return EntityIDMapping.TryRemove(connection, out result);
 }
Пример #4
0
        /// <summary>
        ///   Get the minecraft protocol server of a given minecraft server.
        /// </summary>
        /// <param name="proxyConnection"> The connection this server relates to. </param>
        /// <param name="serverEndPoint"> The current version of the Remote server info. </param>
        public void GetServerVersion(IProxyConnection proxyConnection, RemoteServerInfo serverEndPoint)
        {
            var args = new PluginResultEventArgs<RemoteServerInfo>(serverEndPoint, proxyConnection);

            PluginManager.TriggerPlugin.GetServerVersion(args);

            args.EnsureSuccess ();

            if (serverEndPoint.MinecraftVersion == 0)
            {
                //Look up configuration

                ProxyConfigurationSection settings = ProxyConfigurationSection.Settings;

                IEnumerable<ServerElement> serverList =
                    settings.Server.OfType<ServerElement> ().Where(
                        m => m.EndPoint == serverEndPoint.EndPoint.ToString ());

                ServerElement server = serverList.FirstOrDefault ();

                if (server != null) serverEndPoint.MinecraftVersion = server.MinecraftVersion;
                    //Use default
                else serverEndPoint.MinecraftVersion = ProtocolInformation.MaxSupportedServerVersion;
            }
        }
Пример #5
0
        /// <summary>
        ///   Returns true if the online mode is enabled for a specific user or not.
        /// </summary>
        /// <param name="proxyConnection"> The proxy connection which should be checked </param>
        /// <returns> true if the online mode is enabled, otherwise false. </returns>
        public bool OnlineModeEnabled(IProxyConnection proxyConnection)
        {
            var args = new PluginResultEventArgs<bool?>(null, proxyConnection);
            PluginManager.TriggerPlugin.IsOnlineModeEnabled(args);


            if (args.Result == null)
                return OnlineMode;
            return (bool) args.Result;
        }
Пример #6
0
        /// <summary>
        ///   Get a new server end point for a given proxy connection to.
        /// </summary>
        /// <param name="proxyConnection"> The proxy connection which need a new server connection. </param>
        /// <returns> A RemoteServerInfo object which contains important information about the new backend server. </returns>
        public RemoteServerInfo GetServerEndPoint(IProxyConnection proxyConnection)
        {
            ProxyConfigurationSection settings = ProxyConfigurationSection.Settings;

            IOrderedEnumerable<ServerElement> server =
                settings.Server.OfType<ServerElement> ().Where(
                    m => m.IsDefault || (!string.IsNullOrEmpty(m.DnsName) && proxyConnection.Host.StartsWith(m.DnsName)))
                    .OrderBy(m => m.IsDefault);

            ServerElement possibleResult = server.FirstOrDefault ();
            RemoteServerInfo result = possibleResult == null
                                          ? null
                                          : new RemoteServerInfo(possibleResult.Name,
                                                                 Extensions.ParseEndPoint(possibleResult.EndPoint),
                                                                 possibleResult.MinecraftVersion)
                                                {
                                                    KickMessage = possibleResult.KickMessage
                                                };

            var args = new PluginResultEventArgs<RemoteServerInfo>(result, proxyConnection);
            PluginManager.TriggerPlugin.OnPlayerServerSelection(args);
            args.EnsureSuccess ();
            return args.Result;
        }
Пример #7
0
 public ProxyTerminal()
 {
     this.pc = new ProxyConnection();
 }
 /// <summary>
 ///   Create a new instance of the UserEventArgs class with the specific connection
 /// </summary>
 /// <param name="connection"> The connection this event data belongs to </param>
 public UserEventArgs(IProxyConnection connection)
 {
     Connection = connection;
 }
Пример #9
0
 protected override IProxyConnection GetConnection(TcpClientInfo clientInfo)
 {
     _currentConnection = new MockProxyConnection(clientInfo.Client, clientInfo.IsSecure, _trafficDataStore, "Mock request", _mockSite);
     return(_currentConnection);
 }
 /// <summary>
 ///   Creates a new instance of the <see cref="PacketReceivedEventArgs" /> class
 /// </summary>
 /// <param name="packet"> The packet which should be redirected </param>
 /// <param name="connection"> The connection this packet belongs to </param>
 public PacketReceivedEventArgs(Packet packet, IProxyConnection connection)
 {
     Packet = packet;
     Connection = connection;
 }