Пример #1
0
        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();
                };
            }
        }
Пример #2
0
        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);
            }
        }