protected void Reload() { if (_port_map_listener != null) { foreach (Listener l in _port_map_listener) { l.Stop(); } _port_map_listener = null; } // some logic in configuration updated the config when saving, we need to read it again _config = MergeGetConfiguration(_config); _config.FlushPortMapCache(); ReloadIPRange(); HostMap hostMap = new HostMap(); hostMap.LoadHostFile(); HostMap.Instance().Clear(hostMap); #if !_CONSOLE if (polipoRunner == null) { polipoRunner = new HttpProxyRunner(); } #endif if (_pacServer == null) { _pacServer = new PACServer(); _pacServer.PACFileChanged += pacServer_PACFileChanged; } _pacServer.UpdateConfiguration(_config); if (gfwListUpdater == null) { gfwListUpdater = new GFWListUpdater(); gfwListUpdater.UpdateCompleted += pacServer_PACUpdateCompleted; gfwListUpdater.Error += pacServer_PACUpdateError; } // 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 bool _firstRun = firstRun; for (int i = 1; i <= 5; ++i) { _firstRun = false; try { if (_listener != null && !_listener.isConfigChange(_config)) { Local local = new Local(_config, _transfer, _rangeSet); _listener.GetServices()[0] = local; #if !_CONSOLE if (polipoRunner.HasExited()) { polipoRunner.Stop(); polipoRunner.Start(_config); _listener.GetServices()[3] = new HttpPortForwarder(polipoRunner.RunningPort, _config); } #endif } else { if (_listener != null) { _listener.Stop(); _listener = null; } #if !_CONSOLE polipoRunner.Stop(); polipoRunner.Start(_config); #endif Local local = new Local(_config, _transfer, _rangeSet); List <Listener.Service> services = new List <Listener.Service>(); services.Add(local); services.Add(_pacServer); services.Add(new APIServer(this, _config)); #if !_CONSOLE services.Add(new HttpPortForwarder(polipoRunner.RunningPort, _config)); #endif _listener = new Listener(services); _listener.Start(_config, 0); } break; } 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(I18N.GetString("Port already in use") + string.Format(" {0}", _config.localPort), e); } } Logging.LogUsefulException(e); if (!_firstRun) { ReportError(e); break; } else { Thread.Sleep(1000 * i * i); } if (_listener != null) { _listener.Stop(); _listener = null; } } } _port_map_listener = new List <Listener>(); foreach (KeyValuePair <int, PortMapConfigCache> pair in _config.GetPortMapCache()) { try { Local local = new Local(_config, _transfer, _rangeSet); List <Listener.Service> services = new List <Listener.Service>(); services.Add(local); Listener listener = new Listener(services); listener.Start(_config, pair.Key); _port_map_listener.Add(listener); } 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(I18N.GetString("Port already in use") + string.Format(" {0}", pair.Key), e); } } Logging.LogUsefulException(e); ReportError(e); } } ConfigChanged?.Invoke(this, new EventArgs()); UpdateSystemProxy(); Util.Utils.ReleaseMemory(true); }
protected void Reload() { // some logic in configuration updated the config when saving, we need to read it again _config = MergeGetConfiguration(_config); if (polipoRunner == null) { polipoRunner = new HttpProxyRunner(); } if (_pacServer == null) { _pacServer = new PACServer(); _pacServer.PACFileChanged += pacServer_PACFileChanged; } _pacServer.UpdateConfiguration(_config); if (gfwListUpdater == null) { gfwListUpdater = new GFWListUpdater(); gfwListUpdater.UpdateCompleted += pacServer_PACUpdateCompleted; gfwListUpdater.Error += pacServer_PACUpdateError; } // 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 try { if (_listener != null && !_listener.isConfigChange(_config)) { Local local = new Local(_config); _listener.GetServices()[0] = local; if (polipoRunner.HasExited()) { polipoRunner.Stop(); polipoRunner.Start(_config); } } else { if (_listener != null) { _listener.Stop(); _listener = null; } polipoRunner.Stop(); polipoRunner.Start(_config); Local local = new Local(_config); List <Listener.Service> services = new List <Listener.Service>(); services.Add(local); services.Add(_pacServer); services.Add(new PortForwarder(polipoRunner.RunningPort)); _listener = new Listener(services); _listener.Start(_config); } } 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(I18N.GetString("Port already in use"), e); } } Logging.LogUsefulException(e); ReportError(e); } if (ConfigChanged != null) { ConfigChanged(this, new EventArgs()); } UpdateSystemProxy(); Util.Utils.ReleaseMemory(); }