1
0

BigLib.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. #ifdef WINAMPX
  2. #define WIN32_LEAN_AND_MEAN 1
  3. #include <windows.h>
  4. #include "stdio.h"
  5. #include "proxydt.h"
  6. #include <winsock2.h>
  7. #include ".\ws2tcpip.h"
  8. #include ".\wininet.h"
  9. #include "../jnetlib/jnetlib.h"
  10. #include <urlmon.h>
  11. extern void SendMetadata( char *data, int arg );
  12. HRESULT JNetLibDownloadToFile(LPVOID lpUnused1, LPSTR lpWPADLocation, LPSTR lpTempFile, LPVOID lpUnused2, LPVOID lpUnused3);
  13. enum {
  14. BK_UNKNOWN = 0,
  15. BK_IE4 = 1,
  16. BK_NETSCAPE4 = 2,
  17. BK_NETSCAPE6 = 3,
  18. BK_MOZILLA = 4,
  19. BK_FIREFOX = 5
  20. };
  21. // browser info struct
  22. typedef struct{
  23. LPSTR lpName;
  24. BOOL bSupported;
  25. }BK_INFO;
  26. // browser info
  27. BK_INFO BrowserInfo[] = {
  28. "Unknown", FALSE,
  29. "IE 4.0+", TRUE,
  30. "Netscape 4 or 5", FALSE,
  31. "Netscape 6+", TRUE,
  32. "Mozilla", TRUE,
  33. "Firefox", TRUE
  34. };
  35. // Global variables
  36. int gBrowserKind = BK_UNKNOWN;
  37. int gTryAuto = 1;
  38. // Exported C functions
  39. extern "C" BOOL ProxyInit();
  40. extern "C" void ProxyDeInit();
  41. extern "C" int ResolvProxyFromURL(LPSTR lpURL, LPSTR lpHostname, LPSTR lpDest);
  42. // Global C functions
  43. BOOL IsIEProxySet();
  44. int GetIESettings();
  45. int ResolveURL_IE(LPSTR lpURL, LPSTR lpHostname, LPSTR lpIPAddress, int sizeof_address, int *pnPort);
  46. BOOL IsFirefoxProxySet();
  47. int GetFirefoxSettings();
  48. int ResolveURL_Firefox(LPSTR lpURL, LPSTR lpHostname, LPSTR lpIPAddress, int sizeof_address, int *pnPort);
  49. BOOL IsMozillaProxySet();
  50. int GetMozillaSettings();
  51. int ResolveURL_Mozilla(LPSTR lpURL, LPSTR lpHostname, LPSTR lpIPAddress, int sizeof_address, int *pnPort);
  52. int GetDefaultBrowser();
  53. int ReadWPADFile(LPSTR lpWPADLocation, LPSTR pIPAddress, int *pnPort);
  54. int GetFirefoxOrMozillaSettings(BOOL bFirefox);
  55. BOOL IsFirefoxOrMozillaProxySet(BOOL bFirefox);
  56. int ResolveURL_MozillaOrFirefox(BOOL bFirefox, LPSTR lpURL, LPSTR lpHostname, LPSTR lpIPAddress, int sizeof_address, int *pnPort);
  57. // exported functions
  58. extern "C" BOOL ProxyInit()
  59. {
  60. BOOL bRet;
  61. bRet = FALSE;
  62. gBrowserKind = GetDefaultBrowser();
  63. switch(gBrowserKind) {
  64. case BK_IE4:
  65. bRet = IsIEProxySet();
  66. break;
  67. case BK_MOZILLA:
  68. bRet = IsMozillaProxySet();
  69. break;
  70. case BK_FIREFOX:
  71. bRet = IsFirefoxProxySet();
  72. break;
  73. }
  74. return bRet;
  75. }
  76. extern "C" void ProxyDeInit()
  77. {
  78. }
  79. extern "C" int ResolvProxyFromURL(LPSTR lpURL, LPSTR lpHostname, LPSTR lpDest)
  80. {
  81. // lpURL = URL to resolve
  82. // lpHostname = hostname
  83. // lpDest = where to store the result, such as "www.proxyserver.com:8080"
  84. char szIPAddress[MAX_PATH] = {0};
  85. int ret, nPort=0;
  86. lpDest[0]=0;
  87. if(lpURL && lpHostname && lpDest) {
  88. switch(gBrowserKind) {
  89. case BK_IE4:
  90. ret = ResolveURL_IE(lpURL, lpHostname, szIPAddress, sizeof(szIPAddress), &nPort);
  91. break;
  92. case BK_MOZILLA:
  93. ret = ResolveURL_Mozilla(lpURL, lpHostname, szIPAddress, sizeof(szIPAddress), &nPort);
  94. break;
  95. case BK_FIREFOX:
  96. ret = ResolveURL_Firefox(lpURL, lpHostname, szIPAddress, sizeof(szIPAddress), &nPort);
  97. break;
  98. }
  99. if(ret == 0) {
  100. if ( szIPAddress[0] )
  101. {
  102. wsprintf(lpDest, "%s:%d", szIPAddress, nPort);
  103. return 1;
  104. }
  105. else return 0;
  106. }
  107. else return 0;
  108. }
  109. else return -1;
  110. }
  111. int GetDefaultBrowser()
  112. {
  113. DWORD dwSize, dwType;
  114. TCHAR valueBuf[MAX_PATH] = {0};
  115. DWORD valueSize = sizeof(valueBuf);
  116. HKEY hKey;
  117. long lRet;
  118. memset(valueBuf, 0, sizeof(valueBuf));
  119. lRet = RegOpenKeyEx(HKEY_CLASSES_ROOT, "http\\shell\\open\\ddeexec\\Application", 0, KEY_READ, &hKey);
  120. if (lRet == ERROR_SUCCESS) {
  121. dwSize = valueSize;
  122. lRet = RegQueryValueEx(hKey, "", NULL, &dwType, (LPBYTE)valueBuf, &dwSize);
  123. if(lRet == ERROR_SUCCESS && dwType == REG_SZ) {
  124. if (_tcsicmp(_T("NSShell"), valueBuf) == 0) { //NS 4.x
  125. return BK_NETSCAPE4;
  126. } else if (_tcsicmp(_T("IExplore"), valueBuf) == 0) { //IE 4+
  127. return BK_IE4;
  128. } else if (_tcsicmp(_T("Mozilla"), valueBuf) == 0) { //Mozilla
  129. return BK_MOZILLA;
  130. } else if (_tcsicmp(_T("Firefox"), valueBuf) == 0) { //Firefox
  131. return BK_FIREFOX;
  132. }
  133. }
  134. }
  135. RegCloseKey(hKey);
  136. lRet = RegOpenKeyEx(HKEY_CLASSES_ROOT, "http\\shell\\open\\command", 0, KEY_READ, &hKey);
  137. if(lRet == ERROR_SUCCESS) {
  138. dwSize = valueSize;
  139. lRet = RegQueryValueEx(hKey, "", NULL, &dwType, (LPBYTE)valueBuf, &dwSize);
  140. if(lRet == ERROR_SUCCESS && dwType == REG_SZ) {
  141. if(strstr(valueBuf, "MOZILLA")) {
  142. return BK_MOZILLA;
  143. }
  144. if(strstr(valueBuf, "NETSCAPE")) {
  145. return BK_MOZILLA;
  146. }
  147. if(strstr(valueBuf, "FIREFOX")) {
  148. return BK_FIREFOX;
  149. }
  150. }
  151. }
  152. RegCloseKey(hKey);
  153. return BK_UNKNOWN;
  154. }
  155. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  156. // helper functions for JSProxy.dll
  157. DWORD __stdcall ResolveHostName(LPSTR lpszHostName, LPSTR lpszIPAddress, LPDWORD lpdwIPAddressSize);
  158. BOOL __stdcall IsResolvable(LPSTR lpszHost);
  159. DWORD __stdcall GetIPAddress(LPSTR lpszIPAddress, LPDWORD lpdwIPAddressSize);
  160. BOOL __stdcall IsInNet(LPSTR lpszIPAddress, LPSTR lpszDest, LPSTR lpszMask);
  161. // functions to get IE checkbox state
  162. BOOL GetAutomaticallyDetectSettingsCheckboxState();
  163. BOOL GetUseAProxyServerForYourLanCheckboxState();
  164. BOOL GetAutomaticConfigurationScriptCheckboxState();
  165. BOOL GetBypassProxyServerForLocalAddressesCheckboxState();
  166. // functions to actually get an IP address and port # of the proxy server
  167. int GetAutomaticDetectSettings(LPSTR lpIPAddress, int *pnPort);
  168. int GetProxyServerForLanProxySettings(LPSTR lpIPAddress, int *pnPort);
  169. int GetAutoConfigScriptProxySettings(LPSTR lpIPAddress, int *pnPort);
  170. // various helper functions
  171. BOOL IsDirect(LPSTR proxy);
  172. BOOL IsAProxy(LPSTR proxy);
  173. void reportFuncErr(TCHAR* funcName);
  174. char * strstri(LPSTR lpOne, LPSTR lpTwo);
  175. int GetProxyIP(LPSTR proxy, LPSTR szProxyIP);
  176. int GetProxyPort(LPSTR proxy);
  177. // some global variables
  178. char gszURL[1025] = {0};
  179. char gszHost[256] = {0};
  180. // returns TRUE if the user has set a proxy in IE
  181. BOOL IsIEProxySet()
  182. {
  183. BOOL bAutomaticallyDetectSettings = GetAutomaticallyDetectSettingsCheckboxState();
  184. BOOL bUseAutomaticConfigurationScript = GetAutomaticConfigurationScriptCheckboxState();
  185. BOOL bUseAProxyServerForYourLan = GetUseAProxyServerForYourLanCheckboxState();
  186. if(bAutomaticallyDetectSettings || bUseAutomaticConfigurationScript || bUseAProxyServerForYourLan) {
  187. return TRUE;
  188. }
  189. return FALSE;
  190. }
  191. int ResolveURL_IE(LPSTR lpURL, LPSTR lpHostname, LPSTR lpIPAddress, int sizeof_address, int *pnPort)
  192. {
  193. // get the state of the four checkboxes in the proxy settings dialog for IE
  194. BOOL bAutomaticallyDetectSettings = GetAutomaticallyDetectSettingsCheckboxState();
  195. BOOL bUseAutomaticConfigurationScript = GetAutomaticConfigurationScriptCheckboxState();
  196. BOOL bUseAProxyServerForYourLan = GetUseAProxyServerForYourLanCheckboxState();
  197. //BOOL bBypassProxyServerForLocalAddresses = GetBypassProxyServerForLocalAddressesCheckboxState();
  198. int ret;
  199. lstrcpyn(gszURL, lpURL, 1025);
  200. lstrcpyn(gszHost, lpHostname, 256);
  201. // if nothing checked, return
  202. if(!bAutomaticallyDetectSettings && !bUseAutomaticConfigurationScript && !bUseAProxyServerForYourLan) {
  203. return 0;
  204. }
  205. // if all three checkboxes on...
  206. if(bAutomaticallyDetectSettings && gTryAuto)
  207. {
  208. // try the automatic configuration next
  209. ret = GetAutomaticDetectSettings(lpIPAddress, pnPort);
  210. if(ret == 0 && *pnPort) {
  211. return 0;
  212. }
  213. gTryAuto = 0;
  214. }
  215. if ( bUseAutomaticConfigurationScript)
  216. {
  217. // try the automatic config script method first
  218. ret = GetAutoConfigScriptProxySettings(lpIPAddress, pnPort);
  219. if(ret == 0 && *pnPort ) {
  220. return 0;
  221. }
  222. }
  223. if ( bUseAProxyServerForYourLan)
  224. {
  225. // if still no success, try the "Use a proxy server for your lan" settings
  226. ret = GetProxyServerForLanProxySettings(lpIPAddress, pnPort);
  227. if(ret == 0 && *pnPort) {
  228. return 0;
  229. }
  230. }
  231. // no success...
  232. return 0;
  233. }
  234. // handles the "Automatically Detect" checkbox
  235. int GetAutomaticDetectSettings(LPSTR lpIPAddress, int *pnPort)
  236. {
  237. // By not specifying a domain name, Windows uses the local domain name,
  238. // so form an http request to go to http://wpad/wpad.dat,
  239. // store results in szWPADLocation and call URLDownloadToFileA()
  240. if(lpIPAddress && pnPort) {
  241. // download wpad.dat from the URL in szURL
  242. return ReadWPADFile("http://wpad/wpad.dat", lpIPAddress, pnPort);
  243. }
  244. return -1;
  245. }
  246. // handles the "Use automatic configuration script" checkbox
  247. int GetAutoConfigScriptProxySettings(LPSTR lpIPAddress, int *pnPort)
  248. {
  249. DWORD dwType, dwSize;
  250. HKEY hKey;
  251. char szWPADLocation[MAX_PATH] = {0};
  252. long lRet;
  253. int retval = -1;
  254. if(!lpIPAddress) {
  255. return retval;
  256. }
  257. if(!pnPort) {
  258. return retval;
  259. }
  260. // use the registry read of HKCU\\software\microsoft\windows\current version\internet settings to see if "Use Automatic Configuration Script" is checked
  261. lstrcpyn(szWPADLocation, "", MAX_PATH);
  262. lRet = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, KEY_READ, &hKey);
  263. if (lRet == ERROR_SUCCESS) {
  264. dwSize = sizeof(szWPADLocation);
  265. lRet = RegQueryValueEx(hKey, "AutoConfigURL", NULL, &dwType, (LPBYTE)szWPADLocation, &dwSize);
  266. if(lRet == ERROR_SUCCESS && dwType == REG_SZ) {
  267. retval = ReadWPADFile(szWPADLocation, lpIPAddress, pnPort);
  268. }
  269. }
  270. RegCloseKey(hKey);
  271. return retval; //0 = success
  272. }
  273. // handles the "Use a proxy server for your LAN" checkbox
  274. int GetProxyServerForLanProxySettings(LPSTR lpIPAddress, int *pnPort)
  275. {
  276. DWORD dwType, dwSize;
  277. HKEY hKey;
  278. BOOL bDirectOrProxy;
  279. char szProxy[MAX_PATH] = {0};
  280. long lRet;
  281. int retval = -1;
  282. if(lpIPAddress) {
  283. strcpy(lpIPAddress, "");
  284. }
  285. if(pnPort) {
  286. *pnPort = 0;
  287. }
  288. lRet = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, KEY_READ, &hKey);
  289. if (lRet == ERROR_SUCCESS) {
  290. dwSize = sizeof(szProxy);
  291. lRet = RegQueryValueEx(hKey, "ProxyServer", NULL, &dwType, (LPBYTE)szProxy, &dwSize);
  292. if(lRet == ERROR_SUCCESS && dwType == REG_SZ) {
  293. retval = 0;
  294. bDirectOrProxy = FALSE;
  295. if(IsDirect(szProxy)) {
  296. // string is something like "DIRECT"
  297. // It's a 'direct' kind of proxy.
  298. bDirectOrProxy = TRUE;
  299. // set the 'out' parameters
  300. if(lpIPAddress) {
  301. strcpy(lpIPAddress, "");
  302. }
  303. if(pnPort) {
  304. *pnPort = 0;
  305. }
  306. }
  307. if(IsAProxy(szProxy)) {
  308. char szProxyIP[MAX_PATH] = {0};
  309. // string is something like "D
  310. bDirectOrProxy = TRUE;
  311. GetProxyIP(szProxy, szProxyIP);
  312. // It's a 'regular' kind of proxy, with an IP of %s and a port of %d\n", szProxyIP, GetProxyPort(szProxy)
  313. // set the 'out' parameters
  314. if(lpIPAddress) {
  315. strcpy(lpIPAddress, szProxyIP);
  316. }
  317. if(pnPort) {
  318. *pnPort = GetProxyPort(szProxy);
  319. }
  320. }
  321. if(!bDirectOrProxy) {
  322. // string is something like "10.0.0.1:4543"
  323. LPSTR lpColon = NULL;
  324. if ( isdigit(szProxy[0]) )
  325. {
  326. lpColon = strchr(szProxy, ':');
  327. if(lpColon) {
  328. *lpColon = '\0';
  329. // set the 'out' parameters
  330. if(lpIPAddress) {
  331. strcpy(lpIPAddress, szProxy);
  332. }
  333. *lpColon = ':';
  334. if(pnPort) {
  335. *pnPort = GetProxyPort(szProxy);
  336. }
  337. }
  338. }
  339. else if ( strstr(szProxy,"http=") )
  340. {
  341. char *p = strstr(szProxy,"http=");
  342. int offset= strlen("http=");
  343. char *semi = strchr(p+offset, ';');
  344. if(semi) {
  345. *semi= '\0';
  346. }
  347. lpColon = strchr(p+offset, ':');
  348. if(lpColon) {
  349. *lpColon = '\0';
  350. }
  351. // set the 'out' parameters
  352. if(lpIPAddress) {
  353. strcpy(lpIPAddress, p+offset);
  354. }
  355. if (lpColon)
  356. if(pnPort) {
  357. *pnPort = (short)atoi(lpColon+1);
  358. }
  359. if ( !*pnPort ) *pnPort = 80;
  360. }
  361. else
  362. {
  363. if(lpIPAddress) {
  364. strcpy(lpIPAddress, "");
  365. }
  366. if(pnPort) {
  367. *pnPort = 0;
  368. }
  369. }
  370. }
  371. }
  372. }
  373. RegCloseKey(hKey);
  374. return retval;
  375. }
  376. int ReadWPADFile(LPSTR lpWPADLocation, LPSTR lpIPAddress, int *pnPort)
  377. {
  378. // Declare function pointers for the three autoproxy functions
  379. pfnInternetInitializeAutoProxyDll pInternetInitializeAutoProxyDll;
  380. pfnInternetDeInitializeAutoProxyDll pInternetDeInitializeAutoProxyDll;
  381. pfnInternetGetProxyInfo pInternetGetProxyInfo;
  382. // Declare and populate an AutoProxyHelperVtbl structure, and then
  383. // place a pointer to it in a containing AutoProxyHelperFunctions
  384. // structure, which will be passed to InternetInitializeAutoProxyDll:
  385. AutoProxyHelperVtbl Vtbl = {IsResolvable, GetIPAddress, ResolveHostName, IsInNet };
  386. AutoProxyHelperFunctions HelperFunctions = { &Vtbl };
  387. HMODULE hModJS;
  388. HRESULT hr;
  389. char szTempPath[MAX_PATH] = {0};
  390. char szTempFile[MAX_PATH] = {0};
  391. int retval = 0;
  392. if(!(hModJS = LoadLibrary("jsproxy.dll"))) {
  393. reportFuncErr("LoadLibrary");
  394. return -1;
  395. }
  396. if(!(pInternetInitializeAutoProxyDll = (pfnInternetInitializeAutoProxyDll)
  397. GetProcAddress(hModJS, "InternetInitializeAutoProxyDll")) ||
  398. !(pInternetDeInitializeAutoProxyDll = (pfnInternetDeInitializeAutoProxyDll)
  399. GetProcAddress(hModJS, "InternetDeInitializeAutoProxyDll")) ||
  400. !(pInternetGetProxyInfo = (pfnInternetGetProxyInfo)
  401. GetProcAddress(hModJS, "InternetGetProxyInfo"))) {
  402. FreeLibrary(hModJS);
  403. reportFuncErr("GetProcAddress");
  404. return -1;
  405. }
  406. if(lpIPAddress)
  407. {
  408. strcpy(lpIPAddress, "");
  409. }
  410. if(pnPort) {
  411. *pnPort = 0;
  412. }
  413. GetTempPathA(sizeof(szTempPath)/sizeof(szTempPath[0]), szTempPath);
  414. GetTempFileNameA(szTempPath, "X", 2, szTempFile);
  415. //printf(" Downloading %s ...\n", lpWPADLocation);
  416. hr = JNetLibDownloadToFile(NULL, lpWPADLocation, szTempFile, NULL, NULL);
  417. if(hr == S_OK) {
  418. if(!pInternetInitializeAutoProxyDll(0, szTempFile, NULL, &HelperFunctions, NULL)) {
  419. //printf(" Calling 'InternetInitializeAutoProxyDll' in JSPROXY.DLL failed\n (usually because 'Use Automatic Configuration Script' checkbox is OFF)\n");
  420. pInternetDeInitializeAutoProxyDll(NULL, 0);
  421. FreeLibrary(hModJS);
  422. retval = -1;
  423. }else{
  424. // printf("\n InternetInitializeAutoProxyDll returned: %d\n", returnVal);
  425. // Delete the temporary file
  426. // (or, to examine the auto-config script, comment out the
  427. // file delete and substitute the following printf call)
  428. // printf("\n The auto-config script temporary file is:\n %s\n", szTempFile);
  429. DeleteFileA(szTempFile);
  430. DWORD dwSize = 0;
  431. LPSTR pProxy = NULL;
  432. if(!pInternetGetProxyInfo((LPSTR)gszURL, sizeof(gszURL), (LPSTR)gszHost, sizeof(gszHost), &pProxy, &dwSize)) {
  433. reportFuncErr("InternetGetProxyInfo");
  434. retval = -1;
  435. }else{
  436. // printf("\n Proxy is: %s\n", proxy);
  437. if(IsDirect(pProxy)) {
  438. //printf(" It's a 'direct' kind of proxy.\n");
  439. // set the 'out' parameters
  440. if(lpIPAddress) {
  441. strcpy(lpIPAddress, "");
  442. }
  443. if(pnPort) {
  444. *pnPort = 0;
  445. }
  446. }
  447. if(IsAProxy(pProxy)) {
  448. char szProxyIP[MAX_PATH] = {0};
  449. GetProxyIP(pProxy, szProxyIP);
  450. //printf(" It's a 'regular' kind of proxy, with an IP of %s and a port of %d\n", szProxyIP, GetProxyPort(szProxy));
  451. // set the 'out' parameters
  452. if(lpIPAddress) {
  453. strcpy(lpIPAddress, szProxyIP);
  454. }
  455. if(pnPort) {
  456. *pnPort = GetProxyPort(pProxy);
  457. }
  458. }
  459. }
  460. }
  461. }else{
  462. //printf(" Error downloading %s (hr=0x%X)\n", lpWPADLocation, hr);
  463. // there is no proxy, go direct
  464. if(lpIPAddress) {
  465. strcpy(lpIPAddress, "");
  466. }
  467. if(pnPort) {
  468. *pnPort = 0;
  469. }
  470. retval = 0;
  471. }
  472. if(!pInternetDeInitializeAutoProxyDll(NULL, 0)) {
  473. reportFuncErr("InternetDeInitializeAutoProxyDll");
  474. }
  475. return retval; // 0 = success
  476. }
  477. // Puts "10.0.0.1" into lpDest from a string like "PROXY 10.0.0.1:8088"
  478. // Returns 0 if success, -1 if an error
  479. int GetProxyIP(LPSTR lpProxy, LPSTR lpDest)
  480. {
  481. LPSTR lpData;
  482. LPSTR lpLastColon;
  483. BOOL bDone;
  484. char szProxy[MAX_PATH] = {0};
  485. int ret = 0;
  486. if(lpProxy && lpDest) {
  487. lstrcpyn(szProxy, lpProxy, MAX_PATH);
  488. // find the last ":" in the string
  489. lpLastColon = NULL;
  490. lpData = szProxy;
  491. while(*lpData) {
  492. if(*lpData == ':') {
  493. lpLastColon = lpData;
  494. }
  495. lpData++;
  496. }
  497. if(lpLastColon) {
  498. // truncate the string at the last colon
  499. *lpLastColon = '\0';
  500. bDone = FALSE;
  501. while(lpData > szProxy && !bDone) {
  502. if(*lpData == ' ') {
  503. bDone = TRUE;
  504. lpData++;
  505. }else{
  506. lpData--;
  507. }
  508. }
  509. strcpy(lpDest, lpData);
  510. ret = 0;
  511. }else {
  512. strcpy(lpDest, lpProxy);
  513. ret =0;
  514. }
  515. }else{
  516. ret = -1;
  517. }
  518. return ret;
  519. }
  520. // Returns 8088 from a string like "PROXY 10.0.0.1:8088"
  521. // Returns a port # if success, -1 if an error
  522. int GetProxyPort(LPSTR lpProxy)
  523. {
  524. LPSTR lpData;
  525. LPSTR lpLastColon = NULL;
  526. char szProxy[MAX_PATH] = {0};
  527. int ret = -1;
  528. if(lpProxy) {
  529. lstrcpyn(szProxy, lpProxy, MAX_PATH);
  530. // find the last ":" in the string
  531. lpData = szProxy;
  532. while(*lpData) {
  533. if(*lpData == ':') {
  534. lpLastColon = lpData;
  535. }
  536. lpData++;
  537. }
  538. // from the last colon to the end of the string is the port number
  539. if ( lpLastColon )
  540. {
  541. lpLastColon++;
  542. ret = (unsigned short)atoi(lpLastColon);
  543. }
  544. else ret = 80;
  545. }
  546. return ret;
  547. }
  548. BOOL IsDirect(LPSTR proxy)
  549. {
  550. BOOL bRet = FALSE;
  551. if(proxy) {
  552. if(strstri("DIRECT", proxy)) {
  553. bRet = TRUE;
  554. }
  555. }
  556. return bRet;
  557. }
  558. BOOL IsAProxy(LPSTR proxy)
  559. {
  560. BOOL bRet = FALSE;
  561. if(proxy) {
  562. if(strstri("PROXY", proxy)) {
  563. bRet = TRUE;
  564. }
  565. }
  566. return bRet;
  567. }
  568. // like strstr() but case-insensitive
  569. char * strstri(LPSTR lpOne, LPSTR lpTwo)
  570. {
  571. unsigned int b;
  572. char szOne[MAX_PATH] = {0}, szTwo[MAX_PATH] = {0};
  573. if(lpOne && lpTwo) {
  574. strcpy(szOne, lpOne);
  575. strcpy(szTwo, lpTwo);
  576. for(b=0; b<strlen(szOne); b++) {
  577. szOne[b] = tolower(szOne[b]);
  578. }
  579. for(b=0; b<strlen(szTwo); b++) {
  580. szTwo[b] = tolower(szTwo[b]);
  581. }
  582. }
  583. return strstr(szTwo, szOne);
  584. }
  585. BOOL GetAutomaticallyDetectSettingsCheckboxState()
  586. {
  587. DWORD dwSize, dwType;
  588. HKEY hKey;
  589. BOOL bAutomaticallyDetectSettings = FALSE;
  590. long lRet;
  591. // see if the "Automatically Detect Settings" checkbox is on (I know, it's ugly)
  592. // I noticed that the 9th byte in a binary struct at HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections\DefaultConnectionSettings"
  593. // changes a bit to 1 or 0 based on the state of the checkbox. I'm using Windows XP. Not sure what byte to check on other Windows versions.
  594. BYTE Buffer[200] = {0};
  595. lRet = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections", 0, KEY_READ, &hKey);
  596. if (lRet == ERROR_SUCCESS) {
  597. dwSize = sizeof(Buffer);
  598. lRet = RegQueryValueEx(hKey, "DefaultConnectionSettings", NULL, &dwType, (LPBYTE)&Buffer, &dwSize);
  599. if(lRet == ERROR_SUCCESS && dwType == REG_BINARY) {
  600. if(Buffer[8] & 8) {
  601. bAutomaticallyDetectSettings = TRUE;
  602. }
  603. }
  604. }
  605. RegCloseKey(hKey);
  606. return bAutomaticallyDetectSettings;
  607. }
  608. BOOL GetUseAProxyServerForYourLanCheckboxState()
  609. {
  610. DWORD dwSize, dwValue, dwType;
  611. HKEY hKey;
  612. BOOL bUseAProxyServerForYourLan = FALSE;
  613. long lRet;
  614. // see if the "Use a proxy server for your LAN" checkbox is on
  615. lRet = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, KEY_READ, &hKey);
  616. if (lRet == ERROR_SUCCESS) {
  617. dwSize = sizeof(DWORD);
  618. lRet = RegQueryValueEx(hKey, "ProxyEnable", NULL, &dwType, (LPBYTE)&dwValue, &dwSize);
  619. if(lRet == ERROR_SUCCESS && dwType == REG_DWORD) {
  620. bUseAProxyServerForYourLan = dwValue;
  621. }
  622. }
  623. RegCloseKey(hKey);
  624. return bUseAProxyServerForYourLan;
  625. }
  626. BOOL GetAutomaticConfigurationScriptCheckboxState()
  627. {
  628. DWORD dwType, dwSize;
  629. HKEY hKey;
  630. BOOL bUseAutomaticConfigurationScript = FALSE;
  631. char szWPAD[MAX_PATH] = {0};
  632. long lRet;
  633. #if 1
  634. // use the registry read of HKCU\\software\microsoft\windows\current version\internet settings to see if "Use Automatic Configuration Script" is checked
  635. szWPAD[0] = '\0';
  636. lRet = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, KEY_READ, &hKey);
  637. if (lRet == ERROR_SUCCESS) {
  638. dwSize = sizeof(szWPAD);
  639. lRet = RegQueryValueEx(hKey, "AutoConfigURL", NULL, &dwType, (LPBYTE)szWPAD, &dwSize);
  640. if(lRet == ERROR_SUCCESS && dwType == REG_SZ) {
  641. }
  642. }
  643. RegCloseKey(hKey);
  644. #else
  645. // use DetectAutoProxyURL
  646. if(!DetectAutoProxyUrl(szWPADLocation, sizeof(szWPADLocation), PROXY_AUTO_DETECT_TYPE_DHCP | PROXY_AUTO_DETECT_TYPE_DNS_A)) {
  647. reportFuncErr("DetectAutoProxyUrl");
  648. }
  649. #endif
  650. if(strlen(szWPAD)) {
  651. bUseAutomaticConfigurationScript = TRUE;
  652. }
  653. return bUseAutomaticConfigurationScript;
  654. }
  655. BOOL GetBypassProxyServerForLocalAddressesCheckboxState()
  656. {
  657. DWORD dwSize, dwType;
  658. HKEY hKey;
  659. BOOL bBypassProxyServerForLocalAddresses = FALSE;
  660. char szBuffer[MAX_PATH] = {0};
  661. long lRet;
  662. dwSize = sizeof(szBuffer);
  663. lRet = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, KEY_READ, &hKey);
  664. if (lRet == ERROR_SUCCESS) {
  665. lRet = RegQueryValueEx(hKey, "ProxyOverride", NULL, &dwType, (LPBYTE)&szBuffer, &dwSize);
  666. if(lRet == ERROR_SUCCESS && dwType == REG_SZ) {
  667. }
  668. }
  669. RegCloseKey(hKey);
  670. if(strcmp(szBuffer, "<local>") == 0) {
  671. bBypassProxyServerForLocalAddresses = TRUE;
  672. }
  673. return bBypassProxyServerForLocalAddresses;
  674. }
  675. /* ==================================================================
  676. HELPER FUNCTIONS
  677. ================================================================== */
  678. // ResolveHostName (a helper function)
  679. DWORD __stdcall ResolveHostName(LPSTR lpszHostName, LPSTR lpszIPAddress, LPDWORD lpdwIPAddressSize)
  680. {
  681. DWORD dwIPAddressSize;
  682. addrinfo Hints;
  683. LPADDRINFO lpAddrInfo;
  684. LPADDRINFO IPv4Only;
  685. DWORD error;
  686. // Figure out first whether to resolve a name or an address literal.
  687. // If getaddrinfo() with the AI_NUMERICHOST flag succeeds, then
  688. // lpszHostName points to a string representation of an IPv4 or IPv6
  689. // address. Otherwise, getaddrinfo() should return EAI_NONAME.
  690. ZeroMemory(&Hints, sizeof(addrinfo));
  691. Hints.ai_flags = AI_NUMERICHOST; // Only check for address literals.
  692. Hints.ai_family = PF_UNSPEC; // Accept any protocol family.
  693. Hints.ai_socktype = SOCK_STREAM; // Constrain results to stream socket.
  694. Hints.ai_protocol = IPPROTO_TCP; // Constrain results to TCP.
  695. error = getaddrinfo(lpszHostName, NULL, &Hints, &lpAddrInfo);
  696. if(error != EAI_NONAME) {
  697. if(error != 0) {
  698. error = (error == EAI_MEMORY) ?
  699. ERROR_NOT_ENOUGH_MEMORY : ERROR_INTERNET_NAME_NOT_RESOLVED;
  700. goto quit;
  701. }
  702. freeaddrinfo(lpAddrInfo);
  703. // An IP address (either v4 or v6) was passed in, so if there is
  704. // room in the lpszIPAddress buffer, copy it back out and return.
  705. dwIPAddressSize = lstrlen(lpszHostName);
  706. if((*lpdwIPAddressSize < dwIPAddressSize) || (lpszIPAddress == NULL)) {
  707. *lpdwIPAddressSize = dwIPAddressSize + 1;
  708. error = ERROR_INSUFFICIENT_BUFFER;
  709. goto quit;
  710. }
  711. lstrcpy(lpszIPAddress, lpszHostName);
  712. goto quit;
  713. }
  714. // Call getaddrinfo() again, this time with no flag set.
  715. Hints.ai_flags = 0;
  716. error = getaddrinfo(lpszHostName, NULL, &Hints, &lpAddrInfo);
  717. if(error != 0) {
  718. error = (error == EAI_MEMORY) ?
  719. ERROR_NOT_ENOUGH_MEMORY : ERROR_INTERNET_NAME_NOT_RESOLVED;
  720. goto quit;
  721. }
  722. // Convert the IP address in addrinfo into a string.
  723. // (the following code only handles IPv4 addresses)
  724. IPv4Only = lpAddrInfo;
  725. while(IPv4Only->ai_family != AF_INET) {
  726. IPv4Only = IPv4Only->ai_next;
  727. if(IPv4Only == NULL)
  728. {
  729. error = ERROR_INTERNET_NAME_NOT_RESOLVED;
  730. goto quit;
  731. }
  732. }
  733. error = getnameinfo(IPv4Only->ai_addr, (socklen_t)IPv4Only->ai_addrlen, lpszIPAddress, *lpdwIPAddressSize, NULL, 0, NI_NUMERICHOST);
  734. if(error != 0)
  735. error = ERROR_INTERNET_NAME_NOT_RESOLVED;
  736. quit:
  737. return(error);
  738. }
  739. // IsResolvable (a helper function)
  740. BOOL __stdcall IsResolvable(LPSTR lpszHost)
  741. {
  742. char szDummy[255] = {0};
  743. DWORD dwDummySize = sizeof(szDummy) - 1;
  744. if(ResolveHostName(lpszHost, szDummy, &dwDummySize))
  745. return(FALSE);
  746. return TRUE;
  747. }
  748. // GetIPAddress (a helper function)
  749. DWORD __stdcall GetIPAddress(LPSTR lpszIPAddress, LPDWORD lpdwIPAddressSize)
  750. {
  751. char szHostBuffer[255] = {0};
  752. if(gethostname(szHostBuffer, sizeof(szHostBuffer) - 1) != ERROR_SUCCESS)
  753. return(ERROR_INTERNET_INTERNAL_ERROR);
  754. return(ResolveHostName(szHostBuffer, lpszIPAddress, lpdwIPAddressSize));
  755. }
  756. // IsInNet (a helper function)
  757. BOOL __stdcall IsInNet(LPSTR lpszIPAddress, LPSTR lpszDest, LPSTR lpszMask)
  758. {
  759. DWORD dwDest;
  760. DWORD dwIpAddr;
  761. DWORD dwMask;
  762. dwIpAddr = inet_addr(lpszIPAddress);
  763. dwDest = inet_addr(lpszDest);
  764. dwMask = inet_addr(lpszMask);
  765. if((dwDest == INADDR_NONE) || (dwIpAddr == INADDR_NONE) || ((dwIpAddr & dwMask) != dwDest))
  766. return(FALSE);
  767. return(TRUE);
  768. }
  769. // reportFuncErr (simple error reporting)
  770. void reportFuncErr(TCHAR* funcName)
  771. {
  772. //printf(" ERROR: %s failed with error number %d.\n", funcName, GetLastError());
  773. }
  774. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  775. class CProfileFolder
  776. {
  777. public:
  778. int GetProfileFolder(LPSTR lpProfileFolder, BOOL bFirefox);
  779. private:
  780. int GetProfileFolder_9598ME(LPSTR lpProfileFolder, BOOL bFirefox);
  781. int GetProfileFolder_2000XP(LPSTR lpProfileFolder, BOOL bFirefox);
  782. };
  783. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  784. class CMozSettings
  785. {
  786. public:
  787. CMozSettings(BOOL bFirefox);
  788. virtual ~CMozSettings();
  789. int GetPreference(LPSTR lpPreferenceWanted, int *pnDest);
  790. int GetPreference(LPSTR lpPreferenceWanted, LPSTR lpDest, int sizeof_dest);
  791. private:
  792. CProfileFolder m_pf;
  793. HGLOBAL m_hData;
  794. LPSTR m_lpData;
  795. int m_sizeof_data;
  796. };
  797. int CProfileFolder::GetProfileFolder(LPSTR lpProfileFolder, BOOL bFirefox)
  798. {
  799. // See http://www.mozilla.org/support/firefox/edit for where I got this info:
  800. // On Windows XP/2000, the path is usually %AppData%\Mozilla\Firefox\Profiles\xxxxxxxx.default\, where xxxxxxxx is a random string of characters. Just browse to C:\Documents and Settings\[User Name]\Application Data\Mozilla\Firefox\Profiles\ and the rest should be obvious.
  801. // On Windows 95/98/Me, the path is usually C:\WINDOWS\Application Data\Mozilla\Firefox\Profiles\xxxxxxxx.default\
  802. // On Linux, the path is usually ~/.mozilla/firefox/xxxxxxxx.default/
  803. // On Mac OS X, the path is usually ~/Library/Application Support/Firefox/Profiles/xxxxxxxx.default/
  804. OSVERSIONINFO version;
  805. ZeroMemory(&version, sizeof(version));
  806. version.dwOSVersionInfoSize = sizeof(version);
  807. GetVersionEx(&version);
  808. if(version.dwMajorVersion == 3) {
  809. return -1; // NT 3.51 not supported
  810. }
  811. if(version.dwMajorVersion == 4) {
  812. return GetProfileFolder_9598ME(lpProfileFolder, bFirefox);
  813. }
  814. if(version.dwMajorVersion >= 5) {
  815. return GetProfileFolder_2000XP(lpProfileFolder, bFirefox);
  816. }
  817. return -1;
  818. }
  819. // private function for GetProfileFolder()
  820. // on my test system, the folder to get is c:\windows\application data\mozilla\profiles\default\y3h9azmi.slt
  821. int CProfileFolder::GetProfileFolder_9598ME(LPSTR lpProfileFolder, BOOL bFirefox)
  822. {
  823. WIN32_FIND_DATA fd;
  824. HANDLE hFind;
  825. BOOL bDone, bFound;
  826. char szHomePath[MAX_PATH] = {0};
  827. char szTemp[MAX_PATH] = {0};
  828. if(lpProfileFolder) {
  829. GetEnvironmentVariable("WINDIR", szHomePath, sizeof(szHomePath));
  830. strcpy(lpProfileFolder, szHomePath);
  831. if(bFirefox) {
  832. strcat(lpProfileFolder, "\\Application Data\\Mozilla\\Firefox\\Profiles\\");
  833. }else{
  834. strcat(lpProfileFolder, "\\Application Data\\Mozilla\\Profiles\\default\\");
  835. }
  836. // find the first folder in the the path specified in szProfileFolder
  837. lstrcpyn(szTemp, lpProfileFolder, MAX_PATH-4);
  838. strcat(szTemp, "*.*");
  839. bDone = FALSE;
  840. bFound = FALSE;
  841. hFind = FindFirstFile(szTemp, &fd);
  842. while(hFind != INVALID_HANDLE_VALUE && !bDone) {
  843. if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  844. // we're at a directory.
  845. // make sure it's not "." or ".."
  846. if(fd.cFileName[0] != '.') {
  847. bFound = TRUE;
  848. }
  849. }
  850. bDone = !FindNextFile(hFind, &fd);
  851. }
  852. FindClose(hFind);
  853. if(bFound) {
  854. strcat(lpProfileFolder, fd.cFileName);
  855. return 0;
  856. }
  857. }
  858. return -1;
  859. }
  860. // private function for GetProfileFolder()
  861. int CProfileFolder::GetProfileFolder_2000XP(LPSTR lpProfileFolder, BOOL bFirefox)
  862. {
  863. WIN32_FIND_DATA fd;
  864. HANDLE hFind;
  865. BOOL bDone, bFound;
  866. char szHomePath[MAX_PATH] = {0};
  867. char szTemp[MAX_PATH] = {0};
  868. if(lpProfileFolder) {
  869. GetEnvironmentVariable("APPDATA", szHomePath, sizeof(szHomePath));
  870. strcpy(lpProfileFolder, szHomePath);
  871. if(bFirefox) {
  872. strcat(lpProfileFolder, "\\Mozilla\\Firefox\\Profiles\\");
  873. }else{
  874. strcat(lpProfileFolder, "\\Mozilla\\Profiles\\default\\");
  875. }
  876. // find the first folder in the the path specified in szProfileFolder
  877. strcpy(szTemp, lpProfileFolder);
  878. strcat(szTemp, "*.*");
  879. bDone = FALSE;
  880. bFound = FALSE;
  881. hFind = FindFirstFile(szTemp, &fd);
  882. while(hFind != INVALID_HANDLE_VALUE && !bDone) {
  883. if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  884. // we're at a directory.
  885. // make sure it's not "." or ".."
  886. if(fd.cFileName[0] != '.') {
  887. bFound = TRUE;
  888. }
  889. }
  890. bDone = !FindNextFile(hFind, &fd);
  891. }
  892. FindClose(hFind);
  893. if(bFound) {
  894. strcat(lpProfileFolder, fd.cFileName);
  895. return 0;
  896. }
  897. }
  898. return -1;
  899. }
  900. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  901. CMozSettings::CMozSettings(BOOL bFirefox)
  902. {
  903. WIN32_FIND_DATA fd;
  904. OFSTRUCT of;
  905. HANDLE hFind;
  906. HFILE hPrefsFile;
  907. char szProfileFolder[MAX_PATH] = {0};
  908. char szPrefsFile[MAX_PATH] = {0};
  909. int ret;
  910. m_hData = NULL;
  911. m_lpData = NULL;
  912. ret = m_pf.GetProfileFolder(szProfileFolder, bFirefox);
  913. if(ret == 0) {
  914. // We found the folder where prefs.js lives. Read it in.
  915. strcpy(szPrefsFile, szProfileFolder);
  916. strcat(szPrefsFile, "\\prefs.js");
  917. // get the size of the file and alloc memory for it
  918. hFind = FindFirstFile(szPrefsFile, &fd);
  919. if(hFind != INVALID_HANDLE_VALUE) {
  920. m_hData = GlobalAlloc(GHND, fd.nFileSizeLow + 256);
  921. if(m_hData) {
  922. m_lpData = (LPSTR)GlobalLock(m_hData);
  923. if(m_lpData) {
  924. hPrefsFile = OpenFile(szPrefsFile, &of, OF_READ);
  925. if(hPrefsFile) {
  926. m_sizeof_data = fd.nFileSizeLow;
  927. _lread(hPrefsFile, m_lpData, m_sizeof_data);
  928. _lclose(hPrefsFile);
  929. hPrefsFile = NULL;
  930. }
  931. }
  932. }
  933. FindClose(hFind);
  934. }
  935. }
  936. }
  937. CMozSettings::~CMozSettings()
  938. {
  939. if(m_lpData) {
  940. GlobalUnlock(m_hData);
  941. m_lpData = NULL;
  942. }
  943. if(m_hData) {
  944. GlobalFree(m_hData);
  945. m_hData = NULL;
  946. }
  947. }
  948. int CMozSettings::GetPreference(LPSTR lpPreferenceWanted, LPSTR lpDest, int sizeof_dest)
  949. {
  950. LPSTR lpPointer, lpPointerEnd, lpData;
  951. LPSTR lpLineStart, lpSearchStart, lpFoundString, lpResult;
  952. BOOL bDone;
  953. int nDoubleQuoteCount, retval;
  954. retval = -1;
  955. if(m_lpData) {
  956. if(lpPreferenceWanted) {
  957. if(lpDest) {
  958. *lpDest = '\0';
  959. bDone = FALSE;
  960. lpPointer = m_lpData;
  961. lpPointerEnd = lpPointer + m_sizeof_data;
  962. while(lpPointer < lpPointerEnd && !bDone) {
  963. if(strncmp(lpPointer, "user_pref(", 10) == 0) {
  964. lpLineStart = lpPointer;
  965. lpSearchStart = lpLineStart + 11;
  966. if(strncmp(lpSearchStart, lpPreferenceWanted, strlen(lpPreferenceWanted)) == 0) {
  967. lpFoundString = lpSearchStart + strlen(lpPreferenceWanted);
  968. // lpFoundString almost points to what we want. Skip over the " character it's at now, skip over the " character
  969. // starting the value we want and null-terminate what we want when we find the 3rd " character
  970. lpData = lpFoundString;
  971. nDoubleQuoteCount = 0;
  972. while(nDoubleQuoteCount <= 3 && !bDone && lpData < lpPointerEnd) {
  973. if(*lpData == '"') {
  974. nDoubleQuoteCount++;
  975. if(nDoubleQuoteCount == 2) {
  976. // we're at the starting quote
  977. lpResult = lpData;
  978. lpResult++;
  979. }
  980. if(nDoubleQuoteCount == 3) {
  981. // we're at the ending quote
  982. // null-terminate what we want, and copy it to the dest buffer
  983. *lpData = '\0';
  984. lstrcpyn(lpDest, lpResult, sizeof_dest);
  985. bDone = TRUE;
  986. retval = 0;
  987. }
  988. }
  989. lpData++;
  990. }
  991. }
  992. }
  993. lpPointer++;
  994. }
  995. }
  996. }
  997. }
  998. return retval;
  999. }
  1000. int CMozSettings::GetPreference(LPSTR lpPreferenceWanted, int *pnDest)
  1001. {
  1002. LPSTR lpPointer, lpPointerEnd, lpData;
  1003. LPSTR lpLineStart, lpSearchStart, lpFoundString;
  1004. BOOL bDone;
  1005. int retval;
  1006. retval = -1;
  1007. if(m_lpData) {
  1008. if(lpPreferenceWanted) {
  1009. if(pnDest) {
  1010. bDone = FALSE;
  1011. lpPointer = m_lpData;
  1012. lpPointerEnd = lpPointer + m_sizeof_data;
  1013. while(lpPointer < lpPointerEnd && !bDone) {
  1014. if(strncmp(lpPointer, "user_pref(", 10) == 0) {
  1015. lpLineStart = lpPointer;
  1016. lpSearchStart = lpLineStart + 11;
  1017. if(strncmp(lpSearchStart, lpPreferenceWanted, strlen(lpPreferenceWanted)) == 0) {
  1018. lpFoundString = lpSearchStart + strlen(lpPreferenceWanted);
  1019. // lpFoundString almost points to what we want. Skip over the " character it's at now, skip over the ","
  1020. // starting the value we want and null-terminate what we want when we find the 3rd " character
  1021. lpData = lpFoundString;
  1022. while(*lpData != ',' && lpData < lpPointerEnd) {
  1023. lpData++;
  1024. }
  1025. if(*lpData == ',') {
  1026. lpData++;
  1027. lpFoundString = lpData;
  1028. while(*lpData != ')' && lpData < lpPointerEnd) {
  1029. lpData++;
  1030. }
  1031. if(*lpData == ')') {
  1032. // null-terminate what we want, and copy it to the dest buffer
  1033. *lpData = '\0';
  1034. *pnDest = atoi(lpFoundString);
  1035. bDone = TRUE;
  1036. retval = 0;
  1037. }
  1038. }
  1039. }
  1040. }
  1041. lpPointer++;
  1042. }
  1043. }
  1044. }
  1045. }
  1046. return retval;
  1047. }
  1048. ////////////////////////////////////////////////////////////////////////
  1049. BOOL IsFirefoxProxySet()
  1050. {
  1051. return IsFirefoxOrMozillaProxySet(TRUE);
  1052. }
  1053. BOOL IsMozillaProxySet()
  1054. {
  1055. return IsFirefoxOrMozillaProxySet(FALSE);
  1056. }
  1057. BOOL IsFirefoxOrMozillaProxySet(BOOL bFirefox)
  1058. {
  1059. CMozSettings settings(bFirefox);
  1060. int ret, nValue;
  1061. ret = settings.GetPreference("network.proxy.type", &nValue);
  1062. if(ret == 0) {
  1063. switch(nValue) {
  1064. case 0: // shouldn't get here
  1065. break;
  1066. case 1: // manual configuration
  1067. return TRUE;
  1068. case 2: // automatic configuration
  1069. return TRUE;
  1070. case 4: // auto-detect
  1071. return TRUE;
  1072. default: // don't know
  1073. break;
  1074. }
  1075. }
  1076. return FALSE;
  1077. }
  1078. int ResolveURL_Mozilla(LPSTR lpURL, LPSTR lpHostname, LPSTR lpIPAddress, int sizeof_address, int *pnPort)
  1079. {
  1080. return ResolveURL_MozillaOrFirefox(FALSE, lpURL, lpHostname, lpIPAddress, sizeof_address, pnPort);
  1081. }
  1082. int ResolveURL_Firefox(LPSTR lpURL, LPSTR lpHostname, LPSTR lpIPAddress, int sizeof_address, int *pnPort)
  1083. {
  1084. return ResolveURL_MozillaOrFirefox(TRUE, lpURL, lpHostname, lpIPAddress, sizeof_address, pnPort);
  1085. }
  1086. int ResolveURL_MozillaOrFirefox(BOOL bFirefox, LPSTR lpURL, LPSTR lpHostname, LPSTR lpIPAddress, int sizeof_address, int *pnPort)
  1087. {
  1088. CMozSettings setting(bFirefox);
  1089. int ret, nValue;
  1090. // search for the "network.proxy.http" preference
  1091. ret = setting.GetPreference("network.proxy.type", &nValue);
  1092. if(ret == 0) {
  1093. switch(nValue) {
  1094. case 0:
  1095. // shouldn't get here
  1096. break;
  1097. case 1:
  1098. // manual configuration
  1099. setting.GetPreference("network.proxy.http", lpIPAddress, sizeof_address);
  1100. setting.GetPreference("network.proxy.http_port", pnPort);
  1101. break;
  1102. case 2:
  1103. // automatic configuration
  1104. {
  1105. char szWPADLocation[MAX_PATH] = {0};
  1106. setting.GetPreference("network.proxy.autoconfig_url", szWPADLocation, sizeof(szWPADLocation));
  1107. ret = ReadWPADFile(szWPADLocation, lpIPAddress, pnPort);
  1108. }
  1109. break;
  1110. case 4:
  1111. // Auto-detect proxy settings for this network
  1112. ret = ReadWPADFile("http://wpad/wpad.dat", lpIPAddress, pnPort);
  1113. break;
  1114. default:
  1115. break;
  1116. }
  1117. }
  1118. return ret;
  1119. }
  1120. // My function that downloads from a URL to a file using the Nullsoft JNetLib library instead of using
  1121. // URLDownloadToFile(). Only parameters 2 and 3 are used, to mimick the parameters of URLDownloadToFile().
  1122. HRESULT JNetLibDownloadToFile(LPVOID lpUnused1, LPSTR lpWPADLocation, LPSTR lpTempFile, LPVOID lpUnused2, LPVOID lpUnused3)
  1123. {
  1124. api_httpreceiver *http=0;
  1125. waServiceFactory *sf=0;
  1126. OFSTRUCT of;
  1127. HGLOBAL hData;
  1128. HRESULT hRet = S_FALSE; // default return value
  1129. LPSTR lpData;
  1130. DWORD dwSize;
  1131. HFILE hFile;
  1132. BOOL bDone;
  1133. //JNL jNetLib;
  1134. int ret;
  1135. if(lpWPADLocation && lpTempFile)
  1136. {
  1137. if (WASABI_API_SVC)
  1138. {
  1139. sf = WASABI_API_SVC->service_getServiceByGuid(httpreceiverGUID);
  1140. if (sf) http = (api_httpreceiver *)sf->getInterface();
  1141. }
  1142. if (!http)
  1143. return S_FALSE;
  1144. // init the library and open a connection to the URL
  1145. http->Open();
  1146. http->Connect(lpWPADLocation);
  1147. // loop until JNetLib gets the data.
  1148. // run() returns 0 if OK, -1 if error (call geterrorstr()), or 1 if connection closed.
  1149. bDone = FALSE;
  1150. while(!bDone) {
  1151. ret = http->Run();
  1152. if(ret == -1 || ret == 1) {
  1153. bDone = TRUE;
  1154. }
  1155. Sleep(50);
  1156. }
  1157. dwSize = (DWORD)http->GetContentLength();
  1158. if(dwSize && ret == 1) {
  1159. // Got something!
  1160. // Allocate memory for it and write it to lpTempFile
  1161. hData = GlobalAlloc(GHND, dwSize + 100);
  1162. if(hData) {
  1163. lpData = (LPSTR)GlobalLock(hData);
  1164. if(lpData) {
  1165. http->GetBytes(lpData, (int)dwSize);
  1166. // create the output file and write to it
  1167. hFile = OpenFile(lpTempFile, &of, OF_CREATE);
  1168. if(hFile != HFILE_ERROR) {
  1169. _lwrite(hFile, lpData, (UINT)dwSize);
  1170. _lclose(hFile);
  1171. hRet = S_OK; // success
  1172. }
  1173. GlobalUnlock(hData);
  1174. lpData = NULL;
  1175. }
  1176. GlobalFree(hData);
  1177. hData = NULL;
  1178. }
  1179. }
  1180. }
  1181. if (http && sf)
  1182. sf->releaseInterface(http);
  1183. return hRet;
  1184. }
  1185. #endif