Esempio n. 1
0
 private static ToxNameService FindNameServiceFromStore(ToxNameService[] services, string suffix)
 {
     return services.FirstOrDefault(s => s.Domain == suffix);
 }
Esempio n. 2
0
        public static string DiscoverToxID(string domain, ToxNameService[] services, bool localStoreOnly, bool tox3Only)
        {
            ToxNameService service = null;

            try
            {
                if (!localStoreOnly)
                {
                    service = FindNameServiceFromStore(services, domain.Split('@')[1]) ?? FindNameService(domain.Split('@')[1]);
                }
                else
                {
                    service = FindNameServiceFromStore(services, domain.Split('@')[1]);
                }
            }
            catch { }

            if (service == null && !tox3Only)
            {
                //this name service does not use tox3, how unencrypted of them
                domain = domain.Replace("@", "._tox.");

                string[] records = GetSPFRecords(domain);

                foreach (string record in records)
                {
                    if (record.Contains("v=tox1"))
                    {
                        string[] entries = record.Split(';');

                        foreach (string entry in entries)
                        {
                            string[] parts = entry.Split('=');
                            string   name  = parts[0];
                            string   value = parts[1];

                            if (name == "id")
                            {
                                return(value);
                            }
                        }
                    }
                }
            }
            else if (service != null)
            {
                string public_key;

                if (!string.IsNullOrWhiteSpace(service.PublicKey))
                {
                    public_key = service.PublicKey;
                }
                else
                {
                    return(null);
                }

                string[] split = domain.Split('@');

                ToxDns tox_dns = new ToxDns(new ToxKey(ToxKeyType.Public, public_key));
                int    request_id;
                string dns3_string = tox_dns.GenerateDns3String(split[0], out request_id);

                string query = string.Format("_{0}._tox.{1}", dns3_string, split[1]);

                string[] records = GetSPFRecords(query);

                foreach (string record in records)
                {
                    if (record.Contains("v=tox3"))
                    {
                        string[] entries = record.Split(';');

                        foreach (string entry in entries)
                        {
                            string[] parts = entry.Split('=');
                            string   name  = parts[0];
                            string   value = parts[1];

                            if (name == "id")
                            {
                                string result = tox_dns.DecryptDns3TXT(value, request_id);

                                tox_dns.Dispose();
                                return(result);
                            }
                        }
                    }
                }

                tox_dns.Dispose();
            }

            return(null);
        }