Exemplo n.º 1
0
        public GossipClusterMember(ulong localNodeId, ulong remoteNodeId, IPEndPoint remoteEndPoint, IGossipAuthenticator authenticator, params string[] tags)
        {
            if (remoteEndPoint == null) throw new ArgumentNullException("remoteEndPoint");
            if (authenticator == null) throw new ArgumentNullException("authenticator");

            _maxConnectionInactivity = TimeSpan.FromMinutes(5);
            _timerRemoveInactiveConnections = new Timer(RemoveInactiveConnections, null, _maxConnectionInactivity, _maxConnectionInactivity);
            LocalNodeId = localNodeId;
            RemoteNodeId = remoteNodeId;
            RemoteEndPoint = remoteEndPoint;
            Authenticator = authenticator;
            Tags = new HashSet<string>((tags ?? new string[0]).Select(x => (x ?? "").ToLowerInvariant()));
        }
        public GossipClusterMember(ulong localNodeId, ulong remoteNodeId, IPEndPoint remoteEndPoint, IGossipAuthenticator authenticator, params string[] tags)
        {
            if (remoteEndPoint == null)
            {
                throw new ArgumentNullException("remoteEndPoint");
            }
            if (authenticator == null)
            {
                throw new ArgumentNullException("authenticator");
            }

            _maxConnectionInactivity        = TimeSpan.FromMinutes(5);
            _timerRemoveInactiveConnections = new Timer(RemoveInactiveConnections, null, _maxConnectionInactivity, _maxConnectionInactivity);
            LocalNodeId    = localNodeId;
            RemoteNodeId   = remoteNodeId;
            RemoteEndPoint = remoteEndPoint;
            Authenticator  = authenticator;
            Tags           = new HashSet <string>((tags ?? new string[0]).Select(x => (x ?? "").ToLowerInvariant()));
        }
Exemplo n.º 3
0
        public async Task<bool?> RequestAuthentication(IGossipAuthenticator authenticator)
        {
            var randomNumber = RandomNumbers.Complex(5);
            var challenge = BitConverter.GetBytes(randomNumber);

            await Stream.WriteAsync(LocalNodeId);
            await Stream.WriteAsync(challenge, 0, challenge.Length);
            RecordActivity();

            var expected = authenticator.GenerateHash(challenge);

            RemoteNodeId = await Stream.ReadUInt64Async();

            var response = new byte[expected.Length];
            var read = await Stream.ReadAsync(response, 0, response.Length);
            if (read == 0) return null;
            RecordActivity();
            if (read != response.Length) return false;
            if (!expected.SequenceEqual(response)) return false;

            StartReadingMessageData();
            IsAuthenticated = true;
            return true;
        }
Exemplo n.º 4
0
        private async Task RespondToAuthenticationRequestAsync(IGossipAuthenticator authenticator)
        {
            RemoteNodeId = await Stream.ReadUInt64Async();

            RecordActivity();

            var challenge = new byte[8];
            var read      = await Stream.ReadAsync(challenge, 0, challenge.Length);

            if (read != 8)
            {
                return;
            }

            await Stream.WriteAsync(LocalNodeId);

            byte[] sendBuffer = authenticator.GenerateHash(challenge);
            await Stream.WriteAsync(sendBuffer, 0, sendBuffer.Length);

            RecordActivity();

            IsAuthenticated = true;
            StartReadingMessageData();
        }
Exemplo n.º 5
0
        public async Task <bool?> RequestAuthentication(IGossipAuthenticator authenticator)
        {
            var randomNumber = RandomNumbers.Complex(5);
            var challenge    = BitConverter.GetBytes(randomNumber);

            await Stream.WriteAsync(LocalNodeId);

            await Stream.WriteAsync(challenge, 0, challenge.Length);

            RecordActivity();

            var expected = authenticator.GenerateHash(challenge);

            RemoteNodeId = await Stream.ReadUInt64Async();

            var response = new byte[expected.Length];
            var read     = await Stream.ReadAsync(response, 0, response.Length);

            if (read == 0)
            {
                return(null);
            }
            RecordActivity();
            if (read != response.Length)
            {
                return(false);
            }
            if (!expected.SequenceEqual(response))
            {
                return(false);
            }

            StartReadingMessageData();
            IsAuthenticated = true;
            return(true);
        }
