gpt4 book ai didi

c++ - 当向 CURLOPT_DNS_SERVERS 提供错误 ip 时,Curl 返回全部正常

转载 作者:行者123 更新时间:2023-11-30 17:43:41 27 4
gpt4 key购买 nike

我制作了一个多线程 (pthread) C++ 程序,该程序配置为使用自定义 dns 列表。在我的测试中,我使用 google 的 8.8.8.8 作为良好的 ex,并使用一些随机 IP(例如 113.65.123.138、13.23.123.87)来测试失败。但两种情况下一切都一样,都成功了。

Curl 是在 C-ares 支持下构建的,我已经进行了测试以确保:

curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
cout<<endl<<"Curl version: "<< data->version <<endl
<<"AsyncDNS: "<<( data->features | CURL_VERSION_ASYNCHDNS ? "YES" : "NO" ) <<endl;
//output: Curl version: 7.30.0 \n AsyncDNS: YES

其余代码:

curl_easy_setopt(curl, CURLOPT_DNS_SERVERS, thisThreadData->current_dns->dns_str.c_str()); 

curl_easy_setopt(curl, CURLOPT_DNS_USE_GLOBAL_CACHE,false); //thread safety
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, CONNECT_TIMEOUT);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, CONNECTION_TIMEOUT);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_to_string);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &getUrlOutput->header);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &getUrlOutput->html);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip,deflate");

status=curl_easy_perform(curl);

我已经测试了随机 IP(以防万一我偶然发现了一些有效的 DNS):

$ host google.com 113.65.123.138
;; connection timed out; no servers could be reached
$ host google.com 13.23.123.87
;; connection timed out; no servers could be reached

我错过了什么?

更新

我尝试了 libcurl 的最新版本 (7.33.0) 和 c-ares (1.10.0),结果相同。

此外,如果我为网址提供了错误的域,它会返回 CURLE_HTTP_RETURNED_ERROR (22),而不是 CURLE_COULDNT_RESOLVE_HOST (6)

更新2

忘记提及我使用了 HTML_PROXY 进行连接,这似乎是一个重要方面,请参阅答案。

最佳答案

根据 curl forums ,这是预期的行为,因为 dns 由代理处理。

When I am using a proxy, how do I (or can I) control whether the target url is resolved locally or by the proxy? I have conditions where I need both choices.

使用 HTTP 代理,客户端(curl)将完整的 URL 交给代理,代理将解析主机名
如果您真的想在客户端执行此操作,那么您需要 首先解析名称,然后“重新排列”URL 以使用 IP 仅数字并设置 Host: header 以包含您的主机名 已解决。

在我的例子中,当代理的 DNS 发现错误时,它会返回 html 格式的错误页面,其中包含消息“找不到主机”,html_status 503,这就是为什么curl通过了 dns 检查并表示域正常,但失败并显示 CURLE_HTTP_RETURNED_ERROR

关于c++ - 当向 CURLOPT_DNS_SERVERS 提供错误 ip 时,Curl 返回全部正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20194190/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com