private void MenuItemResetTapDevice_Click(object sender, EventArgs e) { if (MessageBox.Show(this, @"将会清除现有的Tap设备,是否确定?", @"Caution", MessageBoxButtons.YesNo) == DialogResult.Yes) { TapDeviceFunc.DeleteTapDevice(); MenuItemHardwareCheck_Click(sender, e); } }
public VpnRoute() { Tunnel = new NetIntf(TapDeviceFunc.GetTapDevice(Program.UserConfig.TapIntfaceName)); var intf = Diagnostic.GetBestInterface(Program.UserConfig.CurrentProfile.ServerIp); _origin = intf == null ? new NetIntf(IPNetHelper.GetBestIntf(Program.UserConfig.CurrentProfile.ServerIp.ToUInt32())) : new NetIntf(intf); _chnRouteOn = false; _originGateway = GetGateway(); }
public bool SelfCheck() { if (string.IsNullOrEmpty(TapIntfaceName)) { return(false); } if (Profiles == null) { Profiles = new Dictionary <string, Profile>(); CurrentProfileName = ""; } else if (Profiles.Count == 0) { if (!string.IsNullOrEmpty(CurrentProfileName)) { return(false); } } else { if (!Profiles.ContainsKey(CurrentProfileName)) { return(false); } } if (TapDeviceFunc.GetTapDevice(TapIntfaceName) == null) { Logger.Error("vpnConfig.intf not found in system."); var adapter = TapDeviceFunc.GetTapDevice(); if (adapter != null) { if (Regex.IsMatch(adapter.Name, @"^[a-zA-Z0-9]+$")) { Logger.Info($"Changing vpnConfig.intf to {adapter.Name}"); TapIntfaceName = adapter.Name; } } else { return(false); } } return(Profiles.Values.All(profile => profile.SelfCheck())); }
private async void fTapChecker_Load(object sender, EventArgs e) { bool overall = true; try { lbMsg.Text = @"检查Tap-Windows是否安装……"; if (TapDeviceFunc.CheckTapWindowsIntall()) { lbMsg.Text += "OK\n"; } else { lbMsg.Text += "Fail\n"; overall = false; lbMsg.Text += @"正在安装Tap设备……"; Task t = new Task(TapDeviceFunc.InstallTapDriver); t.Start(); await t; lbMsg.Text += "OK\n"; overall = true; } lbMsg.Text += @"获取Tap设备……"; var adapter = TapDeviceFunc.GetTapDevice(); if (adapter != null) { lbMsg.Text += "OK\n"; lbMsg.Text += @"检查Tap设备名……"; if (Regex.IsMatch(adapter.Name, @"^[a-zA-Z0-9]+$")) { lbMsg.Text += "OK\n"; } else { lbMsg.Text += $"Fail({adapter.Name})\n"; overall = false; lbMsg.Text += @"更改Tap设备名……"; Task t = new Task(() => { TapDeviceFunc.RenameTapDevice(TapDeviceFunc.DefaultTapDeviceName, adapter); }); t.Start(); await t; lbMsg.Text += "OK\n"; overall = true; } } else { lbMsg.Text += "Fail\n"; overall = false; lbMsg.Text += @"添加Tap设备……"; Task t = new Task(TapDeviceFunc.AddTapDevice); t.Start(); await t; adapter = TapDeviceFunc.GetTapDevice(); if (adapter == null) { lbMsg.Text += "Fail\n"; lbMsg.Text += @"正在安装Tap设备……"; t = new Task(TapDeviceFunc.InstallTapDriver); t.Start(); await t; lbMsg.Text += "OK\n"; } lbMsg.Text += @"获取Tap设备……"; adapter = TapDeviceFunc.GetTapDevice(); if (adapter == null) { lbMsg.Text += "Fail\n"; overall = false; Logger.Error("Can not get TapDevice anyway"); TapDeviceFunc.TraceNetworkInterface(); } else { TapDeviceFunc.RenameTapDevice(TapDeviceFunc.DefaultTapDeviceName, adapter); lbMsg.Text += "OK\n"; overall = true; } } } catch (Exception ex) { Logger.Error(ex.Message); Logger.Debug(ex.StackTrace); } finally { TapDeviceFunc.GetSetupLog(); if (overall) { lbMsg.Text += @"硬件检测成功,一切就绪,点击此处退出程序。"; } else { lbMsg.Text += "\n"; lbMsg.Text += @"硬件检测出错误,修复失败,正在生成诊断报告……"; new DiagnosticProgress().ShowDialog(this); lbMsg.Text += @"生成完毕,点击此处退出程序。"; } lbMsg.Click += (_sender, _e) => { Application.Exit(); }; } }
private static void Main(string[] args) { Logger.Init(args.Length == 1 && args[0] == "-u"); JSON.Parameters.SerializeNullValues = false; JSON.Parameters.UseExtensions = false; Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); // handle UI exceptions Application.ThreadException += Application_ThreadException; // handle non-UI exceptions AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Application.ApplicationExit += Application_ApplicationExit; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Os.Init(); bool hasRun = false; if (args.Length > 0) { if (args[0] == "-d") { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); if (principal.IsInRole(WindowsBuiltInRole.Administrator)) { if (args.Contains("-r")) { Application.ApplicationExit += (sender, e) => { ProcessStartInfo psi = new ProcessStartInfo { FileName = Application.ExecutablePath, Verb = "runas" }; new Process { StartInfo = psi }.Start(); }; } Application.Run(new TapChecker()); hasRun = true; } } else if (args[0] == "-r") { TapDeviceFunc.GetSetupLog(); hasRun = true; } else if (args[0] == "--test") { } else if (args[0] == "--diagnostic") { Application.Run(new DiagnosticProgress()); hasRun = true; } else if (args[0] == "-u") { GetRunningProc()?.WaitForExit(); if (args.Length == 2 && args[1].StartsWith("--from")) { Updater.RemoveDir(args[1].Substring(7)); } else { try { Updater.Updating(); } catch (Exception e) { Logger.Error(e.Message); Logger.Debug(e.StackTrace); MessageBox.Show($@"更新出错:{e.Message},{Environment.NewLine}日志文件保存在{Logger.Logfile}"); } hasRun = true; } } } if (!hasRun) { SelfCheck(); Application.Run(new EntryWindow()); } }
private static void SelfCheck() { if (!CheckdotNetVersion()) { string msg = @"您尚未安装最新版本的Microsoft .Net Framework,请使用程序目录下的安装包安装。"; Logger.Error(msg); MessageBox.Show(msg); Environment.Exit(1); } if (!NetworkInterface.GetIsNetworkAvailable()) { string msg = @"无可用网络连接,请确保电脑已连接至互联网。"; Logger.Error(msg); MessageBox.Show(msg); Environment.Exit(-2); } if (!File.Exists(ComponentPath.VpnBinaryPath)) { string msg = $@"缺少关键文件{ComponentPath.VpnBinaryPath}"; Logger.Error(msg); MessageBox.Show(msg); Environment.Exit(-3); } if (GetRunningProc() != null) { string msg = @"ShadowVpnSharp正在运行中。"; Logger.Error(msg); MessageBox.Show(msg); Environment.Exit(-4); } try { UserConfig = Config.FromFile(); if (!UserConfig.SelfCheck() || !TapDeviceFunc.CheckTapWindowsIntall()) { if (!File.Exists(ComponentPath.TapInstallerPath)) { string _msg = $@"缺少关键文件{ComponentPath.TapInstallerPath}"; Logger.Error(_msg); MessageBox.Show(_msg); Environment.Exit(-5); } string msg = "没有找到合适的网络设备,即将执行硬件检测。"; Logger.Error(msg); MessageBox.Show(msg); ProcessStartInfo psi = new ProcessStartInfo { FileName = Application.ExecutablePath, Arguments = "-d -r", Verb = "runas" }; Process ps = new Process { StartInfo = psi }; try { ps.Start(); Environment.Exit(2); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } catch (Exception ex) { Logger.Error(ex.Message); Logger.Error(ex.StackTrace); MessageBox.Show(ex.Message); Environment.Exit(-1); } }