Exemplo n.º 6
0
        private async Task RespondToAuthenticationRequestAsync(IGossipAuthenticator authenticator)
        {
            RemoteNodeId = await Stream.ReadUInt64Async();
            RecordActivity();

            var challenge = new byte[8];
            var read = await Stream.ReadAsync(challenge, 0, challenge.Length);
            if (read != 8) return;

            await Stream.WriteAsync(LocalNodeId);

            byte[] sendBuffer = authenticator.GenerateHash(challenge);
            await Stream.WriteAsync(sendBuffer, 0, sendBuffer.Length);
            RecordActivity();

            IsAuthenticated = true;
            StartReadingMessageData();
        }
Exemplo n.º 7
0
 public static async Task<GossipConnection> ConnectAsync(ulong localNodeId, IPEndPoint remoteEndPoint, IGossipAuthenticator authenticator, Action<GossipConnection> onDisconnected = null)
 {
     var client = new TcpClient();
     await client.ConnectAsync(remoteEndPoint.Address, remoteEndPoint.Port);
     var connection = new GossipConnection(client);
     connection.LocalNodeId = localNodeId;
     if (onDisconnected != null) connection.OnDisconnected += onDisconnected;
     await connection.RespondToAuthenticationRequestAsync(authenticator);
     return connection;
 }
Exemplo n.º 8
0
 public GossipClusterMember(ulong localNodeId, IGossipNodeConfig config, IGossipAuthenticator authenticator)
     : this(localNodeId, config.NodeId, config.BindToEndPoint, authenticator, config.Tags)
 {
 }
Exemplo n.º 9
0
        public static async Task <GossipConnection> ConnectAsync(ulong localNodeId, IPEndPoint remoteEndPoint, IGossipAuthenticator authenticator, Action <GossipConnection> onDisconnected = null)
        {
            var client = new TcpClient();
            await client.ConnectAsync(remoteEndPoint.Address, remoteEndPoint.Port);

            var connection = new GossipConnection(client);

            connection.LocalNodeId = localNodeId;
            if (onDisconnected != null)
            {
                connection.OnDisconnected += onDisconnected;
            }
            await connection.RespondToAuthenticationRequestAsync(authenticator);

            return(connection);
        }
Exemplo n.º 10
0
 public GossipClusterMember Join(GossipConnection connection, IPEndPoint endPoint, IGossipAuthenticator authenticator, params string[] tags)
 {
     var clusterMember = _members.GetOrAdd(connection.RemoteNodeId, id => new GossipClusterMember(NodeConfig.NodeId, connection.RemoteNodeId, endPoint, authenticator, tags));
     clusterMember.AttachOpenConnection(connection);
     return clusterMember;
 }
Exemplo n.º 11
0
 public GossipClusterMember(ulong localNodeId, IGossipNodeConfig config, IGossipAuthenticator authenticator)
     : this(localNodeId, config.NodeId, config.BindToEndPoint, authenticator, config.Tags)
 {
 }
Exemplo n.º 12
0
        public GossipClusterMember Join(GossipConnection connection, IPEndPoint endPoint, IGossipAuthenticator authenticator, params string[] tags)
        {
            var clusterMember = _members.GetOrAdd(connection.RemoteNodeId, id => new GossipClusterMember(NodeConfig.NodeId, connection.RemoteNodeId, endPoint, authenticator, tags));

            clusterMember.AttachOpenConnection(connection);
            return(clusterMember);
        }