public ClanApplicationContext() { _clansMenuStrip = new ContextMenuStrip(); _clansMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(menuOpening); // 读取配置并开启 clash 进程 readConfig(); _clash = new Clash(_currentConfig); _clash.Start(); // 读取 API 端口,并连接 API string cfg = File.ReadAllText(_currentConfig); Dictionary <string, object> dict = new Deserializer().Deserialize <Dictionary <string, object> >(cfg); string extController = dict["external-controller"].ToString(); _clashAPI = new ClashAPI(extController); //TODO: external controller protentially not started yet. // 打开系统代理 _sysproxy = new Sysproxy(_clashAPI.config.port); _sysproxy.Enabled = true; // 若当前托管配置记录了 selector 选择,则恢复选择状态 if (_configList.index >= 0 && _configList.files[_configList.index].selections.Count > 0) { restoreSelections(_configList.files[_configList.index].selections); } _trayIcon = new NotifyIcon() { Icon = Clans.Properties.Resources.Clash, ContextMenuStrip = _clansMenuStrip, Visible = true }; }
private void StartClash() { retry: try { clash.Start(); clash.Exited += Clash_Exited; } catch (Clash.ServiceMissingException e) { logger.LogInformation(e, "Clash service not installed."); try { Clash.InstallClashService(); } catch (Exception ex) { logger.LogError(ex, "Install Clash service failed."); ExitApp(); return; } goto retry; } catch (Exception e) { logger.LogError(e, "Start Clash failed."); MessageBox.Show("Start Clash failed.\n" + e.Message); ExitApp(); } }