public void Start(Configuration configuration)
        {
            Server server = configuration.GetCurrentServer();
            if (_process == null)
            {
                Process[] existingPolipo = Process.GetProcessesByName("ss_privoxy");
                foreach (Process p in existingPolipo.Where(IsChildProcess))
                {
                    KillProcess(p);
                }
                string polipoConfig = Resources.privoxy_conf;
                _runningPort = this.GetFreePort();
                polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString());
                polipoConfig = polipoConfig.Replace("__POLIPO_BIND_PORT__", _runningPort.ToString());
                polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1");
                FileManager.ByteArrayToFile(Utils.GetTempPath(UniqueConfigFile), Encoding.UTF8.GetBytes(polipoConfig));

                _process = new Process();
                // Configure the process using the StartInfo properties.
                _process.StartInfo.FileName = "ss_privoxy.exe";
                _process.StartInfo.Arguments = UniqueConfigFile;
                _process.StartInfo.WorkingDirectory = Utils.GetTempPath();
                _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                _process.StartInfo.UseShellExecute = true;
                _process.StartInfo.CreateNoWindow = true;
                _process.Start();

                /*
                 * Add this process to job obj associated with this ss process, so that
                 * when ss exit unexpectedly, this process will be forced killed by system.
                 */
                PolipoJob.AddProcess(_process.Handle);
            }
            RefreshTrayArea();
        }
        public static void Update(Configuration config, bool forceDisable)
        {
            bool global = config.global;
            bool enabled = config.enabled;

            if (forceDisable)
            {
                enabled = false;
            }

            if (enabled)
            {
                if (global)
                {
                    WinINet.SetIEProxy(true, true, "127.0.0.1:" + config.localPort.ToString(), "");
                }
                else
                {
                    string pacUrl;
                    if (config.useOnlinePac && !config.pacUrl.IsNullOrEmpty())
                        pacUrl = config.pacUrl;
                    else
                        pacUrl = $"http://127.0.0.1:{config.localPort}/pac?t={GetTimestamp(DateTime.Now)}";
                    WinINet.SetIEProxy(true, false, "", pacUrl);
                }
            }
            else
            {
                WinINet.SetIEProxy(false, false, "", "");
            }
        }
        public void Start(Configuration configuration)
        {
            Server server = configuration.GetCurrentServer();
            if (_process == null)
            {
                Kill();
                string polipoConfig = Resources.privoxy_conf;
                _runningPort = this.GetFreePort();
                polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString());
                polipoConfig = polipoConfig.Replace("__POLIPO_BIND_PORT__", _runningPort.ToString());
                polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1");
                FileManager.ByteArrayToFile(runningPath + "/privoxy.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig));

                _process = new Process();
                // Configure the process using the StartInfo properties.
                _process.StartInfo.FileName = runningPath + "/ss_privoxy.exe";
                _process.StartInfo.Arguments = " \"" + runningPath + "/privoxy.conf\"";
                _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                _process.StartInfo.UseShellExecute = true;
                _process.StartInfo.CreateNoWindow = true;
                //_process.StartInfo.RedirectStandardOutput = true;
                //_process.StartInfo.RedirectStandardError = true;
                try
                {
                    _process.Start();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }
 private void http_DownloadGFWTemplateCompleted(object sender, DownloadStringCompletedEventArgs e)
 {
     try
     {
         string result = e.Result;
         if (result.IndexOf("__RULES__") > 0 && result.IndexOf("FindProxyForURL") > 0)
         {
             gfwlist_template = result;
             if (lastConfig != null)
             {
                 UpdatePACFromGFWList(lastConfig);
             }
             lastConfig = null;
         }
         else
         {
             Error(this, new ErrorEventArgs(new Exception("Download ERROR")));
         }
     }
     catch (Exception ex)
     {
         if (Error != null)
         {
             Error(this, new ErrorEventArgs(ex));
         }
     }
 }
 public List<Server> MergeConfiguration(Configuration mergeConfig, List<Server> servers)
 {
     if (servers != null)
     {
         for (int j = 0; j < servers.Count; ++j)
         {
             for (int i = 0; i < mergeConfig.configs.Count; ++i)
             {
                 if (mergeConfig.configs[i].server == servers[j].server
                     && mergeConfig.configs[i].server_port == servers[j].server_port
                     && mergeConfig.configs[i].method == servers[j].method
                     && mergeConfig.configs[i].password == servers[j].password
                     && mergeConfig.configs[i].tcp_over_udp == servers[j].tcp_over_udp
                     && mergeConfig.configs[i].udp_over_tcp == servers[j].udp_over_tcp
                     && mergeConfig.configs[i].obfs_tcp == servers[j].obfs_tcp
                     //&& mergeConfig.configs[i].remarks == servers[j].remarks
                     )
                 {
                     servers[j].CopyServer(mergeConfig.configs[i]);
                     break;
                 }
             }
         }
     }
     return servers;
 }
 public TCPRelay(ShadowsocksController controller, Configuration conf)
 {
     _controller = controller;
     _config = conf;
     Handlers = new HashSet<TCPHandler>();
     _lastSweepTime = DateTime.Now;
 }
 public ShadowsocksController()
 {
     _config = Configuration.Load();
     StatisticsConfiguration = StatisticsStrategyConfiguration.Load();
     _strategyManager = new StrategyManager(this);
     StartReleasingMemory();
 }
 public static void Save(Configuration config)
 {
     if (config.index >= config.configs.Count)
     {
         config.index = config.configs.Count - 1;
     }
     if (config.index < 0)
     {
         config.index = 0;
     }
     config.isDefault = false;
     try
     {
         using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
         {
             string jsonString = SimpleJson.SimpleJson.SerializeObject(config);
             sw.Write(jsonString);
             sw.Flush();
         }
     }
     catch (IOException e)
     {
         Console.Error.WriteLine(e);
     }
 }
        public void Start(Configuration configuration)
        {
            try
            {
                config = configuration;
                // Create a TCP/IP socket.
                _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                IPEndPoint localEndPoint = null;
                if (configuration.shareOverLan)
                {
                    localEndPoint = new IPEndPoint(IPAddress.Any, PORT);
                }
                else
                {
                    localEndPoint = new IPEndPoint(IPAddress.Loopback, PORT);
                }

                // Bind the socket to the local endpoint and listen for incoming connections.
                _listener.Bind(localEndPoint);
                _listener.Listen(100);
                _listener.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    _listener);

                WatchPacFile();
            }
            catch (SocketException)
            {
                _listener.Close();
                throw;
            }
        }
 public void UpdatePACFromGFWList(Configuration config)
 {
     WebClient http = new WebClient();
     http.Proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort);
     http.DownloadStringCompleted += http_DownloadStringCompleted;
     http.DownloadStringAsync(new Uri(GFWLIST_URL));
 }
        private void LoadCurrentConfiguration()
        {
            _modifiedConfiguration = controller.GetConfigurationCopy();

            UseProxyCheckBox.Checked = _modifiedConfiguration.useProxy;
            ProxyServerTextBox.Text = _modifiedConfiguration.proxyServer;
            ProxyPortTextBox.Text = _modifiedConfiguration.proxyPort.ToString();
        }
        public static void Update(Configuration config, bool forceDisable)
        {
            bool global = config.global;
            bool enabled = config.enabled;

            if (forceDisable)
            {
                enabled = false;
            }
            RegistryKey registry = null;
            try {
                registry = Utils.OpenUserRegKey( @"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true );
                if ( registry == null ) {
                    Logging.Error( @"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" );
                    return;
                }
                if ( enabled ) {
                    if ( global ) {
                        registry.SetValue( "ProxyEnable", 1 );
                        registry.SetValue( "ProxyServer", "127.0.0.1:" + config.localPort.ToString() );
                        registry.SetValue( "AutoConfigURL", "" );
                    } else {
                        string pacUrl;
                        if ( config.useOnlinePac && ! config.pacUrl.IsNullOrEmpty() )
                            pacUrl = config.pacUrl;
                        else
                            pacUrl = $"http://127.0.0.1:{config.localPort}/pac?t={GetTimestamp( DateTime.Now )}";
                        registry.SetValue( "ProxyEnable", 0 );
                        var readProxyServer = registry.GetValue( "ProxyServer" );
                        registry.SetValue( "ProxyServer", "" );
                        registry.SetValue( "AutoConfigURL", pacUrl );
                    }
                } else {
                    registry.SetValue( "ProxyEnable", 0 );
                    registry.SetValue( "ProxyServer", "" );
                    registry.SetValue( "AutoConfigURL", "" );
                }

                //Set AutoDetectProxy
                IEAutoDetectProxy( ! enabled );

                NotifyIE();
                //Must Notify IE first, or the connections do not chanage
                CopyProxySettingFromLan();
            } catch ( Exception e ) {
                Logging.LogUsefulException( e );
                // TODO this should be moved into views
                MessageBox.Show( I18N.GetString( "Failed to update registry" ) );
            } finally {
                if ( registry != null ) {
                    try {
                        registry.Close();
                        registry.Dispose();
                    } catch (Exception e)
                    { Logging.LogUsefulException(e); }
                }
            }
        }
 public static void Update(Configuration config, bool forceDisable)
 {
     bool global = config.global;
     bool enabled = config.enabled;
     if (forceDisable)
     {
         enabled = false;
     }
     try
     {
         RegistryKey registry =
             Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
                 true);
         if (enabled)
         {
             if (global)
             {
                 registry.SetValue("ProxyEnable", 1);
                 registry.SetValue("ProxyServer", "127.0.0.1:" + config.localPort.ToString());
                 registry.SetValue("AutoConfigURL", "");
             }
             else
             {
                 string pacUrl;
                 if (config.useOnlinePac && !string.IsNullOrEmpty(config.pacUrl))
                     pacUrl = config.pacUrl;
                 else
                     pacUrl = "http://127.0.0.1:" + config.localPort.ToString() + "/pac?t=" + GetTimestamp(DateTime.Now);
                 registry.SetValue("ProxyEnable", 0);
                 var readProxyServer = registry.GetValue("ProxyServer");
                 if (readProxyServer != null && readProxyServer.Equals("127.0.0.1:" + config.localPort.ToString()))
                     registry.SetValue("ProxyServer", "");
                 registry.SetValue("AutoConfigURL", pacUrl);
             }
         }
         else
         {
             registry.SetValue("ProxyEnable", 0);
             if (global)
             {
                 registry.SetValue("ProxyServer", "");
             }
             registry.SetValue("AutoConfigURL", "");
         }
         //Set AutoDetectProxy Off
         IEAutoDetectProxy(false);
         SystemProxy.NotifyIE();
         //Must Notify IE first, or the connections do not chanage
         CopyProxySettingFromLan();
     }
     catch (Exception e)
     {
         Logging.LogUsefulException(e);
         // TODO this should be moved into views
         MessageBox.Show(I18N.GetString("Failed to update registry"));
     }
 }
