static public bool ConnectWithConfirmationLocal(BaseProxyServer item, string homeIP, out bool createdNew, JobLog jobLog) { bool result = false; jobLog.Debug("Start VpnServerConnectWithFullTestLocal for item " + item.JProxyServerId); createdNew = false; int closeAttemptCount = 0; while (NetConnUtils.IsActiveConnectionPresent()) { if (closeAttemptCount > 10) { throw new Exception("Unable to close previous active connection"); } jobLog.Debug("Found previous active connection. Going to close it"); NetConnUtils.CloseAllActiveConnections(false); Thread.Sleep(2 * 1000);//!!! closeAttemptCount++; } ///////test if (homeIP != null) { string externalIP = NetConnUtils.GetMyExternalIP(); if (externalIP == null) { throw new Exception("Ip of default connection is null"); } jobLog.Info("Default ExternalIP: " + externalIP); if (!homeIP.Equals(externalIP)) { throw new Exception("Ip address of default connection not equals home ip address."); } } ////////// if (NetConnUtils.IsConnectionEntryExist(item) == false) { if (item.IsProtocolAvailable(ProxyProtocolTypeEnum.PPTP)) { NetConnUtils.CreateConnectionEntry(item, ProxyProtocolTypeEnum.PPTP); createdNew = true; } else if (item.IsProtocolAvailable(ProxyProtocolTypeEnum.L2TP)) { NetConnUtils.CreateConnectionEntry(item, ProxyProtocolTypeEnum.L2TP); createdNew = true; } else { throw new ArgumentException(VpnSelectorLibRes.Non_PPTP_no_L2TP_protocols_available_for_this_vpn_entry); } } try { NetConnUtils.OpenConnectLocal(item, false); //sync Thread.Sleep(2 * 1000); //!!! //for (int i = 0; i < 60; i++)//~ 1 min //{ if (NetConnUtils.IsConnected(item)) { ConfirmIPAddress(item, homeIP, jobLog); result = true; item.SuccessCount = item.SuccessCount + 1; item.LastSuccessDate = DateTimeOffset.Now; Dm.Instance.SaveObject(item); } else { jobLog.Info("Not connected for item " + item.JProxyServerId); } //Thread.Sleep(1 * 1000); //} //todo error event } finally { //change label if (OnNetworkChekComplatedEvent != null) { NetworkChekComplatedEventArgs e = new NetworkChekComplatedEventArgs(); OnNetworkChekComplatedEvent(null, e); } } return(result); }
static public void ConfirmIPAddress(BaseProxyServer item, string homeIP, JobLog jobLog) { string externalIP = NetConnUtils.GetMyExternalIP(); if (externalIP == null) { throw new Exception("Ip of vpn connection is null"); } jobLog.Info("ExternalIP: " + externalIP); if (homeIP != null) { if (homeIP.Equals(externalIP)) { throw new Exception("Ip address of vpn connection not changed. It equals home ip address."); } } JIPAddressInfo extIPAddressInfo = NetConnUtils.GetIPAddressInfo(externalIP); if (extIPAddressInfo == null) { throw new Exception("IPAddressInfo of vpn connection is null"); } jobLog.Info("New IP address Info: " + Log.PropertyList(extIPAddressInfo)); if (item != null) { if (item.JCountry != null) { if (!string.IsNullOrEmpty(extIPAddressInfo.CountryCode)) { if (extIPAddressInfo.CountryCode.ToLower().Equals(item.JCountry.JCountryId) == false) { //throw new Exception jobLog.Warn("Country code of vpn connection ip address (" + extIPAddressInfo.CountryCode.ToLower() + ") not equals to contry code of VPN server (" + item.JCountry.JCountryId + ")"); JCountry newContry = Dm.Instance.Find <JCountry>(extIPAddressInfo.CountryCode.ToLower()); if (newContry != null) { if (item.JCountryDeclared == null) { item.JCountryDeclared = item.JCountry; } item.JCountry = newContry; Dm.Instance.SaveObject(item); } else { throw new Exception("Country code of vpn connection ip address (" + extIPAddressInfo.CountryCode.ToLower() + ") not a valid country code"); } } } else { throw new Exception("Country code of vpn connection is empty"); } if (!string.IsNullOrEmpty(extIPAddressInfo.City)) { if (item.Town != null) { if (extIPAddressInfo.City.ToLower().Equals(item.Town.ToLower()) == false) { jobLog.Warn("City vpn connection ip address (" + extIPAddressInfo.City + ") not equals to town of VPN server (" + item.Town + "). New City value was set"); if (item.TownDeclared == null) { item.TownDeclared = item.Town; } item.Town = extIPAddressInfo.City; Dm.Instance.SaveObject(item); } } else { item.Town = extIPAddressInfo.City; Dm.Instance.SaveObject(item); } } } else { //todo } jobLog.Info("Test OK for item " + item.JProxyServerId); } //ok //todo NetConnUtils.MyExternalIP = externalIP; NetConnUtils.MyExtIPAddressInfo = extIPAddressInfo; }
private void ScheduleJobChekVpnAll(IList selectedItems, bool all, bool favoritOnly) { StartRefreshing(); // create background tasks, which nevertheless are executed sequentially // it would be possible to create one background thread and perform all the tasks sequentially, // but at the same time the opportunity to destroy the hanging task is lost and to let the others continue // todo worth thinking about limiting the number of waiting threads //prepare task list IList list = null; if (all) { list = Dm.Instance.FindAll(BaseProxyServer.CurrentType); } else if (favoritOnly) { IList list0 = Dm.Instance.FindAll(BaseProxyServer.CurrentType); list = new List <BaseProxyServer>(); foreach (var n in list0) { BaseProxyServer m = (BaseProxyServer)n; if (m.Favorite) { list.Add(m); } } } else { list = selectedItems; } if (list == null || list.Count == 0) { throw new Exception("No items to test found"); } //check for allready running tasks and stop it bool foundWorking = false; foreach (var n in list) { BaseProxyServer m = (BaseProxyServer)n; if (m.JRunningJob != null && m.JRunningJob.JJobType != null && m.JRunningJob.IsWorking()) { foundWorking = true; break; } } if (foundWorking) { DialogResult res = MessageBox.Show(VpnSelectorLibRes.Running_job_found__They_will_be_aborted_or_press__Cancel__to_exit, FrwConstants.WARNING, MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (res == DialogResult.Cancel) { return; } } foreach (var n in list) { BaseProxyServer m = (BaseProxyServer)n; if (favoritOnly == false || m.Favorite) { if (m.JRunningJob != null) { if (m.JRunningJob.JJobType != null) { if (m.JRunningJob.IsWorking()) { JobManager.Instance.AbortJob(m.JRunningJob); } else { //m.JRunningJob.Stage = RunningJobStageEnum.initial.ToString(); //Dm.Instance.SaveObject(m.JRunningJob); } } m.JRunningJob = null; Dm.Instance.SaveObject(m); } } } string homeIP = null; //close active vpns if (NetConnUtils.IsActiveConnectionPresent()) { NetConnUtils.CloseAllActiveConnections(false); homeIP = NetConnUtils.GetMyExternalIP(); } else { homeIP = NetConnUtils.MyExternalIP; } //Schedule tasks foreach (var n in list) { BaseProxyServer m = (BaseProxyServer)n; ScheduleJobChekVpnServer(m, homeIP); JobManager.ScheduleJobToQueue(m.JRunningJob); currQueueJobType = m.JRunningJob.JJobType; } if (currQueueJobType != null) { currQueueJobType.RemoveAllPostJobBatchEventHandlers(); currQueueJobType.PostJobBatch += currQueueJobType.StandartPostLJobBatchEventHandler; JobManager.StartProcessingJobBatch(currQueueJobType); } }