Advertisement
Soverein

Untitled

May 31st, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. public class ProxyServer
  9. {
  10. private const int ProxyPort = 8080;
  11.  
  12. private static Socket proxySocket;
  13. private static byte[] buffer = new byte[4096];
  14.  
  15. public static void Main()
  16. {
  17. StartProxyServer();
  18. }
  19.  
  20. private static void StartProxyServer()
  21. {
  22. proxySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  23. proxySocket.Bind(new IPEndPoint(IPAddress.Any, ProxyPort));
  24. proxySocket.Listen(10);
  25. Console.WriteLine("Proxy server started on port " + ProxyPort);
  26.  
  27. while (true)
  28. {
  29. Socket clientSocket = proxySocket.Accept();
  30. HandleClientRequest(clientSocket);
  31. }
  32. }
  33.  
  34. private static void HandleClientRequest(Socket clientSocket)
  35. {
  36. try
  37. {
  38. byte[] requestBuffer = new byte[4096];
  39. int bytesRead = clientSocket.Receive(requestBuffer);
  40. string request = Encoding.ASCII.GetString(requestBuffer, 0, bytesRead);
  41. Console.WriteLine("Received request:\n" + request);
  42.  
  43. // Perform Tor-Project logic for routing the request through the decentralized network
  44.  
  45. // Connect to the target server
  46. Socket targetSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  47. targetSocket.Connect("example.com", 80);
  48.  
  49. // Forward the client's request to the target server
  50. targetSocket.Send(requestBuffer, bytesRead, SocketFlags.None);
  51.  
  52. // Receive the response from the target server
  53. byte[] responseBuffer = new byte[4096];
  54. int responseBytesRead = targetSocket.Receive(responseBuffer);
  55.  
  56. // Forward the target server's response to the client
  57. clientSocket.Send(responseBuffer, responseBytesRead, SocketFlags.None);
  58.  
  59. // Close the sockets
  60. clientSocket.Close();
  61. targetSocket.Close();
  62. }
  63. catch (Exception ex)
  64. {
  65. Console.WriteLine("An error occurred: " + ex.Message);
  66. }
  67. }
  68. }
  69.  
  70. class Program
  71. {
  72. static async Task Main()
  73. {
  74. // Створення клієнта і налаштування проксі-сервера
  75. HttpClientHandler handler = new HttpClientHandler()
  76. {
  77. Proxy = new WebProxy("http://localhost:8080"),
  78. UseProxy = true
  79. };
  80.  
  81. // Створення HTTP-клієнта з налаштованим проксі-сервером
  82. HttpClient client = new HttpClient(handler);
  83.  
  84. // Виконання запиту GET
  85. HttpResponseMessage response = await client.GetAsync("https://nure.ua/");
  86.  
  87. // Перевірка статусу відповіді
  88. if (response.IsSuccessStatusCode)
  89. {
  90. // Отримання відповіді у вигляді рядка
  91. string responseContent = await response.Content.ReadAsStringAsync();
  92.  
  93. // Виведення відповіді у консоль
  94. Console.WriteLine(responseContent);
  95. }
  96. else
  97. {
  98. Console.WriteLine($"Помилка: {response.StatusCode}");
  99. }
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement