override public void InitApplication() { base.InitApplication(); VpnSelectorDm.MyLocalIP = BaseNetworkUtils.GetHostIp(); if ("127.0.0.1".Equals(VpnSelectorDm.MyLocalIP) == false) { VpnSelectorDm.MyMac = BaseNetworkUtils.GetMacByIP(VpnSelectorDm.MyLocalIP); VpnSelectorDm.DnsIP = BaseNetworkUtils.GetDnsAddressForIP(VpnSelectorDm.MyLocalIP); if (VpnSelectorDm.DnsIP != null) { VpnSelectorDm.DnsMac = BaseNetworkUtils.GetMacByIP(VpnSelectorDm.DnsIP); } } bool _isActiveConnectionPresent = VpnConnUtils.IsActiveConnectionPresent(); if (_isActiveConnectionPresent) { VpnConnUtils.CurrentVPNServer = JVPNServer.FindFromNames(VpnConnUtils.GetActiveConnectionsNames()); } Console.WriteLine("IP: " + VpnSelectorDm.MyLocalIP); Console.WriteLine("MAC: " + VpnSelectorDm.MyMac); Console.WriteLine("DNS: " + VpnSelectorDm.DnsIP); Console.WriteLine("DNS MAC: " + VpnSelectorDm.DnsMac); Console.WriteLine("Current VPN Server: " + VpnConnUtils.CurrentVPNServer); BaseNetworkUtils.ShowNetworkInterfaces(); }
private static void ScheduleJobChekVpnServer(JVPNServer item) { JRunningJob job = null; job = JobManager.Instance.CreateJob("ChekVpn", VpnSelectorLibRes.Test_for_VPN_server, item.Url, JobConcurrentTypeEnum.Wait); job.DoJob += (sd, ew) => { job.JobLog.Info("Plan VPN server test for " + item.Url); bool res = VpnConnUtils.TestConnectionWithConfirmation(item, job.JobLog); Thread.Sleep(5 * 1000); job.JobLog.Info("End VPN server test for " + item.Url); JobManager.Instance.ReportProgresJob(job, 100); if (res) { ew.StageResult = RunningJobResultEnum.ok; } else { ew.StageResult = RunningJobResultEnum.error; } }; item.JRunningJob = job; Dm.Instance.SaveObject(item); }
public void MakeContextMenu(IListProcessor list, List <ToolStripItem> menuItemList, object selectedListItem, object selectedObject, string aspectName) { //base.MakeContextMenu(menuItemList, selectedListItem, selectedObject, aspectName); menuItemList.Add(new ToolStripSeparator()); JVPNServer item = (JVPNServer)selectedObject; List <ToolStripItem> subitems = new List <ToolStripItem>(); if (item != null) { VpnConnUtils.MakeContextMenuForBaseVPNServer(subitems, item); } VpnConnUtils.MakeContextMenuForAllBaseVPNServers(subitems); menuItemList.AddRange(subitems.ToArray()); ToolStripMenuItem groupItem = null; ToolStripMenuItem menuItem = null; menuItem = new ToolStripMenuItem(); menuItem.Text = VpnSelectorLibRes.Import_VPN_servers_from_vpngate_net; menuItem.Click += (s, em) => { try { VpngateImporter.DowloadAndParse(); } catch (Exception ex) { Log.ShowError(ex); } }; menuItemList.Add(menuItem); groupItem = new ToolStripMenuItem(); groupItem.Text = VpnSelectorLibRes.Run_test; menuItemList.Add(groupItem); if (item != null) { menuItem = new ToolStripMenuItem(); menuItem.Text = ((list.SelectedObjects.Count > 1) ? VpnSelectorLibRes.Run_test_for_selected_VPN_servers : VpnSelectorLibRes.Run_test_for_this_VPN_server); menuItem.Click += (s, em) => { try { ScheduleJobChekVpnAll(list, list.SelectedObjects, false, false); } catch (Exception ex) { Log.ShowError(ex); } }; groupItem.DropDownItems.Add(menuItem); if (item.JRunningJob != null && item.JRunningJob.JJobType != null)//JRunningJob.JJobType = null for manualy deleted job { List <ToolStripItem> subitems2 = new List <ToolStripItem>(); JobManager.MakeTitleContextMenuForRunningJob(item.JRunningJob, subitems2, list.ContentContainer); groupItem.DropDownItems.AddRange(subitems2.ToArray <ToolStripItem>()); } if (currQueueJobType != null) { List <ToolStripItem> subitems22 = new List <ToolStripItem>(); JobManager.MakeTitleContextMenuForRunningJobTypeQueue(currQueueJobType, subitems22, list.ContentContainer); groupItem.DropDownItems.AddRange(subitems22.ToArray <ToolStripItem>()); } } menuItem = new ToolStripMenuItem(); menuItem.Text = VpnSelectorLibRes.Run_test_for_favorite_VPN_servers; menuItem.Click += (s, em) => { try { ScheduleJobChekVpnAll(list, null, false, true); } catch (Exception ex) { Log.ShowError(ex); } }; groupItem.DropDownItems.Add(menuItem); menuItem = new ToolStripMenuItem(); menuItem.Text = VpnSelectorLibRes.Run_test_for_all_VPN_servers; menuItem.Click += (s, em) => { try { ScheduleJobChekVpnAll(list, null, true, false); } catch (Exception ex) { Log.ShowError(ex); } }; groupItem.DropDownItems.Add(menuItem); }
private void ScheduleJobChekVpnAll(IListProcessor blist, IList selectedItems, bool all, bool favoritOnly) { blist.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(typeof(JVPNServer)); } else if (favoritOnly) { IList list0 = Dm.Instance.FindAll(typeof(JVPNServer)); list = new List <JVPNServer>(); foreach (var n in list0) { JVPNServer m = (JVPNServer)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) { JVPNServer m = (JVPNServer)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) { JVPNServer m = (JVPNServer)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); } } } //Schedule tasks foreach (var n in list) { JVPNServer m = (JVPNServer)n; ScheduleJobChekVpnServer(m); JobManager.ScheduleJobToQueue(m.JRunningJob); currQueueJobType = m.JRunningJob.JJobType; } if (currQueueJobType != null) { currQueueJobType.RemoveAllPostJobBatchEventHandlers(); currQueueJobType.PostJobBatch += currQueueJobType.StandartPostLJobBatchEventHandler; JobManager.StartProcessingJobBatch(currQueueJobType); } }
//todo log //todo check vpn provider id //todo final message private static void ParseDirect(string s) { List<JVPNServer> tmpList = new List<JVPNServer>(); IList cl = Dm.Instance.FindAll(typeof(JCountry)); TextReader reader = new StringReader(s); HtmlDocument doc = new HtmlDocument(); doc.Load(reader); if (doc.DocumentNode != null) { HtmlNode span = doc.DocumentNode.SelectSingleNode("//span[@id='Label_Table']"); HtmlNode table = span.SelectSingleNode("table[@id='vg_hosts_table_id']"); HtmlNodeCollection trs = table.SelectNodes("tr"); if (trs != null) { bool firstTr = true; foreach (HtmlNode tr in trs) { if (firstTr)//header { firstTr = false; continue; } if (tr.SelectNodes("td[@class='vg_table_header']") != null && tr.SelectNodes("td[@class='vg_table_header']").Count > 0) { continue; } HtmlNodeCollection tds = tr.SelectNodes("td"); if (tds != null) { bool valid = false; int tdnum = 0; foreach (HtmlNode td in tds) { if (tdnum == 5) { string l2tp = ClearTag(td.InnerText); if (string.IsNullOrEmpty(l2tp) == false && l2tp.Contains("L2TP")) { valid = true; break; } } tdnum++; } if (valid) { JVPNServer p = (JVPNServer)Activator.CreateInstance(typeof(JVPNServer)); tdnum = 0; foreach (HtmlNode td in tds) { if (tdnum == 0)//country <img src='../images/flags/JP.png' width='32' height='32' /><br>Japan</td> { string p_JCountry = ClearTag(td.InnerText); if (p_JCountry != null) { foreach (JCountry c in cl) { if (c.Name.Equals(p_JCountry) || (c.Official_name_en != null && c.Official_name_en.Equals(p_JCountry)) || (c.Official_name_fr != null && c.Official_name_fr.Equals(p_JCountry)) ) { p.JCountry = c; break; } } } if (p.JCountry == null) { HtmlNode image = td.SelectSingleNode("img"); if (image != null) { string src = image.GetAttributeValue("src", null); if (src != null) { int indexDot = src.IndexOf(".png"); int indexSplash = src.LastIndexOf("/"); if (indexDot > -1 && indexSplash > -1 && indexDot > indexSplash) { string countryCode = src.Substring(indexSplash + 1, (indexDot - indexSplash) - 1); countryCode = countryCode.ToLower(); foreach (JCountry c in cl) { if (c.JCountryId.Equals(countryCode)) { p.JCountry = c; break; } } } } } } if (p.JCountry == null) { Console.WriteLine("=================================================="); Console.WriteLine("Country not found: " + td.InnerHtml); Console.WriteLine("=================================================="); } } if (tdnum == 1)//url <b><span style='font-size: 9pt;'>word-wind-vpn.opengw.net</span></b><br><span style='font-size: 10pt;'>218.229.183.122</span><br><span style='font-size: 7pt;'>(nttkyo1188122.tkyo.nt.ngn.ppp.infoweb.ne.jp)</span> { HtmlNode b = td.SelectSingleNode("b/span"); string urlsStr = ClearTag(b.InnerText); p.Url = urlsStr; } tdnum++; } tmpList.Add(p); }//valid }//tds } } } List<JVPNServer> newList = new List<JVPNServer>(); List<JVPNServer> sameList = new List<JVPNServer>(); List<JVPNServer> restoredList = new List<JVPNServer>(); List<JVPNServer> archivedList = new List<JVPNServer>(); JVPNProvider pp = (JVPNProvider)Dm.Instance.Find(typeof(JVPNProvider), "5"); foreach (var p0 in tmpList) { //Console.WriteLine(ModelHelper.ModelPropertyList(p0, "\n", null, null)); //Console.WriteLine("=================================================="); JVPNServer foundSame = null; IList oldList = Dm.Instance.FindAll(typeof(JVPNServer)); foreach (var o in oldList) { JVPNServer oldP = (JVPNServer)o; if (oldP.JVPNProvider != null && oldP.JVPNProvider.Equals(pp) && oldP.Url.Equals(p0.Url)) { foundSame = oldP; if (oldP.IsArchived == true) { oldP.IsArchived = false; restoredList.Add(oldP); Dm.Instance.SaveObject(oldP); } break; } } if (foundSame == null) { JVPNServer p = (JVPNServer)Dm.Instance.EmptyObject(typeof(JVPNServer), null); p.JVPNProvider = pp; p.Url = p0.Url; p.JCountry = p0.JCountry; p.AvailableProtocols = new List<string>(); p.AvailableProtocols.Add(VPNProtocolTypeEnum.L2TP.ToString()); p.EncryptionType = VPNEncryptionTypeEnum.Require.ToString(); Dm.Instance.SaveObject(p); newList.Add(p); } else { sameList.Add(foundSame); } } //reverse search IList oldList2 = Dm.Instance.FindAll(typeof(JVPNServer)); foreach (var o in oldList2) { JVPNServer oldP = (JVPNServer)o; if (oldP.JVPNProvider != null && oldP.JVPNProvider.Equals(pp)) { bool found = false; foreach (var p0 in tmpList) { if (oldP.Url.Equals(p0.Url)) { found = true; } } if (!found && oldP.IsArchived == false) { oldP.IsArchived = true; archivedList.Add(oldP); } } } Log.ProcessDebug("================ same ================ " + sameList.Count); foreach (var v in sameList) { Log.ProcessDebug(v.Url); } Log.ProcessDebug("================ new ================ " + newList.Count); foreach (var v in newList) { Log.ProcessDebug(v.Url); } Log.ProcessDebug("================ restored ================ " + restoredList.Count); foreach (var v in restoredList) { Log.ProcessDebug(v.Url); } Log.ProcessDebug("================ archive ================ " + archivedList.Count); foreach (var v in archivedList) { Log.ProcessDebug(v.Url); } }
public bool CheckIfWeNeedVPN(WebEntryInfo webEntryInfo) { string mes = null; JVPNServer allowedVPNServer = null; if (webEntryInfo.AllowedVPNServerId != null) { allowedVPNServer = Dm.Instance.Find <JVPNServer>(webEntryInfo.AllowedVPNServerId); } //if (webEntryInfo.AllowedVPNServerId != null || webEntryInfo.AllowedVPNCountrу != null || webEntryInfo.AllowedVPNTown != null) if (webEntryInfo.AllowedVPNServerId != null) { string allowedMes = WebAccountLibRes.For_this_entity_you_need_access_to_the_Internet_through_a_specific_VPN; if (VpnConnUtils.CurrentVPNServer == null) { mes = allowedMes + WebAccountLibRes.Current_VPN_connection_is_not_established; } else { if (webEntryInfo.AllowedVPNServerId != null) { if (allowedVPNServer == null) { mes = allowedMes + WebAccountLibRes.This_VPN_connection_has_not_been_established; } else if (VpnConnUtils.CurrentVPNServer != allowedVPNServer) { mes = allowedMes + WebAccountLibRes.The_current_VPN_connection_does_not_match; } } } } if (mes != null) { if (allowedVPNServer != null) { DialogResult res = MessageBox.Show(mes + " " + WebAccountLibRes.Click_Yes_to_connect_to_the_desired_VPN_server, FrwConstants.WARNING, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); if (res == DialogResult.Yes) { try { Cursor.Current = Cursors.WaitCursor; bool conRes = VpnConnUtils.ConnectWithConfirmation(allowedVPNServer); return(conRes); } finally { Cursor.Current = Cursors.Default; } } else if (res == DialogResult.No) { //do nothing } else //Cancel or close { return(false); } } else { DialogResult res = MessageBox.Show(mes + " " + WebAccountLibRes.Click_OK_to_ignore_the_warning_and_continue_Click_Cancel_to_cancel, FrwConstants.WARNING, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (res != DialogResult.OK) //Cancel or close { return(false); } } } return(true); }