You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystemProxy.cs 8.1 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System.Windows.Forms;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Runtime.InteropServices;
  5. using System.IO;
  6. using Shadowsocks.Model;
  7. using Shadowsocks.Util;
  8. namespace Shadowsocks.Controller
  9. {
  10. public static class SystemProxy
  11. {
  12. [DllImport("wininet.dll")]
  13. public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
  14. public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
  15. public const int INTERNET_OPTION_REFRESH = 37;
  16. static bool _settingsReturn, _refreshReturn;
  17. public static void NotifyIE()
  18. {
  19. // These lines implement the Interface in the beginning of program
  20. // They cause the OS to refresh the settings, causing IP to realy update
  21. _settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
  22. _refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
  23. }
  24. private static readonly DateTime UnixEpoch
  25. = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  26. public static long ToUnixEpochMilliseconds(this DateTime dt)
  27. => (long)(dt - UnixEpoch).TotalMilliseconds;
  28. private static string GetTimestamp(DateTime value)
  29. {
  30. return value.ToString("yyyyMMddHHmmssfff");
  31. }
  32. public static void Update(Configuration config, bool forceDisable)
  33. {
  34. bool global = config.global;
  35. bool enabled = config.enabled;
  36. if (forceDisable)
  37. {
  38. enabled = false;
  39. }
  40. RegistryKey registry = null;
  41. try {
  42. registry = Utils.OpenUserRegKey( @"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true );
  43. if ( registry == null ) {
  44. Logging.Error( @"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" );
  45. return;
  46. }
  47. if ( enabled ) {
  48. if ( global ) {
  49. registry.SetValue( "ProxyEnable", 1 );
  50. registry.SetValue( "ProxyServer", "127.0.0.1:" + config.localPort.ToString() );
  51. registry.SetValue( "AutoConfigURL", "" );
  52. } else {
  53. string pacUrl;
  54. if ( config.useOnlinePac && ! config.pacUrl.IsNullOrEmpty() )
  55. pacUrl = config.pacUrl;
  56. else
  57. pacUrl = $"http://127.0.0.1:{config.localPort}/pac?t={GetTimestamp( DateTime.Now )}";
  58. registry.SetValue( "ProxyEnable", 0 );
  59. var readProxyServer = registry.GetValue( "ProxyServer" );
  60. registry.SetValue( "ProxyServer", "" );
  61. registry.SetValue( "AutoConfigURL", pacUrl );
  62. }
  63. } else {
  64. registry.SetValue( "ProxyEnable", 0 );
  65. registry.SetValue( "ProxyServer", "" );
  66. registry.SetValue( "AutoConfigURL", "" );
  67. }
  68. //Set AutoDetectProxy
  69. IEAutoDetectProxy( ! enabled );
  70. NotifyIE();
  71. //Must Notify IE first, or the connections do not chanage
  72. CopyProxySettingFromLan();
  73. } catch ( Exception e ) {
  74. Logging.LogUsefulException( e );
  75. // TODO this should be moved into views
  76. MessageBox.Show( I18N.GetString( "Failed to update registry" ) );
  77. } finally {
  78. if ( registry != null ) {
  79. try { registry.Close(); }
  80. catch (Exception e)
  81. { Logging.LogUsefulException(e); }
  82. }
  83. }
  84. }
  85. private static void CopyProxySettingFromLan()
  86. {
  87. RegistryKey registry = null;
  88. try {
  89. registry = Utils.OpenUserRegKey( @"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections", true );
  90. if ( registry == null ) {
  91. Logging.Error( @"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections" );
  92. return;
  93. }
  94. var defaultValue = registry.GetValue( "DefaultConnectionSettings" );
  95. var connections = registry.GetValueNames();
  96. foreach ( var each in connections ) {
  97. switch ( each.ToUpperInvariant() ) {
  98. case "DEFAULTCONNECTIONSETTINGS":
  99. case "LAN CONNECTION":
  100. case "SAVEDLEGACYSETTINGS":
  101. continue;
  102. default:
  103. //set all the connections's proxy as the lan
  104. registry.SetValue( each, defaultValue );
  105. continue;
  106. }
  107. }
  108. NotifyIE();
  109. } catch ( IOException e ) {
  110. Logging.LogUsefulException( e );
  111. } finally {
  112. if ( registry != null ) {
  113. try { registry.Close(); }
  114. catch (Exception e)
  115. { Logging.LogUsefulException(e); }
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// Checks or unchecks the IE Options Connection setting of "Automatically detect Proxy"
  121. /// </summary>
  122. /// <param name="set">Provide 'true' if you want to check the 'Automatically detect Proxy' check box. To uncheck, pass 'false'</param>
  123. private static void IEAutoDetectProxy(bool set)
  124. {
  125. RegistryKey registry = null;
  126. try {
  127. registry = Utils.OpenUserRegKey( @"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections", true );
  128. if ( registry == null ) {
  129. Logging.Error( @"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections" );
  130. return;
  131. }
  132. var defConnection = ( byte[] ) registry.GetValue( "DefaultConnectionSettings" );
  133. var savedLegacySetting = ( byte[] ) registry.GetValue( "SavedLegacySettings" );
  134. const int versionOffset = 4;
  135. const int optionsOffset = 8;
  136. if ( set ) {
  137. defConnection[ optionsOffset ] = ( byte ) ( defConnection[ optionsOffset ] | 8 );
  138. savedLegacySetting[ optionsOffset ] = ( byte ) ( savedLegacySetting[ optionsOffset ] | 8 );
  139. } else {
  140. defConnection[ optionsOffset ] = ( byte ) ( defConnection[ optionsOffset ] & ~8 );
  141. savedLegacySetting[ optionsOffset ] = ( byte ) ( savedLegacySetting[ optionsOffset ] & ~8 );
  142. }
  143. BitConverter.GetBytes(unchecked( BitConverter.ToUInt32( defConnection, versionOffset ) + 1 ) )
  144. .CopyTo( defConnection, versionOffset );
  145. BitConverter.GetBytes(unchecked( BitConverter.ToUInt32( savedLegacySetting, versionOffset ) + 1 ) )
  146. .CopyTo( savedLegacySetting, versionOffset );
  147. registry.SetValue( "DefaultConnectionSettings", defConnection );
  148. registry.SetValue( "SavedLegacySettings", savedLegacySetting );
  149. } catch ( Exception e ) {
  150. Logging.LogUsefulException( e );
  151. } finally {
  152. if (registry != null)
  153. {
  154. try { registry.Close(); }
  155. catch (Exception e)
  156. { Logging.LogUsefulException(e); }
  157. }
  158. }
  159. }
  160. }
  161. }