|
41 | 41 | #include <boost/algorithm/string/join.hpp> |
42 | 42 | #include <boost/optional.hpp> |
43 | 43 | #include <boost/utility/string_ref.hpp> |
| 44 | +#include <rapidjson/document.h> |
| 45 | + |
44 | 46 | using namespace epee; |
45 | 47 |
|
46 | 48 | #undef scala_DEFAULT_LOG_CATEGORY |
@@ -486,14 +488,85 @@ std::vector<std::string> addresses_from_url(const std::string& url, bool& dnssec |
486 | 488 |
|
487 | 489 | std::string get_account_address_as_str_from_url(const std::string& url, bool& dnssec_valid, std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> dns_confirm) |
488 | 490 | { |
489 | | - // attempt to get address from dns query |
490 | | - auto addresses = addresses_from_url(url, dnssec_valid); |
491 | | - if (addresses.empty()) |
| 491 | + std::vector<std::string> valid_ud_tlds{ ".crypto", ".nft", ".blockchain", ".bitcoin", ".wallet", ".888", ".dao", ".x", ".klever", ".zil", ".eth" }; |
| 492 | + |
| 493 | + bool valid_tld = false; |
| 494 | + |
| 495 | + for (const auto& tld : valid_ud_tlds) |
492 | 496 | { |
493 | | - LOG_ERROR("wrong address: " << url); |
494 | | - return {}; |
| 497 | + if (url.find(tld) != std::string::npos) |
| 498 | + { |
| 499 | + valid_tld = true; |
| 500 | + break; |
| 501 | + } |
495 | 502 | } |
496 | | - return dns_confirm(url, addresses, dnssec_valid); |
| 503 | + |
| 504 | + if (!valid_tld) |
| 505 | + { |
| 506 | + // attempt to get address from dns query |
| 507 | + auto addresses = addresses_from_url(url, dnssec_valid); |
| 508 | + if (addresses.empty()) |
| 509 | + { |
| 510 | + LOG_ERROR("wrong address: " << url); |
| 511 | + return {}; |
| 512 | + } |
| 513 | + return dns_confirm(url, addresses, dnssec_valid); |
| 514 | + } else { |
| 515 | + std::vector<std::string> addresses; |
| 516 | + std::vector<std::string> ud_addresses; |
| 517 | + std::string ud_bridge_api = "http://ud-bridge.scalaproject.io/fetch-records/" + url; |
| 518 | + |
| 519 | + epee::net_utils::http::http_simple_client client; |
| 520 | + epee::net_utils::http::url_content u_c; |
| 521 | + |
| 522 | + if (!epee::net_utils::parse_url(ud_bridge_api, u_c)){ |
| 523 | + MERROR("Failed to parse URL " << ud_bridge_api); |
| 524 | + return {}; |
| 525 | + } |
| 526 | + |
| 527 | + epee::net_utils::ssl_support_t ssl_requirement = epee::net_utils::ssl_support_t::e_ssl_support_disabled; |
| 528 | + uint16_t port = 80; |
| 529 | + |
| 530 | + client.set_server(u_c.host, std::to_string(port), boost::none, ssl_requirement); |
| 531 | + epee::net_utils::http::fields_list fields; |
| 532 | + const epee::net_utils::http::http_response_info *info = NULL; |
| 533 | + |
| 534 | + |
| 535 | + if (!client.invoke_get(u_c.uri, std::chrono::seconds(10), "", &info, fields)){ |
| 536 | +LOG_ERROR(ud_bridge_api << " is not responding, skipping."); |
| 537 | +return {}; |
| 538 | + } else{ |
| 539 | + if (info->m_response_code != 200){ |
| 540 | + LOG_ERROR("Failed to get address from " << ud_bridge_api << ", skipping."); |
| 541 | + return {}; |
| 542 | + } |
| 543 | + |
| 544 | + std::string response = info->m_body; |
| 545 | + rapidjson::Document doc; |
| 546 | + |
| 547 | + |
| 548 | + if (doc.Parse(response.c_str()).HasParseError()){ |
| 549 | + LOG_ERROR("Failed to parse response from " << ud_bridge_api << ", skipping."); |
| 550 | + return {}; |
| 551 | + } |
| 552 | + |
| 553 | + if (!doc.HasMember("records")){ |
| 554 | + LOG_ERROR("Failed to get records from " << ud_bridge_api << ", skipping."); |
| 555 | + return {}; |
| 556 | + } |
| 557 | + |
| 558 | + if (!doc["records"].HasMember("crypto.XLA.address")){ |
| 559 | + LOG_ERROR("Failed to get XLA address from " << ud_bridge_api << ", skipping."); |
| 560 | + return {}; |
| 561 | + } else { |
| 562 | + std::string address = doc["records"]["crypto.XLA.address"].GetString(); |
| 563 | + |
| 564 | + addresses.push_back(address); |
| 565 | + return dns_confirm(url, addresses, true); |
| 566 | + } |
| 567 | + } |
| 568 | + } |
| 569 | + |
497 | 570 | } |
498 | 571 |
|
499 | 572 | bool load_txt_records_from_dns(std::vector<std::string> &good_records, const std::vector<std::string> &dns_urls) |
|
0 commit comments