Skip to content

Instantly share code, notes, and snippets.

@kmahyyg
Created January 19, 2023 20:09
Show Gist options
  • Save kmahyyg/65ccf936b5a00a1354bb616fc4aa5dcd to your computer and use it in GitHub Desktop.
Save kmahyyg/65ccf936b5a00a1354bb616fc4aa5dcd to your computer and use it in GitHub Desktop.
Always Set Windows Global Proxy Settings
// RepeatSetProxy.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <windows.h>
#include <WinInet.h>
#include <iostream>
#include <strsafe.h>
#include <cstring>
#include <atlstr.h>
BOOL SetConnectionProxyOptions(LPWSTR proxyServerAddr);
wchar_t* convertCharArrayToLPCWSTR(const char* charArray);
void PrintErrorMessage(LPTSTR lpszFunction);
int main(int argc, char* argv[])
{
std::cout << "Welcome to ProxyShooter!" << std::endl;
if (argc != 2) {
std::cout << "Usage: ProxyShooter.exe 127.0.0.1:11884" << std::endl;
return 5;
}
const char* servAddr = argv[1];
std::cout << "Received Server: " << servAddr << std::endl;
LPWSTR wPtrServ = convertCharArrayToLPCWSTR(servAddr);
while (TRUE) {
BOOL res = SetConnectionProxyOptions(wPtrServ);
if (res != TRUE) {
// if set repeated params, it will return FALSE, but still working.
PrintErrorMessage(LPTSTR(TEXT("SetConnectionProxyOptions")));
}
std::cout << "Set OK!" << std::endl;
InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
InternetSetOption(NULL, INTERNET_OPTION_REFRESH, NULL, 0);
Sleep(1000);
}
return 0;
}
BOOL SetConnectionProxyOptions(LPWSTR proxyServerAddr)
{
INTERNET_PER_CONN_OPTION_LIST conn_options;
BOOL bReturn;
DWORD dwBufferSize = sizeof(conn_options);
{
conn_options.dwSize = dwBufferSize;
conn_options.pszConnection = NULL;
conn_options.dwOptionCount = 3;
conn_options.pOptions = (INTERNET_PER_CONN_OPTION*)malloc(
sizeof(INTERNET_PER_CONN_OPTION) * conn_options.dwOptionCount);
conn_options.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
conn_options.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT | PROXY_TYPE_PROXY;
conn_options.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
conn_options.pOptions[1].Value.pszValue = proxyServerAddr;
conn_options.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS;
conn_options.pOptions[2].Value.pszValue = (LPWSTR)L"<local>;*.lan;*.local;127.*;172.16.*;10.*;192.168.*;localhost";
}
bReturn = InternetSetOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &conn_options, dwBufferSize);
free(conn_options.pOptions);
return bReturn;
}
wchar_t* convertCharArrayToLPCWSTR(const char* charArray)
{
wchar_t* wString = new wchar_t[1024];
MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 1024);
return wString;
}
void PrintErrorMessage(LPTSTR lpszFunction) {
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
std::wcout << "ReturnVal: False, Error: " << (LPTSTR)lpDisplayBuf << std::endl;
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment