Chromium 使用安全 DNS功能源码分析c++

一、选项页安全dns选项如下图:

二、那么如何自定义安全dns功能呢?

1、先看前端部分代码调用

shared.rollup.js

class PrivacyPageBrowserProxyImpl {
     .................................................................
    getSecureDnsResolverList() {
        return sendWithPromise("getSecureDnsResolverList")  //获取dns列表
    }
    getSecureDnsSetting() {
        return sendWithPromise("getSecureDnsSetting")     
    }
    isValidConfig(entry) {
        return sendWithPromise("isValidConfig", entry)  //检测dns是否正确
    }
    probeConfig(entry) {
        return sendWithPromise("probeConfig", entry)
    }
    static getInstance() {
        return instance$g || (instance$g = new PrivacyPageBrowserProxyImpl)
    }
    static setInstance(obj) {
        instance$g = obj
    }
}

2、看c++代码对应的注册函数

chrome\browser\ui\webui\settings\settings_secure_dns_handler.cc

void SecureDnsHandler::RegisterMessages() {
  web_ui()->RegisterMessageCallback(
      "getSecureDnsResolverList",
      base::BindRepeating(&SecureDnsHandler::HandleGetSecureDnsResolverList,
                          base::Unretained(this)));

  web_ui()->RegisterMessageCallback(
      "getSecureDnsSetting",
      base::BindRepeating(&SecureDnsHandler::HandleGetSecureDnsSetting,
                          base::Unretained(this)));

  web_ui()->RegisterMessageCallback(
      "isValidConfig",
      base::BindRepeating(&SecureDnsHandler::HandleIsValidConfig,
                          base::Unretained(this)));

  web_ui()->RegisterMessageCallback(
      "probeConfig", base::BindRepeating(&SecureDnsHandler::HandleProbeConfig,
                                         base::Unretained(this)));

  web_ui()->RegisterMessageCallback(
      "recordUserDropdownInteraction",
      base::BindRepeating(
          &SecureDnsHandler::HandleRecordUserDropdownInteraction,
          base::Unretained(this)));
}

1、先看 前端"getSecureDnsResolverList "dns列表对应c+++获取函数
base::Value::List SecureDnsHandler::GetSecureDnsResolverList() {
  base::Value::List resolvers;

  // Add a custom option to the front of the list
  base::Value::Dict custom;
  custom.Set("name", l10n_util::GetStringUTF8(IDS_SETTINGS_CUSTOM));
  custom.Set("value", std::string());  // Empty value means custom.
  custom.Set("policy", std::string());
  resolvers.Append(std::move(custom));
  
 //providers_ 是dns数据列表来源,定义参考下面介绍
  for (const auto* entry : providers_) {
    net::DnsOverHttpsConfig doh_config({entry->doh_server_config});
    base::Value::Dict dict;
    dict.Set("name", entry->ui_name);
    dict.Set("value", doh_config.ToString());
    dict.Set("policy", entry->privacy_policy);
    resolvers.Append(std::move(dict));
  }

  // Randomize the order of the resolvers, but keep custom in first place.
  base::RandomShuffle(std::next(resolvers.begin()), resolvers.end());

  return resolvers;
}

2、重点看providers_函数 ,其赋值和定义看代码:

chrome\browser\ui\webui\settings\settings_secure_dns_handler.h
  static net::DohProviderEntry::List GetFilteredProviders();

  net::DohProviderEntry::List providers_ = GetFilteredProviders();
  std::unique_ptr<chrome_browser_net::DnsProbeRunner> runner_;
  chrome_browser_net::DnsProbeRunner::NetworkContextGetter
      network_context_getter_ =
          base::BindRepeating(&SecureDnsHandler::GetNetworkContext,
                              base::Unretained(this));

// static
net::DohProviderEntry::List SecureDnsHandler::GetFilteredProviders() {
  return secure_dns::ProvidersForCountry(
      secure_dns::SelectEnabledProviders(net::DohProviderEntry::GetList()),
      country_codes::GetCurrentCountryID());
}

providers_ 通过GetFilteredProviders()获取列表

