Esempio n. 1
0
 private void RBT_Teacher_CheckedChanged(object sender, EventArgs e)
 {
     if (((RadioButton)sender).Checked)
     {
         // 保存配置
         Config.NetType = 1;
         // 判断切换是否合法
         PNL_Student.Visible = RBT_Student.Checked;
         PNL_Teacher.Visible = RBT_Teacher.Checked;
         // TODO: 由于断网,下面的代码没有经过测试
         if (VpnService.Worked())
         {
             if (MessageBox.Show("警告:如果切换网络类型,会断开当前网络。是否切换?", "当前网络正在使用", MessageBoxButtons.YesNo,
                                 MessageBoxIcon.Warning) == DialogResult.Yes)
             {
                 VpnService.Abort();
             }
             else
             {
                 RBT_Student.Checked = true;
                 Config.NetType      = 0;
             }
         }
         ConfigService.SaveConfig(ref Config);
         ReFreshUi();
     }
 }
Esempio n. 2
0
 private void TryLinkTeacher()
 {
     if (VpnService.Worked())
     {
         if (MessageBox.Show("警告:如果切换网络类型,会断开当前网络。是否切换?", "当前网络正在使用", MessageBoxButtons.YesNo,
                             MessageBoxIcon.Warning) != DialogResult.Yes)
         {
             return;
         }
         VpnService.Abort();
     }
     if (SrLinkService.RegisterSchoolNet(Config.TeacherNet.SettingCertify.UserId,
                                         Config.TeacherNet.SettingCertify.Password))
     {
         Config.NetType = 1;
         ShowTip(ToolTipIcon.Info, "切换到教师网", $"{Config.TeacherNet.SettingCertify.UserId}登录成功",
                 false);
     }
     else
     {
         Config.NetType = 0;
         ShowTip(ToolTipIcon.Error, "切换到教师网", "登录失败:用户名/密码错误。请进入配置页面手动尝试。", false);
     }
     ConfigService.SaveConfig(ref Config);
 }
Esempio n. 3
0
 private void StartVpn()
 {
     if (VpnService.Prepare(this) != null)
     {
         var activity = ShowingActivity;
         if (activity?.Handler.Post(activity.VpnServicePrepare) == true)
         {
             Logging.info("Using showing activity to request VPN permission.");
         }
         else
         {
             Logging.info("Starting activity to request VPN permission.");
             StartActivity(new Intent(this, typeof(MainActivity)).SetAction("PREP_VPN").SetFlags(ActivityFlags.NewTask));
         }
     }
     else
     {
         // continue starting vpn service
         try {
             if (vpnHelper == null)
             {
                 vpnHelper = new VpnHelper(this);
             }
             // else it may be reloading
             vpnHelper.VpnConfig = currentConfig.vpn;
             vpnHelper.StartVpn();
         } catch (Exception e) {
             Logging.exception(e, Logging.Level.Error, "Starting VPN");
         }
     }
 }
Esempio n. 4
0
 private void 立即连接ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     _frmDebug.WriteToBoard("(用户操作)执行立即连接");
     TMR_SrLink.Enabled = (Config.NetType == 0 && Config.StudentNet.AutoLink);
     try
     {
         if (Config.NetType == 1)
         {
             TryLinkTeacher();
         }
         else
         {
             if (VpnService.Worked())
             {
                 ShowTip(ToolTipIcon.Warning, "无效操作", "网络已连接");
             }
             else
             {
                 TryLinkStudent(true);
             }
         }
         //if (SrLinkService.TestInternet(1))
         //    ShowTip(ToolTipIcon.Warning, "无效操作", "网络已连接");
         //else
         //    TryLinkStudent(true);
         //if (Global.Linked)
         //    ShowTip(ToolTipIcon.Warning, "无效操作", "网络已连接");
         //else
         //    TryLinkL2tp(true);
     }
     catch (Exception err)
     {
         LoggerService.SaveLog("立即连接ToolStripMenuItem_Click", err);
     }
 }
