hello there
my name is ali and im living in iran, as you know our government is limiting the internet on us.
we have censorship on it that we cannot go to facebook even.
because of that we should use vpn or proxy or anything like that.
recently the have blocked the PPTP and L2TP protocols that we cannot connect any more.
(this blocking is our election is going to be hapened)
so if they can block vpns they can block https proxy too that we are currently using
i has designed something like proxy BUT it will use encryption MD5 to connect and EVERYTHING is encrypted in the connection.
i has designed something but not working completely, i was wondering if anybody can help me
it is the Client side code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
namespace httpsProxyClient
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("CLIENT");
TcpListener listener = new TcpListener(IPAddress.Any, 8889);
listener.Start();
while (true)
{
Socket socket = listener.AcceptSocket();
ParameterizedThreadStart pts = new ParameterizedThreadStart(IncommingRequest);
Thread td = new Thread(pts);
td.Start(socket);
}
}
const int datalenth = 1048576;
static void IncommingRequest(object objclientSocket)
{
while (true)
{
Socket clientSocket = (Socket)objclientSocket;
byte[] buff2 = ReciveData(clientSocket);
string request = Encoding.ASCII.GetString(buff2);
string sUrl = "";
bool isHTTPRequest = true;
if (request.ToLower().StartsWith("connect"))
{
sUrl = request.Substring(request.IndexOf(" ") + 1, request.IndexOf(":") - request.IndexOf(" ") - 1);
isHTTPRequest = false;
}
string proxy = "192.168.1.7";
int proxyPort = 243;
byte[] buffer = new byte[1048576];
int bytes;
TcpClient client = new TcpClient(proxy, proxyPort);
NetworkStream stream = client.GetStream();
byte[] tunnelRequest = Encoding.UTF8.GetBytes(request);
stream.Write(tunnelRequest, 0, tunnelRequest.Length);
stream.Flush();
bytes = stream.Read(buffer, 0, buffer.Length);
clientSocket.Send(buffer, 0, bytes, SocketFlags.None);
Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
buffer = ReciveData(clientSocket);
SslStream sslStream = new SslStream(stream, false, new RemoteCertificateValidationCallback(CertificateValidationCallback));
sslStream.AuthenticateAsClient(sUrl);
byte[] brequest = buffer;
sslStream.Write(brequest, 0, brequest.Length);
sslStream.Flush();
byte[] bt = new byte[1048576];
int cor = sslStream.Read(bt, 0, bt.Length);
int total = cor;
List<byte[]> lst = new List<byte[]>();
lst.Add(cutArray(bt, cor));
do
{
bytes = sslStream.Read(bt, 0, bt.Length);
lst.Add(cutArray(bt, bytes));
total += bytes;
Console.WriteLine(bytes);
} while (bytes == bt.Length);
byte[] sending = new byte[total];
int corsur = 0;
foreach (byte[] data in lst)
{
for (int i = 0; i < data.Length; i++)
{
sending[corsur] = data[i];
corsur++;
}
}
clientSocket.Send(sending, 0, sending.Length, SocketFlags.None);
Console.Write("****"+Encoding.UTF8.GetString(sending, 0, sending.Length));
}
}
static bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
static byte[] cutArray(byte[] toCut, int count)
{
byte[] tmp = new byte[count];
for (int i = 0; i < count; i++)
{
tmp[i] = toCut[i];
}
return tmp;
}
private static byte[] ReciveData(Socket clientSocket)
{
int total = 0;
byte[] data = new byte[datalenth];
byte[] buff1 = new byte[0];
int recint = 0;
do
{
recint = clientSocket.Receive(data);
total += recint;
byte[] temp = buff1;
buff1 = new byte[total];
int cursor = 0;
for (; cursor < temp.Length; cursor++)
{
buff1[cursor] = temp[cursor];
}
for (int i = 0; i < recint; i++)
{
buff1[i + cursor] = data[i];
}
} while (recint == datalenth);
return buff1;
}
}
}
The Server side is Here:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Security;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace httpsProxServer
{
class Program
{
public static void Main(string[] args)
{
Console.Write("Port to Listen:");
TcpListener listener = new TcpListener(IPAddress.Any, int.Parse(Console.ReadLine()));
listener.Start();
while (true)
{
Console.WriteLine("Server Started");
Console.WriteLine("Waiting for client...");
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("Client connected:" + client.Client.RemoteEndPoint.ToString());
ParameterizedThreadStart pts = new ParameterizedThreadStart(IncommingClient);
Thread td = new Thread(pts);
td.Start(client);
}
}
private static void IncommingClient(object objclient)
{
TcpClient client = (TcpClient)objclient;
byte[] data = new byte[1048576];
int recData = client.Client.Receive(data);
data = cutArray(data, recData);
string strData = Encoding.ASCII.GetString(data);
Console.WriteLine(strData);
string host = strData.Substring(strData.IndexOf(" ") + 1, strData.IndexOf(":") - strData.IndexOf(" ") - 1);
int port = int.Parse(strData.Substring(strData.IndexOf(":") + 1, strData.IndexOf(" ", strData.IndexOf(":")) - strData.IndexOf(":") - 1));
IPHostEntry hosts = Dns.GetHostEntry(host);
Socket ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
int addr = 0;
connectAgain:
try
{
ServerSocket.Connect(hosts.AddressList[addr], port);
}
catch
{
if (addr >= hosts.AddressList.Length)
{
Console.WriteLine("No response from {0} Servers", addr);
return;
}
addr++;
goto connectAgain;
}
client.Client.Send(Encoding.UTF8.GetBytes("HTTP/1.0 200 Connection established" + Environment.NewLine + "Proxy-agent: ALProxy 0.62" + Environment.NewLine + Environment.NewLine));
ParameterizedThreadStart pts = new ParameterizedThreadStart(reception);
Thread thrdReception = new Thread(pts);
thrdTrasfer tras = new thrdTrasfer(ServerSocket, client);
thrdReception.Start(tras);
while (true)
{
try
{
byte[] rec = new byte[20480];
int i = client.Client.Receive(rec);
rec = cutArray(rec, i);
ServerSocket.Send(rec);
Console.WriteLine("Client----->Server");
}
catch (SocketException ex)
{
Console.WriteLine(ex.Message);
break;
}
}
}
static void reception(object objSocket)
{
thrdTrasfer serverSocket = (thrdTrasfer)objSocket;
int ohtimes = 0;
const int maxtokill = 10;
while (true)
{
try
{
byte[] buff = ReciveData(serverSocket.serverSocket);
if (buff.Length == 0)
{
if (ohtimes >= maxtokill)
return;
ohtimes++;
}
serverSocket.Client.Client.Send(buff);
Console.WriteLine("Server----->Client:" + buff.Length);
}
catch (Exception ec)
{
Console.WriteLine(ec.Message);
}
}
}
private static byte[] ReciveData(Socket clientSocket)
{
int total = 0;
byte[] data = new byte[1048576];
byte[] buff1 = new byte[0];
int recint = 0;
do
{
recint = clientSocket.Receive(data);
total += recint;
byte[] temp = buff1;
buff1 = new byte[total];
int cursor = 0;
for (; cursor < temp.Length; cursor++)
{
buff1[cursor] = temp[cursor];
}
for (int i = 0; i < recint; i++)
{
buff1[i + cursor] = data[i];
}
} while (recint == 1048576);
return buff1;
}
static byte[] cutArray(byte[] toCut, int count)
{
byte[] tmp = new byte[count];
for (int i = 0; i < count; i++)
{
tmp[i] = toCut[i];
}
return tmp;
}
}
struct thrdTrasfer
{
public Socket serverSocket;
public TcpClient Client;
public thrdTrasfer(Socket s,TcpClient c)
{
serverSocket = s;
Client = c;
}
}
}
Thank you for reading this
Have a nice day
P.S.: no encypting is in this codes. i has encrypting classes currently but this codes are not working :(