Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Conquer Online 2 > CO2 Programming
You last visited: Today at 07:09

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[TuT][C#]Creating your own proxy - Part 1

Discussion on [TuT][C#]Creating your own proxy - Part 1 within the CO2 Programming forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
[TuT][C#]Creating your own proxy - Part 1

First of all, I will not target Conquer, but just how a proxy works in general with connection etc. I will cover 4 things in this tutorial. Server sockets, Client sockets, proxy logic and then the actual creation of a proxy. Why do I cover the server and client thing? Because a proxy will be a middle man, so it will have both advantages as a client and a server. For the client it is a server, for the server it is a client. I will not show how to deal with packets, modifying them, structure them etc.

I am also aware that there will be mistakes within the tutorial, so please bare over with it.

I will split it into 4 tutorials, which will cover the 4 things as I mentioned before.

Before we begin, then create a new console application project. Mine is called "ProxyExample_Server".

The Server Socket
Quote:
  • The Listener
  • Accepting Connections
  • Receiving Data
  • Handling Data
  • Sending Data
The Listener
The first thing we will do, when creating a server socket. It is to bind the socket to a specific port on our network and in this tutorial I will just use port 7788. For all network handling you should make a namespace. Just create a new folder called "Network" and for every class you want to have that namespace, then just right click the folder and choose "Class".

The first class we make is "SocketServer". Within that class we will handle connections and such things, but once we have finished accepting connections etc. then we will move on to the SocketClient. Create a new class called SocketClient now.

Close SocketClient.cs and open SocketServer.cs.

At first we need to import some namespaces and declare 2 const int.
Namespaces:
Code:
using System.Net;
using System.Net.Sockets;
Const int:
Code:
public const int MIN_PACKETSIZE = 4, MAX_PACKETSIZE = 1024;
The consts will be used when the connection is accepted and the holding buffer has to be created and when data is received.

Now we need a few variables declared.
Code:
        private IPEndPoint EndPoint;
        private Socket ServerSocket;
EndPoint is simply the EndPoint. This is the EndPoint we're binding our socket to.

ServerSocket is our actual serversocket, which will handle connections etc.

Next thing we do is creating a constructor for our SocketServer class. The constructor should have one parameter called Port as an int.
Within the constructor we will create our socket and endpoint.
Code:
        public SocketServer(int Port)
        {
        }
First thing we do is creating our socket. When calling new Socket() it requires 3 parameters.
AddressFamily, SocketType, ProtocolType.

AddressFamily is the network address family we want to use, as we want to use the actual network we will choose InterNetwork. I will not get into the others as they're not important in this case.

SocketType is the type of our socket and we will use Stream. I will not get into the other socket types as we won't use them either.

ProtocolType is the type of our protocol and we will use Tcp, there is other types of protocols as well, such as Udp etc. We will use Tcp, because it's what's normally used when working with "Client/Server" things. Udp is more used when streaming, because it's about speed and not safety. It will not report if there was any errors sending the packets, but Tcp will, however it's not really the main point here. Some games still uses Udp, if I'm correct HoN does, so it doesn't mean you can't use it, but there is several reasons why you shouldn't.
Code:
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
The next thing is to create our endpoint. It only requires 2 things. An IPAddress and a port. Our port is 7788 as I mentioned before and for the IPAddress we will use IPAddress.Any, because it will get the most likely possible IP for us.
Code:
EndPoint = new IPEndPoint(IPAddress.Any, Port);
This should be the final constructor.
Code:
        public SocketServer(int Port)
        {
            ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            EndPoint = new IPEndPoint(IPAddress.Any, Port);
        }
Now we have the main thing done. We need to create our listener and we will do that by creating 3 new methods. One called Listen() with no parameters, one called BeginAccept() with no parameters as well and one called Accept_Callback() as a private method with one parameter as IAsyncResult.
Code:
        public void Listen()
        {
        }

        public void BeginAccept()
        {
        }

        private void Accept_Callback(IAsyncResult asyncResult)
        {
        }
Now let's construct the methods.
The Listen method is used to bind our socket to the endpoint and then starting to listen for connections.

The BeginAccept method is used to get our socket beginning accepting a connection. This will be called once within the Listen method.

Let's start with the Listen Method.

First we call Socket.Bind(), it requires one parameter as an endpoint. We have already declared our endpoint, so it's just calling that within the parameter.
Code:
ServerSocket.Bind(EndPoint);
Now we need to start listening for connections and we will do that using ServerSocket.Listen(), it requires one parameter as an int, which is the Backlog. Which is the maximum length of the pending connection queue.
I will choose 100, but mainly you can choose any value that you'd like to.
Code:
ServerSocket.Listen(100);
At last we call BeginAccept() within the method.
Code:
BeginAccept();
This should be the final Listen method.
Code:
        public void Listen()
        {
            ServerSocket.Bind(EndPoint);
            ServerSocket.Listen(100);

            BeginAccept();
        }
Accepting Connections
Now let's continue to the BeginAccept method.
Within that method we will call Socket.BeginAccept(), which requires 2 parameters. An asynccallback and an object. Our callback is already created, which is just the Accept_Callback method we made. The object should be a new SocketClient.
Code:
ServerSocket.BeginAccept(new AsyncCallback(Accept_Callback), new SocketClient());
Now we need to handle our connections, which is what we do in the accept callback.
First thing we do is calling BeginAccept() again.
Second thing we do is making a try catch around the method, but not around BeginAccept.
Code:
        private void Accept_Callback(IAsyncResult asyncResult)
        {
            BeginAccept();

            try
            {
            }
            catch
            {
            }
        }
Now let's open SocketClient as we need to handle a few things in there before we can continue with the Accept_Callback method.

Create a new method called EndAccept and it should be a Socket with 2 parameters. One as Socket and one as IAsyncResult. We need to declare the same namespaces as in SocketServer as well.
Code:
        public Socket EndAccept(Socket serverSocket, IAsyncResult asyncResult)
        {
        }
Now what we do in here is returning Socket.EndAccept(), but what use for? We will then make it equal to the SocketClients socket and check if it was not null before handling this connection. That means we have to declare a variable for our Socket, which is what we do then.
Code:
public Socket ClientSocket;
Code:
        public Socket EndAccept(Socket ServerSocket, IAsyncResult asyncResult)
        {
            return ServerSocket.EndAccept(asyncResult);
        }
Now let's get back to our Accept_Callback.
At first we will get the SocketClient that we're dealing with. We do that by checking if AsyncState is not null and then creating a SocketClient = AsyncState as SocketClient. AsyncState is System.Object.
Code:
                if (asyncResult.AsyncState != null)
                {
                    SocketClient sClient = asyncResult.AsyncState as SocketClient;
                }
Then we have to do the check as I said before. It's simply done with an if statement then making clientSocket equal to EndAccept and at last check if it's not null.
Code:
                    if ((sClient.ClientSocket = sClient.EndAccept(ServerSocket, asyncResult)) != null)
                    {
                    }
Next thing we do is creating a delegate void called ConnectionEvent with SocketClient as parameter.

We will invoke that as the next thing, because then we will handle the actualy connection within that.
Code:
public delegate void ConnectionEvent(SocketClient sClient);
First of all before we invoke, then we need to declare the connection even within our ServerSocket class. When that's done, then it's just invoking it.
Code:
public ConnectionEvent onConnection;
Code:
onConnection.Invoke(sClient);
Receiving Data
Now go back to the SocketClient class and create a new method called BeginReceive() and a private method with IAsyncResult as parameter called Receive_Callback().
Code:
        public void BeginReceive()
        {
        }

        private void Receive_Callback(IAsyncResult asyncResult)
        {
        }
The BeginReceive method should call Socket.BeginReceive().
It takes 6 parameters.
First parameter is a byte array, which is our hold buffer. We need to declare that as well within our SocketClient class.
Code:
public byte[] Buffer;
Now in the BeginReceive method make it a new Buffer with the SocketServer.MAX_PACKETSIZE.
Code:
Buffer = new byte[SocketServer.MAX_PACKETSIZE];
Next parameter is the offsite within the buffer we want to hold the received data and it should be at 0.

The next parameter is the size of the packet, which should be the max packetsize we want to receive, because we do not have a fixed size on all the packets we're going to receive and nobody normally have that. MAX_PACKETSIZE is 1024, because it is the default packetsize.

The next parameter is SocketFlags, which should just be None, because we ain't going to use that.

The last parameter we will put data into is just the AsyncCallback, which we already have created as Receive_Callback.

We will not send any objects with it, so just put null at last.
Code:
ClientSocket.BeginReceive(Buffer, 0, SocketServer.MAX_PACKETSIZE, SocketFlags.None, new AsyncCallback(Receive_Callback), null);
This should be the BeginReceive method.
Code:
        public void BeginReceive()
        {
            Buffer = new byte[SocketServer.MAX_PACKETSIZE];
            ClientSocket.BeginReceive(Buffer, 0, SocketServer.MAX_PACKETSIZE, SocketFlags.None, new AsyncCallback(Receive_Callback), null);
        }
Now let's go back to our Accept_Callback and the last thing we do is just calling SocketClient.BeginReceive().
Code:
sClient.BeginReceive();
This should be our final Accept_Callback.
Code:
private void Accept_Callback(IAsyncResult asyncResult)
        {
            BeginAccept();

            try
            {
                if (asyncResult.AsyncState != null)
                {
                    SocketClient sClient = asyncResult.AsyncState as SocketClient;

                    if ((sClient.ClientSocket = sClient.EndAccept(ServerSocket, asyncResult)) != null)
                    {
                        onConnection.Invoke(sClient);
                        sClient.BeginReceive();
                    }
                }
            }
            catch
            {
            }
        }
Handling Data

Now we got two last things to do within our Server Socket. It is to construct the receive callback and then actually using it.

First thing we do is creating 1 delegate void called BufferEvent, which is the event we will invoke, when handling data packets. We need to declare this event within our SocketClient as well with one other event, where we will use the ConnectionEvent, but for disconnection this time.
Code:
public delegate void BufferEvent(SocketClient sClient, byte[] Buffer);
Code:
public BufferEvent onReceive;
public ConnectionEvent onDisconnection;
Now make a try/catch around the receive callback and in the catch block invoke the onDisconnection.
Code:
            try
            {
            }
            catch
            {
                onDisconnection.Invoke(this);
            }
Now first we need to declare one variable in the method as SocketError.
Code:
SocketError socketError;
The next thing we do is declaring a variable as an int called bufferSize and make it equal to Socket.EndReceive, which has to have two parameters. IAsyncResult and an out parameter as SocketError.
Code:
int bufferSize = ClientSocket.EndReceive(asyncResult, out socketError);
Now we do a check if bufferSize is above/equal to the minsize and below/equal to the maxsize.
Code:
                if (bufferSize >= SocketServer.MIN_PACKETSIZE && bufferSize <= SocketServer.MAX_PACKETSIZE)
                {
                }
After that we do a check if the SocketError is Success.
Code:
                    if (socketError == SocketError.Success)
                    {
                    }
At the very bottom of the try block we will invoke onDisconnection.
Code:
onDisconnection.Invoke(this);
Now we will declare a new buffer with the size of bufferSize.
Code:
byte[] recBuffer = new byte[bufferSize];
Now we need to copy SocketClient.Buffer into recBuffer, which I would recommend using the native call to Msvcrt.memcpy, however System.Buffer.BlockCopy could be used as well.

This is the wrapper I will be using.
Code:
    public unsafe class Msvcrt
    {
        [DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
        public static extern void* Memcpy(void* dest, void* src, uint count);

        public static void MemoryCopy(byte[] Dest, byte[] Src, uint Count)
        {
            fixed (byte* dest = Dest, src = Src)
                Memcpy(dest, src, Count);
        }
    }
Just call the MemoryCopy method.
Dest as recBuffer, Src as Buffer and Count as Buffer.Length.
Code:
Msvcrt.MemoryCopy(recBuffer, Buffer, (uint)Buffer.Length);
Then invoke the onReceive event.
Code:
onReceive.Invoke(this, recBuffer);
At last call BeginReceive() and put a return below it.
Code:
                        BeginReceive();
                        return;
This should be the final Receive_Callback.
Code:
        private void Receive_Callback(IAsyncResult asyncResult)
        {
            try
            {
                SocketError socketError;
                int bufferSize = ClientSocket.EndReceive(asyncResult, out socketError);
                if (bufferSize >= SocketServer.MIN_PACKETSIZE && bufferSize <= SocketServer.MAX_PACKETSIZE)
                {
                    if (socketError == SocketError.Success)
                    {
                        byte[] recBuffer = new byte[bufferSize];
                        Msvcrt.MemoryCopy(recBuffer, Buffer, (uint)Buffer.Length);
                        onReceive.Invoke(this, recBuffer);
                        BeginReceive();
                        return;
                    }
                }
                onDisconnection.Invoke(this);
            }
            catch
            {
                onDisconnection.Invoke(this);
            }
        }
Now our ServerSocket is finished and we can use it. Open Program.cs and create 3 methods equal to these.
Code:
        static void OnConnection(SocketClient sClient)
        {
        }
        static void OnDisconnection(SocketClient sClient)
        {
        }
        static void OnReceive(SocketClient sClient, byte[] Buffer)
        {
        }
First we will use OnConnection, which is where we will set the 2 events of Disconnection and Receive.
Code:
        static void OnConnection(SocketClient sClient)
        {
            sClient.onDisconnection = new ConnectionEvent(OnDisconnection);
            sClient.onReceive = new BufferEvent(OnReceive);
        }
Now OnDisconnection we will just write if the client has been disconnected and then put a try/catch and check if the client is connected, then disconnect it. reUse have to be false, otherwise they cannot connect again.
Code:
        static void OnDisconnection(SocketClient sClient)
        {
            Console.WriteLine("A client has been disconnected from the server...");
            try
            {
                if (sClient.ClientSocket.Connected)
                    sClient.ClientSocket.Disconnect(false);
            }
            catch
            {
            }
        }
Sending Data
What we will do in the OnReceive method is just sending the packet back to the client. However we haven't created the Send method yet, so let's do that at first. Go back to the SocketClient and create a method called Send with a byte[] parameter.
Code:
        public void Send(byte[] Packet)
        {
        }
Now create another private method called Send_Callback with the parameter as IAsyncResult. Within the callback put a try/catch and then call ClientSocket.EndSend(), which has to take one parameter as the IAsyncResult.
Code:
        private void Send_Callback(IAsyncResult asyncResult)
        {
            try
            {
                ClientSocket.EndSend(asyncResult);
            }
            catch { }
        }
Now in the Send method put another try/catch and within there we call Socket.BeginSend, which takes same parameters as BeginReceive, however 1 thing should be different. The size should not be the max size, but the size of the packet. In the catch we will invoke onDisconnection.
Code:
        public void Send(byte[] Packet)
        {
            try
            {
                ClientSocket.BeginSend(Packet, 0, Packet.Length, SocketFlags.None, new AsyncCallback(Send_Callback), null);
            }
            catch
            {
                onDisconnection.Invoke(this);
            }
        }
Now we can go back to the OnReceive method and basically we will just call Send and then send the packet back to the client.
Code:
        static void OnReceive(SocketClient sClient, byte[] Buffer)
        {
            Console.WriteLine("A client has send data to the server...");
            sClient.Send(Buffer);
            Console.WriteLine("The server has send data back to the client...");
        }
Now how do we set the socket up and uses it?
Code:
        static void Main(string[] args)
        {
            SocketServer server = new SocketServer(7788);
            server.onConnection = new ConnectionEvent(OnConnection);
            server.Listen();
        }
This could be used for a Conquer private server as well, but that wasn't the main point tho. It was to understand the server socket. In part 2 I will explain the client socket and then show an example of a connection between the server and the client, but save the project as it is for now, because I will use same project in the next tutorials!

Good luck and I think that's it.

Any questions feel free to ask.

Part 2:
I don't have a username is offline  
Thanks
4 Users
Old 01/22/2012, 14:10   #2
 
Real~Death's Avatar
 
elite*gold: 0
Join Date: Jun 2007
Posts: 1,272
Received Thanks: 246
Oracle HTTP Server is usually port 7788
Real~Death is offline  
Old 01/22/2012, 14:15   #3
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
Quote:
Originally Posted by Real~Death View Post
Oracle HTTP Server is usually port 7788
I don't use Oracle lol and it doesn't really matter as it's just an example.
I don't have a username is offline  
Old 01/22/2012, 15:36   #4
 
Captivate's Avatar
 
elite*gold: 0
Join Date: Jul 2010
Posts: 1,532
Received Thanks: 575
Finish ProjectX already...
Captivate is offline  
Old 01/22/2012, 16:21   #5
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
Quote:
Originally Posted by Captivate View Post
Finish ProjectX already...
Should be done within February/March lol.
I don't have a username is offline  
Thanks
1 User
Old 01/23/2012, 01:29   #6
 
elite*gold: 0
Join Date: Apr 2009
Posts: 101
Received Thanks: 1
Explain a good ,thank you for effort.
koko20 is offline  
Old 01/23/2012, 08:34   #7
 
(ali)'s Avatar
 
elite*gold: 0
Join Date: Dec 2011
Posts: 3
Received Thanks: 1
thanks
(ali) is offline  
Old 01/23/2012, 13:47   #8
 
OELABOELA's Avatar
 
elite*gold: 223
Join Date: Dec 2007
Posts: 1,076
Received Thanks: 257
I hope you will continue on this, so we get some proxy developers on private servers. Will be fun
OELABOELA is offline  
Old 01/23/2012, 14:42   #9
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
Quote:
Originally Posted by OELABOELA View Post
I hope you will continue on this, so we get some proxy developers on private servers. Will be fun
I will, but got a few projects + college as well, so ain't got that much time for it.
I don't have a username is offline  
Old 01/23/2012, 17:38   #10
 
U2_Caparzo's Avatar
 
elite*gold: 0
Join Date: Aug 2011
Posts: 314
Received Thanks: 90
Quote:
Originally Posted by I don't have a username View Post
Should be done within February/March lol.
¬¬ i start my classes in march xD
U2_Caparzo is offline  
Old 01/24/2012, 06:46   #11
 
elite*gold: 0
Join Date: Aug 2010
Posts: 951
Received Thanks: 76
"public const" gave me errors?
denominator is offline  
Old 01/24/2012, 07:10   #12
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
Quote:
Originally Posted by denominator View Post
"public const" gave me errors?
You need to be more specific with what error you get.
I don't have a username is offline  
Old 01/24/2012, 07:12   #13
 
elite*gold: 0
Join Date: Aug 2010
Posts: 951
Received Thanks: 76
Code:
Error	1	Expected class, delegate, enum, interface, or struct	C:\Users\Alan\AppData\Local\Temporary Projects\TestBot\SocketServer.cs	12	12	TestBot
denominator is offline  
Old 01/24/2012, 07:13   #14
 
elite*gold: 0
The Black Market: 136/0/0
Join Date: Dec 2011
Posts: 4,252
Received Thanks: 685
Really nice! Thank you.
Keyosk is offline  
Old 01/24/2012, 07:14   #15
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
Quote:
Originally Posted by denominator View Post
Code:
Error	1	Expected class, delegate, enum, interface, or struct	C:\Users\Alan\AppData\Local\Temporary Projects\TestBot\SocketServer.cs	12	12	TestBot
Did you put them inside the SocketServer class?

Quote:
Originally Posted by Keyosk View Post
Really nice! Thank you.
Thanks
I don't have a username is offline  
Reply


Similar Threads Similar Threads
[Release] New proxy for creating 2moons accaunt(other country)
12/09/2008 - Dekaron Exploits, Hacks, Bots, Tools & Macros - 13 Replies
So i saw few topics about changing Ip and things like that to create acc. if u r not from USA. Here is proxy that works for me every time :) PHYLTERSHEKAN - HOMEPAGE http://i233.photobucket.com/albums/ee213/7kiramez ak7/proxy.jpg then just press RUN or SAVE FILE :cool:
Creating My Own Proxy
05/18/2007 - Conquer Online 2 - 14 Replies
Ok, so I'm posting this here because it's more of a question thread for any of the proxy developers out there. I am attempting to make my own proxy using auto it and have been successful in logging in, however, when I close the sockets the connection remains. I changed my Server.dat to send to 127.1.1.1 and my script takes the clients packets and sends them to the CO server and takes the CO servers responses and sends them to the client. I have ran my script and connected, I have ran the CO...
Creating forum Ip Proxy
11/14/2006 - CO2 Guides & Templates - 2 Replies
Have you been banned from the Offical Conquer Online forums or just want to rage their forums with Tubgirl or goatse or maybe even gay buttsechs? I'll tell you how in this simple tutorial 1) Get a proxy. You can find a list of proxy ips from this site http://www.samair.ru/proxy/ <--- Checking if the Ip works and how fast it is 2) to check to see if the ip is up navigate to



All times are GMT +1. The time now is 07:10.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.