public static string DiscoverToxID(string domain, ToxNameService[] services, bool localStoreOnly) { ToxNameService service; if (!localStoreOnly) service = FindNameService(domain.Split('@')[1]) ?? FindNameServiceFromStore(services, domain.Split('@')[1]); else service = FindNameServiceFromStore(services, domain.Split('@')[1]); if (service == null) { //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 { 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)); uint 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; }
private static ToxNameService FindNameServiceFromStore(ToxNameService[] services, string suffix) { return services.Where(s => s.Domain == suffix).First(); }