Improve: getPublicIP may fail and block mater program

This commit is contained in:
yuanyuanxiang
2025-08-20 13:32:09 +08:00
parent 43eb3dfba4
commit 34b00787d2
3 changed files with 48 additions and 17 deletions

View File

@@ -183,22 +183,28 @@ public:
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>IP, <20><>ȡʧ<C8A1>ܷ<EFBFBD><DCB7>ؿ<EFBFBD>
std::string getPublicIP() {
clock_t t = clock();
HINTERNET hInternet, hConnect;
DWORD bytesRead;
char buffer[1024] = { 0 };
hInternet = InternetOpen("Mozilla/5.0", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (!hInternet) return "";
if (!hInternet) {
Mprintf("getPublicIP failed cost %d ms.\n", clock() - t);
return "";
}
hConnect = InternetOpenUrl(hInternet, "https://api.ipify.org", NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_SECURE, 0);
if (!hConnect) {
InternetCloseHandle(hInternet);
Mprintf("getPublicIP failed cost %d ms.\n", clock() - t);
return "";
}
InternetReadFile(hConnect, buffer, sizeof(buffer) - 1, &bytesRead);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
Mprintf("getPublicIP succeed cost %d ms.\n", clock() - t);
return std::string(buffer);
}