Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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:
C#
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";//host;
                    int proxyPort = 243;//443;
                    byte[] buffer = new byte[1048576];
                    int bytes;

                    // Connect socket
                    TcpClient client = new TcpClient(proxy, proxyPort);
                    NetworkStream stream = client.GetStream();

                    // Establish Tcp tunnel
                    byte[] tunnelRequest = Encoding.UTF8.GetBytes(request);

                    stream.Write(tunnelRequest, 0, tunnelRequest.Length);
                    stream.Flush();

                    // Read response to CONNECT request
                    // There should be loop that reads multiple packets
                    bytes = stream.Read(buffer, 0, buffer.Length);
                    clientSocket.Send(buffer, 0, bytes, SocketFlags.None);
                    Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
                    
                    //reading users new request
                    buffer = ReciveData(clientSocket);
                    // Wrap in SSL stream
                    //SslStream sslStream = new SslStream(stream);
                    SslStream sslStream = new SslStream(stream, false, new RemoteCertificateValidationCallback(CertificateValidationCallback));
                    sslStream.AuthenticateAsClient(sUrl);

                    // Send request
                    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));
                    //Console.Write(Encoding.UTF8.GetString(bt, 0, cor));
                    // clientSocket.Send(bt, 0, cor, SocketFlags.None);
                    // Read response
                    do
                    {
                        bytes = sslStream.Read(bt, 0, bt.Length);
                        lst.Add(cutArray(bt, bytes));
                        total += bytes;
                        Console.WriteLine(bytes);
                        // Console.Write(Encoding.UTF8.GetString(bt, 0, bytes));
                        //clientSocket.Send(bt, 0, bytes, SocketFlags.None);

                    } 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));
                    /////////////////////////////////////////////////////////////////////

                    //Console.Write(sUrl);
               /* }
                catch (Exception ex)
                { Console.WriteLine(ex.Message); break; }*/
            }
        }

        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:
C#
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);
            //FIX IT LATER
            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 :(
Posted
Updated 23-Mar-13 15:16pm
v3

The first thing you need to learn is that MD5 is not an encryption algorithm.

It is a Hashing algorithm. The difference is that encryption can be reversed - Hashing cannot.
Hashing is destructive - it throws away information to generate a small, fast code which can be used to verify data, but not to encrypt it. Look at using TripleDES or similar instead, because if you have designed your system around MD5 you are going to find that this is never, ever going to work. This may in fact be your problem...
 
Share this answer
 
you even did not read the question completely.
THERE IS NO PROBLEM WITH ENCRYPTION MY FRIEND. No encrypting included code is not working
For your Information Im using TripleDES With MD5

thank you anyway
 
Share this answer
 
salam Ali
this is your answer

have good time
 
Share this answer
 
Comments
OriginalGriff 20-Dec-22 5:15am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 8 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful.
And since the link is pretty much irrelevant to the problem as stated, it looks even worse for you ...

Stick to new questions and give decent answers and you'll be fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900