C# Proxy?

10/24/2008 05:31 high6#1
Anyone have an example of a simple C# proxy? I can't get any sources I find online to work.
10/24/2008 08:56 Leo_2#2
i know but a few people who could give you a working example, it's highly unlikley that they would however..
10/24/2008 11:58 ChingChong23#3
No, make something your self instead of getting the hard part done for you.
10/24/2008 12:12 MushyPeas#4
Quote:
Originally Posted by high6 View Post
Anyone have an example of a simple C# proxy? I can't get any sources I find online to work.
/\
||
Not some noob, I suppose he means a basic generic proxy system for C#.
Otherwise I'd be quite surprised he's asking for a working co proxy source xD.
10/24/2008 19:43 Some-Guy#5
@ChingChong23 Learn a little about the people you slag off before you post.

I beleive high6 is referring to either a general proxy source, or better: an outdated co proxy source, since other than the encryptions the method would still be the same.

/sorry for a kinda useless post, I didn't use c# although I did have a try I got rid of all my attempts (I'm useless with it)
10/24/2008 22:04 high6#6
Sorry, I am asking for a general proxy.
10/25/2008 17:26 unknownone#7
I'm not gonna give you a full proxy, but here's a demonstration on how you might set up a basic proxy to listen on some ports, accept connectionson those ports, and handle events on those accepted connections. This uses a single thread for all connections.

Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;


namespace CoProxyExample
{
    class CoProxy
    {
        private TcpListener m_authlistener;
        private TcpListener m_gamelistener;
        private ArrayList m_read_sock_set;
        private List<CoConnection> m_Connections;

        public CoProxy()
        {
            IPAddress localhost = IPAddress.Parse("127.0.0.1");
            m_authlistener = new TcpListener(localhost, 9958);
            m_gamelistener = new TcpListener(localhost, 5816);
            m_authlistener.Start();
            m_gamelistener.Start();

            m_read_sock_set = new ArrayList();
            m_read_sock_set.Add(m_authlistener.Server);
            m_read_sock_set.Add(m_gamelistener.Server);

            m_Connections = new List<CoConnection>();
        }
        public void RunProxy()
        {
            bool stopproxy = false;
            IPAddress loginserverIP = IPAddress.Parse("69.59.142.13");
            ArrayList tmp_sock_set = new ArrayList();
            while (true)
            {
                tmp_sock_set.Clear();
                tmp_sock_set = (ArrayList)m_read_sock_set.Clone();
                Socket.Select(tmp_sock_set, null, null, 1000);
                for (int i=0;i<tmp_sock_set.Count;i++)
                {
                    if (tmp_sock_set[i] == m_authlistener.Server)
                    {
                        Socket s1 = m_authlistener.AcceptSocket();
                        m_Connections.Add(new AuthConnection(s1));
                        m_Connections[m_Connections.Count - 1].ConnectToServer(loginserverIP, 9958);
                        m_read_sock_set.Add(s1);
                        m_read_sock_set.Add(m_Connections[m_Connections.Count-1].serversocket);
                    }
                    else if (tmp_sock_set[i] == m_gamelistener.Server)
                    {
                        Socket s2 = m_gamelistener.AcceptSocket();
                        m_read_sock_set.Add(s2);
                        m_Connections.Add(new GameConnection(s2));
                        m_read_sock_set.Add(m_Connections[m_Connections.Count - 1].serversocket);
                    }
                    else
                    {
                        FindConnection((Socket)tmp_sock_set[i]);
                    }
                }
                if (stopproxy)
                {
                    break;
                }
            }
            m_authlistener.Stop();
            m_maplistener.Stop();
        }

        private void FindConnection(Socket s)
        {
            for(int i=0;i<m_Connections.Count;i++)
            {
                if (m_Connections[i].IsPending(s)) break;
                if (m_Connections[i].IsDisconnected())
                {
                    m_read_sock_set.Remove(m_Connections[i].clientsocket);
                    m_read_sock_set.Remove(m_Connections[i].serversocket);
                    m_Connections.Remove(m_Connections[i]);
                    break;
                }
            }
        }
    }
}
The CoConnection is an abstract class which would contain the socket descriptor for the connection to the client and to the server, and would have the basic send/receive methods and IsPending() etc. You'd derive from this to contain the information specific to AuthConnection or GameConnection such as encryptions.

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using CoProxyExample.Packets;

