Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Net.Http;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- public class ProxyServer
- {
- private const int ProxyPort = 8080;
- private static Socket proxySocket;
- private static byte[] buffer = new byte[4096];
- public static void Main()
- {
- StartProxyServer();
- }
- private static void StartProxyServer()
- {
- proxySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- proxySocket.Bind(new IPEndPoint(IPAddress.Any, ProxyPort));
- proxySocket.Listen(10);
- Console.WriteLine("Proxy server started on port " + ProxyPort);
- while (true)
- {
- Socket clientSocket = proxySocket.Accept();
- HandleClientRequest(clientSocket);
- }
- }
- private static void HandleClientRequest(Socket clientSocket)
- {
- try
- {
- byte[] requestBuffer = new byte[4096];
- int bytesRead = clientSocket.Receive(requestBuffer);
- string request = Encoding.ASCII.GetString(requestBuffer, 0, bytesRead);
- Console.WriteLine("Received request:\n" + request);
- // Perform Tor-Project logic for routing the request through the decentralized network
- // Connect to the target server
- Socket targetSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- targetSocket.Connect("example.com", 80);
- // Forward the client's request to the target server
- targetSocket.Send(requestBuffer, bytesRead, SocketFlags.None);
- // Receive the response from the target server
- byte[] responseBuffer = new byte[4096];
- int responseBytesRead = targetSocket.Receive(responseBuffer);
- // Forward the target server's response to the client
- clientSocket.Send(responseBuffer, responseBytesRead, SocketFlags.None);
- // Close the sockets
- clientSocket.Close();
- targetSocket.Close();
- }
- catch (Exception ex)
- {
- Console.WriteLine("An error occurred: " + ex.Message);
- }
- }
- }
- class Program
- {
- static async Task Main()
- {
- // Створення клієнта і налаштування проксі-сервера
- HttpClientHandler handler = new HttpClientHandler()
- {
- Proxy = new WebProxy("http://localhost:8080"),
- UseProxy = true
- };
- // Створення HTTP-клієнта з налаштованим проксі-сервером
- HttpClient client = new HttpClient(handler);
- // Виконання запиту GET
- HttpResponseMessage response = await client.GetAsync("https://nure.ua/");
- // Перевірка статусу відповіді
- if (response.IsSuccessStatusCode)
- {
- // Отримання відповіді у вигляді рядка
- string responseContent = await response.Content.ReadAsStringAsync();
- // Виведення відповіді у консоль
- Console.WriteLine(responseContent);
- }
- else
- {
- Console.WriteLine($"Помилка: {response.StatusCode}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement