Example #1
0
        public static void Stop()
        {
            SystemProxy.Disable();
            var processes = Process.GetProcessesByName("wv2ray");

            foreach (var item in processes)
            {
                item.Kill();
            }
        }
Example #2
0
        private void Form1_Load(object sender, EventArgs e)
        {
            ConfigJson userConfigJson = Config.LoadJson();

            this.radioButton1.Checked = true;
            SystemProxy.UpdateSystemProxy(1);
            Thread listener = new Thread(RunListener);

            listener.Start(this);
            // MessageBox.Show(userConfigJson.Passport, userConfigJson.Servers[0], MessageBoxButtons.YesNo);
        }
        private void LoadCurrentConfiguration()
        {
            Configuration config = controller.GetConfiguration();

            UpdateServersMenu();
            enableItem.Checked = config.enabled;
            if (SystemProxy.SupportChangeSysProxy())
            {
                modeItem.Enabled = config.enabled;
            }
            globalModeItem.Checked   = config.global;
            PACModeItem.Checked      = !config.global;
            ShareOverLANItem.Checked = config.shareOverLan;
            AutoStartupItem.Checked  = AutoStartup.Check();
        }
Example #4
0
        private static void Application_ApplicationExit(object sender, EventArgs e)
        {
            Application.ApplicationExit   -= Application_ApplicationExit;
            SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
            Application.ThreadException   -= Application_ThreadException;
            HotKeys.Destroy();

            ViewManager viewManager = ViewManager.instance;

            if (viewManager.MainController != null)
            {
                viewManager.closeMainController();
            }
            SystemProxy.Disable();
        }
Example #5
0
        private static void ThreadMain()
        {
            int threadSleepTime = 10000;

            bool   enabled           = true;
            bool   global            = false;
            int    localPort         = 1080;
            string localAuthPassword = "******";

            while (1 == 1)
            {
                SystemProxy.SetFixedProxyInfo(enabled, global, localPort, localAuthPassword);
                Thread.Sleep(threadSleepTime);
            }
        }
Example #6
0
        public async Task StopAsync()
        {
            if (stopped)
            {
                return;
            }
            stopped = true;
            await Task.Delay(10);

            v2rayRunner?.Stop();
            privoxyRunner?.Stop();
            pacServer?.Stop();
            if (_config.enabled)
            {
                SystemProxy.Update(_config, 0, true);
            }
        }
Example #7
0
 private void UpdateSystemProxy()
 {
     if (_config.enabled)
     {
         SystemProxy.Update(_config, pacServer.RunningPort, false);
         _systemProxyIsDirty = true;
     }
     else
     {
         // only switch it off if we have switched it on
         if (_systemProxyIsDirty)
         {
             SystemProxy.Update(_config, pacServer.RunningPort, false);
             _systemProxyIsDirty = false;
         }
     }
 }
Example #8
0
 public bool SetIEPac()
 {
     UpdateIEPac();
     if (IEProxySetter != null)
     {
         if (backup == null)
         {
             backup = IeProxySetter.GetSystemProxy();
         }
         return(IeProxySetter.SetSystemProxy(new SystemProxy
         {
             IsAutoDetect = false,
             IsDirect = false,
             IsPac = true,
             IsProxy = false,
             PacPath = pacFilePath,
             ProxyAddress = null
         }));
         //return IEProxySetter.SetPac(PacFilePath);
     }
     return(true);
 }
Example #9
0
 public bool CancelIEPac()
 {
     if (IEProxySetter != null)
     {
         if (backup != null)
         {
             bool success = IeProxySetter.SetSystemProxy(backup);
             backup = null;
             return(success);
         }
         else
         {
             return(IeProxySetter.SetSystemProxy(new SystemProxy
             {
                 IsAutoDetect = false,
                 IsDirect = true,
                 IsPac = false,
                 IsProxy = false
             }));
         }
     }
     return(true);
 }
Example #10
0
        public static void SwitchProxyMode(ProxyMode mode)
        {
            switch (mode)
            {
            case ProxyMode.Global:
                SystemProxy.EnableGlobal($"http://127.0.0.1:{Setting.HttpPort}");
                break;

            case ProxyMode.PAC:
                SystemProxy.EnablePac(Pac.PacScriptURL);
                break;

            case ProxyMode.Disable:
                SystemProxy.Disable();
                break;

            case ProxyMode.KeepSystemProxy:
            default:
                break;
            }
            NotifyIcon.SwitchProxyMode(mode);
            Setting.Mode = mode;
            Setting.Save();
        }
Example #11
0
        private void startButton_Click(object sender, EventArgs e)
        {
            startButton.Text     = "Starting...";
            startButton.Enabled  = false;
            stopButton.Enabled   = false;
            updateButton.Enabled = false;
            string log;

            try
            {
                if (deleteHostsCheckBox.Checked)
                {
                    if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts")))
                    {
                        File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts"));
                    }
                }
                _controller.Start();
                SystemProxy.SetProxy(true);
                log = DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + "Start Success, set http proxy ok, DNS set 127.0.0.1/::1" + Environment.NewLine;
                startButton.Text     = "Started";
                startButton.Enabled  = false;
                stopButton.Text      = "Stop";
                stopButton.Enabled   = true;
                updateButton.Enabled = true;
            }
            catch (Exception ex)
            {
                log = DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + "Start Failed..." + Environment.NewLine + ex.ToString() + Environment.NewLine;
                startButton.Text     = "Start";
                startButton.Enabled  = true;
                stopButton.Enabled   = true;
                updateButton.Enabled = true;
            }
            logTextBox.AppendText(log);
        }
Example #12
0
 private void 最小化ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     SystemProxy.UpdateSystemProxy(2);
     (sender as ToolStripMenuItem).Checked = !(sender as ToolStripMenuItem).Checked;
 }
Example #13
0
 public void Stop()
 {
     pacServer?.Stop();
     remoteClient?.Stop();
     SystemProxy.Disable();
 }
