Esempio n. 1
0
        static public bool IsConnected(BaseProxyServer ps)
        {
            RasConnection conn = null;

            if (ps == null)
            {
                if (dialer != null)
                {
                    conn = RasConnection.GetActiveConnectionByName(dialer.EntryName, dialer.PhoneBookPath);
                }
            }
            else
            {
                conn = RasConnection.GetActiveConnectionByName(ps.GetConnectionName(), RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User));
            }

            if (conn != null)
            {
                RasConnectionStatus st = conn.GetConnectionStatus();
                if (st.ConnectionState == RasConnectionState.Connected)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Esempio n. 2
0
        static public void MakeFavoriteContextMenu(List <ToolStripItem> items)
        {
            BaseProxyServer   defaultItem = GetDefaulBaseProxyServer();
            ToolStripMenuItem menuItem    = new ToolStripMenuItem();

            menuItem.Text = VpnSelectorLibRes.Favorit_connections;
            items.Add(menuItem);

            IList list = Dm.Instance.FindAll(BaseProxyServer.CurrentType);

            foreach (var o in list)
            {
                BaseProxyServer item = (BaseProxyServer)o;
                if (item.Favorite == true)
                {
                    ToolStripMenuItem subMenuItem = new ToolStripMenuItem();
                    subMenuItem.Text = item.GetConnectionName();
                    if (item == defaultItem)
                    {
                        subMenuItem.Font = new Font(subMenuItem.Font, FontStyle.Bold);
                    }
                    menuItem.DropDownItems.Add(subMenuItem);

                    List <ToolStripItem> subitems = new List <ToolStripItem>();
                    MakeContextMenuForBaseProxyServer(subitems, item);
                    subMenuItem.DropDownItems.AddRange(subitems.ToArray());
                }
            }
        }
Esempio n. 3
0
 static public void CloseConnect(BaseProxyServer ps, bool async)
 {
     if (ps == null)
     {
         if (dialer != null)
         {
             RasConnection conn = RasConnection.GetActiveConnectionByName(dialer.EntryName, dialer.PhoneBookPath);
             if (conn != null)
             {
                 conn.HangUp();
             }
         }
     }
     else
     {
         RasConnection conn = RasConnection.GetActiveConnectionByName(ps.GetConnectionName(), RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User));
         if (conn != null)
         {
             conn.HangUp();
         }
     }
     if (!async)
     {
         NetConnUtils.CurrentProxyServer = null;
     }
 }
Esempio n. 4
0
        static private void OpenConnectLocal(BaseProxyServer ps, bool async)
        {
            CreateDialerAndBeginWatch();//if not created yet
            //http://www.dotnetobject.com/Thread-Connecting-VPN-using-C

            dialer.EntryName     = ps.GetConnectionName();
            dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
            try
            {
                try
                {
                    RasConnection conn = RasConnection.GetActiveConnectionByName(ps.GetConnectionName(), dialer.PhoneBookPath);
                    if (conn != null)
                    {
                        conn.HangUp();
                    }
                }
                catch (Exception)
                {
                    //no connection present
                }
                if (!string.IsNullOrEmpty(ps.GetProxyProvider().VPNLogin))
                {
                    System.Net.NetworkCredential cred = new System.Net.NetworkCredential(ps.GetProxyProvider().VPNLogin, ps.GetProxyProvider().VPNPassword);
                    dialer.Credentials = cred;
                }
                //dialer.Dial();//alt DialAsync()
                if (async)
                {
                    handle = dialer.DialAsync();
                }
                else
                {
                    handle = dialer.Dial();
                    NetConnUtils.CurrentProxyServer = ps;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 5
0
        static public bool IsConnectionEntryExist(BaseProxyServer ps)
        {
            string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);

            if (path != null && File.Exists(path))
            {
                string name = ps.GetConnectionName();
                return(RasEntry.Exists(name, path));
            }
            else
            {
                return(false);
            }
        }
Esempio n. 6
0
        static public void CreateConnectionEntry(BaseProxyServer ps, ProxyProtocolTypeEnum protocolType)
        {
            if (!ps.IsProtocolAvailable(protocolType))
            {
                throw new ArgumentException("Protocol " + protocolType.ToString() + " not avilable");
            }

            //http://stackoverflow.com/questions/36213393/get-connection-status-vpn-using-dotras
            // File.WriteAllText("your rasphone.pbk  path","")//Add
            RasPhoneBook rasPhoneBook1    = new RasPhoneBook();
            string       rasPhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);//alt RasPhoneBookType.AllUsers

            rasPhoneBook1.Open(rasPhoneBookPath);

            string         deviceTypeStr = "(" + protocolType.ToString() + ")";
            RasVpnStrategy strategy      = (protocolType == ProxyProtocolTypeEnum.L2TP) ? RasVpnStrategy.L2tpOnly : RasVpnStrategy.PptpOnly;
            //alt
            //RasVpnStrategy strategy = RasVpnStrategy.Default;
            RasEntry entry = RasEntry.CreateVpnEntry(ps.GetConnectionName(), ps.Url,
                                                     strategy,
                                                     RasDevice.GetDeviceByName(deviceTypeStr, RasDeviceType.Vpn, false));

            entry.EncryptionType = ps.EncryptionType.ToEnum <RasEncryptionType>();
            if (protocolType == ProxyProtocolTypeEnum.L2TP && !string.IsNullOrEmpty(ps.GetProxyProvider().UserPresharedKey))
            {
                entry.Options.UsePreSharedKey = true;
            }
            rasPhoneBook1.Entries.Add(entry);
            if (protocolType == ProxyProtocolTypeEnum.L2TP && !string.IsNullOrEmpty(ps.GetProxyProvider().UserPresharedKey))
            {
                entry.UpdateCredentials(RasPreSharedKey.Client, ps.GetProxyProvider().UserPresharedKey);
            }

            if (!string.IsNullOrEmpty(ps.GetProxyProvider().VPNLogin))
            {
                //entry.UpdateCredentials(new System.Net.NetworkCredential(ps.JProxyProvider.VPNLogin, ps.JProxyProvider.VPNPassword), false);
            }
        }
Esempio n. 7
0
        static public void RemoveConnectionEntry(BaseProxyServer ps)
        {
            string       name             = ps.GetConnectionName();
            RasPhoneBook rasPhoneBook1    = new RasPhoneBook();
            string       rasPhoneBookPath = null;

            rasPhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
            rasPhoneBook1.Open(rasPhoneBookPath);
            RasEntry entryFound = null;

            foreach (RasEntry entry1 in rasPhoneBook1.Entries)
            {
                if (entry1.Name.Equals(name))
                {
                    entryFound = entry1;
                    break;
                }
            }
            if (entryFound != null)
            {
                entryFound.Remove();
            }
        }
Esempio n. 8
0
        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);
        }