fork download
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <wininet.h>
  4.  
  5.  
  6. BOOL EnableProxy(LPCSTR addr, UINT port, const std::wstring& issuer)
  7. {
  8. INTERNET_PER_CONN_OPTION_LIST option_list;
  9. INTERNET_PER_CONN_OPTION options[3];
  10.  
  11. option_list.dwSize = sizeof(option_list);
  12. option_list.pszConnection = NULL;
  13. option_list.dwOptionCount = 3;
  14. option_list.pOptions = options;
  15.  
  16. options[0].dwOption = INTERNET_PER_CONN_FLAGS;
  17. options[0].Value.dwValue = PROXY_TYPE_DIRECT | PROXY_TYPE_PROXY;
  18.  
  19. char proxy_server[128];
  20. _snprintf_s(proxy_server, sizeof(proxy_server) - 1, "%s:%d", addr, port);
  21.  
  22. options[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
  23. options[1].Value.pszValue = proxy_server;
  24.  
  25. options[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS;
  26. options[2].Value.pszValue = (LPSTR)"localhost";
  27.  
  28. if (InternetSetOption(nullptr, INTERNET_OPTION_PER_CONNECTION_OPTION, &option_list, option_list.dwSize) == TRUE)
  29. {
  30. const auto h_store = CertOpenSystemStoreA(0, "ROOT");
  31. if (!issuer.empty() && h_store)
  32. {
  33. HINTERNET hint = InternetOpen(
  34. "WebTestClient",
  35. INTERNET_OPEN_TYPE_PRECONFIG,
  36. NULL,
  37. NULL,
  38. 0);
  39. if(hint)
  40. {
  41. PCCERT_CONTEXT certContext = nullptr;
  42. while(true)
  43. {
  44. certContext = CertFindCertificateInStore(h_store,
  45. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  46. 0,
  47. CERT_FIND_SUBJECT_STR_W,
  48. issuer.c_str(),
  49. certContext);
  50. if (certContext)
  51. {
  52. auto error = GetLastError();
  53. InternetSetOption(hint,
  54. INTERNET_OPTION_CLIENT_CERT_CONTEXT,
  55. (void *) certContext,
  56. sizeof(CERT_CONTEXT));
  57. error = GetLastError();
  58. std::cerr << error << std::endl;
  59. }
  60. else
  61. {
  62. break;
  63. }
  64. }
  65. }
  66. }
  67.  
  68. InternetSetOption(nullptr, INTERNET_OPTION_SETTINGS_CHANGED, nullptr, 0);
  69. InternetSetOption(nullptr, INTERNET_OPTION_REFRESH, nullptr, 0);
  70. return TRUE;
  71. }
  72. else
  73. {
  74. return FALSE;
  75. }
  76. }
  77.  
  78. int main()
  79. {
  80. std::cout << "Hello, World!" << std::endl;
  81. EnableProxy("localhost", 8080, L"My");
  82. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:10: fatal error: Windows.h: No such file or directory
 #include <Windows.h>
          ^~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty