internal bool SetToWinINET(string sConnectionName) { bool result; try { WinINETProxyInfo.INTERNET_PER_CONN_OPTION_LIST iNTERNET_PER_CONN_OPTION_LIST = default(WinINETProxyInfo.INTERNET_PER_CONN_OPTION_LIST); WinINETProxyInfo.INTERNET_PER_CONN_OPTION[] array = new WinINETProxyInfo.INTERNET_PER_CONN_OPTION[5]; if (sConnectionName == "DefaultLAN") { sConnectionName = null; } iNTERNET_PER_CONN_OPTION_LIST.Connection = sConnectionName; iNTERNET_PER_CONN_OPTION_LIST.OptionCount = array.Length; iNTERNET_PER_CONN_OPTION_LIST.OptionError = 0; int num = 0; if (this._bDirect) { num |= 1; } if (this._bAutoDetect) { num |= 8; } if (this._bAutoDetectWasUserSet) { num |= 8; } if (this._bUseConfigScript) { num |= 4; } if (this._bProxiesSpecified) { num |= 2; } array[0] = new WinINETProxyInfo.INTERNET_PER_CONN_OPTION(); array[0].dwOption = 1; array[0].Value.dwValue = num; array[1] = new WinINETProxyInfo.INTERNET_PER_CONN_OPTION(); array[1].dwOption = 2; array[1].Value.pszValue = Marshal.StringToHGlobalAnsi(this.CalculateProxyString()); array[2] = new WinINETProxyInfo.INTERNET_PER_CONN_OPTION(); array[2].dwOption = 3; array[2].Value.pszValue = Marshal.StringToHGlobalAnsi(this._sHostsThatBypass); array[3] = new WinINETProxyInfo.INTERNET_PER_CONN_OPTION(); array[3].dwOption = 4; array[3].Value.pszValue = Marshal.StringToHGlobalAnsi(this._sScriptURL); array[4] = new WinINETProxyInfo.INTERNET_PER_CONN_OPTION(); array[4].dwOption = 5; array[4].Value.dwValue = 0; if (this._bAutoDetectWasUserSet) { array[4].Value.dwValue = 1; } int num2 = 0; for (int i = 0; i < array.Length; i++) { num2 += Marshal.SizeOf(array[i]); } IntPtr intPtr = Marshal.AllocCoTaskMem(num2); IntPtr intPtr2 = intPtr; for (int j = 0; j < array.Length; j++) { Marshal.StructureToPtr(array[j], intPtr2, false); intPtr2 = (IntPtr)((long)intPtr2 + (long)Marshal.SizeOf(array[j])); } iNTERNET_PER_CONN_OPTION_LIST.pOptions = intPtr; iNTERNET_PER_CONN_OPTION_LIST.Size = Marshal.SizeOf(iNTERNET_PER_CONN_OPTION_LIST); int size = iNTERNET_PER_CONN_OPTION_LIST.Size; bool flag = WinINETProxyInfo.InternetSetOption_1(IntPtr.Zero, 75, ref iNTERNET_PER_CONN_OPTION_LIST, size); int lastWin32Error = Marshal.GetLastWin32Error(); if (flag) { WinINETProxyInfo.InternetSetOption(IntPtr.Zero, 95, IntPtr.Zero, 0); } else { Trace.WriteLine("[Fiddler] SetProxy failed. WinINET Error #" + lastWin32Error.ToString("x")); } Marshal.FreeHGlobal(array[0].Value.pszValue); Marshal.FreeHGlobal(array[1].Value.pszValue); Marshal.FreeHGlobal(array[2].Value.pszValue); Marshal.FreeCoTaskMem(intPtr); result = flag; } catch (Exception ex) { Trace.WriteLine("[Fiddler] SetProxy failed. " + ex.Message); result = false; } return(result); }
public bool GetFromWinINET(string sConnectionName) { this.Clear(); bool result; try { WinINETProxyInfo.INTERNET_PER_CONN_OPTION_LIST iNTERNET_PER_CONN_OPTION_LIST = default(WinINETProxyInfo.INTERNET_PER_CONN_OPTION_LIST); WinINETProxyInfo.INTERNET_PER_CONN_OPTION[] array = new WinINETProxyInfo.INTERNET_PER_CONN_OPTION[5]; if (sConnectionName == "DefaultLAN") { sConnectionName = null; } iNTERNET_PER_CONN_OPTION_LIST.Connection = sConnectionName; iNTERNET_PER_CONN_OPTION_LIST.OptionCount = array.Length; iNTERNET_PER_CONN_OPTION_LIST.OptionError = 0; array[0] = new WinINETProxyInfo.INTERNET_PER_CONN_OPTION(); array[0].dwOption = 1; array[1] = new WinINETProxyInfo.INTERNET_PER_CONN_OPTION(); array[1].dwOption = 2; array[2] = new WinINETProxyInfo.INTERNET_PER_CONN_OPTION(); array[2].dwOption = 3; array[3] = new WinINETProxyInfo.INTERNET_PER_CONN_OPTION(); array[3].dwOption = 4; array[4] = new WinINETProxyInfo.INTERNET_PER_CONN_OPTION(); array[4].dwOption = 10; int num = 0; for (int i = 0; i < array.Length; i++) { num += Marshal.SizeOf(array[i]); } IntPtr intPtr = Marshal.AllocCoTaskMem(num); IntPtr intPtr2 = intPtr; for (int j = 0; j < array.Length; j++) { Marshal.StructureToPtr(array[j], intPtr2, false); intPtr2 = (IntPtr)((long)intPtr2 + (long)Marshal.SizeOf(array[j])); } iNTERNET_PER_CONN_OPTION_LIST.pOptions = intPtr; iNTERNET_PER_CONN_OPTION_LIST.Size = Marshal.SizeOf(iNTERNET_PER_CONN_OPTION_LIST); int size = iNTERNET_PER_CONN_OPTION_LIST.Size; bool flag = WinINETProxyInfo.InternetQueryOption(IntPtr.Zero, 75, ref iNTERNET_PER_CONN_OPTION_LIST, ref size); int lastWin32Error = Marshal.GetLastWin32Error(); if (!flag) { if (87 == lastWin32Error) { array[4].dwOption = 5; intPtr2 = intPtr; for (int k = 0; k < array.Length; k++) { Marshal.StructureToPtr(array[k], intPtr2, false); intPtr2 = (IntPtr)((long)intPtr2 + (long)Marshal.SizeOf(array[k])); } iNTERNET_PER_CONN_OPTION_LIST.pOptions = intPtr; iNTERNET_PER_CONN_OPTION_LIST.Size = Marshal.SizeOf(iNTERNET_PER_CONN_OPTION_LIST); size = iNTERNET_PER_CONN_OPTION_LIST.Size; flag = WinINETProxyInfo.InternetQueryOption(IntPtr.Zero, 75, ref iNTERNET_PER_CONN_OPTION_LIST, ref size); lastWin32Error = Marshal.GetLastWin32Error(); } if (!flag) { FiddlerApplication.Log.LogFormat("Fiddler was unable to get information about the proxy for '{0}' [0x{1}].\n", new object[] { sConnectionName ?? "DefaultLAN", lastWin32Error.ToString("x") }); } } if (flag) { intPtr2 = intPtr; for (int l = 0; l < array.Length; l++) { Marshal.PtrToStructure(intPtr2, array[l]); intPtr2 = (IntPtr)((long)intPtr2 + (long)Marshal.SizeOf(array[l])); } this._bDirect = (1 == (array[0].Value.dwValue & 1)); this._bUseConfigScript = (4 == (array[0].Value.dwValue & 4)); this._bAutoDetect = (8 == (array[0].Value.dwValue & 8)); this._bProxiesSpecified = (2 == (array[0].Value.dwValue & 2)); if (array[4].dwOption == 10) { this._bAutoDetectWasUserSet = (8 == (array[4].Value.dwValue & 8)); } else { this._bAutoDetectWasUserSet = (this._bAutoDetect && 1 == (array[4].Value.dwValue & 1)); } this._sScriptURL = Marshal.PtrToStringAnsi(array[3].Value.pszValue); Utilities.GlobalFree(array[3].Value.pszValue); if (array[1].Value.pszValue != IntPtr.Zero) { string manualProxies = Marshal.PtrToStringAnsi(array[1].Value.pszValue); Utilities.GlobalFree(array[1].Value.pszValue); this.SetManualProxies(manualProxies); } if (array[2].Value.pszValue != IntPtr.Zero) { this._sHostsThatBypass = Marshal.PtrToStringAnsi(array[2].Value.pszValue); Utilities.GlobalFree(array[2].Value.pszValue); } } Marshal.FreeCoTaskMem(intPtr); result = flag; } catch (Exception eX) { FiddlerApplication.ReportException(eX, "Unable to get proxy information for " + (sConnectionName ?? "DefaultLAN")); result = false; } return(result); }