0


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

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

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

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

  1. shared.rollup.js
  2. class PrivacyPageBrowserProxyImpl {
  3. .................................................................
  4. getSecureDnsResolverList() {
  5. return sendWithPromise("getSecureDnsResolverList") //获取dns列表
  6. }
  7. getSecureDnsSetting() {
  8. return sendWithPromise("getSecureDnsSetting")
  9. }
  10. isValidConfig(entry) {
  11. return sendWithPromise("isValidConfig", entry) //检测dns是否正确
  12. }
  13. probeConfig(entry) {
  14. return sendWithPromise("probeConfig", entry)
  15. }
  16. static getInstance() {
  17. return instance$g || (instance$g = new PrivacyPageBrowserProxyImpl)
  18. }
  19. static setInstance(obj) {
  20. instance$g = obj
  21. }
  22. }

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

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

  1. void SecureDnsHandler::RegisterMessages() {
  2. web_ui()->RegisterMessageCallback(
  3. "getSecureDnsResolverList",
  4. base::BindRepeating(&SecureDnsHandler::HandleGetSecureDnsResolverList,
  5. base::Unretained(this)));
  6. web_ui()->RegisterMessageCallback(
  7. "getSecureDnsSetting",
  8. base::BindRepeating(&SecureDnsHandler::HandleGetSecureDnsSetting,
  9. base::Unretained(this)));
  10. web_ui()->RegisterMessageCallback(
  11. "isValidConfig",
  12. base::BindRepeating(&SecureDnsHandler::HandleIsValidConfig,
  13. base::Unretained(this)));
  14. web_ui()->RegisterMessageCallback(
  15. "probeConfig", base::BindRepeating(&SecureDnsHandler::HandleProbeConfig,
  16. base::Unretained(this)));
  17. web_ui()->RegisterMessageCallback(
  18. "recordUserDropdownInteraction",
  19. base::BindRepeating(
  20. &SecureDnsHandler::HandleRecordUserDropdownInteraction,
  21. base::Unretained(this)));
  22. }
  23. 1、先看 前端"getSecureDnsResolverList "dns列表对应c+++获取函数
  24. base::Value::List SecureDnsHandler::GetSecureDnsResolverList() {
  25. base::Value::List resolvers;
  26. // Add a custom option to the front of the list
  27. base::Value::Dict custom;
  28. custom.Set("name", l10n_util::GetStringUTF8(IDS_SETTINGS_CUSTOM));
  29. custom.Set("value", std::string()); // Empty value means custom.
  30. custom.Set("policy", std::string());
  31. resolvers.Append(std::move(custom));
  32. //providers_ 是dns数据列表来源,定义参考下面介绍
  33. for (const auto* entry : providers_) {
  34. net::DnsOverHttpsConfig doh_config({entry->doh_server_config});
  35. base::Value::Dict dict;
  36. dict.Set("name", entry->ui_name);
  37. dict.Set("value", doh_config.ToString());
  38. dict.Set("policy", entry->privacy_policy);
  39. resolvers.Append(std::move(dict));
  40. }
  41. // Randomize the order of the resolvers, but keep custom in first place.
  42. base::RandomShuffle(std::next(resolvers.begin()), resolvers.end());
  43. return resolvers;
  44. }
  45. 2、重点看providers_函数 ,其赋值和定义看代码:
  46. chrome\browser\ui\webui\settings\settings_secure_dns_handler.h
  47. static net::DohProviderEntry::List GetFilteredProviders();
  48. net::DohProviderEntry::List providers_ = GetFilteredProviders();
  49. std::unique_ptr<chrome_browser_net::DnsProbeRunner> runner_;
  50. chrome_browser_net::DnsProbeRunner::NetworkContextGetter
  51. network_context_getter_ =
  52. base::BindRepeating(&SecureDnsHandler::GetNetworkContext,
  53. base::Unretained(this));
  54. // static
  55. net::DohProviderEntry::List SecureDnsHandler::GetFilteredProviders() {
  56. return secure_dns::ProvidersForCountry(
  57. secure_dns::SelectEnabledProviders(net::DohProviderEntry::GetList()),
  58. country_codes::GetCurrentCountryID());
  59. }
  60. providers_ 通过GetFilteredProviders()获取列表
  61. 3、最后看列表定义net::DohProviderEntry::GetList()
  62. net\dns\public\doh_provider_entry.cc
  63. const DohProviderEntry::List& DohProviderEntry::GetList() {
  64. // See /net/docs/adding_doh_providers.md for instructions on modifying this
  65. // DoH provider list.
  66. //
  67. // The provider names in these entries should be kept in sync with the
  68. // DohProviderId histogram suffix list in
  69. // tools/metrics/histograms/metadata/histogram_suffixes_list.xml.
  70. static const base::NoDestructor<DohProviderEntry::List> providers{{
  71. new DohProviderEntry(
  72. "AlekBergNl",
  73. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  74. DohProviderAlekBergNl, base::FEATURE_ENABLED_BY_DEFAULT),
  75. DohProviderIdForHistogram::kAlekBergNl,
  76. /*ip_strs=*/{}, /*dns_over_tls_hostnames=*/{},
  77. "https://dnsnl.alekberg.net/dns-query{?dns}",
  78. /*ui_name=*/"alekberg.net (NL)",
  79. /*privacy_policy=*/"https://alekberg.net/privacy",
  80. /*display_globally=*/false,
  81. /*display_countries=*/{"NL"}, LoggingLevel::kNormal),
  82. new DohProviderEntry(
  83. "CleanBrowsingAdult",
  84. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  85. DohProviderCleanBrowsingAdult, base::FEATURE_ENABLED_BY_DEFAULT),
  86. /*provider_id_for_histogram=*/absl::nullopt,
  87. {"185.228.168.10", "185.228.169.11", "2a0d:2a00:1::1",
  88. "2a0d:2a00:2::1"},
  89. /*dns_over_tls_hostnames=*/{"adult-filter-dns.cleanbrowsing.org"},
  90. "https://doh.cleanbrowsing.org/doh/adult-filter{?dns}",
  91. /*ui_name=*/"", /*privacy_policy=*/"",
  92. /*display_globally=*/false, /*display_countries=*/{},
  93. LoggingLevel::kNormal),
  94. new DohProviderEntry(
  95. "CleanBrowsingFamily",
  96. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  97. DohProviderCleanBrowsingFamily, base::FEATURE_ENABLED_BY_DEFAULT),
  98. DohProviderIdForHistogram::kCleanBrowsingFamily,
  99. {"185.228.168.168", "185.228.169.168",
  100. "2a0d:2a00:1::", "2a0d:2a00:2::"},
  101. /*dns_over_tls_hostnames=*/{"family-filter-dns.cleanbrowsing.org"},
  102. "https://doh.cleanbrowsing.org/doh/family-filter{?dns}",
  103. /*ui_name=*/"CleanBrowsing (Family Filter)",
  104. /*privacy_policy=*/"https://cleanbrowsing.org/privacy",
  105. /*display_globally=*/true, /*display_countries=*/{},
  106. LoggingLevel::kNormal),
  107. new DohProviderEntry(
  108. "CleanBrowsingSecure",
  109. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  110. DohProviderCleanBrowsingSecure, base::FEATURE_ENABLED_BY_DEFAULT),
  111. /*provider_id_for_histogram=*/absl::nullopt,
  112. {"185.228.168.9", "185.228.169.9", "2a0d:2a00:1::2",
  113. "2a0d:2a00:2::2"},
  114. /*dns_over_tls_hostnames=*/{"security-filter-dns.cleanbrowsing.org"},
  115. "https://doh.cleanbrowsing.org/doh/security-filter{?dns}",
  116. /*ui_name=*/"", /*privacy_policy=*/"", /*display_globally=*/false,
  117. /*display_countries=*/{}, LoggingLevel::kNormal),
  118. new DohProviderEntry(
  119. "Cloudflare",
  120. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  121. DohProviderCloudflare, base::FEATURE_ENABLED_BY_DEFAULT),
  122. DohProviderIdForHistogram::kCloudflare,
  123. {"1.1.1.1", "1.0.0.1", "2606:4700:4700::1111",
  124. "2606:4700:4700::1001"},
  125. /*dns_over_tls_hostnames=*/
  126. {"one.one.one.one", "1dot1dot1dot1.cloudflare-dns.com"},
  127. "https://chrome.cloudflare-dns.com/dns-query",
  128. /*ui_name=*/"Cloudflare (1.1.1.1)",
  129. "https://developers.cloudflare.com/1.1.1.1/privacy/"
  130. /*privacy_policy=*/"public-dns-resolver/",
  131. /*display_globally=*/true, /*display_countries=*/{},
  132. LoggingLevel::kExtra),
  133. new DohProviderEntry(
  134. "Comcast",
  135. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  136. DohProviderComcast, base::FEATURE_ENABLED_BY_DEFAULT),
  137. /*provider_id_for_histogram=*/absl::nullopt,
  138. {"75.75.75.75", "75.75.76.76", "2001:558:feed::1",
  139. "2001:558:feed::2"},
  140. /*dns_over_tls_hostnames=*/{"dot.xfinity.com"},
  141. "https://doh.xfinity.com/dns-query{?dns}", /*ui_name=*/"",
  142. /*privacy_policy*/ "", /*display_globally=*/false,
  143. /*display_countries=*/{}, LoggingLevel::kExtra),
  144. new DohProviderEntry(
  145. "Cox",
  146. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  147. DohProviderCox, base::FEATURE_DISABLED_BY_DEFAULT),
  148. /*provider_id_for_histogram=*/absl::nullopt,
  149. {"68.105.28.11", "68.105.28.12", "2001:578:3f::30"},
  150. /*dns_over_tls_hostnames=*/{"dot.cox.net"},
  151. "https://doh.cox.net/dns-query",
  152. /*ui_name=*/"", /*privacy_policy=*/"",
  153. /*display_globally=*/false, /*display_countries=*/{},
  154. LoggingLevel::kNormal),
  155. new DohProviderEntry(
  156. "Cznic",
  157. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  158. DohProviderCznic, base::FEATURE_ENABLED_BY_DEFAULT),
  159. DohProviderIdForHistogram::kCznic,
  160. {"185.43.135.1", "193.17.47.1", "2001:148f:fffe::1",
  161. "2001:148f:ffff::1"},
  162. /*dns_over_tls_hostnames=*/{"odvr.nic.cz"}, "https://odvr.nic.cz/doh",
  163. /*ui_name=*/"CZ.NIC ODVR",
  164. /*privacy_policy=*/"https://www.nic.cz/odvr/",
  165. /*display_globally=*/false, /*display_countries=*/{"CZ"},
  166. LoggingLevel::kNormal),
  167. new DohProviderEntry(
  168. "Dnssb",
  169. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  170. DohProviderDnssb, base::FEATURE_ENABLED_BY_DEFAULT),
  171. DohProviderIdForHistogram::kDnsSb,
  172. {"185.222.222.222", "45.11.45.11", "2a09::", "2a11::"},
  173. /*dns_over_tls_hostnames=*/{"dns.sb"},
  174. "https://doh.dns.sb/dns-query{?dns}", /*ui_name=*/"DNS.SB",
  175. /*privacy_policy=*/"https://dns.sb/privacy/",
  176. /*display_globally=*/false, /*display_countries=*/{"EE", "DE"},
  177. LoggingLevel::kNormal),
  178. new DohProviderEntry(
  179. "Google",
  180. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  181. DohProviderGoogle, base::FEATURE_ENABLED_BY_DEFAULT),
  182. DohProviderIdForHistogram::kGoogle,
  183. {"8.8.8.8", "8.8.4.4", "2001:4860:4860::8888",
  184. "2001:4860:4860::8844"},
  185. /*dns_over_tls_hostnames=*/
  186. {"dns.google", "dns.google.com", "8888.google"},
  187. "https://dns.google/dns-query{?dns}",
  188. /*ui_name=*/"Google (Public DNS)",
  189. "https://developers.google.com/speed/public-dns/"
  190. /*privacy_policy=*/"privacy",
  191. /*display_globally=*/true, /*display_countries=*/{},
  192. LoggingLevel::kExtra),
  193. new DohProviderEntry(
  194. "GoogleDns64",
  195. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  196. DohProviderGoogleDns64, base::FEATURE_ENABLED_BY_DEFAULT),
  197. /*provider_id_for_histogram=*/absl::nullopt,
  198. {"2001:4860:4860::64", "2001:4860:4860::6464"},
  199. /*dns_over_tls_hostnames=*/{"dns64.dns.google"},
  200. "https://dns64.dns.google/dns-query{?dns}",
  201. /*ui_name=*/"", /*privacy_policy=*/"",
  202. /*display_globally=*/false,
  203. /*display_countries=*/{}, LoggingLevel::kNormal),
  204. new DohProviderEntry(
  205. "Iij",
  206. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  207. DohProviderIij, base::FEATURE_ENABLED_BY_DEFAULT),
  208. DohProviderIdForHistogram::kIij, /*ip_strs=*/{},
  209. /*dns_over_tls_hostnames=*/{}, "https://public.dns.iij.jp/dns-query",
  210. /*ui_name=*/"IIJ (Public DNS)",
  211. /*privacy_policy=*/"https://public.dns.iij.jp/",
  212. /*display_globally=*/false, /*display_countries=*/{"JP"},
  213. LoggingLevel::kNormal),
  214. new DohProviderEntry(
  215. "NextDns",
  216. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  217. DohProviderNextDns, base::FEATURE_ENABLED_BY_DEFAULT),
  218. DohProviderIdForHistogram::kNextDns, /*ip_strs=*/{},
  219. /*dns_over_tls_hostnames=*/{}, "https://chromium.dns.nextdns.io",
  220. /*ui_name=*/"NextDNS",
  221. /*privacy_policy=*/"https://nextdns.io/privacy",
  222. /*display_globally=*/false, /*display_countries=*/{"US"},
  223. LoggingLevel::kNormal),
  224. new DohProviderEntry(
  225. "OpenDNS",
  226. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  227. DohProviderOpenDNS, base::FEATURE_ENABLED_BY_DEFAULT),
  228. DohProviderIdForHistogram::kOpenDns,
  229. {"208.67.222.222", "208.67.220.220", "2620:119:35::35",
  230. "2620:119:53::53"},
  231. /*dns_over_tls_hostnames=*/{},
  232. "https://doh.opendns.com/dns-query{?dns}", /*ui_name=*/"OpenDNS",
  233. "https://www.cisco.com/c/en/us/about/legal/"
  234. /*privacy_policy=*/"privacy-full.html",
  235. /*display_globally=*/true, /*display_countries=*/{},
  236. LoggingLevel::kNormal),
  237. new DohProviderEntry(
  238. "OpenDNSFamily",
  239. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  240. DohProviderOpenDNSFamily, base::FEATURE_ENABLED_BY_DEFAULT),
  241. /*provider_id_for_histogram=*/absl::nullopt,
  242. {"208.67.222.123", "208.67.220.123", "2620:119:35::123",
  243. "2620:119:53::123"},
  244. /*dns_over_tls_hostnames=*/{},
  245. "https://doh.familyshield.opendns.com/dns-query{?dns}",
  246. /*ui_name=*/"", /*privacy_policy=*/"", /*display_globally=*/false,
  247. /*display_countries=*/{}, LoggingLevel::kNormal),
  248. new DohProviderEntry(
  249. "Quad9Cdn",
  250. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  251. DohProviderQuad9Cdn, base::FEATURE_ENABLED_BY_DEFAULT),
  252. /*provider_id_for_histogram=*/absl::nullopt,
  253. {"9.9.9.11", "149.112.112.11", "2620:fe::11", "2620:fe::fe:11"},
  254. /*dns_over_tls_hostnames=*/{"dns11.quad9.net"},
  255. "https://dns11.quad9.net/dns-query", /*ui_name=*/"",
  256. /*privacy_policy=*/"", /*display_globally=*/false,
  257. /*display_countries=*/{}, LoggingLevel::kNormal),
  258. new DohProviderEntry(
  259. "Quad9Insecure",
  260. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  261. DohProviderQuad9Insecure, base::FEATURE_ENABLED_BY_DEFAULT),
  262. /*provider_id_for_histogram=*/absl::nullopt,
  263. {"9.9.9.10", "149.112.112.10", "2620:fe::10", "2620:fe::fe:10"},
  264. /*dns_over_tls_hostnames=*/{"dns10.quad9.net"},
  265. "https://dns10.quad9.net/dns-query", /*ui_name=*/"",
  266. /*privacy_policy=*/"", /*display_globally=*/false,
  267. /*display_countries=*/{}, LoggingLevel::kNormal),
  268. new DohProviderEntry(
  269. "Quad9Secure",
  270. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  271. DohProviderQuad9Secure, base::FEATURE_DISABLED_BY_DEFAULT),
  272. DohProviderIdForHistogram::kQuad9Secure,
  273. {"9.9.9.9", "149.112.112.112", "2620:fe::fe", "2620:fe::9"},
  274. /*dns_over_tls_hostnames=*/{"dns.quad9.net", "dns9.quad9.net"},
  275. "https://dns.quad9.net/dns-query", /*ui_name=*/"Quad9 (9.9.9.9)",
  276. /*privacy_policy=*/"https://www.quad9.net/home/privacy/",
  277. /*display_globally=*/true, /*display_countries=*/{},
  278. LoggingLevel::kExtra),
  279. new DohProviderEntry(
  280. "Quickline",
  281. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  282. DohProviderQuickline, base::FEATURE_ENABLED_BY_DEFAULT),
  283. /*provider_id_for_histogram=*/absl::nullopt,
  284. {"212.60.61.246", "212.60.63.246", "2001:1a88:10:ffff::1",
  285. "2001:1a88:10:ffff::2"},
  286. /*dns_over_tls_hostnames=*/{"dot.quickline.ch"},
  287. "https://doh.quickline.ch/dns-query{?dns}",
  288. /*ui_name=*/"", /*privacy_policy=*/"",
  289. /*display_globally=*/false,
  290. /*display_countries=*/{}, LoggingLevel::kNormal),
  291. new DohProviderEntry(
  292. "Spectrum1",
  293. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  294. DohProviderSpectrum1, base::FEATURE_ENABLED_BY_DEFAULT),
  295. /*provider_id_for_histogram=*/absl::nullopt,
  296. {"209.18.47.61", "209.18.47.62", "2001:1998:0f00:0001::1",
  297. "2001:1998:0f00:0002::1"},
  298. /*dns_over_tls_hostnames=*/{},
  299. "https://doh-01.spectrum.com/dns-query{?dns}",
  300. /*ui_name=*/"", /*privacy_policy=*/"",
  301. /*display_globally=*/false,
  302. /*display_countries=*/{}, LoggingLevel::kNormal),
  303. new DohProviderEntry(
  304. "Spectrum2",
  305. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  306. DohProviderSpectrum2, base::FEATURE_ENABLED_BY_DEFAULT),
  307. /*provider_id_for_histogram=*/absl::nullopt,
  308. {"209.18.47.61", "209.18.47.62", "2001:1998:0f00:0001::1",
  309. "2001:1998:0f00:0002::1"},
  310. /*dns_over_tls_hostnames=*/{},
  311. "https://doh-02.spectrum.com/dns-query{?dns}",
  312. /*ui_name=*/"", /*privacy_policy=*/"",
  313. /*display_globally=*/false,
  314. /*display_countries=*/{}, LoggingLevel::kNormal),
  315. new DohProviderEntry(
  316. "Switch",
  317. MAKE_BASE_FEATURE_WITH_STATIC_STORAGE(
  318. DohProviderSwitch, base::FEATURE_DISABLED_BY_DEFAULT),
  319. /*provider_id_for_histogram=*/absl::nullopt,
  320. {"130.59.31.251", "130.59.31.248", "2001:620:0:ff::2",
  321. "2001:620:0:ff::3"},
  322. /*dns_over_tls_hostnames=*/{"dns.switch.ch"},
  323. "https://dns.switch.ch/dns-query", /*ui_name=*/"",
  324. /*privacy_policy=*/"", /*display_globally=*/false,
  325. /*display_countries=*/{}, LoggingLevel::kNormal),
  326. }};
  327. return *providers;
  328. }

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

标签: 安全 c++ windows

本文转载自: https://blog.csdn.net/jangdong/article/details/142633166
版权归原作者 风清扬_jd 所有, 如有侵权,请联系我们删除。

“Chromium 使用安全 DNS功能源码分析c++”的评论:

还没有评论