예제 #1
5
 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 (registry == null) return;
         if (enabled)
         {
             if (global)
             {
                 registry.SetValue("ProxyEnable", 1);
                 registry.SetValue("ProxyServer", "127.0.0.1:" + config.HttpProxyPort);
                 registry.SetValue("AutoConfigURL", "");
             }
             else
             {
                 string pacUrl;
                 if (config.UseOnlinePac && !string.IsNullOrEmpty(config.PacUrl))
                     pacUrl = config.PacUrl;
                 else
                     pacUrl = "http://127.0.0.1:" + config.PACServerPort + "/pac?t=" + GetTimestamp(DateTime.Now);
                 registry.SetValue("ProxyEnable", 0);
                 registry.SetValue("ProxyServer", "");
                 registry.SetValue("AutoConfigURL", pacUrl);
             }
         }
         else
         {
             registry.SetValue("ProxyEnable", 0);
             registry.SetValue("ProxyServer", "");
             registry.SetValue("AutoConfigURL", "");
         }
         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("Failed to update registry", "Xsocks", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
예제 #2
0
 public void UpdatePacFromGfwList(Configuration config)
 {
     WebClient http = new WebClient();
     http.Proxy = new WebProxy(IPAddress.Loopback.ToString(), config.HttpProxyPort);
     http.DownloadStringCompleted += HttpDownloadStringCompleted;
     http.DownloadStringAsync(new Uri(GfwlistUrl));
 }
예제 #3
0
        public override void Start(Configuration config)
        {
            if (Process == null)
            {
                Process[] existingPolipo = Process.GetProcessesByName("xsocks");
                foreach (Process p in existingPolipo)
                {
                    if (Process.GetCurrentProcess().Id == p.Id)
                    {
                        continue;
                    }
                    try
                    {
                        p.Kill();
                        p.WaitForExit();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }

                config.Socks5ProxyPort = RunningPort = GetFreePort(config.Socks5ProxyPort);
                Server server = config.GetCurrentServer();

                string arguments = string.Format("-s {0}:{1} -k {2} -l {3}:{4} -n -V",
                    server.Host, server.Port, server.Password,
                    config.ShareOverLan ? "0.0.0.0" : "127.0.0.1",
                    RunningPort);

                Process = new Process();
                // Configure the process using the StartInfo properties.
                Process.StartInfo.FileName = Temppath + "/xsocks.exe";
                Process.StartInfo.Arguments = arguments;
                //_process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                Process.StartInfo.UseShellExecute = false;
                Process.StartInfo.CreateNoWindow = true;
                Process.StartInfo.RedirectStandardOutput = true;
                Process.StartInfo.RedirectStandardError = true;
                //Process.EnableRaisingEvents = true;
                Process.OutputDataReceived += Process_LogDataReceived;
                Process.ErrorDataReceived += Process_LogDataReceived;
                Process.Start();
                Process.BeginOutputReadLine();
                Process.BeginErrorReadLine();
            }
        }
예제 #4
0
        public override void Start(Configuration config)
        {
            if (Process == null)
            {
                Process[] existingPolipo = Process.GetProcessesByName("xs_polipo");
                foreach (Process p in existingPolipo)
                {
                    try
                    {
                        p.Kill();
                        p.WaitForExit();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }

                config.HttpProxyPort = RunningPort = GetFreePort(config.HttpProxyPort);

                string polipoConfig = Resources.polipo_config;
                polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", config.Socks5ProxyPort.ToString());
                polipoConfig = polipoConfig.Replace("__POLIPO_BIND_PORT__", RunningPort.ToString());
                polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", config.ShareOverLan ? "0.0.0.0" : "127.0.0.1");
                FileManager.ByteArrayToFile(Temppath + "/polipo.conf", Encoding.UTF8.GetBytes(polipoConfig));

                Process = new Process();
                // Configure the process using the StartInfo properties.
                Process.StartInfo.FileName = Temppath + "/xs_polipo.exe";
                Process.StartInfo.Arguments = "-c \"" + Temppath + "/polipo.conf\"";
                //Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                Process.StartInfo.UseShellExecute = false;
                Process.StartInfo.CreateNoWindow = true;
                Process.StartInfo.RedirectStandardOutput = true;
                Process.StartInfo.RedirectStandardError = true;
                //Process.EnableRaisingEvents = true;
                Process.OutputDataReceived += Process_LogDataReceived;
                Process.ErrorDataReceived += Process_LogDataReceived;
                Process.Start();
                Process.BeginOutputReadLine();
                Process.BeginErrorReadLine();
            }
        }
예제 #5
0
        public void Reload()
        {
            // some logic in configuration updated the config when saving, we need to read it again
            _config = Configuration.Load();

            Server server = _config.GetCurrentServer();
            if (String.IsNullOrEmpty(server.Host) ||
                (server.Port <= 0 || server.Port > 65535) ||
                String.IsNullOrEmpty(server.Password))
            {
                return;
            }

            if (_xsocksRunner == null)
            {
                _xsocksRunner = new XsocksRunner();
                _xsocksRunner.LogMessageReceived += _xsocksRunner_LogMessageReceived;
            }
            if (_polipoRunner == null)
            {
                _polipoRunner = new PolipoRunner();
            }
            if (_pacServer == null)
            {
                _pacServer = new PACServer();
                _pacServer.PACFileChanged += PACServerPACFileChanged;
            }
            if (_gfwListUpdater == null)
            {
                _gfwListUpdater = new GFWListUpdater();
                _gfwListUpdater.UpdateCompleted += PACServerPACUpdateCompleted;
                _gfwListUpdater.Error += PACServerPACUpdateError;
            }

            if (_listener != null)
            {
                _listener.Stop();
            }

            // don't put polipoRunner.Start() before pacServer.Stop()
            // or bind will fail when switching bind address from 0.0.0.0 to 127.0.0.1
            // though UseShellExecute is set to true now
            // http://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open
            _xsocksRunner.Stop();
            _polipoRunner.Stop();

            try
            {
                _xsocksRunner.Start(_config);
                _polipoRunner.Start(_config);

                _pacServer.HttpProxyPort = _polipoRunner.RunningPort;

                List<Listener.IService> services = new List<Listener.IService>();
                services.Add(_pacServer);
                _listener = new Listener(services);
                _listener.Start(_config.PACServerPort, _config.ShareOverLan);
            }
            catch (Exception e)
            {
                // translate Microsoft language into human language
                // i.e. An attempt was made to access a socket in a way forbidden by its access permissions => Port already in use
                if (e is SocketException)
                {
                    SocketException se = (SocketException)e;
                    if (se.SocketErrorCode == SocketError.AccessDenied)
                    {
                        e = new Exception("Port already in use", e);
                    }
                }
                Logging.LogUsefulException(e);
                ReportError(e);
            }

            if (ConfigChanged != null)
            {
                ConfigChanged(this, new EventArgs());
            }

            UpdateSystemProxy();
            Utils.ReleaseMemory();
        }
예제 #6
0
 protected void SaveConfig(Configuration newConfig)
 {
     Configuration.Save(newConfig);
     Reload();
 }
예제 #7
0
 public XsocksController()
 {
     _config = Configuration.Load();
 }
예제 #8
0
 public abstract void Start(Configuration config);
예제 #9
0
 public static void Save(Configuration config)
 {
     if (config.Index >= config.Servers.Count)
     {
         config.Index = config.Servers.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);
     }
 }