using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace OnionCommunication
{
class Program
{
static async Task Main(string[] args)
{
string torProxyHost = "127.0.0.1"; // Tor proxy IP
int torProxyPort = 9050; // Tor proxy port (genellikle 9050 veya 9150)
string onionAddress = "xxxxxx.onion"; // Hedef Onion adresi
int targetPort = 8080; // Hedef port
// Tor üzerinden .onion adresine mesaj iletme
await SendRequestThroughTor(torProxyHost, torProxyPort, onionAddress, targetPort);
// Basit bir web sunucusu başlatma
StartWebServer();
}
static async Task SendRequestThroughTor(string proxyHost, int proxyPort, string destinationHost, int destinationPort)
{
try
{
using (TcpClient proxyClient = new TcpClient(proxyHost, proxyPort))
using (NetworkStream proxyStream = proxyClient.GetStream())
{
// SOCKS5 handshake
await Socks5Handshake(proxyStream);
// SOCKS5 connection request to .onion service
await Socks5ConnectRequest(proxyStream, destinationHost, destinationPort);
// HTTP GET isteği gönderme
string httpRequest = "GET / HTTP/1.1\r\nHost: " + destinationHost + "\r\nConnection: close\r\n\r\n";
byte[] requestBytes = Encoding.ASCII.GetBytes(httpRequest);
await proxyStream.WriteAsync(requestBytes, 0, requestBytes.Length);
// Yanıtı okuma
using (StreamReader reader = new StreamReader(proxyStream, Encoding.ASCII))
{
string response = await reader.ReadToEndAsync();
Console.WriteLine("Onion Sunucusundan Gelen Yanıt:\n" + response);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Hata: " + ex.Message);
}
}
static async Task Socks5Handshake(NetworkStream proxyStream)
{
byte[] handshake = new byte[] { 0x05, 0x01, 0x00 };
await proxyStream.WriteAsync(handshake, 0, handshake.Length);
byte[] response = new byte[2];
await proxyStream.ReadAsync(response, 0, response.Length);
if (response[1] != 0x00)
{
throw new Exception("SOCKS5 handshake başarısız oldu.");
}
}
static async Task Socks5ConnectRequest(NetworkStream proxyStream, string destinationHost, int destinationPort)
{
byte[] hostBytes = Encoding.ASCII.GetBytes(destinationHost);
byte[] connectRequest = new byte[7 + hostBytes.Length];
connectRequest[0] = 0x05; // SOCKS5
connectRequest[1] = 0x01; // Connect command
connectRequest[2] = 0x00; // Reserved
connectRequest[3] = 0x03; // Domain name
connectRequest[4] = (byte)hostBytes.Length;
Array.Copy(hostBytes, 0, connectRequest, 5, hostBytes.Length);
connectRequest[5 + hostBytes.Length] = (byte)(destinationPort >> 8);
connectRequest[6 + hostBytes.Length] = (byte)(destinationPort & 0xFF);
await proxyStream.WriteAsync(connectRequest, 0, connectRequest.Length);
byte[] response = new byte[10];
await proxyStream.ReadAsync(response, 0, response.Length);
if (response[1] != 0x00)
{
throw new Exception("SOCKS5 connect request başarısız oldu.");
}
}
static void StartWebServer()
{
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://*:8080/");
listener.Start();
Console.WriteLine("Web sunucusu başlatıldı. İstekler bekleniyor...");
Task.Run(async () =>
{
while (true)
{
HttpListenerContext context = await listener.GetContextAsync();
HttpListenerRequest request = context.Request;
using (StreamReader reader = new StreamReader(request.InputStream, request.ContentEncoding))
{
string requestBody = await reader.ReadToEndAsync();
Console.WriteLine("Gelen İstek:\n" + requestBody);
}
HttpListenerResponse response = context.Response;
string responseString = "<html><body>İstek alındı!</body></html>";
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
using (Stream output = response.OutputStream)
{
await output.WriteAsync(buffer, 0, buffer.Length);
}
}
});
}
}
}