Esempio n. 5
0
        public void VpnServicePrepare()
        {
            var r = VpnService.Prepare(this);

            if (r == null)
            {
                StartServiceWithAction(BgService.Actions.START_VPN);
            }
            else
            {
                StartActivityForResult(r, REQ_VPN);
            }
        }
        private void StartVPN()
        {
            Intent vpnIntent = VpnService.Prepare(this);

            if (vpnIntent != null)
            {
                StartActivityForResult(vpnIntent, VPN_REQUEST_CODE);
            }
            else
            {
                OnActivityResult(VPN_REQUEST_CODE, Result.Ok, null);
            }
        }
        private void StartVPNService()
        {
            Intent intent = VpnService.Prepare(m_activity);

            if (intent != null)
            {
                m_activity.StartActivityForResult(intent, VPN_START);
            }
            else
            {
                OnActivityResult(VPN_START, Result.Ok, null);
            }
        }
Esempio n. 8
0
 private void FRM_Main_Load(object sender, EventArgs e)
 {
     try
     {
         版本ToolStripMenuItem.Text = StringHelper.GetAppString("Version");
         WindowState = FormWindowState.Minimized;
         ShowScreen(new SubFrmNormal(_frmDebug));
         VpnService.Abort();
         TMR_SrLink.Enabled = (Config.NetType == 0 && Config.StudentNet.AutoLink);
     }
     catch (Exception err)
     {
         LoggerService.SaveLog("FRM_Main_Load", err);
     }
 }
Esempio n. 9
0
        private void StartVPNService()
        {
            Log.Debug(TAG, "StartVPNService");

            Intent intent = VpnService.Prepare(context);

            if (intent != null)
            {
                if (activity != null)
                {
                    activity.StartActivityForResult(intent, VPN_START);
                }
            }
            else
            {
                OnActivityResult(VPN_START, Result.Ok, null);
            }
        }
Esempio n. 10
0
        private void TryRestoreLastProfile(Context context)
        {
            LogsManager.Instance.Debug("EventsReceiver.TryRestoreLastProfile");

            if (VpnService.Prepare(context.ApplicationContext) != null)
            {
                LogsManager.Instance.Debug("VpnService.Prepare requires confirmation");
                return;
            }

            if (!OptionsManager.SystemLastProfileRestore)
            {
                LogsManager.Instance.Debug("EventsReceiver: SystemRestoreLastProfile disabled");
                return;
            }

            string lastProfile = OptionsManager.SystemLastProfileActivated;

            if (Utils.Empty(lastProfile))
            {
                LogsManager.Instance.Debug("EventsReceiver: lastProfile is empty");
                return;
            }

            LogsManager.Instance.Debug("EventsReceiver: restoring last profile1");

            try
            {
                Bundle args = new Bundle();
                args.PutString(AndroidVPNService.PARAM_PROFILE, lastProfile);

                Intent intent = new Intent(context, typeof(AndroidVPNService));
                intent.PutExtra(AndroidVPNService.PARAM_START, true);
                intent.PutExtra(AndroidVPNService.EXTRA_RUN_ARGS, args);

                context.StartService(intent);
            }
            catch (Exception e)
            {
                LogsManager.Instance.Error("TryRestoreLastProfile", e);
            }
        }
Esempio n. 11
0
        private void BindService()
        {
            Intent confirmIntent = VpnService.Prepare(m_context.ApplicationContext);

            if (confirmIntent != null)
            {
                // The only case when m_context is not an Activity is from the BootReceiver but in that case the VPN confirmation should already been granted and the confirmIntent will be null

                Activity activity = m_context as Activity;
                if (activity != null)
                {
                    activity.StartActivityForResult(confirmIntent, VPN_REQUEST_CODE);
                }
                else
                {
                    LogsManager.Instance.Error("Failed to cast context to Activity");
                }
            }
            else
            {
                HandleActivityResult(VPN_REQUEST_CODE, Result.Ok, null);
            }
        }
Esempio n. 12
0
 public AppExitHandler(IModals modals, VpnService vpnService)
 {
     _vpnService = vpnService;
     _modals     = modals;
 }
Esempio n. 13
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("连接结束");
        }