static private bool CreateAndConnectToProxyAsync(BaseProxyServer item) { bool statusChanged = false; if (!NetConnUtils.IsConnected(item)) { if (NetConnUtils.IsActiveConnectionPresent()) { NetConnUtils.CloseAllActiveConnections(false); } if (NetConnUtils.IsConnectionEntryExist(item) == false) { if (item.IsProtocolAvailable(ProxyProtocolTypeEnum.PPTP)) { NetConnUtils.CreateConnectionEntry(item, ProxyProtocolTypeEnum.PPTP); } else if (item.IsProtocolAvailable(ProxyProtocolTypeEnum.L2TP)) { NetConnUtils.CreateConnectionEntry(item, ProxyProtocolTypeEnum.L2TP); } else { throw new ArgumentException(VpnSelectorLibRes.Non_PPTP_no_L2TP_protocols_available_for_this_vpn_entry); } } NetConnUtils.OpenConnect(item); //DotRasUtils.CurrentProxyServer = item; statusChanged = true; } return(statusChanged); }
static public bool DisconnectWithConfirmation(BaseProxyServer item, string homeIP, bool createdNew, JobLog jobLog) { if (NetConnUtils.IsActiveConnectionPresent()) { NetConnUtils.CloseAllActiveConnections(false); Thread.Sleep(2 * 1000); } if (createdNew) { NetConnUtils.RemoveConnectionEntry(item); } return(true); }
static public bool ConnectOrDisconnectDefautBaseProxyServerAsync() { bool statusChanged = false; if (NetConnUtils.IsActiveConnectionPresent()) { NetConnUtils.CloseAllActiveConnections(true); statusChanged = true; } else { BaseProxyServer item = GetDefaulBaseProxyServer(); if (item != null) { statusChanged = CreateAndConnectToProxyAsync(item); } else { MessageBox.Show(VpnSelectorLibRes.Double_click_on_this_icon_creates_VPN_connection_to_default_VPN_server_which_not_set__No_connection_created_, VpnSelectorLibRes.Warning_, MessageBoxButtons.OK, MessageBoxIcon.Information); } } return(statusChanged); }
protected void CreateVPNSupport() { NetConnUtils.InitSettings(); // // vpnButton // this.vpnButton = new System.Windows.Forms.ToolStripSplitButton(); this.vpnButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; this.vpnButton.Enabled = true; this.vpnButton.ImageTransparentColor = System.Drawing.Color.Magenta; this.vpnButton.Name = "vpnButton"; this.vpnButton.Size = new System.Drawing.Size(19, 23); this.vpnButton.Text = "VPN"; this.vpnButton.ToolTipText = "VPN"; vpnButton.Image = VpnSelectorLibRes.network_off; //vpnButton.DoubleClick += VpnButton_DoubleClick; - do not working // // ipAddressLabel // this.ipAddressLabel = new System.Windows.Forms.ToolStripStatusLabel(); this.ipAddressLabel.AutoToolTip = true; this.ipAddressLabel.Enabled = false; this.ipAddressLabel.Name = "ipAddressLabel"; this.ipAddressLabel.Size = new System.Drawing.Size(66, 20); this.ipAddressLabel.Text = "127.0.0.1"; this.ipAddressLabel.ToolTipText = "127.0.0.1"; this.statusBar.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.eventWarrningButton, this.vpnButton, this.ipAddressLabel }); if (useMainTrayIconForVPN && BaseApplicationContext.NotifyIcon != null) { vpnNotifyIcon = BaseApplicationContext.NotifyIcon; } else { vpnNotifyIcon = new NotifyIcon(components) { ContextMenuStrip = new ContextMenuStrip(), Icon = vpnOffIcon, Text = "VPN", Visible = true }; } vpnButton.MouseDown += VpnButton_MouseDown; vpnNotifyIcon.ContextMenuStrip.Opening += VpnNotifyIconContextMenuStrip_Opening; vpnNotifyIcon.DoubleClick += VpnNotifyIcon_DoubleClick; NetConnUtils.OnNetworkChekComplatedEvent += JustNetworkUtils_OnNetworkChekComplatedEvent; NetConnUtils.VpnDisconnectedEvent += VpnSelector_OnVpnDisconnectedEvent; NetConnUtils.VpnConnectedEvent += VpnSelector_OnVpnConnectedEvent; NetConnUtils.VpnDialerErrorEvent += VpnSelector_OnVpnDialerErrorEvent; NetConnUtils.CreateDialerAndBeginWatch(); bool _isActiveConnectionPresent = NetConnUtils.IsActiveConnectionPresent(); if (_isActiveConnectionPresent) { NetConnUtils.CurrentProxyServer = BaseProxyServer.FindFromNames(NetConnUtils.GetActiveConnectionsNames()); ProcessVpnConnectedEvent(); //DotRasUtils.CurrentProxyServer = DotRasUtils.CurrentProxyServer; } else { NetConnUtils.ConfirmIpAddressAsync(); } }
static public void MakeContextMenuForAllBaseProxyServers(List <ToolStripItem> items) { ToolStripMenuItem menuItem = new ToolStripMenuItem(); menuItem.Text = VpnSelectorLibRes.Disconnect_from_active_VPN_connection; if (!NetConnUtils.IsActiveConnectionPresent()) { menuItem.Enabled = false; } menuItem.Click += (s, em) => { try { NetConnUtils.CloseAllActiveConnections(true); } catch (Exception ex) { Log.ShowError(ex); } }; items.Add(menuItem); menuItem = new ToolStripMenuItem(); menuItem.Text = VpnSelectorLibRes.Show_created_VPN_entries_info; menuItem.Click += (s, em) => { try { MessageBox.Show(NetConnUtils.ShowConnectionEntries()); } catch (Exception ex) { Log.ShowError(ex); } }; items.Add(menuItem); menuItem = new ToolStripMenuItem(); menuItem.Text = VpnSelectorLibRes.Show_active_VPN_connection_info; if (!NetConnUtils.IsActiveConnectionPresent()) { menuItem.Enabled = false; } menuItem.Click += (s, em) => { try { MessageBox.Show(NetConnUtils.ShowActiveConnections()); } catch (Exception ex) { Log.ShowError(ex); } }; items.Add(menuItem); menuItem = new ToolStripMenuItem(); menuItem.Text = VpnSelectorLibRes.Delete_all_VPN_entries; menuItem.Click += (s, em) => { try { DialogResult res = MessageBox.Show(null, VpnSelectorLibRes.Delete_VPN_entries__Press__Yes__to_delete_, FrwCRUDRes.WARNING, MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (res == DialogResult.Yes) { if (NetConnUtils.IsActiveConnectionPresent()) { NetConnUtils.CloseAllActiveConnections(true); } NetConnUtils.RemoveAllConnectionEntry(); } } catch (Exception ex) { Log.ShowError(ex); } }; items.Add(menuItem); }
static public void MakeContextMenuForBaseProxyServer(List <ToolStripItem> items, BaseProxyServer item) { ToolStripMenuItem menuItem = new ToolStripMenuItem(); menuItem.Text = VpnSelectorLibRes.Create_VPN_entry_ + " PPTP " + item.GetConnectionName(); if (NetConnUtils.IsConnectionEntryExist(item) || item.IsProtocolAvailable(ProxyProtocolTypeEnum.PPTP) == false) { menuItem.Enabled = false; } menuItem.Click += (s, em) => { try { NetConnUtils.CreateConnectionEntry(item, ProxyProtocolTypeEnum.PPTP); } catch (Exception ex) { Log.ShowError(ex); } }; items.Add(menuItem); menuItem = new ToolStripMenuItem(); menuItem.Text = VpnSelectorLibRes.Create_VPN_entry_ + " L2TP " + item.GetConnectionName(); if (NetConnUtils.IsConnectionEntryExist(item) || item.IsProtocolAvailable(ProxyProtocolTypeEnum.L2TP) == false) { menuItem.Enabled = false; } menuItem.Click += (s, em) => { try { NetConnUtils.CreateConnectionEntry(item, ProxyProtocolTypeEnum.L2TP); } catch (Exception ex) { Log.ShowError(ex); } }; items.Add(menuItem); menuItem = new ToolStripMenuItem(); menuItem.Text = VpnSelectorLibRes.Connect_to_VPN_server; if (NetConnUtils.IsConnected(item)) { menuItem.Enabled = false; } if (NetConnUtils.IsConnectionEntryExist(item)) { menuItem.Text = menuItem.Text + VpnSelectorLibRes.__created_; } menuItem.Text = menuItem.Text + " " + item.GetConnectionName(); menuItem.Click += (s, em) => { try { DialogResult res = DialogResult.Cancel; if (NetConnUtils.IsActiveConnectionPresent()) { res = MessageBox.Show(VpnSelectorLibRes.Active_VPN_connection_found____ + NetConnUtils.ShowConnectionEntries() + VpnSelectorLibRes.__Press_OK_to_close_it_and_connect_to_selected_VPN_server_, VpnSelectorLibRes.Warning_, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (res == DialogResult.OK) { NetConnUtils.CloseAllActiveConnections(false); } } else { res = DialogResult.OK; } if (res == DialogResult.OK) { bool statusChanged = CreateAndConnectToProxyAsync(item); } } catch (Exception ex) { Log.ShowError(ex); } }; items.Add(menuItem); menuItem = new ToolStripMenuItem(); menuItem.Text = VpnSelectorLibRes.Disonnect_from_VPN_server_ + item.GetConnectionName(); if (!NetConnUtils.IsConnected(item)) { menuItem.Enabled = false; } menuItem.Click += (s, em) => { try { NetConnUtils.CloseConnect(item, true); } catch (Exception ex) { Log.ShowError(ex); } }; items.Add(menuItem); menuItem = new ToolStripMenuItem(); menuItem.Text = VpnSelectorLibRes.Set_as_default_VPN_connection_ + item.GetConnectionName(); if (NetConnUtils.GetDefaulBaseProxyServer() == item) { menuItem.Enabled = false; } menuItem.Click += (s, em) => { try { BaseProxyServerDefault = item; JSetting setting = FrwConfig.Instance.GetProperty(DEFAULT_JPROXY_SERVER); setting.Value = item; } catch (Exception ex) { Log.ShowError(ex); } }; items.Add(menuItem); }
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); }
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); } }