예제 #1
0
        // Switch from TLS connection to non-TLS connection.
        internal SwitchClear(Cluster cluster, Connection conn, byte[] sessionToken)
        {
            // Obtain non-TLS addresses.
            string      command = cluster.useServicesAlternate ? "service-clear-alt" : "service-clear-std";
            string      result  = Info.Request(conn, command);
            List <Host> hosts   = Host.ParseServiceHosts(result);
            Host        clearHost;

            // Find first valid non-TLS host.
            foreach (Host host in hosts)
            {
                try
                {
                    clearHost = host;

                    string alternativeHost;
                    if (cluster.ipMap != null && cluster.ipMap.TryGetValue(clearHost.name, out alternativeHost))
                    {
                        clearHost = new Host(alternativeHost, clearHost.port);
                    }

                    IPAddress[] addresses = Connection.GetHostAddresses(clearHost.name, cluster.connectionTimeout);

                    foreach (IPAddress ia in addresses)
                    {
                        try
                        {
                            clearAddress       = ia;
                            clearSocketAddress = new IPEndPoint(ia, clearHost.port);
                            clearConn          = new Connection(clearSocketAddress, cluster.connectionTimeout, null);

                            try
                            {
                                AdminCommand admin = new AdminCommand(ThreadLocalData.GetBuffer(), 0);

                                if (!admin.Authenticate(cluster, clearConn, sessionToken))
                                {
                                    throw new AerospikeException("Authentication failed");
                                }
                                return;                                 // Authenticated clear connection.
                            }
                            catch (Exception)
                            {
                                clearConn.Close();
                            }
                        }
                        catch (Exception)
                        {
                            // Try next address.
                        }
                    }
                }
                catch (Exception)
                {
                    // Try next host.
                }
            }
            throw new AerospikeException("Invalid non-TLS address: " + result);
        }
예제 #2
0
        private void SetAddress(Cluster cluster, Dictionary <string, string> map, string addressCommand, string tlsName)
        {
            string result;

            if (!map.TryGetValue(addressCommand, out result) || result == null || result.Length == 0)
            {
                // Server does not support service level call (service-clear-std, ...).
                // Load balancer detection is not possible.
                return;
            }

            List <Host> hosts = Host.ParseServiceHosts(result);
            Host        h;

            // Search real hosts for seed.
            foreach (Host host in hosts)
            {
                h = host;

                string alt;
                if (cluster.ipMap != null && cluster.ipMap.TryGetValue(h.name, out alt))
                {
                    h = new Host(alt, h.port);
                }

                if (h.Equals(this.primaryHost))
                {
                    // Found seed which is not a load balancer.
                    return;
                }
            }

            // Seed not found, so seed is probably a load balancer.
            // Find first valid real host.
            foreach (Host host in hosts)
            {
                try
                {
                    h = host;

                    string alt;
                    if (cluster.ipMap != null && cluster.ipMap.TryGetValue(h.name, out alt))
                    {
                        h = new Host(alt, h.port);
                    }

                    IPAddress[] addresses = Connection.GetHostAddresses(h.name, cluster.connectionTimeout);

                    foreach (IPAddress address in addresses)
                    {
                        try
                        {
                            IPEndPoint socketAddress = new IPEndPoint(address, h.port);
                            Connection conn          = (cluster.tlsPolicy != null) ?
                                                       new TlsConnection(cluster.tlsPolicy, tlsName, socketAddress, cluster.connectionTimeout, null) :
                                                       new Connection(socketAddress, cluster.connectionTimeout, null);

                            try
                            {
                                if (cluster.user != null)
                                {
                                    AdminCommand admin = new AdminCommand(ThreadLocalData.GetBuffer(), 0);

                                    if (!admin.Authenticate(cluster, conn, this.sessionToken))
                                    {
                                        throw new AerospikeException("Authentication failed");
                                    }
                                }

                                // Authenticated connection.  Set real host.
                                SetAliases(address, tlsName, h.port);
                                this.primaryHost    = new Host(address.ToString(), tlsName, h.port);
                                this.primaryAddress = socketAddress;
                                this.primaryConn.Close();
                                this.primaryConn = conn;
                                return;
                            }
                            catch (Exception)
                            {
                                conn.Close();
                            }
                        }
                        catch (Exception)
                        {
                            // Try next address.
                        }
                    }
                }
                catch (Exception)
                {
                    // Try next host.
                }
            }

            // Failed to find a valid address. IP Address is probably internal on the cloud
            // because the server access-address is not configured.  Log warning and continue
            // with original seed.
            if (Log.InfoEnabled())
            {
                Log.Info("Invalid address " + result + ". access-address is probably not configured on server.");
            }
        }
        private void SetAddress(Cluster cluster, Dictionary <string, string> map, string addressCommand, string tlsName)
        {
            string      result = map[addressCommand];
            List <Host> hosts  = Host.ParseServiceHosts(result);
            Host        h;

            // Search real hosts for seed.
            foreach (Host host in hosts)
            {
                h = host;

                string alt;
                if (cluster.ipMap != null && cluster.ipMap.TryGetValue(h.name, out alt))
                {
                    h = new Host(alt, h.port);
                }

                if (h.Equals(this.primaryHost))
                {
                    // Found seed which is not a load balancer.
                    return;
                }
            }

            // Seed not found, so seed is probably a load balancer.
            // Find first valid real host.
            foreach (Host host in hosts)
            {
                try
                {
                    h = host;

                    string alt;
                    if (cluster.ipMap != null && cluster.ipMap.TryGetValue(h.name, out alt))
                    {
                        h = new Host(alt, h.port);
                    }

                    IPAddress[] addresses = Connection.GetHostAddresses(h.name, cluster.connectionTimeout);

                    foreach (IPAddress address in addresses)
                    {
                        try
                        {
                            IPEndPoint socketAddress = new IPEndPoint(address, h.port);
                            Connection conn          = (cluster.tlsPolicy != null) ?
                                                       new TlsConnection(cluster.tlsPolicy, tlsName, socketAddress, cluster.connectionTimeout, cluster.maxSocketIdleMillis, null) :
                                                       new Connection(socketAddress, cluster.connectionTimeout, cluster.maxSocketIdleMillis, null);

                            try
                            {
                                if (cluster.user != null)
                                {
                                    AdminCommand admin = new AdminCommand(ThreadLocalData.GetBuffer(), 0);

                                    if (!admin.Authenticate(cluster, conn, this.sessionToken))
                                    {
                                        throw new AerospikeException("Authentication failed");
                                    }
                                }

                                // Authenticated connection.  Set real host.
                                SetAliases(addresses, tlsName, h.port);
                                this.primaryHost    = new Host(address.ToString(), tlsName, h.port);
                                this.primaryAddress = socketAddress;
                                this.primaryConn.Close();
                                this.primaryConn = conn;
                                return;
                            }
                            catch (Exception)
                            {
                                conn.Close();
                            }
                        }
                        catch (Exception)
                        {
                            // Try next address.
                        }
                    }
                }
                catch (Exception)
                {
                    // Try next host.
                }
            }

            // Failed to find a valid host.
            throw new AerospikeException("Invalid address: " + result);
        }