예제 #1
0
        /// <summary>
        ///   Gets the identity information of the remote peer.
        /// </summary>
        /// <param name="connection">
        ///   The currenty connection to the remote peer.
        /// </param>
        /// <param name="cancel"></param>
        /// <returns></returns>
        public async Task <Peer> GetRemotePeer(PeerConnection connection, CancellationToken cancel)
        {
            var muxer = await connection.MuxerEstablished.Task;

            log.Debug("Get remote identity");
            Peer remote = connection.RemotePeer;

            using (var stream = await muxer.CreateStreamAsync("id", cancel))
            {
                await connection.EstablishProtocolAsync("/multistream/", stream, cancel);

                await connection.EstablishProtocolAsync("/ipfs/id/", stream, cancel);

                var info = await ProtoBufHelper.ReadMessageAsync <Identify>(stream, cancel);

                if (remote == null)
                {
                    remote = new Peer();
                    connection.RemotePeer = remote;
                }

                remote.AgentVersion    = info.AgentVersion;
                remote.ProtocolVersion = info.ProtocolVersion;
                if (info.PublicKey == null || info.PublicKey.Length == 0)
                {
                    throw new InvalidDataException("Public key is missing.");
                }
                remote.PublicKey = Convert.ToBase64String(info.PublicKey);
                if (remote.Id == null)
                {
                    remote.Id = MultiHash.ComputeHash(info.PublicKey);
                }

                if (info.ListenAddresses != null)
                {
                    remote.Addresses = info.ListenAddresses
                                       .Select(b =>
                    {
                        try
                        {
                            return(new MultiAddress(b));
                        }
                        catch
                        {
                            return(null);
                        }
                    })
                                       .Where(a => a != null)
                                       .ToList();
                }
            }

            // TODO: Verify the Peer ID

            connection.IdentityEstablished.TrySetResult(remote);

            log.Debug($"Peer id '{remote}' of {connection.RemoteAddress}");
            return(remote);
        }
예제 #2
0
 /// <inheritdoc />
 public async Task ProcessMessageAsync(PeerConnection connection,
                                       Stream stream,
                                       CancellationToken cancel = default)
 {
     connection.SecurityEstablished.SetResult(true);
     await connection.EstablishProtocolAsync("/multistream/", CancellationToken.None).ConfigureAwait(false);
 }
예제 #3
0
        /// <summary>
        ///   Gets the identity information of the remote peer.
        /// </summary>
        /// <param name="connection">
        ///   The currenty connection to the remote peer.
        /// </param>
        /// <param name="cancel"></param>
        /// <returns></returns>
        public async Task <Peer> GetRemotePeerAsync(PeerConnection connection, CancellationToken cancel)
        {
            var muxer = await connection.MuxerEstablished.Task.ConfigureAwait(false);

            log.Debug("Get remote identity");
            Peer remote = connection.RemotePeer;

            if (remote == null)
            {
                remote = new Peer();
                connection.RemotePeer = remote;
            }

            // Read the remote peer identify info.
            using (var stream = await muxer.CreateStreamAsync("id", cancel).ConfigureAwait(false))
            {
                await connection.EstablishProtocolAsync("/multistream/", stream, cancel).ConfigureAwait(false);

                await connection.EstablishProtocolAsync("/ipfs/id/", stream, cancel).ConfigureAwait(false);
                await UpdateRemotePeerAsync(remote, stream, cancel).ConfigureAwait(false);
            }

            // It should always contain the address we used for connections, so
            // that NAT translations are maintained.
            if (connection.RemoteAddress != null && !remote.Addresses.Contains(connection.RemoteAddress))
            {
                var addrs = remote.Addresses.ToList();
                addrs.Add(connection.RemoteAddress);
                remote.Addresses = addrs;
            }

            connection.IdentityEstablished.TrySetResult(remote);

            log.Debug($"Peer id '{remote}' of {connection.RemoteAddress}");
            return(remote);
        }