Пример #1
0
        /// <summary>
        ///   Read the identify message and update the peer information.
        /// </summary>
        /// <param name="remote"></param>
        /// <param name="stream"></param>
        /// <param name="cancel"></param>
        /// <returns></returns>
        public async Task UpdateRemotePeerAsync(Peer remote, Stream stream, CancellationToken cancel)
        {
            var info = await ProtoBufHelper.ReadMessageAsync <Identify>(stream, cancel).ConfigureAwait(false);

            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 => MultiAddress.TryCreate(b))
                                   .Where(a => a != null)
                                   .Select(a => a.WithPeerId(remote.Id))
                                   .ToList();
            }
            if (remote.Addresses.Count() == 0)
            {
                log.Warn($"No listen address for {remote}");
            }

            if (!remote.IsValid())
            {
                throw new InvalidDataException($"Invalid peer {remote}.");
            }
        }
Пример #2
0
        public async Task <IEnumerable <MultiAddress> > ListAsync(CancellationToken cancel = default(CancellationToken))
        {
            if (ipfs.Options.Discovery.BootstrapPeers != null)
            {
                return(ipfs.Options.Discovery.BootstrapPeers);
            }

            try
            {
                var json = await ipfs.Config.GetAsync("Bootstrap", cancel);

                if (json == null)
                {
                    return(new MultiAddress[0]);
                }

                return(json
                       .Select(a => MultiAddress.TryCreate((string)a))
                       .Where(a => a != null));
            }
            catch (KeyNotFoundException)
            {
                var strings = defaults.Select(a => a.ToString());
                await ipfs.Config.SetAsync("Bootstrap", JToken.FromObject(strings), cancel);

                return(defaults);
            }
        }
        public void TryCreate_FromBytes()
        {
            var good  = MultiAddress.TryCreate("/ip4/1.2.3.4/tcp/80");
            var good1 = MultiAddress.TryCreate(good.ToArray());

            Assert.Equal(good, good1);

            Assert.Null(MultiAddress.TryCreate(new byte[] { 0x7f }));
        }
        public async Task <IEnumerable <MultiAddress> > ListAsync(CancellationToken cancel = default)
        {
            var json = await Client.DoCommandAsync("bootstrap/list", cancel);

            var addrs = (JArray)(JObject.Parse(json)["Peers"]);

            return(addrs
                   .Select(a => MultiAddress.TryCreate((string)a))
                   .Where(ma => ma != null));
        }
Пример #5
0
 /// <inheritdoc />
 public override IEnumerable <MultiAddress> GetAddresses(Message message)
 {
     return(message.AdditionalRecords
            .OfType <TXTRecord>()
            .SelectMany(t => t.Strings)
            .Where(s => s.StartsWith("dnsaddr="))
            .Select(s => s.Substring(8))
            .Select(s => MultiAddress.TryCreate(s))
            .Where(a => a != null));
 }
Пример #6
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.ConfigureAwait(false);

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

            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);

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

                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 => MultiAddress.TryCreate(b))
                                       .Where(a => a != null)
                                       .ToList();
                }
                if (remote.Addresses.Count() == 0)
                {
                    log.Warn($"No listen address for {remote}");
                }
            }

            // TODO: Verify the Peer ID

            connection.IdentityEstablished.TrySetResult(remote);

            log.Debug($"Peer id '{remote}' of {connection.RemoteAddress}");
            return(remote);
        }
Пример #7
0
        public async Task <IEnumerable <Peer> > AddressesAsync(CancellationToken cancel = default(CancellationToken))
        {
            var json = await ipfs.DoCommandAsync("swarm/addrs", cancel);

            return(((JObject)JObject.Parse(json)["Addrs"])
                   .Properties()
                   .Select(p => new Peer {
                Id = p.Name,
                Addresses = ((JArray)p.Value)
                            .Select(a => MultiAddress.TryCreate((string)a))
                            .Where(ma => ma != null)
            }));
        }
Пример #8
0
        public async Task <IEnumerable <MultiAddress> > ListAddressFiltersAsync(bool persist             = false,
                                                                                CancellationToken cancel = default)
        {
            try
            {
                var json = await _configApi.GetAsync("Swarm.AddrFilters", cancel).ConfigureAwait(false);

                return(json == null ? new MultiAddress[0] : json.Select(a => MultiAddress.TryCreate((string)a)).Where(a => a != null));
            }
            catch (KeyNotFoundException)
            {
                var strings = DefaultFilters.Select(a => a.ToString());
                await _configApi.SetAsync("Swarm.AddrFilters", JToken.FromObject(strings), cancel)
                .ConfigureAwait(false);

                return(DefaultFilters);
            }
        }
Пример #9
0
        public async Task <IEnumerable <MultiAddress> > ListAsync(CancellationToken cancel = default)
        {
            if (_discoveryOptions.BootstrapPeers != null)
            {
                return(_discoveryOptions.BootstrapPeers);
            }

            try
            {
                var json = await _configApi.GetAsync("Bootstrap", cancel);

                return(json == null ? new MultiAddress[0] : json.Select(a => MultiAddress.TryCreate((string)a)).Where(a => a != null));
            }
            catch (KeyNotFoundException)
            {
                var strings = Defaults.Select(a => a.ToString());
                await _configApi.SetAsync("Bootstrap", JToken.FromObject(strings), cancel).ConfigureAwait(false);

                return(Defaults);
            }
        }
Пример #10
0
        public async Task <IEnumerable <MultiAddress> > ListAddressFiltersAsync(bool persist = false, CancellationToken cancel = default)
        {
            JArray addrs;

            if (persist)
            {
                addrs = await Client.Config.GetAsync("Swarm.AddrFilters", cancel) as JArray;
            }
            else
            {
                var json = await Client.DoCommandAsync("swarm/filters", cancel);

                addrs = (JObject.Parse(json)["Strings"]) as JArray;
            }

            if (addrs == null)
            {
                return(new MultiAddress[0]);
            }
            return(addrs
                   .Select(a => MultiAddress.TryCreate((string)a))
                   .Where(ma => ma != null));
        }
 public void TryCreate_FromString()
 {
     Assert.NotNull(MultiAddress.TryCreate("/ip4/1.2.3.4/tcp/80"));
     Assert.Null(MultiAddress.TryCreate("/tcp/alpha")); // bad port
     Assert.Null(MultiAddress.TryCreate("/foobar"));    // bad protocol
 }