Esempio n. 1
0
        public Socket Accept()
        {
            byte[] response = new byte[262];

            if (_socket.Receive(response) < 10)
            {
                throw new SocksClientException("The connection was reset by the remote peer.");
            }

            if (response[0] != SocksClient.SOCKS_VERSION)
            {
                throw new SocksClientException("Socks version 5 is not supported by the proxy server.");
            }

            SocksReplyCode reply = (SocksReplyCode)response[1];

            if (reply != SocksReplyCode.Succeeded)
            {
                throw new SocksClientException("Socks proxy server request failed: " + reply.ToString());
            }

            _dstEP = new SocksEndPoint(response, 3);

            return(_socket);
        }
Esempio n. 2
0
        public int ReceiveFrom(byte[] buffer, int offset, int size, out SocksEndPoint remoteEP)
        {
            byte[]   datagram = new byte[262 + size];
            EndPoint dummyEP  = new IPEndPoint(IPAddress.Any, 0);

            int bytesReceived = _udpSocket.ReceiveFrom(datagram, 0, datagram.Length, SocketFlags.None, ref dummyEP);

            if (bytesReceived < 10)
            {
                throw new SocksClientException("The connection was reset by the remote peer.");
            }

            remoteEP = new SocksEndPoint(datagram, 3);

            int dataOffset = 6 + remoteEP.GetAddressSize();
            int dataSize   = bytesReceived - dataOffset;

            if (dataSize > size)
            {
                dataSize = size;
            }

            Buffer.BlockCopy(datagram, dataOffset, buffer, offset, dataSize);

            return(dataSize);
        }
Esempio n. 3
0
        private SocksEndPoint Request(Socket socket, SocksRequestCommand command, SocksEndPoint dstAddr)
        {
            socket.Send(dstAddr.CreateRequest(command));

            byte[] response = new byte[262];

            if (socket.Receive(response) < 10)
            {
                throw new SocksClientException("The connection was reset by the remote peer.");
            }

            if (response[0] != SOCKS_VERSION)
            {
                throw new SocksClientException("Socks version 5 is not supported by the proxy server.");
            }

            SocksReplyCode reply = (SocksReplyCode)response[1];

            if (reply != SocksReplyCode.Succeeded)
            {
                throw new SocksClientException("Socks proxy server request failed: " + reply.ToString());
            }

            return(new SocksEndPoint(response, 3));
        }
Esempio n. 4
0
        internal SocksUdpAssociateRequestHandler(Socket controlSocket, Socket udpSocket, SocksEndPoint relayEP)
        {
            _controlSocket = controlSocket;
            _udpSocket     = udpSocket;
            _relayEP       = relayEP;

            _relayEndPoint = _relayEP.GetEndPoint();

            _watchThread = new Thread(ControlSocketWatchAsync);
            _watchThread.IsBackground = true;
            _watchThread.Start();
        }
Esempio n. 5
0
        public SocksBindRequestHandler Bind(SocksEndPoint endpoint, int timeout = 10000)
        {
            //connect to proxy server
            Socket socket = GetProxyConnection(timeout);

            socket.SendTimeout    = 30000;
            socket.ReceiveTimeout = 30000;

            try
            {
                Negotiate(socket);
                SocksEndPoint bindEP = Request(socket, SocksRequestCommand.Bind, endpoint);

                return(new SocksBindRequestHandler(socket, bindEP));
            }
            catch
            {
                socket.Dispose();
                throw;
            }
        }
Esempio n. 6
0
        public SocksConnectRequestHandler Connect(SocksEndPoint remoteEP)
        {
            //connect to proxy server
            Socket socket = GetProxyConnection();

            socket.SendTimeout    = 30000;
            socket.ReceiveTimeout = 30000;

            try
            {
                Negotiate(socket);
                SocksEndPoint bindEP = Request(socket, SocksRequestCommand.Connect, remoteEP);

                return(new SocksConnectRequestHandler(socket, remoteEP, bindEP));
            }
            catch
            {
                socket.Dispose();
                throw;
            }
        }
Esempio n. 7
0
        public SocksUdpAssociateRequestHandler UdpAssociate(IPEndPoint localEP, int timeout = 10000)
        {
            //bind local ep
            Socket udpSocket = new Socket(localEP.AddressFamily, SocketType.Dgram, ProtocolType.Udp);

            udpSocket.Bind(localEP);

            //connect to proxy server
            Socket socket = GetProxyConnection(timeout);

            socket.SendTimeout    = 30000;
            socket.ReceiveTimeout = 30000;

            try
            {
                Negotiate(socket);

                SocksEndPoint relayEP = Request(socket, SocksRequestCommand.UdpAssociate, new SocksEndPoint((IPEndPoint)udpSocket.LocalEndPoint));

                return(new SocksUdpAssociateRequestHandler(socket, udpSocket, relayEP));
            }
            catch
            {
                if (socket != null)
                {
                    socket.Dispose();
                }

                if (udpSocket != null)
                {
                    udpSocket.Dispose();
                }

                throw;
            }
        }
Esempio n. 8
0
        public SocksClient(IPEndPoint proxyEndPoint, NetworkCredential credentials = null)
        {
            _proxyEP = new SocksEndPoint(proxyEndPoint);

            Init(credentials);
        }
Esempio n. 9
0
        public SocksClient(IPAddress proxyAddress, int port = 1080, NetworkCredential credentials = null)
        {
            _proxyEP = new SocksEndPoint(proxyAddress, port);

            Init(credentials);
        }
Esempio n. 10
0
        public void SendTo(byte[] buffer, int offset, int size, SocksEndPoint remoteEP)
        {
            byte[] datagram = remoteEP.CreateUdpDatagram(buffer, offset, size);

            _udpSocket.SendTo(datagram, _relayEndPoint);
        }
Esempio n. 11
0
 public SocksBindRequestHandler(Socket socket, SocksEndPoint bindEP)
 {
     _socket = socket;
     _bindEP = bindEP;
 }
Esempio n. 12
0
 public SocksConnectRequestHandler(Socket socket, SocksEndPoint dstEP, SocksEndPoint bindEP)
 {
     _socket = socket;
     _dstEP  = dstEP;
     _bindEP = bindEP;
 }
Esempio n. 13
0
        public SocksClient(SocksEndPoint proxyEndPoint, NetworkCredential credentials = null)
        {
            _proxyEP = proxyEndPoint;

            Init(credentials);
        }