コード例 #1
0
        protected virtual void MakeConnection()
        {
            if (_socks != null)
            {
                IPAddressSet a = null;
                try
                {
                    a = new IPAddressSet(IPAddress.Parse(_socks.DestName));
                }
                catch (FormatException)
                {
                    try
                    {
                        a = new IPAddressSet(_socks.DestName);
                    }
                    catch (Exception)
                    {
                    }
                }

                if (a != null && !SocksApplicapable(_socks.ExcludingNetworks, a))
                {
                    _addressSet = a;
                    _host       = _socks.DestName;
                    _port       = _socks.DestPort;
                    _socks      = null;
                }
            }

            string dest = _socks == null ? _host : _socks.ServerName;
            int    port = _socks == null ? _port : _socks.ServerPort;
            string msg  = _socks == null?GetHostDescription() : "SOCKS Server";

            if (_addressSet == null)
            {
                try
                {
                    _addressSet = new IPAddressSet(IPAddress.Parse(dest));
                }
                catch (FormatException)
                {
                    _errorMessage = String.Format("The {0} {1} was not found.", msg, dest);
                    _addressSet   = new IPAddressSet(dest);
                }
            }

            _errorMessage     = String.Format("Failed to connect {0} {1}. Please check the address and the port.", msg, dest);
            _socket           = NetUtil.ConnectTCPSocket(_addressSet, port);
            _connectedAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address;

            if (_socks != null)
            {
                _errorMessage = "An error occurred while SOCKS negotiation.";
                _socks.Connect(_socket);
                _host = _socks.DestName;
                _port = _socks.DestPort;
            }

            _tcpConnected = true;
        }
