WinInet how to use sock proxy

2019-09-14 22:46发布

问题:

I'd like to use WinInet to access a web page but with a sock4/sock5 proxy.

Here is what I've tried

HINTERNET hOpen = InternetOpenA(helper.USER_AGENT, INTERNET_OPEN_TYPE_PROXY, ("SOCKS5=200.100.5.1:702", NULL, 0);

This doesn't seem to work, I'm supplying an invalid sock proxy so that when it does the web page connection it should not go through, but in my tests it's going through meaning the sock proxy is not being used but rather my actual plain internet.

I've even made a helper function to set the proxy in InternetSetOption as well

bool changeProxy(const char* socket) {
char strProxyList[MAX_PATH];
bool success = false;

memset(strProxyList, 0, MAX_PATH);

lstrcpyA(strProxyList, "SOCKS5=");

INTERNET_PROXY_INFO proxy;

proxy.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
proxy.lpszProxy = (LPCTSTR)strProxyList;
proxy.lpszProxyBypass = NULL;

if (TRUE == InternetSetOptionA(hOpen, INTERNET_OPTION_PROXY, &proxy, sizeof(INTERNET_PROXY_INFO)))
{
    printf("Socket set to %s\n", socket);
    success = true;
}
else
{
    printf("Failed to set socket to %s\n", socket);
}

InternetSetOptionA(NULL, INTERNET_OPTION_PROXY_SETTINGS_CHANGED, NULL, 0);
return success;

}

In which I've tried calling this function after the InternetOpenA() and right before the InternetConnectA() function but for whatever reason it doesn't use the proxy but rather my plain internet.

Here is my connection code snippet.

 hConnect = InternetConnectA(hOpen, host, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, 3, 0, 0);
hRequest = HttpOpenRequestA(hConnect, "POST", uri, NULL, CHECKER_URL, NULL, INTERNET_FLAG_SECURE, 0);

**

UPDATE:

**

Using this code I was able to get the proxy set.

But for some reason it only allows me to set the proxy one time and never again while the process is running.

HINTERNET hOpen = InternetOpenA(helper.USER_AGENT, 0, NULL, NULL, 0);
INTERNET_PROXY_INFO proxy = { 0 };

proxy.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
proxy.lpszProxy = (LPCTSTR)socket;
proxy.lpszProxyBypass = NULL;

if (UrlMkSetSessionOption(INTERNET_OPTION_PROXY, &proxy, sizeof(proxy), 0) != 0)
{
    success = false;
}
UrlMkSetSessionOption(INTERNET_OPTION_PROXY_SETTINGS_CHANGED, NULL, 0, 0);

I want to change the proxy mid program after doing this once but using the same code the proxy change a 2nd time doesn't take affect.

I've even tried closing the handle and reinstantiating it but no luck.

When trying to use

InternetSetOptionA(hOpen, INTERNET_OPTION_PER_CONNECTION_OPTION, &proxy, sizeof(INTERNET_PROXY_INFO)))

I get a return value of false so it's not setting that way for some reason. But using INTERNET_OPTION_PROXY sets it.

回答1:

In order to use a SOCKS proxy, try using CERN_PROXY_INTERNET_ACCESS instead of INTERNET_OPEN_TYPE_PROXY. And change SOCKS5= to SOCKS=.

Also, your changeProxy() function has several logic bugs. Your typecast from char[] to LPCTSTR is suspicious and wrong, and you are not actually providing any proxy server in the list anyway.