bool Check()
        {
            if ((RouteDescriptionToDirection(GuiUtils.GetSelected(CboRoutesOtherwise)) == "out") && (TableRoutingController.Items.Count == 0))
            {
                if (GuiUtils.MessageYesNo(Messages.WindowsSettingsRouteOutEmptyList) == false)
                {
                    return(false);
                }
            }

            if (GuiUtils.GetCheck(ChkLockAllowDNS) == false)
            {
                bool hostNameUsed = false;
                foreach (TableRoutingControllerItem item in TableRoutingController.Items)
                {
                    if (IpAddress.IsIP(item.Ip) == false)
                    {
                        hostNameUsed = true;
                        break;
                    }
                }

                if (hostNameUsed)
                {
                    if (Engine.Instance.OnAskYesNo(Messages.WindowsSettingsRouteWithHostname) == false)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        public List <string> GetBootstrapUrls()
        {
            List <string> urls = new List <string>();

            // Manual Urls
            foreach (string url in Engine.Instance.Storage.Get("bootstrap.urls").Split(';'))
            {
                string sUrl = url.Trim();
                if (sUrl != "")
                {
                    if (IpAddress.IsIP(sUrl))
                    {
                        sUrl = "http://" + sUrl;
                    }
                    string host = UtilsCore.HostFromUrl(sUrl);
                    if (host != "")
                    {
                        urls.Add(sUrl);
                    }
                }
            }

            // Manifest Urls
            if (Manifest != null)
            {
                XmlNodeList nodesUrls = Manifest.SelectNodes("//urls/url");
                foreach (XmlNode nodeUrl in nodesUrls)
                {
                    urls.Add(nodeUrl.Attributes["address"].Value);
                }
            }

            return(urls);
        }
Exemplo n.º 3
0
        private void cbbSelectEquip_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Equipment equip     = (sender as ComboBox).SelectedItem as Equipment;
            string    equipName = equip.Name;
            int       equipID   = equip.Index;
            IpAddress agentIP   = null;

            try
            {
                object strIP = App.DBHelper.returnScalar(string.Format("SELECT IP_Address FROM IPAddress WHERE(IP_EquipID = {0}) AND (IP_IsDefaultIP = 1)", equipID));
                if (strIP == null)
                {
                    MessageBox.Show("数据库中没有" + equipName + "的默认管理IP地址");
                    return;
                }
                if (IpAddress.IsIP(strIP.ToString()))
                {
                    agentIP = new IpAddress(strIP.ToString());
                }
                else
                {
                    MessageBox.Show("地址格式错误");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取数据库出现错误\n" + ex.Message);
                return;
            }
            ConstructListByIP(agentIP, equipName);
        }
Exemplo n.º 4
0
        public override bool OnDnsSwitchDo(IpAddresses dns)
        {
            string mode = Engine.Instance.Storage.GetLower("dns.mode");

            if (mode == "auto")
            {
                string[] interfaces = GetInterfaces();
                foreach (string i in interfaces)
                {
                    string i2 = i.Trim();

                    string currentStr = SystemShell.Shell("/usr/sbin/networksetup", new string[] { "-getdnsservers", SystemShell.EscapeInsideQuote(i2) });

                    // v2
                    IpAddresses current = new IpAddresses();
                    foreach (string line in currentStr.Split('\n'))
                    {
                        string ip = line.Trim();
                        if (IpAddress.IsIP(ip))
                        {
                            current.Add(ip);
                        }
                    }

                    if (dns.Equals(current) == false)
                    {
                        DnsSwitchEntry e = new DnsSwitchEntry();
                        e.Name = i2;
                        e.Dns  = current.Addresses;
                        m_listDnsSwitch.Add(e);

                        SystemShell s = new SystemShell();
                        s.Path = LocateExecutable("networksetup");
                        s.Arguments.Add("-setdnsservers");
                        s.Arguments.Add(SystemShell.EscapeInsideQuote(i2));
                        if (dns.IPs.Count == 0)
                        {
                            s.Arguments.Add("empty");
                        }
                        else
                        {
                            foreach (IpAddress ip in dns.IPs)
                            {
                                s.Arguments.Add(ip.Address);
                            }
                        }
                        s.Run();

                        Engine.Instance.Logs.Log(LogType.Verbose, MessagesFormatter.Format(Messages.NetworkAdapterDnsDone, i2, ((current.Count == 0) ? "Automatic" : current.Addresses), dns.Addresses));
                    }
                }

                Recovery.Save();
            }

            base.OnDnsSwitchDo(dns);

            return(true);
        }
Exemplo n.º 5
0
        public override bool OnDnsSwitchDo(string dns)
        {
            string mode = Engine.Instance.Storage.GetLower("dns.mode");

            if (mode == "auto")
            {
                string[] interfaces = GetInterfaces();
                foreach (string i in interfaces)
                {
                    string i2 = i.Trim();

                    string current = ShellCmd("networksetup -getdnsservers \"" + i2 + "\"");

                    // v2
                    List <string> ips = new List <string>();
                    foreach (string line in current.Split('\n'))
                    {
                        string ip = line.Trim();
                        if (IpAddress.IsIP(ip))
                        {
                            ips.Add(ip);
                        }
                    }

                    if (ips.Count != 0)
                    {
                        current = String.Join(",", ips.ToArray());
                    }
                    else
                    {
                        current = "";
                    }
                    if (current != dns)
                    {
                        // Switch
                        Engine.Instance.Logs.Log(LogType.Verbose, Messages.Format(Messages.NetworkAdapterDnsDone, i2, ((current == "") ? "Automatic" : current), dns));

                        DnsSwitchEntry e = new DnsSwitchEntry();
                        e.Name = i2;
                        e.Dns  = current;
                        m_listDnsSwitch.Add(e);

                        string dns2 = dns.Replace(",", "\" \"");
                        ShellCmd("networksetup -setdnsservers \"" + i2 + "\" \"" + dns2 + "\"");
                    }
                }

                Recovery.Save();
            }

            base.OnDnsSwitchDo(dns);

            return(true);
        }
Exemplo n.º 6
0
 public static bool IsIP(string v)
 {
     if (v == "On-link")
     {
         return(true);
     }
     if (IpAddress.IsIP(v))
     {
         return(true);
     }
     return(false);
 }
Exemplo n.º 7
0
        private void ParseIP()
        {
            string _buffer = (string)pars.GetPar("ip").argValues[0];

            if (!IpAddress.IsIP(_buffer))
            {
                Logger.Log("SNMP device Request has problems with Parsing IPAddress", Logger.MessageType.ERROR);
                throw new ArgumentException("Can't parse IP Address");
            }
            else
            {
                _targetIP = IPAddress.Parse(_buffer);
            }
        }
Exemplo n.º 8
0
        public static XmlDocument FetchUrls(string title, string authPublicKey, List <string> urls, Dictionary <string, string> parameters)
        {
            parameters["login"]    = Engine.Instance.Storage.Get("login");
            parameters["password"] = Engine.Instance.Storage.Get("password");
            parameters["system"]   = Platform.Instance.GetSystemCode();
            parameters["version"]  = Constants.VersionInt.ToString(CultureInfo.InvariantCulture);

            string firstError = "";
            int    hostN      = 0;

            foreach (string url in urls)
            {
                string host = UtilsCore.HostFromUrl(url);

                hostN++;
                if (IpAddress.IsIP(host) == false)
                {
                    // If locked network are enabled, skip the hostname and try only by IP.
                    // To avoid DNS issue (generally, to avoid losing time).
                    if (Engine.Instance.NetworkLockManager.IsDnsResolutionAvailable(host) == false)
                    {
                        continue;
                    }
                }

                try
                {
                    RouteScope  routeScope = new RouteScope(host);
                    XmlDocument xmlDoc     = FetchUrl(authPublicKey, url, parameters);
                    routeScope.End();
                    if (xmlDoc == null)
                    {
                        throw new Exception("No answer.");
                    }

                    if (xmlDoc.DocumentElement.Attributes["error"] != null)
                    {
                        throw new Exception(xmlDoc.DocumentElement.Attributes["error"].Value);
                    }

                    return(xmlDoc);
                }
                catch (Exception e)
                {
                    string info      = e.Message;
                    string proxyMode = Engine.Instance.Storage.Get("proxy.mode").ToLowerInvariant();
                    string proxyWhen = Engine.Instance.Storage.Get("proxy.when").ToLowerInvariant();
                    string proxyAuth = Engine.Instance.Storage.Get("proxy.auth").ToLowerInvariant();
                    if (proxyMode != "none")
                    {
                        info += " - with '" + proxyMode + "' (" + proxyWhen + ") proxy and '" + proxyAuth + "' auth";
                    }

                    if (Engine.Instance.Storage.GetBool("advanced.expert"))
                    {
                        Engine.Instance.Logs.Log(LogType.Verbose, MessagesFormatter.Format(Messages.ExchangeTryFailed, title, hostN.ToString(), info));
                    }

                    if (firstError == "")
                    {
                        firstError = info;
                    }
                }
            }

            throw new Exception(firstError);
        }
Exemplo n.º 9
0
        private void btnAddIP_Click(object sender, RoutedEventArgs e)
        {
            string    strIpAddr = tbAddIP.Text.Trim();
            IpAddress ipAddr, ipMask, ipGateWay;
            string    strIpMask    = tbAddMask.Text.Trim();
            string    strIpGateWay = tbAddGateway.Text.Trim();
            string    ipName       = tbAddIPName.Text.Trim();
            string    ipEquipName  = cbbAddIPEquipName.Text;

            if (ipEquipName.Equals("选择设备"))
            {
                MessageBox.Show("请选择一个设备");
                return;
            }
            if (!IpAddress.IsIP(strIpAddr))
            {
                MessageBox.Show("地址格式不正确");
                return;
            }
            ipAddr = new IpAddress(strIpAddr);
            if (string.IsNullOrEmpty(strIpMask))
            {
                ipMask = null;
            }
            else
            {
                if (!IpAddress.IsIP(strIpMask))
                {
                    MessageBox.Show("掩码应是一个IP地址格式");
                    return;
                }
                ipMask = new IpAddress(strIpMask);
                if (!ipMask.IsValidMask())
                {
                    MessageBox.Show("掩码格式不正确");
                    return;
                }
            }
            if (string.IsNullOrEmpty(strIpGateWay))
            {
                ipGateWay = null;
            }
            else
            {
                if (!IpAddress.IsIP(strIpGateWay))
                {
                    MessageBox.Show("网关格式错误");
                    return;
                }
                ipGateWay = new IpAddress(strIpGateWay);
            }
            string getIDSql = "SELECT Equip_Index FROM Equipments WHERE Equip_Name = '" + ipEquipName + "'";
            int    equipID  = Convert.ToInt32(App.DBHelper.returnScalar(getIDSql));

            if (equipID <= 0)
            {
                MessageBox.Show("数据库中未找到相应的设备号");
                return;
            }
            string insertSql = string.Format("INSERT INTO IPAddress(IP_Address, IP_EquipID, IP_Mask, IP_GateWay, IP_Name) VALUES('{0}',{1},'{2}','{3}','{4}')", strIpAddr, equipID, strIpMask, strIpGateWay, ipName);

            if (!App.DBHelper.ExecuteReturnBool(insertSql))
            {
                MessageBox.Show("插入IP失败");
                return;
            }
            IPInfor4DB ipInfo     = new IPInfor4DB(ipAddr, equipID, ipEquipName, ipMask, ipGateWay, ipName, false);
            string     getipIDSql = string.Format("SELECT IP_Index FROM IPAddress WHERE  IP_EquipID = {0} AND IP_Address = '{1}'", equipID, strIpAddr);

            ipInfo.IpIndex = Convert.ToInt32(App.DBHelper.returnScalar(getipIDSql));
            ipInformationList.Add(ipInfo);
            tbAddIP.Text      = "";
            tbAddIPName.Text  = "";
            tbAddMask.Text    = "";
            tbAddGateway.Text = "";
        }
Exemplo n.º 10
0
        void ReadOptions()
        {
            Storage s = Engine.Instance.Storage;

            // General

            GuiUtils.SetCheck(ChkConnect, s.GetBool("connect"));
            GuiUtils.SetCheck(ChkNetLock, s.GetBool("netlock"));

            GuiUtils.SetCheck(ChkGeneralStartLast, s.GetBool("servers.startlast"));
            GuiUtils.SetCheck(ChkGeneralOsxVisible, s.GetBool("gui.osx.visible"));
            // GuiUtils.SetCheck (ChkGeneralOsxDock, s.GetBool ("gui.osx.dock")); // See this FAQ: https://airvpn.org/topic/13331-its-possible-to-hide-the-icon-in-dock-bar-under-os-x/
            GuiUtils.SetCheck(ChkGeneralOsxNotifications, s.GetBool("gui.osx.notifications"));
            GuiUtils.SetCheck(ChkUiSystemBarShowInfo, s.GetBool("gui.osx.sysbar.show_info"));
            GuiUtils.SetCheck(ChkUiSystemBarShowSpeed, s.GetBool("gui.osx.sysbar.show_speed"));
            GuiUtils.SetCheck(ChkUiSystemBarShowServer, s.GetBool("gui.osx.sysbar.show_server"));

            GuiUtils.SetCheck(ChkExitConfirm, s.GetBool("gui.exit_confirm"));

            // UI
            string uiUnit = s.Get("ui.unit");

            if (uiUnit == "bytes")
            {
                GuiUtils.SetSelected(CboUiUnit, Messages.WindowsSettingsUiUnit1);
            }
            else if (uiUnit == "bits")
            {
                GuiUtils.SetSelected(CboUiUnit, Messages.WindowsSettingsUiUnit2);
            }
            else
            {
                GuiUtils.SetSelected(CboUiUnit, Messages.WindowsSettingsUiUnit0);
            }
            GuiUtils.SetCheck(ChkUiIEC, s.GetBool("ui.iec"));

            /*
             * string interfaceMode = GuiUtils.InterfaceColorMode ();
             * if (interfaceMode == "Dark")
             *      GuiUtils.SetSelected (CboGeneralOsxInterfaceStyle,"Dark");
             * else
             *      GuiUtils.SetSelected (CboGeneralOsxInterfaceStyle,"Default");
             */

            // Protocols
            String protocol = s.Get("mode.protocol").ToUpperInvariant();
            int    port     = s.GetInt("mode.port");
            int    entryIP  = s.GetInt("mode.alt");

            if (protocol == "AUTO")
            {
                GuiUtils.SetCheck(ChkProtocolsAutomatic, true);
            }
            else
            {
                bool found = false;

                int iRow = 0;
                foreach (TableProtocolsControllerItem itemProtocol in TableProtocolsController.Items)
                {
                    if ((itemProtocol.Protocol == protocol) &&
                        (itemProtocol.Port == port) &&
                        (itemProtocol.IP == entryIP))
                    {
                        found = true;
                        TableProtocols.SelectRow(iRow, false);
                        TableProtocols.ScrollRowToVisible(iRow);
                        break;
                    }
                    iRow++;
                }

                if (found == false)
                {
                    GuiUtils.SetCheck(ChkProtocolsAutomatic, true);
                }
                else
                {
                    GuiUtils.SetCheck(ChkProtocolsAutomatic, false);
                }
            }

            // Proxy

            GuiUtils.SetSelected(CboProxyType, s.Get("proxy.mode"));
            if (s.Get("proxy.when") == "always")
            {
                GuiUtils.SetSelected(CboProxyWhen, Messages.WindowsSettingsProxyWhenAlways);
            }
            else if (s.Get("proxy.when") == "web")
            {
                GuiUtils.SetSelected(CboProxyWhen, Messages.WindowsSettingsProxyWhenWeb);
            }
            else if (s.Get("proxy.when") == "openvpn")
            {
                GuiUtils.SetSelected(CboProxyWhen, Messages.WindowsSettingsProxyWhenOpenVPN);
            }
            else if (s.Get("proxy.when") == "none")
            {
                GuiUtils.SetSelected(CboProxyWhen, Messages.WindowsSettingsProxyWhenNone);
            }
            else
            {
                GuiUtils.SetSelected(CboProxyWhen, Messages.WindowsSettingsProxyWhenAlways);
            }
            TxtProxyHost.StringValue = s.Get("proxy.host");
            TxtProxyPort.StringValue = s.Get("proxy.port");
            GuiUtils.SetSelected(CboProxyAuthentication, s.Get("proxy.auth"));
            TxtProxyLogin.StringValue              = s.Get("proxy.login");
            TxtProxyPassword.StringValue           = s.Get("proxy.password");
            TxtProxyTorControlPort.StringValue     = s.Get("proxy.tor.control.port");
            TxtProxyTorControlPassword.StringValue = s.Get("proxy.tor.control.password");

            // Routes
            GuiUtils.SetSelected(CboRoutesOtherwise, RouteDirectionToDescription(s.Get("routes.default")));

            string routes = s.Get("routes.custom");

            string[] routes2 = routes.Split(';');
            foreach (string route in routes2)
            {
                string[] routeEntries = route.Split(',');
                if (routeEntries.Length < 2)
                {
                    continue;
                }

                TableRoutingControllerItem item = new TableRoutingControllerItem();
                item.Ip     = routeEntries[0];
                item.Action = routeEntries[1];
                item.Icon   = routeEntries[1];
                if (routeEntries.Length == 3)
                {
                    item.Notes = routeEntries[2];
                }
                TableRoutingController.Items.Add(item);
            }

            TableRoutingController.RefreshUI();

            // Advanced - General

            GuiUtils.SetCheck(ChkAdvancedExpertMode, s.GetBool("advanced.expert"));
            GuiUtils.SetCheck(ChkAdvancedCheckRoute, s.GetBool("advanced.check.route"));
            string ipV6Mode = s.Get("ipv6.mode");

            if (ipV6Mode == "none")
            {
                GuiUtils.SetSelected(CboIpV6, "None");
            }
            else if (ipV6Mode == "disable")
            {
                GuiUtils.SetSelected(CboIpV6, "Disable");
            }
            else
            {
                GuiUtils.SetSelected(CboIpV6, "None");
            }

            GuiUtils.SetCheck(ChkAdvancedPingerEnabled, s.GetBool("pinger.enabled"));
            GuiUtils.SetCheck(ChkRouteRemoveDefaultGateway, s.GetBool("routes.remove_default"));

            TxtAdvancedOpenVpnPath.StringValue = s.Get("tools.openvpn.path");
            GuiUtils.SetCheck(ChkAdvancedSkipAlreadyRun, s.GetBool("advanced.skip_alreadyrun"));
            GuiUtils.SetCheck(ChkAdvancedProviders, s.GetBool("advanced.providers"));

            int manifestRefresh = s.GetInt("advanced.manifest.refresh");

            if (manifestRefresh == 60)
            {
                GuiUtils.SetSelected(CboAdvancedManifestRefresh, "Every one hour");
            }
            else if (manifestRefresh == 10)
            {
                GuiUtils.SetSelected(CboAdvancedManifestRefresh, "Every ten minute");
            }
            else if (manifestRefresh == 1)
            {
                GuiUtils.SetSelected(CboAdvancedManifestRefresh, "Every minute");
            }
            else if (manifestRefresh == 0)
            {
                GuiUtils.SetSelected(CboAdvancedManifestRefresh, "Never");
            }
            else
            {
                GuiUtils.SetSelected(CboAdvancedManifestRefresh, "Automatic");
            }

            int openVpnSndBuf = s.GetInt("openvpn.sndbuf");

            if (openVpnSndBuf == -2)
            {
                GuiUtils.SetSelected(CboOpenVpnSndBuf, Messages.Automatic);
            }
            else if (openVpnSndBuf == -1)
            {
                GuiUtils.SetSelected(CboOpenVpnSndBuf, Messages.WindowsSettingsOpenVpnDefault);
            }
            else if (openVpnSndBuf == 1024 * 8)
            {
                GuiUtils.SetSelected(CboOpenVpnSndBuf, "8 KB");
            }
            else if (openVpnSndBuf == 1024 * 16)
            {
                GuiUtils.SetSelected(CboOpenVpnSndBuf, "16 KB");
            }
            else if (openVpnSndBuf == 1024 * 32)
            {
                GuiUtils.SetSelected(CboOpenVpnSndBuf, "32 KB");
            }
            else if (openVpnSndBuf == 1024 * 64)
            {
                GuiUtils.SetSelected(CboOpenVpnSndBuf, "64 KB");
            }
            else if (openVpnSndBuf == 1024 * 128)
            {
                GuiUtils.SetSelected(CboOpenVpnSndBuf, "128 KB");
            }
            else if (openVpnSndBuf == 1024 * 256)
            {
                GuiUtils.SetSelected(CboOpenVpnSndBuf, "256 KB");
            }
            else if (openVpnSndBuf == 1024 * 512)
            {
                GuiUtils.SetSelected(CboOpenVpnSndBuf, "512 KB");
            }

            int openVpnRcvBuf = s.GetInt("openvpn.rcvbuf");

            if (openVpnRcvBuf == -2)
            {
                GuiUtils.SetSelected(CboOpenVpnRcvBuf, Messages.Automatic);
            }
            else if (openVpnRcvBuf == -1)
            {
                GuiUtils.SetSelected(CboOpenVpnRcvBuf, Messages.WindowsSettingsOpenVpnDefault);
            }
            else if (openVpnRcvBuf == 1024 * 8)
            {
                GuiUtils.SetSelected(CboOpenVpnRcvBuf, "8 KB");
            }
            else if (openVpnRcvBuf == 1024 * 16)
            {
                GuiUtils.SetSelected(CboOpenVpnRcvBuf, "16 KB");
            }
            else if (openVpnRcvBuf == 1024 * 32)
            {
                GuiUtils.SetSelected(CboOpenVpnRcvBuf, "32 KB");
            }
            else if (openVpnRcvBuf == 1024 * 64)
            {
                GuiUtils.SetSelected(CboOpenVpnRcvBuf, "64 KB");
            }
            else if (openVpnRcvBuf == 1024 * 128)
            {
                GuiUtils.SetSelected(CboOpenVpnRcvBuf, "128 KB");
            }
            else if (openVpnRcvBuf == 1024 * 256)
            {
                GuiUtils.SetSelected(CboOpenVpnRcvBuf, "256 KB");
            }
            else if (openVpnRcvBuf == 1024 * 512)
            {
                GuiUtils.SetSelected(CboOpenVpnRcvBuf, "512 KB");
            }

            // Advanced - DNS

            string dnsMode = s.Get("dns.mode");

            if (dnsMode == "none")
            {
                GuiUtils.SetSelected(CboDnsSwitchMode, "Disabled");
            }
            else
            {
                GuiUtils.SetSelected(CboDnsSwitchMode, "Automatic");
            }

            GuiUtils.SetCheck(ChkDnsCheck, s.GetBool("dns.check"));

            TableDnsServersController.Clear();
            string[] dnsServers = s.Get("dns.servers").Split(',');
            foreach (string dnsServer in dnsServers)
            {
                if (IpAddress.IsIP(dnsServer))
                {
                    TableDnsServersController.Add(dnsServer);
                }
            }

            // Advanced - Lock
            string lockMode = s.Get("netlock.mode");

            GuiUtils.SetSelected(CboLockMode, "None");
            if (lockMode == "auto")
            {
                GuiUtils.SetSelected(CboLockMode, "Automatic");
            }
            else
            {
                foreach (NetworkLockPlugin lockPlugin in Engine.Instance.NetworkLockManager.Modes)
                {
                    if (lockPlugin.GetCode() == lockMode)
                    {
                        GuiUtils.SetSelected(CboLockMode, lockPlugin.GetName());
                    }
                }
            }
            GuiUtils.SetCheck(ChkLockAllowPrivate, s.GetBool("netlock.allow_private"));
            GuiUtils.SetCheck(ChkLockAllowPing, s.GetBool("netlock.allow_ping"));
            GuiUtils.SetCheck(ChkLockAllowDNS, s.GetBool("netlock.allow_dns"));
            TxtLockAllowedIPS.StringValue = s.Get("netlock.allowed_ips");

            // Advanced - Logging
            GuiUtils.SetCheck(ChkLoggingEnabled, s.GetBool("log.file.enabled"));
            GuiUtils.SetCheck(ChkLogLevelDebug, s.GetBool("log.level.debug"));
            TxtLoggingPath.StringValue = s.Get("log.file.path");

            // Advanced - OVPN Directives
            GuiUtils.SetSelected(CboOpenVpnDirectivesSkipDefault, (s.GetBool("openvpn.skip_defaults") ? Messages.WindowsSettingsOpenVpnDirectivesDefaultSkip2 : Messages.WindowsSettingsOpenVpnDirectivesDefaultSkip1));
            TxtAdvancedOpenVpnDirectivesDefault.StringValue = s.Get("openvpn.directives");
            TxtAdvancedOpenVpnDirectivesCustom.StringValue  = s.Get("openvpn.custom");
            TxtOpenVpnDirectivesCustomPath.StringValue      = s.Get("openvpn.directives.path");

            // Advanced - Events
            ReadOptionsEvent("app.start", 0);
            ReadOptionsEvent("app.stop", 1);
            ReadOptionsEvent("session.start", 2);
            ReadOptionsEvent("session.stop", 3);
            ReadOptionsEvent("vpn.pre", 4);
            ReadOptionsEvent("vpn.up", 5);
            ReadOptionsEvent("vpn.down", 6);

            TableAdvancedEventsController.RefreshUI();
        }
Exemplo n.º 11
0
        private void cbbSelectEquip_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Equipment equip     = (sender as ComboBox).SelectedItem as Equipment;
            string    equipName = equip.Name;
            int       equipID   = equip.Index;
            IpAddress agentIP   = null;

            try
            {
                object strIP = App.DBHelper.returnScalar(string.Format("SELECT IP_Address FROM IPAddress WHERE(IP_EquipID = {0}) AND (IP_IsDefaultIP = 1)", equipID));
                if (strIP == null)
                {
                    MessageBox.Show("数据库中没有" + equipName + "的默认管理IP地址");
                    return;
                }
                if (IpAddress.IsIP(strIP.ToString()))
                {
                    agentIP = new IpAddress(strIP.ToString());
                }
                else
                {
                    MessageBox.Show("地址格式错误");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("读取数据库出现错误\n" + e.ToString());
            }

            OctetString     community = new OctetString("public");
            AgentParameters param     = new AgentParameters(community);

            // Set SNMP version to 2 (GET-BULK only works with SNMP ver 2 and 3)
            param.Version = SnmpVersion.Ver2;
            // Construct target
            UdpTarget target = new UdpTarget((IPAddress)agentIP, 161, 2000, 1);

            // Define Oid that is the root of the MIB
            //  tree you wish to retrieve
            Oid rootOid1 = new Oid("1.3.6.1.2.1.2.2.1.1"); // ifIndex
            Oid rootOid2 = new Oid("1.3.6.1.2.1.2.2.1.2"); // ifDescr

            // This Oid represents last Oid returned by
            //  the SNMP agent
            Oid lastOid1 = (Oid)rootOid1.Clone();
            Oid lastOid2 = (Oid)rootOid2.Clone();

            // Pdu class used for all requests
            Pdu pdu = new Pdu(PduType.GetBulk);

            // In this example, set NonRepeaters value to 0
            pdu.NonRepeaters = 0;
            // MaxRepetitions tells the agent how many Oid/Value pairs to return
            // in the response.
            pdu.MaxRepetitions = 5;

            // Loop through results
            while (lastOid1 != null && lastOid2 != null)
            {
                // When Pdu class is first constructed, RequestId is set to 0
                // and during encoding id will be set to the random value
                // for subsequent requests, id will be set to a value that
                // needs to be incremented to have unique request ids for each
                // packet
                if (pdu.RequestId != 0)
                {
                    pdu.RequestId += 1;
                }
                // Clear Oids from the Pdu class.
                pdu.VbList.Clear();
                // Initialize request PDU with the last retrieved Oid
                pdu.VbList.Add(lastOid1);
                pdu.VbList.Add(lastOid2);
                // Make SNMP request
                SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param);
                // You should catch exceptions in the Request if using in real application.

                // If result is null then agent didn't reply or we couldn't parse the reply.
                if (result != null)
                {
                    // ErrorStatus other then 0 is an error returned by
                    // the Agent - see SnmpConstants for error definitions
                    if (result.Pdu.ErrorStatus != 0)
                    {
                        // agent reported an error with the request
                        Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                                          result.Pdu.ErrorStatus,
                                          result.Pdu.ErrorIndex);
                        MessageBox.Show(string.Format("SNMP应答数据包中有错误。 Error {0} index {1}", result.Pdu.ErrorStatus, result.Pdu.ErrorIndex));
                        lastOid1 = null;
                        lastOid2 = null;
                        break;
                    }
                    else
                    {
                        // Walk through returned variable bindings
                        foreach (Vb v in result.Pdu.VbList)
                        {
                            // Check that retrieved Oid is "child" of the root OID
                            if (rootOid1.IsRootOf(v.Oid))
                            {
                                if (v.Value.Type == SnmpConstants.SMI_ENDOFMIBVIEW)
                                {
                                    lastOid1 = null;
                                }
                                else
                                {
                                    lastOid1 = v.Oid;
                                }
                                var f = v.Value;
                                //Interface interface = new Interface(v.Value.,)
                            }
                            else if (rootOid2.IsRootOf(v.Oid))
                            {
                                if (v.Value.Type == SnmpConstants.SMI_ENDOFMIBVIEW)
                                {
                                    lastOid2 = null;
                                }
                                else
                                {
                                    lastOid2 = v.Oid;
                                }
                                var f = v.Value;
                            }
                            else
                            {
                                // we have reached the end of the requested
                                // MIB tree. Set lastOid to null and exit loop
                                lastOid1 = null;
                                lastOid2 = null;
                            }



//                             if (rootOid.IsRootOf(v.Oid))
//                             {
//                                 Console.WriteLine("{0} ({1}): {2}",
//                                     v.Oid.ToString(),
//                                     SnmpConstants.GetTypeName(v.Value.Type),
//                                     v.Value.ToString());
//                                 if (v.Value.Type == SnmpConstants.SMI_ENDOFMIBVIEW)
//                                     lastOid = null;
//                                 else
//                                     lastOid = v.Oid;
//                             }
//                             else
//                             {
//                                 // we have reached the end of the requested
//                                 // MIB tree. Set lastOid to null and exit loop
//                                 lastOid = null;
//                           }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("No response received from SNMP agent.");
                }
            }
            target.Close();
        }
Exemplo n.º 12
0
        public void ReadOptions()
        {
            Storage s = Engine.Instance.Storage;

            // General
            chkConnect.Checked             = s.GetBool("connect");
            chkNetLock.Checked             = s.GetBool("netlock");
            chkUiMinimizeStart.Checked     = s.GetBool("gui.windows.start_minimized");
            chkUiMinimizeTray.Checked      = s.GetBool("gui.windows.tray");
            chkSystemNotifications.Checked = s.GetBool("gui.windows.notifications");
            chkGeneralStartLast.Checked    = s.GetBool("servers.startlast");
            chkExitConfirm.Checked         = s.GetBool("gui.exit_confirm");
            chkOsSingleInstance.Checked    = s.GetBool("os.single_instance");

            // Ui
            string uiUnit = s.Get("ui.unit");

            if (uiUnit == "bytes")
            {
                cboUiUnit.SelectedIndex = 1;
            }
            else if (uiUnit == "bits")
            {
                cboUiUnit.SelectedIndex = 2;
            }
            else
            {
                cboUiUnit.SelectedIndex = 0;
            }

            if (s.Get("gui.font.normal.name") != "")
            {
                chkUiFontGeneral.Checked = true;
                lblUiFontGeneral.Text    = s.Get("gui.font.normal.name") + ", " + s.GetFloat("gui.font.normal.size").ToString();
            }
            else
            {
                chkUiFontGeneral.Checked = false;
            }
            UpdateUiFontGeneral();

            // Protocol
            String protocol  = s.Get("mode.protocol").ToUpperInvariant();
            int    port      = s.GetInt("mode.port");
            int    alternate = s.GetInt("mode.alt");

            if (protocol == "AUTO")
            {
                chkProtocolsAutomatic.Checked = true;
            }
            else
            {
                bool found = false;

                foreach (Controls.ListViewItemProtocol itemProtocol in lstProtocols.Items)
                {
                    if ((itemProtocol.Protocol == protocol) &&
                        (itemProtocol.Port == port) &&
                        (itemProtocol.Entry == alternate))
                    {
                        found = true;
                        itemProtocol.Selected = true;
                        lstProtocols.EnsureVisible(itemProtocol.Index);
                        break;
                    }
                }

                if (found == false)
                {
                    chkProtocolsAutomatic.Checked = true;
                }
                else
                {
                    chkProtocolsAutomatic.Checked = false;
                }
            }

            // Proxy
            cboProxyMode.Text               = s.Get("proxy.mode");
            txtProxyHost.Text               = s.Get("proxy.host");
            txtProxyPort.Text               = s.Get("proxy.port");
            cboProxyAuthentication.Text     = s.Get("proxy.auth");
            txtProxyLogin.Text              = s.Get("proxy.login");
            txtProxyPassword.Text           = s.Get("proxy.password");
            txtProxyTorControlPort.Text     = s.Get("proxy.tor.control.port");
            txtProxyTorControlPassword.Text = s.Get("proxy.tor.control.password");


            // Routes
            cboRoutesOtherwise.Text = RouteDirectionToDescription(s.Get("routes.default"));

            string routes = s.Get("routes.custom");

            String[] routes2 = routes.Split(';');
            foreach (String route in routes2)
            {
                String[] routeEntries = route.Split(',');
                if (routeEntries.Length < 2)
                {
                    continue;
                }

                string ip     = routeEntries[0];
                string action = routeEntries[1];
                string notes  = "";
                if (routeEntries.Length == 3)
                {
                    notes = routeEntries[2];
                }

                ListViewItem item = new ListViewItem();
                item.Text = ip;
                item.SubItems.Add(RouteDirectionToDescription(action));
                item.SubItems.Add(notes);
                item.ImageKey = action;
                lstRoutes.Items.Add(item);
            }

            // Advanced - General
            chkExpert.Checked             = s.GetBool("advanced.expert");
            chkAdvancedCheckRoute.Checked = s.GetBool("advanced.check.route");

            string ipV6 = s.Get("ipv6.mode");

            if (ipV6 == "none")
            {
                cboIpV6.Text = "None";
            }
            else if (ipV6 == "disable")
            {
                cboIpV6.Text = "Disable";
            }
            else
            {
                cboIpV6.Text = "None";
            }


            chkAdvancedPingerEnabled.Checked = s.GetBool("pinger.enabled");
            chkRouteRemoveDefault.Checked    = s.GetBool("routes.remove_default");

            chkWindowsTapUp.Checked                 = s.GetBool("windows.tap_up");
            chkWindowsDhcpSwitch.Checked            = s.GetBool("windows.dhcp_disable");
            chkWindowsDisableDriverUpgrade.Checked  = s.GetBool("windows.disable_driver_upgrade");
            chkWindowsIPv6DisableAtOs.Checked       = s.GetBool("windows.ipv6.os_disable");
            chkWindowsDnsForceAllInterfaces.Checked = s.GetBool("windows.dns.force_all_interfaces");
            chkWindowsDnsLock.Checked               = s.GetBool("windows.dns.lock");
            chkWindowsWfp.Checked = s.GetBool("windows.wfp");

            txtExePath.Text = s.Get("executables.openvpn");

            int manifestRefresh = s.GetInt("advanced.manifest.refresh");

            if (manifestRefresh == 60)
            {
                cboAdvancedManifestRefresh.SelectedIndex = 4;
            }
            else if (manifestRefresh == 10)
            {
                cboAdvancedManifestRefresh.SelectedIndex = 3;
            }
            else if (manifestRefresh == 1)
            {
                cboAdvancedManifestRefresh.SelectedIndex = 2;
            }
            else if (manifestRefresh == 0)
            {
                cboAdvancedManifestRefresh.SelectedIndex = 1;
            }
            else
            {
                cboAdvancedManifestRefresh.SelectedIndex = 0;
            }

            int openVpnSndBuf = s.GetInt("openvpn.sndbuf");

            if (openVpnSndBuf == -2)
            {
                cboOpenVpnSndbuf.SelectedIndex = 0;
            }
            else if (openVpnSndBuf == -1)
            {
                cboOpenVpnSndbuf.SelectedIndex = 1;
            }
            else if (openVpnSndBuf == 1024 * 8)
            {
                cboOpenVpnSndbuf.SelectedIndex = 2;
            }
            else if (openVpnSndBuf == 1024 * 16)
            {
                cboOpenVpnSndbuf.SelectedIndex = 3;
            }
            else if (openVpnSndBuf == 1024 * 32)
            {
                cboOpenVpnSndbuf.SelectedIndex = 4;
            }
            else if (openVpnSndBuf == 1024 * 64)
            {
                cboOpenVpnSndbuf.SelectedIndex = 5;
            }
            else if (openVpnSndBuf == 1024 * 128)
            {
                cboOpenVpnSndbuf.SelectedIndex = 6;
            }
            else if (openVpnSndBuf == 1024 * 256)
            {
                cboOpenVpnSndbuf.SelectedIndex = 7;
            }
            else if (openVpnSndBuf == 1024 * 512)
            {
                cboOpenVpnSndbuf.SelectedIndex = 8;
            }

            int openVpnRcvBuf = s.GetInt("openvpn.rcvbuf");

            if (openVpnRcvBuf == -2)
            {
                cboOpenVpnRcvbuf.SelectedIndex = 0;
            }
            else if (openVpnRcvBuf == -1)
            {
                cboOpenVpnRcvbuf.SelectedIndex = 1;
            }
            else if (openVpnRcvBuf == 1024 * 8)
            {
                cboOpenVpnRcvbuf.SelectedIndex = 2;
            }
            else if (openVpnRcvBuf == 1024 * 16)
            {
                cboOpenVpnRcvbuf.SelectedIndex = 3;
            }
            else if (openVpnRcvBuf == 1024 * 32)
            {
                cboOpenVpnRcvbuf.SelectedIndex = 4;
            }
            else if (openVpnRcvBuf == 1024 * 64)
            {
                cboOpenVpnRcvbuf.SelectedIndex = 5;
            }
            else if (openVpnRcvBuf == 1024 * 128)
            {
                cboOpenVpnRcvbuf.SelectedIndex = 6;
            }
            else if (openVpnRcvBuf == 1024 * 256)
            {
                cboOpenVpnRcvbuf.SelectedIndex = 7;
            }
            else if (openVpnRcvBuf == 1024 * 512)
            {
                cboOpenVpnRcvbuf.SelectedIndex = 8;
            }

            // Advanced - DNS
            cboDnsSwitchMode.Text = s.Get("dns.mode");
            string dnsMode = s.Get("dns.mode");

            if (dnsMode == "none")
            {
                cboDnsSwitchMode.Text = "Disabled";
            }
            else if (dnsMode == "auto")
            {
                cboDnsSwitchMode.Text = "Automatic";
            }
            else if (dnsMode == "resolvconf")
            {
                cboDnsSwitchMode.Text = "Resolvconf (Linux only)";
            }
            else if (dnsMode == "rename")
            {
                cboDnsSwitchMode.Text = "Renaming (Linux only)";
            }
            else
            {
                cboDnsSwitchMode.Text = "Automatic";
            }

            chkDnsCheck.Checked = s.GetBool("dns.check");

            lstDnsServers.Items.Clear();
            string[] dnsServers = s.Get("dns.servers").Split(',');
            foreach (string dnsServer in dnsServers)
            {
                if (IpAddress.IsIP(dnsServer))
                {
                    lstDnsServers.Items.Add(new ListViewItem(dnsServer));
                }
            }

            // Advanced - Lock
            string lockMode = s.Get("netlock.mode");

            cboLockMode.Text = "None";
            if (lockMode == "auto")
            {
                cboLockMode.Text = "Automatic";
            }
            else
            {
                foreach (NetworkLockPlugin lockPlugin in Engine.Instance.NetworkLockManager.Modes)
                {
                    if (lockPlugin.GetCode() == lockMode)
                    {
                        cboLockMode.Text = lockPlugin.GetName();
                    }
                }
            }
            chkLockAllowPrivate.Checked = s.GetBool("netlock.allow_private");
            chkLockAllowPing.Checked    = s.GetBool("netlock.allow_ping");
            txtLockAllowedIPS.Text      = s.Get("netlock.allowed_ips");

            // Advanced - Logging
            chkLoggingEnabled.Checked = s.GetBool("log.file.enabled");
            txtLogPath.Text           = s.Get("log.file.path");
            chkLogLevelDebug.Checked  = s.GetBool("log.level.debug");

            // Advanced - OVPN Directives
            cboOpenVpnDirectivesDefaultSkip.SelectedIndex = (s.GetBool("openvpn.skip_defaults") ? 1:0);
            txtOpenVpnDirectivesBase.Text   = s.Get("openvpn.directives");
            txtOpenVpnDirectivesCustom.Text = s.Get("openvpn.custom");

            // Advanced - Events
            ReadOptionsEvent("app.start", 0);
            ReadOptionsEvent("app.stop", 1);
            ReadOptionsEvent("session.start", 2);
            ReadOptionsEvent("session.stop", 3);
            ReadOptionsEvent("vpn.pre", 4);
            ReadOptionsEvent("vpn.up", 5);
            ReadOptionsEvent("vpn.down", 6);

            chkSystemStart.Checked = Platform.Instance.GetAutoStart();
        }