コード例 #2
0
ファイル: socks.cs プロジェクト: nwtajcky/RDManager
        public static Socket ConnectTCPSocket(IPAddressSet addr, int port)
        {
            IPAddress primary = addr.Primary;

            if (primary == null)
            {
                return(null);
            }

            Socket s = new Socket(primary.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try {
                s.Connect(new IPEndPoint(primary, port));
                return(s);
            }
            catch (Exception ex) {
                IPAddress secondary = addr.Secondary;
                if (secondary == null)
                {
                    throw ex;
                }
                s = new Socket(secondary.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                s.Connect(new IPEndPoint(secondary, port));
                return(s);
            }
        }
コード例 #3
0
        protected virtual void MakeConnection()
        {
            //まずSOCKSを使うべきかどうかを判定する
            if (_socks != null)
            {
                IPAddressSet a = null;
                try {
                    a = new IPAddressSet(IPAddress.Parse(_socks.DestName));
                }
                catch (FormatException) {
                    try {
                        a = new IPAddressSet(_socks.DestName);
                    }
                    catch (Exception) {                    //ここで名前解決できずとも、SOCKSサーバが解決できるかもしれないのでエラーにはしない
                    }
                }

                if (a != null && !SocksApplicapable(_socks.ExcludingNetworks, a))                //SOCKSを使わないことが確定
                {
                    _addressSet = a;
                    _host       = _socks.DestName;
                    _port       = _socks.DestPort;
                    _socks      = null;
                }
            }

            string dest = _socks == null? _host : _socks.ServerName;
            int    port = _socks == null? _port : _socks.ServerPort;
            string msg  = _socks == null?GetHostDescription() : "SOCKS Server";

            if (_addressSet == null)
            {
                try {
                    _addressSet = new IPAddressSet(IPAddress.Parse(dest));                     //最初からIPアドレス形式のときは手で変換。そうでないとDNSの逆引きをしてタイムアウト、とかややこしいことが起こる
                }
                catch (FormatException) {
                    _errorMessage = String.Format("The {0} {1} was not found.", msg, dest);
                    _addressSet   = new IPAddressSet(dest);
                }
            }

            _errorMessage     = String.Format("Failed to connect {0} {1}. Please check the address and the port.", msg, dest);
            _socket           = NetUtil.ConnectTCPSocket(_addressSet, port);
            _connectedAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address;

            if (_socks != null)
            {
                _errorMessage = "An error occurred while SOCKS negotiation.";
                _socks.Connect(_socket);
                _host = _socks.DestName;
                _port = _socks.DestPort;
            }

            _tcpConnected = true;
        }
コード例 #4
0
        private void Run()
        {
            _tcpConnected  = false;
            _ignoreTimeout = false;
            _succeeded     = false;
            try
            {
                _addressSet   = null;
                _errorMessage = null;
                MakeConnection();

                _errorMessage = null;
                Negotiate();

                _succeeded = true;
            }
            catch (Exception ex)
            {
                if (_errorMessage == null)
                {
                    _errorMessage = ex.Message;
                }
                else
                {
                    _errorMessage += ex.Message;
                }
            }
            finally
            {
                if (_async)
                {
                    ExitProcess();
                }
                else
                {
                    _event.Set();
                    _event.Close();
                }

                if (_tcpConnected && !_interrupted && _errorMessage != null)
                {
                    try
                    {
                        _socket.Close();
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
コード例 #5
0
ファイル: socks.cs プロジェクト: rfyiamcool/solrex
        public static Socket ConnectTCPSocket(IPAddressSet addr, int port)
        {
            IPAddress primary = addr.Primary;
            if(primary==null) return null;

            Socket s = new Socket(primary.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            try {
                s.Connect(new IPEndPoint(primary, port));
                return s;
            }
            catch(Exception ex) {
                IPAddress secondary = addr.Secondary;
                if(secondary==null) throw ex;
                s = new Socket(secondary.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                s.Connect(new IPEndPoint(secondary, port));
                return s;
            }
        }
コード例 #6
0
        private static bool SocksApplicapable(string nss, IPAddressSet address)
        {
            foreach (string netaddress in nss.Split(';'))
            {
                if (netaddress.Length == 0)
                {
                    continue;
                }

                if (!NetUtil.IsNetworkAddress(netaddress))
                {
                    throw new FormatException(String.Format("{0} is not suitable as a network address.", netaddress));
                }
                if (NetUtil.NetAddressIncludesIPAddress(netaddress, address.Primary))
                {
                    return(false);
                }
                else if (address.Secondary != null && NetUtil.NetAddressIncludesIPAddress(netaddress, address.Secondary))
                {
                    return(false);
                }
            }
            return(true);
        }
コード例 #7
0
        private void Run()
        {
            _tcpConnected = false;
            _ignoreTimeout = false;
            _succeeded = false;
            try {
                _addressSet = null;
                _errorMessage = null;
                MakeConnection();

                _errorMessage = null;
                Negotiate();

                _succeeded = true;
            }
            catch(Exception ex) {
                if(_errorMessage==null)
                    _errorMessage = ex.Message;
                else
                    _errorMessage += ex.Message;
            }
            finally {
                if(_async) {
                    ExitProcess();
                }
                else {
                    _event.Set();
                    _event.Close();
                }

                if(_tcpConnected && !_interrupted && _errorMessage!=null) {
                    try {
                        _socket.Close();
                    }
                    catch(Exception) { //�����ł����ƕ‚��邱�Ƃ��o���Ȃ��ꍇ��������
                    }
                }
            }
        }
コード例 #8
0
        private static bool SocksApplicapable(string nss, IPAddressSet address)
        {
            foreach(string netaddress in nss.Split(';')) {
                if(netaddress.Length==0) continue;

                if(!NetUtil.IsNetworkAddress(netaddress)) {
                    throw new FormatException(String.Format("{0} is not suitable as a network address.", netaddress));
                }
                if(NetUtil.NetAddressIncludesIPAddress(netaddress, address.Primary))
                    return false;
                else if(address.Secondary!=null && NetUtil.NetAddressIncludesIPAddress(netaddress, address.Secondary))
                    return false;
            }
            return true;
        }
コード例 #9
0
        protected virtual void MakeConnection()
        {
            //�܂�SOCKS��g���ׂ����ǂ����𔻒肷��
            if(_socks!=null) {
                IPAddressSet a = null;
                try {
                    a = new IPAddressSet(IPAddress.Parse(_socks.DestName));
                }
                catch(FormatException ) {
                    try {
                        a = new IPAddressSet(_socks.DestName);
                    }
                    catch(Exception) { //�����Ŗ��O����ł����Ƃ�ASOCKS�T�[�o������ł��邩�����Ȃ��̂ŃG���[�ɂ͂��Ȃ�
                    }
                }

                if(a!=null && !SocksApplicapable(_socks.ExcludingNetworks, a)) { //SOCKS��g��Ȃ����Ƃ��m��
                    _addressSet = a;
                    _host = _socks.DestName;
                    _port = _socks.DestPort;
                    _socks = null;
                }
            }

            string dest = _socks==null? _host : _socks.ServerName;
            int    port = _socks==null? _port : _socks.ServerPort;
            string msg  = _socks==null? GetHostDescription() : "SOCKS Server";
            if(_addressSet==null) {
                try {
                    _addressSet = new IPAddressSet(IPAddress.Parse(dest)); //�ŏ�����IP�A�h���X�`���̂Ƃ��͎�ŕϊ��B�����łȂ���DNS�̋t��������ă^�C���A�E�g�A�Ƃ���₱�������Ƃ��N����
                }
                catch(FormatException ) {
                    _errorMessage = String.Format("The {0} {1} was not found.", msg, dest);
                    _addressSet = new IPAddressSet(dest);
                }
            }

            _errorMessage = String.Format("Failed to connect {0} {1}. Please check the address and the port.", msg, dest);
            _socket = NetUtil.ConnectTCPSocket(_addressSet, port);
            _connectedAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address;

            if(_socks!=null) {
                _errorMessage = "An error occurred while SOCKS negotiation.";
                _socks.Connect(_socket);
                _host = _socks.DestName;
                _port = _socks.DestPort;
            }

            _tcpConnected = true;
        }