Пример #1
0
        private async void RBT_Student_CheckedChangedAsync(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                // 保存配置
                Config.NetType = 0;
                // 判断切换是否合法
                PNL_Student.Visible = RBT_Student.Checked;
                PNL_Teacher.Visible = RBT_Teacher.Checked;
                if (Config.StudentNet.SettingCertify.Enable == false ||
                    string.IsNullOrEmpty(Config.StudentNet.SettingCertify.UserId) ||
                    string.IsNullOrEmpty(Config.StudentNet.SettingCertify.Password))
                {
                    var f = new FrmCertify();
                    if (f.ShowDialog() != DialogResult.Yes)
                    {
                        RBT_Teacher.Checked = true;
                        Config.NetType      = 1;
                    }
                }
                else
                {
                    await SrLinkService.RegisterSchoolNetAsync(Config.StudentNet.SettingCertify.UserId,
                                                               Config.StudentNet.SettingCertify.Password, 1);
                }

                ConfigService.SaveConfig(ref Config);
                ReFreshUi();
            }
        }
Пример #2
0
        private async void TryLinkStudent(bool force = false)
        {
            //if (Global.Linked)
            //{
            //    _frmDebug.WriteToBoard($"Linked = {Global.Linked} 检测到网络已连接,停止计时器事件,结束");
            //    TMR_SrLink.Enabled = false;
            //    return;
            //}

            //_frmDebug.WriteToBoard($"Linked = {Global.Linked} 网络未连接 - 可连接");
            //if (Global.Running)
            //{
            //    _frmDebug.WriteToBoard($"Running = {Global.Running} 检测到线程忙,结束");
            //    return;
            //}
            // TODO: 可能会出现问题,Vpn虽然忙但并不是在连接,可能导致永远不会进入连接
            if (VpnService.Worked())
            {
                _frmDebug.WriteToBoard($"检测到线程忙,结束");
                return;
            }

            //_frmDebug.WriteToBoard($"Running = {Global.Running} 线程空闲 - 可连接");

            if (!ConfigService.EnableTryLink(ref Config) && !force)
            {
                // 这句提示不应该出现
                _frmDebug.WriteToBoard($"EnableTryLink = {ConfigService.EnableTryLink(ref Config)} 未启用自动连接,结束");
                return;
            }


            _frmDebug.WriteToBoard(
                $"EnableTryLink = {ConfigService.EnableTryLink(ref Config)}, force = {force} 开始执行连接");

            //Global.Running = true;
            ShowTip(ToolTipIcon.None, "L2TP连接中", "请耐心等待");

            if (SrLinkService.SettingEnable(ref Config, "Certify"))
            {
                var res = await SrLinkService.RegisterSchoolNetAsync(Config.StudentNet.SettingCertify.UserId,
                                                                     Config.StudentNet.SettingCertify.Password);

                if (res)
                {
                    _frmDebug.WriteToBoard("认证成功");
                }
                else
                {
                    ShowTip(ToolTipIcon.Error, "认证失败", "内网认证失败");
                }
            }

            if (SrLinkService.SettingEnable(ref Config, "Link"))
            {
                var res = SrLinkService.LinkVpnAsync(Config.StudentNet.SettingLink.ServerIp,
                                                     Config.StudentNet.SettingLink.UserId, Config.StudentNet.SettingLink.Password);
                if (res)
                {
                    // 调整TMR至第二天的设定时间启动
                    var nextTime = DateTime.Now.AddDays(1).Date + Config.StudentNet.StartTime.TimeOfDay;
                    TMR_SrLink.Interval = (int)nextTime.Subtract(DateTime.Now).TotalMilliseconds;
                    ShowTip(ToolTipIcon.Info, "连接成功", $"{StringHelper.GetAppString("AdapterName")}已连接");
                }
                else
                {
                    Config.StudentNet.AutoLink = false;
                    ShowTip(ToolTipIcon.Info, "连接失败", "已取消自动连接。请检查配置信息,并使用手动连接测试配置,连接成功后再重新开启自动连接。");
                }
            }

            if (SrLinkService.SettingEnable(ref Config, "Mail"))
            {
                var res = await SrLinkService.SendIpAsync(Config.StudentNet.SettingMail.Address);

                if (res)
                {
                    _frmDebug.WriteToBoard("Ip发送成功");
                }
                else
                {
                    ShowTip(ToolTipIcon.Error, "Ip发送失败", "响应超时");
                }
            }

            // Global.Running = false;
            _frmDebug.WriteToBoard("连接结束");
        }