namespace CoProxyExample
{
    abstract class CoConnection
    {

        private Socket m_clientConnection;
        private Socket m_serverConnection;
        private bool hasconnected;

        public CoConnection(Socket s)
        {
            m_clientConnection = s;
            m_serverConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            hasconnected = false;
        }

        public Socket clientsocket
        { 
            get { return m_clientConnection; }
        }

        public Socket serversocket
        {
            get { return m_serverConnection; }
        }

        public void ConnectToServer(IPAddress serverip, int port)
        {
            m_serverConnection.Connect(serverip, port);
            hasconnected = true;
        }

        public bool IsPending(Socket s)
        {
            if (m_clientConnection == s)
            {
                ReceiveFromClient();
                return true;
            }
            else if(m_serverConnection == s)
            {
                ReceiveFromServer();
                return true;
            }
            return false;
        }

        public bool IsDisconnected()
        {
            if (!hasconnected) return false;
            if (!m_clientConnection.Connected)
            {
                m_serverConnection.Disconnect(false);
                return true;
            }
            if (!m_serverConnection.Connected)
            {
                m_clientConnection.Disconnect(false);
                return true;
            }
            return false;
        }

        private void ReceiveFromClient()
        {
            Packet packet = new Packet(2048);
            int bsize = 0;
            bsize = m_clientConnection.Receive(packet.Buffer);
            if (bsize == 0)
            {
                m_clientConnection.Disconnect(false);
                return;
            }
            HandlePacketFromClient(packet);
        }

        private void ReceiveFromServer()
        {
            Packet packet = new Packet(2048);
            int bsize = 0;
            bsize = m_serverConnection.Receive(packet.Buffer);
            if (bsize == 0)
            {
                m_serverConnection.Disconnect(false);
                return;
            }
            HandlePacketFromServer(packet);
        }

        private void SendToServer(Packet packet)
        {
            m_serverConnection.Send(packet.Buffer, packet.Size, SocketFlags.None);
        }

        private void SendToClient(Packet packet)
        {
            m_clientConnection.Send(packet.Buffer, packet.Size, SocketFlags.None);
        }
    }
}
10/25/2008 17:40 high6#8
Thanks,

Can you listen to a whole port with a proxy though? The sources I have seen use TcpListener.Bind() to bind to a port and capture/direct all traffic on that port. Never works for me though.

I will try what you have though, thanks again :D.
10/25/2008 18:02 unknownone#9
These do listen on a particular port. The TcpListener class has a few different constructors for that. The constructor I've used automatically calls Bind() with the arguments passed to it, so it's the same thing.
Binding doesn't capture all traffic on a port, it waits for new connections to it, which is when you Accept() the connections, give each new connection a descriptor and then listen for data on those.
10/26/2008 02:21 high6#10
Quote:
Originally Posted by unknownone View Post
These do listen on a particular port. The TcpListener class has a few different constructors for that. The constructor I've used automatically calls Bind() with the arguments passed to it, so it's the same thing.
Binding doesn't capture all traffic on a port, it waits for new connections to it, which is when you Accept() the connections, give each new connection a descriptor and then listen for data on those.
So with the port binded you have to direct the traffic right?
11/01/2008 10:40 iliveoncaffiene#11
Port binding means that you have a process or listener attached to a port.
Only 1 process may listen on a port at a time, so binding to a port basically "latches" the process onto the port for listening.

After that it doesn't "direct" the traffic, it receives all of the incoming data.
The proxy forwards the data from the server to the client and from the client to the server, acting as a "middle-man" between the client and the server.
11/03/2008 04:05 high6#12
Quote:
Originally Posted by iliveoncaffiene View Post
Port binding means that you have a process or listener attached to a port.
Only 1 process may listen on a port at a time, so binding to a port basically "latches" the process onto the port for listening.

After that it doesn't "direct" the traffic, it receives all of the incoming data.
The proxy forwards the data from the server to the client and from the client to the server, acting as a "middle-man" between the client and the server.
Well either way, none of the examples work for me.
11/12/2008 22:14 Sk1nny#13
Quote:
Originally Posted by high6 View Post
Well either way, none of the examples work for me.
could ya show what you tried so I know what not to do.
11/13/2008 15:07 high6#14
I used everything he showed except select cause I don't really understand it.
12/31/2008 13:05 scorpion1900#15
thnxxxxxxxxxxxxxxxxxxxx