Exemplo n.º 1
0
        async Task <bool> QueryEndPoint(Uri endpoint, bool querySubNodes)
        {
            try
            {
                if (endpoint == LocalNodeInfo.PublicEndPoint)
                {
                    return(false);
                }

                var client         = new NodeClient(endpoint);
                var nodeInfoResult = (await client.DownloadNodeInfo());
                if (nodeInfoResult.ResultType == DownloadResultTypes.Timeout || nodeInfoResult.ResultType == DownloadResultTypes.NotFound)
                {
                    return(false);
                }
                var nodeInfo = nodeInfoResult.Data;
                if (nodeInfo != null)
                {
                    if (nodeInfo.NetworkKey != NetworkKey)
                    {
                        Log.Info($"Queryied node not is invalid {nodeInfo.PublicEndPoint}. Wrong network key.", this);
                        return(false);
                    }


                    {
                        if (nodeInfo.NodeId == LocalId)
                        {
                            return(false);
                        }

                        var queryResult = (await client.DownloadKademliaNodes(nodeInfo.NodeKey, LocalId)).Data;
                        if (queryResult != null)
                        {
                            if (nodeInfo.IsPublicEndPoint)
                            {
                                AddValidNode(nodeInfo);
                            }
                            else
                            {
                                Log.Warn($"Node has no public endpoint {endpoint}.", this);
                            }

                            if (querySubNodes)
                            {
                                foreach (var node in queryResult.Nodes)
                                {
                                    if (node.IsPublicEndPoint)
                                    {
                                        var c2  = new NodeClient(node.PublicEndPoint);
                                        var ni2 = (await c2.DownloadNodeInfo()).Data;
                                        if (ni2 != null && ni2.NetworkKey == NetworkKey && node.NodeKey == ni2.NodeKey)
                                        {
                                            AddValidNode(ni2);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.IgnoreException(ex, this);
                Log.Error($"Node is invalid {endpoint}.", this);
            }
            return(true);
        }