//STATIC METHODS public static Boolean GetProxySettingsData(out ProxySettingsData proxySettingsData) { proxySettingsData = new ProxySettingsData(); Object proxyEnabledValueObject; Object proxyOverrideValueObject; Object proxyServerValueObject; try { proxyEnabledValueObject = Registry.GetValue(ProxySettingsProcessResource.KeyName_InternetSettings, ProxySettingsProcessResource.ValueName_ProxyEnable, null); proxyOverrideValueObject = Registry.GetValue(ProxySettingsProcessResource.KeyName_InternetSettings, ProxySettingsProcessResource.ValueName_ProxyOverride, null); proxyServerValueObject = Registry.GetValue(ProxySettingsProcessResource.KeyName_InternetSettings, ProxySettingsProcessResource.ValueName_ProxyServer, null); } catch (Exception exception) { proxySettingsData = null; EnterpriseLibraryLogWriter.WriteExceptionLogEntry(exception); return false; } return ProxySettingsData.BuildProxySettingsData(proxyEnabledValueObject, proxyServerValueObject, proxyOverrideValueObject, out proxySettingsData); }
//FUNCTIONS private DataRow BuildRow(ProxySettingsData proxySettingsData) { DataRow dataRow = NewRow(); dataRow[ProxyProfilesDataTableResource.Column_BypassLocal] = proxySettingsData.BypassProxyForLocalAddress; if (proxySettingsData.FtpProtocolIpEndPoint != null) { dataRow[ProxyProfilesDataTableResource.Column_FtpProxyServerAddress] = proxySettingsData.FtpProtocolIpEndPoint.ToString(); } if (proxySettingsData.HttpProtocolIpEndPoint != null) { dataRow[ProxyProfilesDataTableResource.Column_HttpProxyServerAddress] = proxySettingsData.HttpProtocolIpEndPoint.ToString(); } if (proxySettingsData.HttpsProtocolIpEndPoint != null) { dataRow[ProxyProfilesDataTableResource.Column_HttpsProxyServerAddress] = proxySettingsData.HttpsProtocolIpEndPoint.ToString(); } if (proxySettingsData.ProxyServerIpEndPoint != null) { dataRow[ProxyProfilesDataTableResource.Column_ProxyServerAddress] = proxySettingsData.ProxyServerIpEndPoint.ToString(); } dataRow[ProxyProfilesDataTableResource.Column_SameProxyAllProtocols] = proxySettingsData.UseSameProxyServerForAllProtocols.ToString(); if (proxySettingsData.SocksProtocolIpEndPoint != null) { dataRow[ProxyProfilesDataTableResource.Column_SocksProxyServerAddress] = proxySettingsData.SocksProtocolIpEndPoint.ToString(); } return dataRow; }
//METHODS public Boolean AddProxySettingsData(ProxySettingsData proxySettingsData) { if (proxySettingsData.Validate()) { proxySettingsDataList.Add(proxySettingsData); return true; } else { return false; } }
public static Boolean BuildProxySettingsData(Object proxyEnabledValueObject, Object proxyServerValueObject, Object proxyOverrideValueObject, out ProxySettingsData proxySettingsData) { proxySettingsData = new ProxySettingsData(); proxySettingsData.ProxyEnable = MyDataConverter.ToBooleanNullable(proxyEnabledValueObject); String proxyServerValue = MyDataConverter.ToString(proxyServerValueObject); if (proxyServerValue != null) { if (proxyServerValue.Contains(";")) { foreach (String proxyServerValueElement in proxyServerValue.Split(";".ToCharArray())) { String[] proxyAddressArray = proxyServerValueElement.Split(":".ToCharArray()); if (proxyAddressArray.Length == 2) { IPEndPoint ipEndPoint; if (proxyAddressArray[0].StartsWith(ProxySettingsDataResource.Protocol_Ftp)) { if (IpEndPointFactory.BuildIpEndPoint(proxyAddressArray[0].Replace(ProxySettingsDataResource.Protocol_Ftp, null), proxyAddressArray[1], out ipEndPoint)) { proxySettingsData.FtpProtocolIpEndPoint = ipEndPoint; continue; } else { return false; } } if (proxyAddressArray[0].StartsWith(ProxySettingsDataResource.Protocol_Http)) { if (IpEndPointFactory.BuildIpEndPoint(proxyAddressArray[0].Replace(ProxySettingsDataResource.Protocol_Http, null), proxyAddressArray[1], out ipEndPoint)) { proxySettingsData.HttpProtocolIpEndPoint = ipEndPoint; continue; } else { return false; } } if (proxyAddressArray[0].StartsWith(ProxySettingsDataResource.Protocol_Https)) { if (IpEndPointFactory.BuildIpEndPoint(proxyAddressArray[0].Replace(ProxySettingsDataResource.Protocol_Https, null), proxyAddressArray[1], out ipEndPoint)) { proxySettingsData.HttpsProtocolIpEndPoint = ipEndPoint; continue; } else { return false; } } if (proxyAddressArray[0].StartsWith(ProxySettingsDataResource.Protocol_Socks)) { if (IpEndPointFactory.BuildIpEndPoint(proxyAddressArray[0].Replace(ProxySettingsDataResource.Protocol_Socks, null), proxyAddressArray[1], out ipEndPoint)) { proxySettingsData.SocksProtocolIpEndPoint = ipEndPoint; continue; } else { return false; } } EnterpriseLibraryLogWriter.WriteLogEntry(EnterpriseLibraryLogWriterResource.Message_RegistryInvalidProxyAddress, EnterpriseLibraryLogWriterResource.Category_Error, 0, 0, TraceEventType.Error, EnterpriseLibraryLogWriterResource.Title_RegistryDataError); proxySettingsData = null; return false; } else { EnterpriseLibraryLogWriter.WriteLogEntry(EnterpriseLibraryLogWriterResource.Message_RegistryInvalidProxyAddress, EnterpriseLibraryLogWriterResource.Category_Error, 0, 0, TraceEventType.Error, EnterpriseLibraryLogWriterResource.Title_RegistryDataError); proxySettingsData = null; return false; } } proxySettingsData.UseSameProxyServerForAllProtocols = false; } else { String[] proxyAddressArray = proxyServerValue.Split(":".ToCharArray()); if (proxyAddressArray.Length == 2) { IPEndPoint ipEndPoint; if (IpEndPointFactory.BuildIpEndPoint(proxyAddressArray[0], proxyAddressArray[1], out ipEndPoint)) { proxySettingsData.ProxyServerIpEndPoint = ipEndPoint; proxySettingsData.UseSameProxyServerForAllProtocols = true; } else { proxySettingsData = null; return false; } } else { EnterpriseLibraryLogWriter.WriteLogEntry(EnterpriseLibraryLogWriterResource.Message_RegistryInvalidProxyAddress, EnterpriseLibraryLogWriterResource.Category_Error, 0, 0, TraceEventType.Error, EnterpriseLibraryLogWriterResource.Title_RegistryDataError); proxySettingsData = null; return false; } } } String proxyOverrideValue = MyDataConverter.ToString(proxyOverrideValueObject); if (proxyOverrideValue == null) { proxySettingsData.BypassProxyForLocalAddress = false; } else { if (proxyOverrideValue.Contains(ProxySettingsDataResource.ProxyOverrideValue_Local)) { proxySettingsData.BypassProxyForLocalAddress = true; proxySettingsData.Exceptions = proxyOverrideValue; } else { proxySettingsData.Exceptions = proxyOverrideValue; } } return true; }
//EVENTS private void saveButton_Click(object sender, EventArgs e) { ProxySettingsData proxySettingsData = new ProxySettingsData(); proxySettingsData.BypassProxyForLocalAddress = bypassProxyCheckBox.Checked; proxySettingsData.Exceptions = (exceptionsTextBox.Text.Length == 0 ? null : exceptionsTextBox.Text); proxySettingsData.ProxyEnable = true; proxySettingsData.UseSameProxyServerForAllProtocols = useSameProxyCheckBox.Checked; if (ftpProxyAddressTextBox.Text.Length > 0) { IPEndPoint ipEndPoint; if (IpEndPointFactory.BuildIpEndPoint(ftpProxyAddressTextBox.Text, ftpProxyPortTextBox.Text, out ipEndPoint)) { proxySettingsData.FtpProtocolIpEndPoint = ipEndPoint; } else { MessageBox.Show(MessageBoxResource.Invalid_FtpAddress, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } } if (httpProxyPortTextBox.Text.Length > 0) { IPEndPoint ipEndPoint; if (IpEndPointFactory.BuildIpEndPoint(httpProxyAddressTextBox.Text, httpProxyPortTextBox.Text, out ipEndPoint)) { proxySettingsData.HttpProtocolIpEndPoint = ipEndPoint; } else { MessageBox.Show(MessageBoxResource.Invalid_HttpAddress, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } } if (httpsProxyPortTextBox.Text.Length > 0) { IPEndPoint ipEndPoint; if (IpEndPointFactory.BuildIpEndPoint(httpsProxyAddressTextBox.Text, httpsProxyPortTextBox.Text, out ipEndPoint)) { proxySettingsData.HttpsProtocolIpEndPoint = ipEndPoint; } else { MessageBox.Show(MessageBoxResource.Invalid_HttpsAddress, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } } if (proxyServerAddressTextBox.Text.Length > 0) { IPEndPoint ipEndPoint; if (IpEndPointFactory.BuildIpEndPoint(proxyServerAddressTextBox.Text, proxyServerPortTextBox.Text, out ipEndPoint)) { proxySettingsData.ProxyServerIpEndPoint = ipEndPoint; } else { MessageBox.Show(MessageBoxResource.Invalid_ProxyAddress, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } } if (socksProxyAddressTextBox.Text.Length > 0) { IPEndPoint ipEndPoint; if (IpEndPointFactory.BuildIpEndPoint(socksProxyAddressTextBox.Text, socksProxyPortTextBox.Text, out ipEndPoint)) { proxySettingsData.SocksProtocolIpEndPoint = ipEndPoint; } else { MessageBox.Show(MessageBoxResource.Invalid_SocksAddress, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } } if (proxySettingsData.Validate()) { if (proxyProfilesProcess.AddProxySettingsData(proxySettingsData)) { if (proxyProfilesProcess.WriteProxySettingsDataList()) { MessageBox.Show(MessageBoxResource.Success_ProxyProfilesSaved, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); Close(); } else { MessageBox.Show(MessageBoxResource.Error_WriteProfiles, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } else { MessageBox.Show(MessageBoxResource.Error_AddProfile, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } else { MessageBox.Show(MessageBoxResource.Invalid_ProxySettingsData, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } }
public Boolean RemoveProxySettingsData(ProxySettingsData proxySettingsData) { return proxySettingsDataList.Remove(proxySettingsData); }
public static Boolean SetProxySettings(ProxySettingsData proxySettingsData) { if (proxySettingsData.Validate()) { try { Registry.SetValue(ProxySettingsProcessResource.KeyName_InternetSettings, ProxySettingsProcessResource.ValueName_ProxyEnable, (proxySettingsData.ProxyEnable.Value ? 1 : 0)); if (proxySettingsData.ProxyOverride == null) { Object proxyOverrideValueObject; proxyOverrideValueObject = Registry.GetValue(ProxySettingsProcessResource.KeyName_InternetSettings, ProxySettingsProcessResource.ValueName_ProxyOverride, null); if (proxyOverrideValueObject != null) { RegistryKey softwareRegistryKey = Registry.CurrentUser.OpenSubKey(ProxySettingsProcessResource.SubKeyName_Software); RegistryKey mircosoftRegistryKey = softwareRegistryKey.OpenSubKey(ProxySettingsProcessResource.SubKeyName_Microsoft); RegistryKey windowsRegistryKey = mircosoftRegistryKey.OpenSubKey(ProxySettingsProcessResource.SubKeyName_Windows); RegistryKey currentVersionRegistryKey = windowsRegistryKey.OpenSubKey(ProxySettingsProcessResource.SubKeyName_CurrentVersion); RegistryKey internetSettings = currentVersionRegistryKey.OpenSubKey(ProxySettingsProcessResource.SubKeyName_InternetSettings, true); internetSettings.DeleteValue(ProxySettingsProcessResource.Value_ProxyOverride, true); internetSettings.Close(); currentVersionRegistryKey.Close(); windowsRegistryKey.Close(); mircosoftRegistryKey.Close(); softwareRegistryKey.Close(); } } else { Registry.SetValue(ProxySettingsProcessResource.KeyName_InternetSettings, ProxySettingsProcessResource.ValueName_ProxyOverride, proxySettingsData.ProxyOverride); } Registry.SetValue(ProxySettingsProcessResource.KeyName_InternetSettings, ProxySettingsProcessResource.ValueName_ProxyServer, proxySettingsData.ProxyServer); } catch (Exception exception) { EnterpriseLibraryLogWriter.WriteExceptionLogEntry(exception); return false; } } else { return false; } return true; }