コード例 #1
0
        /// <summary>
        /// This method permit to get back the real ip behind a proxy and check the list of banned IP.
        /// </summary>
        private bool GetAndCheckForwardedIp(string packet)
        {
            var splitPacket = packet.Split(new[] { "\n" }, StringSplitOptions.None);

            foreach (var packetEach in splitPacket)
            {
                if (packetEach != null)
                {
                    if (!string.IsNullOrEmpty(packetEach))
                    {
                        if (packetEach.ToLower().Contains("x-forwarded-for: "))
                        {
                            string newIp = packetEach.ToLower().Replace("x-forwarded-for: ", "");
                            _ip = newIp;
                            ClassLog.Log("HTTP/HTTPS API - X-Forwarded-For ip of the client is: " + newIp, 7, 2);
                            var checkBanResult = ClassApiBan.FilterCheckIp(_ip);
                            if (!checkBanResult) // Is Banned
                            {
                                return(false);
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(newIp))
                                {
                                    ClassApiBan.FilterInsertIp(newIp);
                                }
                                return(true);
                            }
                        }
                    }
                }
            }

            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Start to listen incoming client.
        /// </summary>
        /// <returns></returns>
        public async Task StartHandleClientHttpAsync()
        {
            try
            {
                _ip = ((IPEndPoint)(_client.Client.RemoteEndPoint)).Address.ToString();
            }
            catch
            {
                CloseClientConnection();
                return;
            }
            var checkBanResult = true;

            if (_ip != "127.0.0.1") // Do not check localhost ip.
            {
                checkBanResult = ClassApiBan.FilterCheckIp(_ip);
            }
            int totalWhile = 0;

            if (checkBanResult)
            {
                if (_ip != "127.0.0.1")
                {
                    ClassApiBan.FilterInsertIp(_ip);
                }
                try
                {
                    while (_clientStatus)
                    {
                        try
                        {
                            byte[] buffer = new byte[ClassConnectorSetting.MaxNetworkPacketSize];
                            using (NetworkStream clientHttpReader = new NetworkStream(_client.Client))
                            {
                                using (var bufferedStreamNetwork = new BufferedStream(clientHttpReader, ClassConnectorSetting.MaxNetworkPacketSize))
                                {
                                    int received = await bufferedStreamNetwork.ReadAsync(buffer, 0, buffer.Length);

                                    if (received > 0)
                                    {
                                        string packet = Encoding.UTF8.GetString(buffer, 0, received);
                                        try
                                        {
                                            if (!GetAndCheckForwardedIp(packet))
                                            {
                                                break;
                                            }
                                        }
                                        catch
                                        {
                                        }

                                        packet = ClassUtilsNode.GetStringBetween(packet, "GET", "HTTP");

                                        packet = packet.Replace("/", "");
                                        packet = packet.Replace(" ", "");
                                        ClassLog.Log("HTTP API - packet received from IP: " + _ip + " - " + packet, 6, 2);
                                        await HandlePacketHttpAsync(packet);

                                        break;
                                    }
                                    else
                                    {
                                        totalWhile++;
                                    }
                                    if (totalWhile >= 8)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        catch
                        {
                            break;
                        }
                    }
                }
                catch
                {
                }

                CloseClientConnection();
            }
        }