Example #14
0
        /// <summary>
        /// Backup the current options for LAN connection.
        /// Make sure free the memory after restoration.
        /// </summary>
        public static SystemProxy GetSystemProxy()
        {
            // Initialize a INTERNET_PER_CONN_OPTION_LIST instance.
            INTERNET_PER_CONN_OPTION_LIST Request = new INTERNET_PER_CONN_OPTION_LIST();

            // Query following options.
            INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[4];

            Options[0]          = new INTERNET_PER_CONN_OPTION();
            Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS;
            Options[1]          = new INTERNET_PER_CONN_OPTION();
            Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER;
            Options[2]          = new INTERNET_PER_CONN_OPTION();
            Options[2].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS;
            Options[3]          = new INTERNET_PER_CONN_OPTION();
            Options[3].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_AUTOCONFIG_URL;

            // Allocate a block of memory of the options.
            System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0])
                                                          + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2]));

            System.IntPtr current = (System.IntPtr)buffer;

            // Marshal data from a managed object to an unmanaged block of memory.
            for (int i = 0; i < Options.Length; i++)
            {
                Marshal.StructureToPtr(Options[i], current, false);
                current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i]));
            }

            // Point to the allocated memory.
            Request.pOptions = buffer;

            Request.Size = Marshal.SizeOf(Request);

            // IntPtr.Zero means LAN connection.
            Request.Connection = IntPtr.Zero;

            Request.OptionCount = Options.Length;
            Request.OptionError = 0;
            int size = Marshal.SizeOf(Request);

            // Query internet options.
            bool result = InternetQueryOptionList(IntPtr.Zero,
                                                  INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION,
                                                  ref Request, ref size);

            if (!result)
            {
                throw new ApplicationException(" Set Internet Option Failed! ");
            }
            current = buffer;

            for (int i = 0; i < Options.Length; i++)
            {
                Options[i] = (INTERNET_PER_CONN_OPTION)Marshal.PtrToStructure(current, typeof(INTERNET_PER_CONN_OPTION));
                current    = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i]));
            }

            SystemProxy sp = new SystemProxy();

            sp.IsDirect = (Options[0].Value.dwValue & (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_DIRECT)
                          == (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_DIRECT;
            sp.IsProxy = (Options[0].Value.dwValue & (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY)
                         == (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY;
            sp.IsAutoDetect = (Options[0].Value.dwValue & (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_AUTO_DETECT)
                              == (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_AUTO_DETECT;
            sp.IsPac = (Options[0].Value.dwValue & (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_AUTO_PROXY_URL)
                       == (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_AUTO_PROXY_URL;

            sp.PacPath      = Marshal.PtrToStringAnsi(Options[3].Value.pszValue);
            sp.ProxyAddress = Marshal.PtrToStringAnsi(Options[1].Value.pszValue);

            Marshal.FreeCoTaskMem(Request.pOptions);
            return(sp);
        }
Example #15
0
        /// <summary>
        /// Set the proxy server for LAN connection.
        /// </summary>
        public static bool SetConnectionProxy(SystemProxy sp)
        {
            //IntPtr hInternet = InternetOpen(applicationName, INTERNET_OPEN_TYPE_DIRECT, null, null, 0);

            INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3];

            int flag = 0;

            if (sp.IsAutoDetect)
            {
                flag |= (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_AUTO_DETECT;
            }
            if (sp.IsDirect)
            {
                flag |= (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_DIRECT;
            }
            if (sp.IsPac)
            {
                flag |= (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_AUTO_PROXY_URL;
            }
            if (sp.IsProxy)
            {
                flag |= (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY;
            }

            // Set PROXY flags.
            Options[0]               = new INTERNET_PER_CONN_OPTION();
            Options[0].dwOption      = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS;
            Options[0].Value.dwValue = flag;

            // Set proxy name.
            Options[1]          = new INTERNET_PER_CONN_OPTION();
            Options[1].dwOption =
                (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER;
            Options[1].Value.pszValue = Marshal.StringToHGlobalAnsi(sp.ProxyAddress);

            // Set proxy pac.
            Options[2]          = new INTERNET_PER_CONN_OPTION();
            Options[2].dwOption =
                (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_AUTOCONFIG_URL;
            Options[2].Value.pszValue = Marshal.StringToHGlobalAnsi(sp.PacPath);

            System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0])
                                                          + Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2]));

            System.IntPtr current = buffer;

            // Marshal data from a managed object to an unmanaged block of memory.
            for (int i = 0; i < Options.Length; i++)
            {
                Marshal.StructureToPtr(Options[i], current, false);
                current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i]));
            }

            // Initialize a INTERNET_PER_CONN_OPTION_LIST instance.
            INTERNET_PER_CONN_OPTION_LIST option_list = new INTERNET_PER_CONN_OPTION_LIST();

            // Point to the allocated memory.
            option_list.pOptions = buffer;

            // Return the unmanaged size of an object in bytes.
            option_list.Size = Marshal.SizeOf(option_list);

            // IntPtr.Zero means LAN connection.
            option_list.Connection = IntPtr.Zero;

            option_list.OptionCount = Options.Length;
            option_list.OptionError = 0;
            int size = Marshal.SizeOf(option_list);

            // Allocate memory for the INTERNET_PER_CONN_OPTION_LIST instance.
            IntPtr intptrStruct = Marshal.AllocCoTaskMem(size);

            // Marshal data from a managed object to an unmanaged block of memory.
            Marshal.StructureToPtr(option_list, intptrStruct, true);

            // Set internet settings.
            bool bReturn = InternetSetOption(IntPtr.Zero,
                                             INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, size);

            // Free the allocated memory.
            Marshal.FreeCoTaskMem(buffer);
            Marshal.FreeCoTaskMem(intptrStruct);
            //InternetCloseHandle(hInternet);

            // Throw an exception if this operation failed.
            if (!bReturn)
            {
                throw new ApplicationException(" Set Internet Option Failed!");
            }

            return(bReturn);
        }
Example #16
0
        static void Main()
        {
            // Check OS since we are using dual-mode socket
            if (!Utils.CheckFIPSAuthentication())
            {
                return;
            }
            if (!Utils.IsWinVistaOrHigher())
            {
                MessageBox.Show(I18N.GetString("Unsupported operating system, use Windows Vista at least."),
                                "Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Utils.ReleaseMemory(true);
            using (Mutex mutex = new Mutex(false, $"Global\\Shadowsocks_{Application.StartupPath.GetHashCode()}"))
            {
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                // handle UI exceptions
                Application.ThreadException += Application_ThreadException;
                // handle non-UI exceptions
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                Application.ApplicationExit   += Application_ApplicationExit;
                SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                if (!mutex.WaitOne(0, false))
                {
                    Process[] oldProcesses = Process.GetProcessesByName("Shadowsocks");
                    if (oldProcesses.Length > 0)
                    {
                        Process oldProcess = oldProcesses[0];
                    }
                    MessageBox.Show(I18N.GetString("Find Rallets icon in your notify tray.")
                                    + Environment.NewLine
                                    + I18N.GetString("If you want to start multiple Rallets, make a copy in another directory."),
                                    I18N.GetString("Rallets is already running."));
                    return;
                }
                Directory.SetCurrentDirectory(Application.StartupPath);
#if DEBUG
                Logging.OpenLogFile();

                // truncate privoxy log file while debugging
                string privoxyLogFilename = Utils.GetTempPath("privoxy.log");
                if (File.Exists(privoxyLogFilename))
                {
                    using (new FileStream(privoxyLogFilename, FileMode.Truncate)) { }
                }
#else
                Logging.OpenLogFile();
#endif
                Test(); // TODO
                _controller     = ShadowsocksController.one();
                _viewController = MenuViewController.one();
                NotificationRunner.one().Start();
                HotKeys.Init();
                _controller.Start();
                // 程序退出前把代理设置清除
                Application.ApplicationExit += new EventHandler((s, e) => {
                    SystemProxy.Disable();
                });
                Application.Run();
            }
        }