3、最后看列表定义net::DohProviderEntry::GetList()
net\dns\public\doh_provider_entry.cc
const DohProviderEntry::List& DohProviderEntry::GetList() {
  // See /net/docs/adding_doh_providers.md for instructions on modifying this
  // DoH provider list.
  //
  // The provider names in these entries should be kept in sync with the
  // DohProviderId histogram suffix list in
  // tools/metrics/histograms/metadata/histogram_suffixes_list.xml.
  static const base::NoDestructor<DohProviderEntry::List> providers{{
      new DohProviderEntry(
          "AlekBergNl",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderAlekBergNl, base::FEATURE_ENABLED_BY_DEFAULT),
          DohProviderIdForHistogram::kAlekBergNl,
          /*ip_strs=*/{}, /*dns_over_tls_hostnames=*/{},
          "https://dnsnl.alekberg.net/dns-query{?dns}",
          /*ui_name=*/"alekberg.net (NL)",
          /*privacy_policy=*/"https://alekberg.net/privacy",
          /*display_globally=*/false,
          /*display_countries=*/{"NL"}, LoggingLevel::kNormal),
      new DohProviderEntry(
          "CleanBrowsingAdult",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderCleanBrowsingAdult, base::FEATURE_ENABLED_BY_DEFAULT),
          /*provider_id_for_histogram=*/absl::nullopt,
          {"185.228.168.10", "185.228.169.11", "2a0d:2a00:1::1",
           "2a0d:2a00:2::1"},
          /*dns_over_tls_hostnames=*/{"adult-filter-dns.cleanbrowsing.org"},
          "https://doh.cleanbrowsing.org/doh/adult-filter{?dns}",
          /*ui_name=*/"", /*privacy_policy=*/"",
          /*display_globally=*/false, /*display_countries=*/{},
          LoggingLevel::kNormal),
      new DohProviderEntry(
          "CleanBrowsingFamily",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderCleanBrowsingFamily, base::FEATURE_ENABLED_BY_DEFAULT),
          DohProviderIdForHistogram::kCleanBrowsingFamily,
          {"185.228.168.168", "185.228.169.168",
           "2a0d:2a00:1::", "2a0d:2a00:2::"},
          /*dns_over_tls_hostnames=*/{"family-filter-dns.cleanbrowsing.org"},
          "https://doh.cleanbrowsing.org/doh/family-filter{?dns}",
          /*ui_name=*/"CleanBrowsing (Family Filter)",
          /*privacy_policy=*/"https://cleanbrowsing.org/privacy",
          /*display_globally=*/true, /*display_countries=*/{},
          LoggingLevel::kNormal),
      new DohProviderEntry(
          "CleanBrowsingSecure",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderCleanBrowsingSecure, base::FEATURE_ENABLED_BY_DEFAULT),
          /*provider_id_for_histogram=*/absl::nullopt,
          {"185.228.168.9", "185.228.169.9", "2a0d:2a00:1::2",
           "2a0d:2a00:2::2"},
          /*dns_over_tls_hostnames=*/{"security-filter-dns.cleanbrowsing.org"},
          "https://doh.cleanbrowsing.org/doh/security-filter{?dns}",
          /*ui_name=*/"", /*privacy_policy=*/"", /*display_globally=*/false,
          /*display_countries=*/{}, LoggingLevel::kNormal),
      new DohProviderEntry(
          "Cloudflare",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderCloudflare, base::FEATURE_ENABLED_BY_DEFAULT),
          DohProviderIdForHistogram::kCloudflare,
          {"1.1.1.1", "1.0.0.1", "2606:4700:4700::1111",
           "2606:4700:4700::1001"},
          /*dns_over_tls_hostnames=*/
          {"one.one.one.one", "1dot1dot1dot1.cloudflare-dns.com"},
          "https://chrome.cloudflare-dns.com/dns-query",
          /*ui_name=*/"Cloudflare (1.1.1.1)",
          "https://developers.cloudflare.com/1.1.1.1/privacy/"
          /*privacy_policy=*/"public-dns-resolver/",
          /*display_globally=*/true, /*display_countries=*/{},
          LoggingLevel::kExtra),
      new DohProviderEntry(
          "Comcast",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderComcast, base::FEATURE_ENABLED_BY_DEFAULT),
          /*provider_id_for_histogram=*/absl::nullopt,
          {"75.75.75.75", "75.75.76.76", "2001:558:feed::1",
           "2001:558:feed::2"},
          /*dns_over_tls_hostnames=*/{"dot.xfinity.com"},
          "https://doh.xfinity.com/dns-query{?dns}", /*ui_name=*/"",
          /*privacy_policy*/ "", /*display_globally=*/false,
          /*display_countries=*/{}, LoggingLevel::kExtra),
      new DohProviderEntry(
          "Cox",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderCox, base::FEATURE_DISABLED_BY_DEFAULT),
          /*provider_id_for_histogram=*/absl::nullopt,
          {"68.105.28.11", "68.105.28.12", "2001:578:3f::30"},
          /*dns_over_tls_hostnames=*/{"dot.cox.net"},
          "https://doh.cox.net/dns-query",
          /*ui_name=*/"", /*privacy_policy=*/"",
          /*display_globally=*/false, /*display_countries=*/{},
          LoggingLevel::kNormal),
      new DohProviderEntry(
          "Cznic",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderCznic, base::FEATURE_ENABLED_BY_DEFAULT),
          DohProviderIdForHistogram::kCznic,
          {"185.43.135.1", "193.17.47.1", "2001:148f:fffe::1",
           "2001:148f:ffff::1"},
          /*dns_over_tls_hostnames=*/{"odvr.nic.cz"}, "https://odvr.nic.cz/doh",
          /*ui_name=*/"CZ.NIC ODVR",
          /*privacy_policy=*/"https://www.nic.cz/odvr/",
          /*display_globally=*/false, /*display_countries=*/{"CZ"},
          LoggingLevel::kNormal),
      new DohProviderEntry(
          "Dnssb",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderDnssb, base::FEATURE_ENABLED_BY_DEFAULT),
          DohProviderIdForHistogram::kDnsSb,
          {"185.222.222.222", "45.11.45.11", "2a09::", "2a11::"},
          /*dns_over_tls_hostnames=*/{"dns.sb"},
          "https://doh.dns.sb/dns-query{?dns}", /*ui_name=*/"DNS.SB",
          /*privacy_policy=*/"https://dns.sb/privacy/",
          /*display_globally=*/false, /*display_countries=*/{"EE", "DE"},
          LoggingLevel::kNormal),
      new DohProviderEntry(
          "Google",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderGoogle, base::FEATURE_ENABLED_BY_DEFAULT),
          DohProviderIdForHistogram::kGoogle,
          {"8.8.8.8", "8.8.4.4", "2001:4860:4860::8888",
           "2001:4860:4860::8844"},
          /*dns_over_tls_hostnames=*/
          {"dns.google", "dns.google.com", "8888.google"},
          "https://dns.google/dns-query{?dns}",
          /*ui_name=*/"Google (Public DNS)",
          "https://developers.google.com/speed/public-dns/"
          /*privacy_policy=*/"privacy",
          /*display_globally=*/true, /*display_countries=*/{},
          LoggingLevel::kExtra),
      new DohProviderEntry(
          "GoogleDns64",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderGoogleDns64, base::FEATURE_ENABLED_BY_DEFAULT),
          /*provider_id_for_histogram=*/absl::nullopt,
          {"2001:4860:4860::64", "2001:4860:4860::6464"},
          /*dns_over_tls_hostnames=*/{"dns64.dns.google"},
          "https://dns64.dns.google/dns-query{?dns}",
          /*ui_name=*/"", /*privacy_policy=*/"",
          /*display_globally=*/false,
          /*display_countries=*/{}, LoggingLevel::kNormal),
      new DohProviderEntry(
          "Iij",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderIij, base::FEATURE_ENABLED_BY_DEFAULT),
          DohProviderIdForHistogram::kIij, /*ip_strs=*/{},
          /*dns_over_tls_hostnames=*/{}, "https://public.dns.iij.jp/dns-query",
          /*ui_name=*/"IIJ (Public DNS)",
          /*privacy_policy=*/"https://public.dns.iij.jp/",
          /*display_globally=*/false, /*display_countries=*/{"JP"},
          LoggingLevel::kNormal),
      new DohProviderEntry(
          "NextDns",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderNextDns, base::FEATURE_ENABLED_BY_DEFAULT),
          DohProviderIdForHistogram::kNextDns, /*ip_strs=*/{},
          /*dns_over_tls_hostnames=*/{}, "https://chromium.dns.nextdns.io",
          /*ui_name=*/"NextDNS",
          /*privacy_policy=*/"https://nextdns.io/privacy",
          /*display_globally=*/false, /*display_countries=*/{"US"},
          LoggingLevel::kNormal),
      new DohProviderEntry(
          "OpenDNS",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderOpenDNS, base::FEATURE_ENABLED_BY_DEFAULT),
          DohProviderIdForHistogram::kOpenDns,
          {"208.67.222.222", "208.67.220.220", "2620:119:35::35",
           "2620:119:53::53"},
          /*dns_over_tls_hostnames=*/{},
          "https://doh.opendns.com/dns-query{?dns}", /*ui_name=*/"OpenDNS",
          "https://www.cisco.com/c/en/us/about/legal/"
          /*privacy_policy=*/"privacy-full.html",
          /*display_globally=*/true, /*display_countries=*/{},
          LoggingLevel::kNormal),
      new DohProviderEntry(
          "OpenDNSFamily",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderOpenDNSFamily, base::FEATURE_ENABLED_BY_DEFAULT),
          /*provider_id_for_histogram=*/absl::nullopt,
          {"208.67.222.123", "208.67.220.123", "2620:119:35::123",
           "2620:119:53::123"},
          /*dns_over_tls_hostnames=*/{},
          "https://doh.familyshield.opendns.com/dns-query{?dns}",
          /*ui_name=*/"", /*privacy_policy=*/"", /*display_globally=*/false,
          /*display_countries=*/{}, LoggingLevel::kNormal),
      new DohProviderEntry(
          "Quad9Cdn",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderQuad9Cdn, base::FEATURE_ENABLED_BY_DEFAULT),
          /*provider_id_for_histogram=*/absl::nullopt,
          {"9.9.9.11", "149.112.112.11", "2620:fe::11", "2620:fe::fe:11"},
          /*dns_over_tls_hostnames=*/{"dns11.quad9.net"},
          "https://dns11.quad9.net/dns-query", /*ui_name=*/"",
          /*privacy_policy=*/"", /*display_globally=*/false,
          /*display_countries=*/{}, LoggingLevel::kNormal),
      new DohProviderEntry(
          "Quad9Insecure",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderQuad9Insecure, base::FEATURE_ENABLED_BY_DEFAULT),
          /*provider_id_for_histogram=*/absl::nullopt,
          {"9.9.9.10", "149.112.112.10", "2620:fe::10", "2620:fe::fe:10"},
          /*dns_over_tls_hostnames=*/{"dns10.quad9.net"},
          "https://dns10.quad9.net/dns-query", /*ui_name=*/"",
          /*privacy_policy=*/"", /*display_globally=*/false,
          /*display_countries=*/{}, LoggingLevel::kNormal),
      new DohProviderEntry(
          "Quad9Secure",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderQuad9Secure, base::FEATURE_DISABLED_BY_DEFAULT),
          DohProviderIdForHistogram::kQuad9Secure,
          {"9.9.9.9", "149.112.112.112", "2620:fe::fe", "2620:fe::9"},
          /*dns_over_tls_hostnames=*/{"dns.quad9.net", "dns9.quad9.net"},
          "https://dns.quad9.net/dns-query", /*ui_name=*/"Quad9 (9.9.9.9)",
          /*privacy_policy=*/"https://www.quad9.net/home/privacy/",
          /*display_globally=*/true, /*display_countries=*/{},
          LoggingLevel::kExtra),
      new DohProviderEntry(
          "Quickline",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderQuickline, base::FEATURE_ENABLED_BY_DEFAULT),
          /*provider_id_for_histogram=*/absl::nullopt,
          {"212.60.61.246", "212.60.63.246", "2001:1a88:10:ffff::1",
           "2001:1a88:10:ffff::2"},
          /*dns_over_tls_hostnames=*/{"dot.quickline.ch"},
          "https://doh.quickline.ch/dns-query{?dns}",
          /*ui_name=*/"", /*privacy_policy=*/"",
          /*display_globally=*/false,
          /*display_countries=*/{}, LoggingLevel::kNormal),
      new DohProviderEntry(
          "Spectrum1",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderSpectrum1, base::FEATURE_ENABLED_BY_DEFAULT),
          /*provider_id_for_histogram=*/absl::nullopt,
          {"209.18.47.61", "209.18.47.62", "2001:1998:0f00:0001::1",
           "2001:1998:0f00:0002::1"},
          /*dns_over_tls_hostnames=*/{},
          "https://doh-01.spectrum.com/dns-query{?dns}",
          /*ui_name=*/"", /*privacy_policy=*/"",
          /*display_globally=*/false,
          /*display_countries=*/{}, LoggingLevel::kNormal),
      new DohProviderEntry(
          "Spectrum2",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderSpectrum2, base::FEATURE_ENABLED_BY_DEFAULT),
          /*provider_id_for_histogram=*/absl::nullopt,
          {"209.18.47.61", "209.18.47.62", "2001:1998:0f00:0001::1",
           "2001:1998:0f00:0002::1"},
          /*dns_over_tls_hostnames=*/{},
          "https://doh-02.spectrum.com/dns-query{?dns}",
          /*ui_name=*/"", /*privacy_policy=*/"",
          /*display_globally=*/false,
          /*display_countries=*/{}, LoggingLevel::kNormal),
      new DohProviderEntry(
          "Switch",
          MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
              DohProviderSwitch, base::FEATURE_DISABLED_BY_DEFAULT),
          /*provider_id_for_histogram=*/absl::nullopt,
          {"130.59.31.251", "130.59.31.248", "2001:620:0:ff::2",
           "2001:620:0:ff::3"},
          /*dns_over_tls_hostnames=*/{"dns.switch.ch"},
          "https://dns.switch.ch/dns-query", /*ui_name=*/"",
          /*privacy_policy=*/"", /*display_globally=*/false,
          /*display_countries=*/{}, LoggingLevel::kNormal),
  }};
  return *providers;
}

    
三、至此数据来源分析完毕,如果想要用自己的dns只需要在
net\dns\public\doh_provider_entry.cc
const DohProviderEntry::List& DohProviderEntry::GetList() 函数里面按照此格式追加即可。
不同版本内核的浏览器有所差异。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值