/// <summary> /// Start to listen incoming client. /// </summary> /// <returns></returns> public async Task StartHandleClientHttpAsync() { var isWhitelisted = true; if (ClassFilteringMiner.CheckMinerIsBannedByIP(_ip)) { isWhitelisted = false; } int totalWhile = 0; if (isWhitelisted) { await Task.Run(() => MaxKeepAliveFunctionAsync()).ConfigureAwait(false); try { while (_clientStatus) { try { using (CancellationTokenSource cancellationPacket = new CancellationTokenSource(1000)) { using (NetworkStream clientHttpReader = new NetworkStream(_client.Client)) { using (BufferedStream bufferedStreamNetwork = new BufferedStream(clientHttpReader, ClassConnectorSetting.MaxNetworkPacketSize)) { byte[] buffer = new byte[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 = ClassUtility.GetStringBetween(packet, "GET /", "HTTP"); packet = packet.Replace("%7C", "|"); // Translate special character | packet = packet.Replace(" ", ""); // Remove empty,space characters ClassLog.ConsoleWriteLog("HTTP API - packet received from IP: " + _ip + " - " + packet, ClassLogEnumeration.IndexPoolApiLog, ClassLogConsoleEnumeration.IndexPoolConsoleYellowLog); await HandlePacketHttpAsync(packet); break; } else { totalWhile++; } if (totalWhile >= 8) { break; } } } } } catch (Exception error) { ClassLog.ConsoleWriteLog("HTTP API - exception error: " + error.Message, ClassLogEnumeration.IndexPoolApiErrorLog, ClassLogConsoleEnumeration.IndexPoolConsoleRedLog); break; } } } catch { } } CloseClientConnection(); }
/// <summary> /// Start to listen incoming client. /// </summary> /// <returns></returns> public async Task StartHandleClientHttpAsync() { var isWhitelisted = true; if (ClassRpcSetting.RpcWalletApiIpWhitelist.Count > 0) { if (!ClassRpcSetting.RpcWalletApiIpWhitelist.Contains(_ip)) { ClassConsole.ConsoleWriteLine(_ip + " is not whitelisted.", ClassConsoleColorEnumeration.IndexConsoleRedLog, ClassConsoleLogLevelEnumeration.LogLevelApi); isWhitelisted = false; } } int totalWhile = 0; if (isWhitelisted) { await Task.Run(() => MaxKeepAliveFunctionAsync()).ConfigureAwait(false); try { while (_clientStatus) { try { using (NetworkStream clientHttpReader = new NetworkStream(_client.Client)) { using (BufferedStream bufferedStreamNetwork = new BufferedStream(clientHttpReader, ClassConnectorSetting.MaxNetworkPacketSize)) { byte[] buffer = new byte[ClassConnectorSetting.MaxNetworkPacketSize]; int received = await bufferedStreamNetwork.ReadAsync(buffer, 0, buffer.Length); if (received > 0) { string packet = Encoding.UTF8.GetString(buffer, 0, received); if (ClassRpcSetting.RpcWalletApiEnableXForwardedForResolver) { try { if (!GetAndCheckForwardedIp(packet)) { break; } } catch { } } packet = ClassUtility.GetStringBetween(packet, "GET /", "HTTP"); packet = packet.Replace("%7C", "|"); // Translate special character | packet = packet.Replace(" ", ""); // Remove empty,space characters ClassConsole.ConsoleWriteLine("HTTP API - packet received from IP: " + _ip + " - " + packet, ClassConsoleColorEnumeration.IndexConsoleYellowLog, ClassConsoleLogLevelEnumeration.LogLevelApi); if (ClassRpcSetting.RpcWalletApiKeyRequestEncryption != string.Empty) { packet = ClassAlgo.GetDecryptedResultManual(ClassAlgoEnumeration.Rijndael, packet, ClassRpcSetting.RpcWalletApiKeyRequestEncryption, ClassWalletNetworkSetting.KeySize); if (packet == ClassAlgoErrorEnumeration.AlgoError) { ClassConsole.ConsoleWriteLine("HTTP API - wrong packet received from IP: " + _ip + " - " + packet, ClassConsoleColorEnumeration.IndexConsoleRedLog, ClassConsoleLogLevelEnumeration.LogLevelApi); break; } ClassConsole.ConsoleWriteLine("HTTP API - decrypted packet received from IP: " + _ip + " - " + packet, ClassConsoleColorEnumeration.IndexConsoleYellowLog, ClassConsoleLogLevelEnumeration.LogLevelApi); } await HandlePacketHttpAsync(packet); break; } else { totalWhile++; } if (totalWhile >= 8) { break; } } } } catch (Exception error) { ClassConsole.ConsoleWriteLine("HTTP API - exception error: " + error.Message, ClassConsoleColorEnumeration.IndexConsoleYellowLog, ClassConsoleLogLevelEnumeration.LogLevelApi); break; } } } catch { } } CloseClientConnection(); }