Click here to Skip to main content
15,897,518 members
Articles / Programming Languages / C# 4.0

Golabi Proxy Server

Rate me:
Please Sign up or sign in to vote.
4.79/5 (10 votes)
20 Sep 2011CPOL2 min read 66.1K   3.7K   21  
This is a proxy server with a client that can encrypt your browser request.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using EncryptionLibrary;

namespace GolabiProxyClient
{
    class ClientConnection
    {
        private Socket clientSocket;
        private ConnectionInfo _Info;
        private Thread handler;
        public bool _ContinueRecive;
        public bool _finished;

        public ClientConnection(Socket client, ConnectionInfo info)
        {
            _finished = false;
            this._Info = info;
            client.SendTimeout = -1;
            client.ReceiveTimeout = -1;
            this.clientSocket = client;
        }

        public void StartHandling()
        {
            handler = new Thread(new ThreadStart(Handler));
            handler.Priority = ThreadPriority.Highest;
            handler.Start();
        }

        public void StopHandling()
        {
            _ContinueRecive = false;
            if (handler != null)
            {
                handler.Abort();
            }
        }

        private void Handler()
        {
            _ContinueRecive = true;
            bool recvRequest = true;
            string EOL = "\r\n";

            string requestPayload = "";
            List<string> requestLines = new List<string>();
            byte[] requestBuffer = new byte[1];
            byte[] responseBuffer = new byte[1];

            requestLines.Clear();

            try
            {
                //State 0: Handle Request from Client
                while (recvRequest && _ContinueRecive)
                {
                    if (this.clientSocket.Receive(requestBuffer) == 0)
                    {
                        break;
                    }
                    string fromByte = UTF8Encoding.UTF8.GetString(requestBuffer);
                    requestPayload += fromByte;
                    if (requestPayload.EndsWith(EOL + EOL))
                    {
                        recvRequest = false;
                    }
                }

                //State 1: Creating a new socket to connect to server
                Socket destServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                destServerSocket.Connect(this._Info.ServerIP,this._Info.ServerPort);
                destServerSocket.ReceiveTimeout = 5000;
                destServerSocket.SendTimeout = 5000;

                //State 2: Sending New Request Information to Destination Server and Relay Response to Client
                if (this._Info.EncryptionEnabled)
                {
                    destServerSocket.Send(ASCIIEncoding.ASCII.GetBytes(Encryption.Encrypt(requestPayload, this._Info.Key) + "farhadserverfinish"));
                }
                else
                {
                    destServerSocket.Send(ASCIIEncoding.ASCII.GetBytes(requestPayload + "farhadserverfinish"));
                }

                if (false)
                {
                    while (destServerSocket.Receive(responseBuffer) != 0)
                    {
                        int tmp = responseBuffer[0];
                        tmp -= 2;
                        if (tmp < 0)
                        {
                            tmp += 256;
                        }
                        responseBuffer[0] = (byte)tmp;
                        this.clientSocket.Send(responseBuffer);
                    }

                }
                else
                {
                    while (destServerSocket.Receive(responseBuffer) != 0)
                    {
                        this.clientSocket.Send(responseBuffer);
                    }
                }
                destServerSocket.Disconnect(false);
                this.clientSocket.Disconnect(false);

                _ContinueRecive = false;
                _finished = true;
            }
            catch (Exception ex)
            {
                this._Info.MessageCenter.setMessage(ex.Message);
                this._ContinueRecive = false;
                this._finished = true;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer رایان پایا داده محاسب
Iran (Islamic Republic of) Iran (Islamic Republic of)
Bachelor of computer software engineer at Urmia University of Technology,

Masters of computer software engineer at Shahid Beheshti University,

Microsoft Certified Solution Developer : Web Applications,

Microsoft Specialist : HTML5 , CSS3 , javascript,

MCTS at Mojtame Fanni Tehran (MFT)


My Server Solution Blog

Comments and Discussions