Beispiel #14
0
        public TCPHandler(ShadowsocksController controller, Configuration config, TCPRelay tcprelay, Socket socket)
        {
            this._controller = controller;
            this._config = config;
            this._tcprelay = tcprelay;
            this._connection = socket;

            lastActivity = DateTime.Now;
        }
        public TCPHandler(ShadowsocksController controller, Configuration config, TCPRelay tcprelay, Socket socket)
        {
            _controller = controller;
            _config = config;
            _tcprelay = tcprelay;
            _connection = socket;
            _proxyTimeout = config.proxy.proxyTimeout * 1000;
            _serverTimeout = config.GetCurrentServer().timeout * 1000;

            lastActivity = DateTime.Now;
        }
Beispiel #16
0
 public bool isConfigChange(Configuration config)
 {
     if (this._shareOverLAN != config.shareOverLan
         //|| _buildinHttpProxy != config.buildinHttpProxy
         || _authUser != config.authUser
         || _authPass != config.authPass
         || _socket == null
         || ((IPEndPoint)_socket.LocalEndPoint).Port != config.localPort)
     {
         return true;
     }
     return false;
 }
Beispiel #17
0
        public ServerCrawler(ShadowsocksController controller)
        {
            this.controller = controller;
            configuration = controller.GetConfigurationCopy();

            this.serverList = new List<Server>();
            foreach (var server in configuration.configs)
            {
                if (server.remarks == CRAWLER_REMARKS)
                {
                    this.serverList.Add(server);
                }
            }
        }
 public List<Server> MergeConfiguration(Configuration mergeConfig, List<Server> servers)
 {
     List<Server> missingServers = new List<Server>();
     if (servers != null)
     {
         for (int j = 0; j < servers.Count; ++j)
         {
             for (int i = 0; i < mergeConfig.configs.Count; ++i)
             {
                 if (mergeConfig.configs[i].server == servers[j].server
                     && mergeConfig.configs[i].server_port == servers[j].server_port
                     && mergeConfig.configs[i].method == servers[j].method
                     && mergeConfig.configs[i].protocol == servers[j].protocol
                     && mergeConfig.configs[i].obfs == servers[j].obfs
                     && mergeConfig.configs[i].password == servers[j].password
                     && mergeConfig.configs[i].tcp_over_udp == servers[j].tcp_over_udp
                     && mergeConfig.configs[i].udp_over_tcp == servers[j].udp_over_tcp
                     )
                 {
                     servers[j].CopyServer(mergeConfig.configs[i]);
                     break;
                 }
             }
         }
     }
     for (int i = 0; i < mergeConfig.configs.Count; ++i)
     {
         int j = 0;
         for (; j < servers.Count; ++j)
         {
             if (mergeConfig.configs[i].server == servers[j].server
                 && mergeConfig.configs[i].server_port == servers[j].server_port
                 && mergeConfig.configs[i].method == servers[j].method
                 && mergeConfig.configs[i].protocol == servers[j].protocol
                 && mergeConfig.configs[i].obfs == servers[j].obfs
                 && mergeConfig.configs[i].password == servers[j].password
                 && mergeConfig.configs[i].tcp_over_udp == servers[j].tcp_over_udp
                 && mergeConfig.configs[i].udp_over_tcp == servers[j].udp_over_tcp
                 )
             {
                 break;
             }
         }
         if (j == servers.Count)
         {
             missingServers.Add(mergeConfig.configs[i]);
         }
     }
     return missingServers;
 }
        public static void Update(Configuration config, bool forceDisable)
        {
            if (!SupportChangeSysProxy())
            {
                return;
            }

            bool global = config.global;
            bool enabled = config.enabled;
            if (forceDisable)
            {
                enabled = false;
            }
            try
            {
                RegistryKey registry =
                    Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
                        true);
                if (enabled)
                {
                    if (global)
                    {
                        registry.SetValue("ProxyEnable", 1);
                        registry.SetValue("ProxyServer", "127.0.0.1:" + config.localPort.ToString());
                        registry.SetValue("AutoConfigURL", "");
                    }
                    else
                    {
                        registry.SetValue("ProxyEnable", 0);
                        registry.SetValue("ProxyServer", "");
                        registry.SetValue("AutoConfigURL", "http://127.0.0.1:" + config.localPort.ToString() + "/pac?t=" + GetTimestamp(DateTime.Now));
                    }
                }
                else
                {
                    registry.SetValue("ProxyEnable", 0);
                    registry.SetValue("ProxyServer", "");
                    registry.SetValue("AutoConfigURL", "");
                }
                SystemProxy.NotifyIE();
                //Must Notify IE first, or the connections do not chanage
                CopyProxySettingFromLan();
            }
            catch (Exception e)
            {
                Logging.LogUsefulException(e);
                // TODO this should be moved into views
                MessageBox.Show(I18N.GetString("Failed to update registry"));
            }
        }
 public void Start(Configuration config, byte[] firstPacket, int length, Socket socket, int targetPort)
 {
     this._firstPacket = firstPacket;
     this._firstPacketLength = length;
     this._local = socket;
     this._targetPort = targetPort;
     this._config = config;
     if ((_config.authUser ?? "").Length == 0)
     {
         Connect();
     }
     else
     {
         RspHttpHandshakeReceive();
     }
 }
 public void Start(Configuration config, byte[] firstPacket, int length, Socket socket, int targetPort)
 {
     this._firstPacket = firstPacket;
     this._firstPacketLength = length;
     this._local = socket;
     this._targetPort = targetPort;
     this._config = config;
     if ((_config.authUser ?? "").Length == 0 || Util.Utils.isMatchSubNet(((IPEndPoint)this._local.RemoteEndPoint).Address, "127.0.0.0/8"))
     {
         Connect();
     }
     else
     {
         RspHttpHandshakeReceive();
     }
 }
 public void UpdatePACFromGFWList(Configuration config)
 {
     if (gfwlist_template == null)
     {
         lastConfig = config;
         WebClient http = new WebClient();
         http.Proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort);
         http.DownloadStringCompleted += http_DownloadGFWTemplateCompleted;
         http.DownloadStringAsync(new Uri(GFWLIST_TEMPLATE_URL));
     }
     else
     {
         WebClient http = new WebClient();
         http.Proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort);
         http.DownloadStringCompleted += http_DownloadStringCompleted;
         http.DownloadStringAsync(new Uri(GFWLIST_URL));
     }
 }
        public void Start(Configuration config)
        {
            this._config = config;
            this._shareOverLAN = config.shareOverLan;

            if (CheckIfPortInUse(_config.localPort))
                throw new Exception(I18N.GetString("Port already in use"));

            try
            {
                // Create a TCP/IP socket.
                _tcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                _tcpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                _udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                IPEndPoint localEndPoint = null;
                if (_shareOverLAN)
                {
                    localEndPoint = new IPEndPoint(IPAddress.Any, _config.localPort);
                }
                else
                {
                    localEndPoint = new IPEndPoint(IPAddress.Loopback, _config.localPort);
                }

                // Bind the socket to the local endpoint and listen for incoming connections.
                _tcpSocket.Bind(localEndPoint);
                _udpSocket.Bind(localEndPoint);
                _tcpSocket.Listen(1024);

                // Start an asynchronous socket to listen for connections.
                Console.WriteLine("Shadowsocks started");
                _tcpSocket.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    _tcpSocket);
                UDPState udpState = new UDPState();
                _udpSocket.BeginReceiveFrom(udpState.buffer, 0, udpState.buffer.Length, 0, ref udpState.remoteEndPoint, new AsyncCallback(RecvFromCallback), udpState);
            }
            catch (SocketException)
            {
                _tcpSocket.Close();
                throw;
            }
        }
        public void Start(Configuration configuration)
        {
            Server server = configuration.GetCurrentServer();
            if (_process == null)
            {
                Process[] existingPolipo = Process.GetProcessesByName("ss_privoxy");
                foreach (Process p in existingPolipo)
                {
                    try
                    {
                        p.CloseMainWindow();
                        p.WaitForExit(100);
                        if (!p.HasExited)
                        {
                            p.Kill();
                            p.WaitForExit();
                        }
                    }
                    catch (Exception e)
                    {
                        Logging.LogUsefulException(e);
                    }
                }
                string polipoConfig = Resources.privoxy_conf;
                _runningPort = this.GetFreePort();
                polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString());
                polipoConfig = polipoConfig.Replace("__POLIPO_BIND_PORT__", _runningPort.ToString());
                polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1");
                FileManager.ByteArrayToFile(Utils.GetTempPath("privoxy.conf"), Encoding.UTF8.GetBytes(polipoConfig));

                _process = new Process();
                // Configure the process using the StartInfo properties.
                _process.StartInfo.FileName = "ss_privoxy.exe";
                _process.StartInfo.Arguments = "privoxy.conf";
                _process.StartInfo.WorkingDirectory = Utils.GetTempPath();
                _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                _process.StartInfo.UseShellExecute = true;
                _process.StartInfo.CreateNoWindow = true;
                _process.Start();
            }
            RefreshTrayArea();
        }
        public static void Update(Configuration config, bool forceDisable, PACServer pacSrv)
        {
            bool global = config.global;
            bool enabled = config.enabled;

            if (forceDisable)
            {
                enabled = false;
            }

            try
            {
                if (enabled)
                {
                    if (global)
                    {
                        Sysproxy.SetIEProxy(true, true, "127.0.0.1:" + config.localPort.ToString(), "");
                    }
                    else
                    {
                        string pacUrl;
                        if (config.useOnlinePac && !config.pacUrl.IsNullOrEmpty())
                        {
                            pacUrl = config.pacUrl;
                        }
                        else
                        {
                            pacUrl = pacSrv.PacUrl;
                        }
                        Sysproxy.SetIEProxy(true, false, "", pacUrl);
                    }
                }
                else
                {
                    Sysproxy.SetIEProxy(false, false, "", "");
                }
            }
            catch (ProxyException ex)
            {
                Logging.LogUsefulException(ex);
            }
        }
        public void Start(Configuration configuration)
        {
            Server server = configuration.GetCurrentServer();
            if (_process == null)
            {
                Process[] existingPolipo = Process.GetProcessesByName("ss_privoxy");
                foreach (Process p in existingPolipo)
                {
                    try
                    {
                        p.Kill();
                        p.WaitForExit();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
                string polipoConfig = Resources.privoxy_conf;
                _runningPort = this.GetFreePort();
                polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString());
                polipoConfig = polipoConfig.Replace("__POLIPO_BIND_PORT__", _runningPort.ToString());
                polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1");
                FileManager.ByteArrayToFile(temppath + "/privoxy.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig));

                if (!(temppath.EndsWith("\\") || temppath.EndsWith("/"))) {
                    temppath = temppath + "\\";
                }
                _process = new Process();
                // Configure the process using the StartInfo properties.
                _process.StartInfo.FileName = temppath + "ss_privoxy.exe";
                _process.StartInfo.Arguments = " \"" + temppath + "privoxy.conf\"";
                _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                _process.StartInfo.UseShellExecute = true;
                _process.StartInfo.CreateNoWindow = true;
                //_process.StartInfo.RedirectStandardOutput = true;
                //_process.StartInfo.RedirectStandardError = true;
                _process.Start();
            }
            RefreshTrayArea();
        }
        public void Start(Configuration configuration)
        {
            if (_process == null)
            {
                Process[] existingPrivoxy = Process.GetProcessesByName("ss_privoxy");
                foreach (Process p in existingPrivoxy.Where(IsChildProcess))
                {
                    KillProcess(p);
                }
                string privoxyConfig = Resources.privoxy_conf;
                _runningPort = GetFreePort();
                privoxyConfig = privoxyConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString());
                privoxyConfig = privoxyConfig.Replace("__PRIVOXY_BIND_PORT__", _runningPort.ToString());
                privoxyConfig = privoxyConfig.Replace("__PRIVOXY_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1");
                FileManager.ByteArrayToFile(Utils.GetTempPath(_uniqueConfigFile), Encoding.UTF8.GetBytes(privoxyConfig));

                _process = new Process
                {
                    // Configure the process using the StartInfo properties.
                    StartInfo =
                    {
                        FileName = "ss_privoxy.exe",
                        Arguments = _uniqueConfigFile,
                        WorkingDirectory = Utils.GetTempPath(),
                        WindowStyle = ProcessWindowStyle.Hidden,
                        UseShellExecute = true,
                        CreateNoWindow = true
                    }
                };
                _process.Start();

                /*
                 * Add this process to job obj associated with this ss process, so that
                 * when ss exit unexpectedly, this process will be forced killed by system.
                 */
                _privoxyJob.AddProcess(_process.Handle);
            }
        }
 public static void Save(Configuration config)
 {
     if (config.index >= config.configs.Count)
         config.index = config.configs.Count - 1;
     if (config.index < -1)
         config.index = -1;
     if (config.index == -1 && config.strategy == null)
         config.index = 0;
     config.isDefault = false;
     try
     {
         using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
         {
             string jsonString = JsonConvert.SerializeObject(config, Formatting.Indented);
             sw.Write(jsonString);
             sw.Flush();
         }
     }
     catch (IOException e)
     {
         Console.Error.WriteLine(e);
     }
 }
        public void Start(Configuration configuration)
        {
            Server server = configuration.GetCurrentServer();
            if (_process == null)
            {
                Process[] existingPolipo = Process.GetProcessesByName("ss_polipo");
                foreach (Process p in existingPolipo)
                {
                    try
                    {
                        p.Kill();
                        p.WaitForExit();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
                string temppath = Path.GetTempPath();
                string polipoConfig = Resources.polipo_config;
                polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", server.local_port.ToString());
                polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1");
                FileManager.ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig));
                FileManager.UncompressFile(temppath + "/ss_polipo.exe", Resources.polipo_exe);

                _process = new Process();
                // Configure the process using the StartInfo properties.
                _process.StartInfo.FileName = temppath + "/ss_polipo.exe";
                _process.StartInfo.Arguments = "-c \"" + temppath + "/polipo.conf\"";
                _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                _process.StartInfo.UseShellExecute = true;
                _process.StartInfo.CreateNoWindow = true;
                //_process.StartInfo.RedirectStandardOutput = true;
                //_process.StartInfo.RedirectStandardError = true;
                _process.Start();
            }
        }
 protected void SaveConfig(Configuration newConfig)
 {
     Configuration.Save(newConfig);
     Reload();
 }