Ejemplo n.º 1
0
        public void ScanDir(string path, bool recursive, List <ConnectionInfo> connections)
        {
            if (path == "")
            {
                return;
            }

            try
            {
                foreach (string filePath in Directory.GetFiles(path))
                {
                    FileInfo fileInfo = new FileInfo(filePath);

                    string extension = fileInfo.Extension.ToLowerInvariant().Trim();
                    if ((extension != ".ovpn") && (extension != ".conf"))
                    {
                        continue;
                    }

                    // Already listed?
                    XmlElement nodeProfile = null;
                    foreach (XmlElement nodeFind in Profiles.ChildNodes)
                    {
                        string pathFind = UtilsXml.XmlGetAttributeString(nodeFind, "path", "");
                        if (pathFind == fileInfo.FullName)
                        {
                            nodeProfile = nodeFind;
                            break;
                        }
                    }

                    // Skip if is already checked
                    if ((nodeProfile != null) && (UtilsXml.XmlGetAttributeString(nodeProfile, "checked", "") != ""))
                    {
                        continue;
                    }

                    if (Platform.Instance.FileExists(filePath) == false)
                    {
                        continue;
                    }

                    // Compute values
                    FileInfo file  = new FileInfo(filePath);
                    string   hosts = "";

                    try
                    {
                        string ovpnOriginal = Platform.Instance.FileContentsReadText(file.FullName);

                        OvpnBuilder ovpnBuilder = new OvpnBuilder();
                        ovpnBuilder.AppendDirectives(ovpnOriginal, "Original");
                        //string ovpnNormalized = ovpnBuilder.Get();

                        foreach (OvpnBuilder.Directive remoteDirective in ovpnBuilder.GetDirectiveList("remote"))
                        {
                            string host    = remoteDirective.Text;
                            int    posPort = host.IndexOf(" ");
                            if (posPort != -1)
                            {
                                host = host.Substring(0, posPort).Trim();
                            }
                            if (hosts != "")
                            {
                                hosts += ",";
                            }
                            hosts += host;
                        }

                        if (nodeProfile == null)
                        {
                            nodeProfile = Profiles.OwnerDocument.CreateElement("profile");
                            Profiles.AppendChild(nodeProfile);
                        }

                        UtilsXml.XmlSetAttributeString(nodeProfile, "remote", hosts);
                        UtilsXml.XmlSetAttributeString(nodeProfile, "path", file.FullName);

                        UtilsXml.XmlSetAttributeString(nodeProfile, "checked", "1");
                    }
                    catch (System.Exception e)
                    {
                        string message = MessagesFormatter.Format(Messages.ProvidersOpenVpnErrorProfile, file.FullName, this.Title, e.Message);                         // TOTRANSLATE
                        Engine.Instance.Logs.Log(LogType.Warning, message);
                    }
                }

                if (recursive)
                {
                    foreach (string dirPath in Directory.GetDirectories(path))
                    {
                        ScanDir(dirPath, recursive, connections);
                    }
                }
            }
            catch (System.Exception e)
            {
                Engine.Instance.Logs.Log(e);
            }
        }
Ejemplo n.º 2
0
        public override void OnBuildOvpnDefaults(OvpnBuilder ovpn)
        {
            base.OnBuildOvpnDefaults(ovpn);

            ovpn.AppendDirectives(Manifest.Attributes["openvpn_directives"].Value.Replace("\t", "").Trim(), "Provider level");
        }
Ejemplo n.º 3
0
        public override void OnBuildOvpnDefaults(OvpnBuilder ovpn)
        {
            base.OnBuildOvpnDefaults(ovpn);

            ovpn.AppendDirectives(Manifest.Attributes["openvpn_directives"].Value.Replace("\t", "").Trim(), "Provider level");
        }
Ejemplo n.º 4
0
        public override void OnBuildConnectionActive(ConnectionInfo connection, ConnectionActive connectionActive)
        {
            base.OnBuildConnectionActive(connection, connectionActive);

            OvpnBuilder    ovpn = connectionActive.OpenVpnProfileStartup;
            ConnectionMode mode = GetMode();

            if (mode.Protocol == "SSH")
            {
                connectionActive.SshLocalPort       = Engine.Instance.Storage.GetInt("ssh.port");
                connectionActive.SshRemotePort      = mode.Port;
                connectionActive.SshPortDestination = mode.SshPortDestination;
                if (connectionActive.SshLocalPort == 0)
                {
                    connectionActive.SshLocalPort = RandomGenerator.GetInt(1024, 64 * 1024);
                }
            }
            else if (mode.Protocol == "SSL")
            {
                connectionActive.SslLocalPort  = Engine.Instance.Storage.GetInt("ssl.port");
                connectionActive.SslRemotePort = mode.Port;
                if (connectionActive.SslLocalPort == 0)
                {
                    connectionActive.SslLocalPort = RandomGenerator.GetInt(1024, 64 * 1024);
                }
            }

            {
                string modeDirectives    = mode.Directives;
                string paramUserTA       = "";
                string paramUserTlsCrypt = "";
                if (User != null)
                {
                    paramUserTA       = UtilsXml.XmlGetAttributeString(User, "ta", "");
                    paramUserTlsCrypt = UtilsXml.XmlGetAttributeString(User, "tls_crypt", "");
                }
                modeDirectives = modeDirectives.Replace("{@user-ta}", paramUserTA);
                modeDirectives = modeDirectives.Replace("{@user-tlscrypt}", paramUserTlsCrypt);
                ovpn.AppendDirectives(modeDirectives, "Mode level");
            }

            // Pick the IP
            IpAddress ip           = null;
            string    entryIpLayer = Engine.Instance.Storage.Get("network.entry.iplayer");

            if (entryIpLayer == "ipv6-ipv4")
            {
                ip = connection.IpsEntry.GetV6ByIndex(mode.EntryIndex);
                if (ip == null)
                {
                    ip = connection.IpsEntry.GetV4ByIndex(mode.EntryIndex);
                }
            }
            else if (entryIpLayer == "ipv4-ipv6")
            {
                ip = connection.IpsEntry.GetV4ByIndex(mode.EntryIndex);
                if (ip == null)
                {
                    ip = connection.IpsEntry.GetV6ByIndex(mode.EntryIndex);
                }
            }
            else if (entryIpLayer == "ipv6-only")
            {
                ip = connection.IpsEntry.GetV6ByIndex(mode.EntryIndex);
            }
            else if (entryIpLayer == "ipv4-only")
            {
                ip = connection.IpsEntry.GetV4ByIndex(mode.EntryIndex);
            }

            if (ip != null)
            {
                IpAddress remoteAddress = ip.Clone();
                int       remotePort    = mode.Port;

                if (mode.Protocol == "SSH")
                {
                    remoteAddress = "127.0.0.1";
                    remotePort    = connectionActive.SshLocalPort;
                }
                else if (mode.Protocol == "SSL")
                {
                    remoteAddress = "127.0.0.1";
                    remotePort    = connectionActive.SslLocalPort;
                }

                ovpn.AppendDirective("remote", remoteAddress.Address + " " + remotePort.ToString(), "");

                // Adjust the protocol
                OvpnBuilder.Directive dProto = ovpn.GetOneDirective("proto");
                if (dProto != null)
                {
                    dProto.Text = dProto.Text.ToLowerInvariant();
                    if (dProto.Text == "tcp")
                    {
                        if (remoteAddress.IsV6)
                        {
                            dProto.Text = "tcp6";
                        }
                    }
                    else if (dProto.Text == "udp")
                    {
                        if (remoteAddress.IsV6)
                        {
                            dProto.Text = "udp6";
                        }
                    }
                }

                if ((mode.Protocol == "SSH") || (mode.Protocol == "SSL"))
                {
                    if (Constants.FeatureIPv6ControlOptions)
                    {
                        if (((ip.IsV4) && (connectionActive.TunnelIPv4)) ||
                            ((ip.IsV6) && (connectionActive.TunnelIPv6)))
                        {
                            connectionActive.AddRoute(ip, "net_gateway", "VPN Entry IP");
                        }
                    }
                    else
                    {
                        string routesDefault = Engine.Instance.Storage.Get("routes.default");
                        if (routesDefault == "in")
                        {
                            connectionActive.AddRoute(ip, "net_gateway", "VPN Entry IP");
                        }
                    }
                }
            }

            connectionActive.Protocol = mode.Protocol;
            if (ip != null)
            {
                connectionActive.Address = ip.Clone();
            }
        }
Ejemplo n.º 5
0
        public override void OnBuildOvpn(ConnectionInfo connection, OvpnBuilder ovpn)
        {
            base.OnBuildOvpn(connection, ovpn);

            ConnectionMode mode = GetMode();

            int proxyPort = 0;

            if (mode.Protocol == "SSH")
            {
                proxyPort = Engine.Instance.Storage.GetInt("ssh.port");
                if (proxyPort == 0)
                {
                    proxyPort = RandomGenerator.GetInt(1024, 64 * 1024);
                }
            }
            else if (mode.Protocol == "SSL")
            {
                proxyPort = Engine.Instance.Storage.GetInt("ssl.port");
                if (proxyPort == 0)
                {
                    proxyPort = RandomGenerator.GetInt(1024, 64 * 1024);
                }
            }
            else
            {
                proxyPort = 0;
            }

            {
                string modeDirectives = mode.Directives;
                string paramUserTA    = "";
                if (User != null)
                {
                    paramUserTA = Utils.XmlGetAttributeString(User, "ta", "");
                }
                modeDirectives = modeDirectives.Replace("{@user-ta}", paramUserTA);
                ovpn.AppendDirectives(modeDirectives, "Mode level");
            }

            // Pick the IP
            IpAddress ip            = null;
            string    protocolEntry = Engine.Instance.Storage.Get("protocol.ip.entry");

            if (protocolEntry == "ipv6-ipv4")
            {
                ip = connection.IpsEntry.GetV6ByIndex(mode.EntryIndex);
                if (ip == null)
                {
                    ip = connection.IpsEntry.GetV4ByIndex(mode.EntryIndex);
                }
            }
            else if (protocolEntry == "ipv4-ipv6")
            {
                ip = connection.IpsEntry.GetV4ByIndex(mode.EntryIndex);
                if (ip == null)
                {
                    ip = connection.IpsEntry.GetV6ByIndex(mode.EntryIndex);
                }
            }
            else if (protocolEntry == "ipv6-only")
            {
                ip = connection.IpsEntry.GetV6ByIndex(mode.EntryIndex);
            }
            else if (protocolEntry == "ipv4-only")
            {
                ip = connection.IpsEntry.GetV4ByIndex(mode.EntryIndex);
            }

            if (ip != null)
            {
                if (mode.Protocol == "SSH")
                {
                    ovpn.AppendDirective("remote", "127.0.0.1 " + Conversions.ToString(proxyPort), "");
                }
                else if (mode.Protocol == "SSL")
                {
                    ovpn.AppendDirective("remote", "127.0.0.1 " + Conversions.ToString(proxyPort), "");
                }
                else
                {
                    ovpn.AppendDirective("remote", ip.AddressQ + " " + mode.Port.ToString(), "");
                }

                string routesDefault = Engine.Instance.Storage.Get("routes.default");
                if (routesDefault == "in")
                {
                    if ((mode.Protocol == "SSH") || (mode.Protocol == "SSL"))
                    {
                        ovpn.AppendDirective("route", ip.ToOpenVPN() + " net_gateway", "VPN Entry IP");                         // ClodoIPv6 // ToFix
                    }
                }
            }

            ovpn.Protocol  = mode.Protocol; // TOCLEAN
            ovpn.Address   = ip;
            ovpn.Port      = mode.Port;
            ovpn.ProxyPort = proxyPort;
        }
Ejemplo n.º 6
0
        // Parse the OpenVPN configuration file. Check if is valid, import external files, normalize.
        public Dictionary <string, string> OvpnParse(FileInfo file, bool interactive)
        {
            try
            {
                Dictionary <string, string> dictInfo = new Dictionary <string, string>();

                string ovpnOriginal = Platform.Instance.FileContentsReadText(file.FullName);


                OvpnBuilder ovpnBuilder = new OvpnBuilder();
                ovpnBuilder.AppendDirectives(ovpnOriginal, "Original");
                string ovpnNormalized = ovpnBuilder.Get();

                OvpnBuilder.Directive directiveRemote = ovpnBuilder.GetOneDirective("remote");
                if (directiveRemote != null)
                {
                    string host    = directiveRemote.Text;
                    int    posPort = host.IndexOf(" ");
                    if (posPort != -1)
                    {
                        host = host.Substring(0, posPort).Trim();
                    }
                    dictInfo["remote"] = host;
                }
                else
                {
                    dictInfo["remote"] = "";
                }

                dictInfo["ovpn"]    = ovpnNormalized;
                dictInfo["path"]    = file.FullName;
                dictInfo["country"] = "";

                // Compute user-friendly name
                {
                    string name = TitleForDisplay + file.FullName;

                    name = name.Replace(GetPathScan(), "").Trim();

                    //name = Regex.Replace(name, "udp", "", RegexOptions.IgnoreCase);
                    //name = Regex.Replace(name, "tcp", "", RegexOptions.IgnoreCase);
                    name = Regex.Replace(name, "tblk", "", RegexOptions.IgnoreCase);                     // TunnelBlick
                    name = Regex.Replace(name, "ovpn", "", RegexOptions.IgnoreCase);                     // OpenVPN

                    foreach (string countryName in CountriesManager.Name2Code.Keys)
                    {
                        if (name.IndexOf(countryName) != -1)
                        {
                            dictInfo["country"] = CountriesManager.Name2Code[countryName];
                        }
                    }

                    // Cleaning
                    name = name.Replace("-", " - ").Trim();
                    name = name.Replace("_", " - ").Trim();
                    name = name.Replace(".", " - ").Trim();

                    name = name.Replace("\\", " - ").Trim();
                    name = name.Replace("/", " - ").Trim();

                    for (; ;)
                    {
                        string orig = name;

                        name = name.Replace("  ", " ");
                        name = name.Replace("\t", " ");
                        name = name.Replace("- -", "-");

                        name = name.Trim(" -".ToCharArray());

                        if (name == orig)
                        {
                            break;
                        }
                    }

                    if (dictInfo.ContainsKey("protocol"))
                    {
                        name += " - " + dictInfo["protocol"].ToUpperInvariant();
                    }

                    dictInfo["name"] = name;
                }

                return(dictInfo);
            }
            catch (System.Exception e)
            {
                string message = MessagesFormatter.Format("Profiles scan, {1} (in profile '{1}')", e.Message, file.FullName);                 // TOTRANSLATE
                if (interactive)
                {
                    Engine.Instance.Logs.Log(LogType.Fatal, message);
                }
                else
                {
                    Engine.Instance.Logs.Log(LogType.Warning, message);
                }
                return(null);
            }
        }
Ejemplo n.º 7
0
        // Parse the OpenVPN configuration file. Check if is valid, import external files, normalize.
        public Dictionary<string, string> OvpnParse(FileInfo file, bool interactive)
        {
            try
            {
                Dictionary<string, string> dictInfo = new Dictionary<string, string>();

                string ovpnOriginal = Platform.Instance.FileContentsReadText(file.FullName);

                OvpnBuilder ovpnBuilder = new OvpnBuilder();
                ovpnBuilder.AppendDirectives(ovpnOriginal, "Original");
                string ovpnNormalized = ovpnBuilder.Get();

                OvpnBuilder.Directive directiveRemote = ovpnBuilder.GetOneDirective("remote");
                if(directiveRemote != null)
                {
                    string host = directiveRemote.Text;
                    int posPort = host.IndexOf(" ");
                    if (posPort != -1)
                        host = host.Substring(0, posPort).Trim();
                    dictInfo["remote"] = host;
                }
                else
                    dictInfo["remote"] = "";

                dictInfo["ovpn"] = ovpnNormalized;
                dictInfo["path"] = file.FullName;
                dictInfo["country"] = "";

                // Compute user-friendly name
                {
                    string name = TitleForDisplay + file.FullName;

                    name = name.Replace(GetPathScan(), "").Trim();

                    //name = Regex.Replace(name, "udp", "", RegexOptions.IgnoreCase);
                    //name = Regex.Replace(name, "tcp", "", RegexOptions.IgnoreCase);
                    name = Regex.Replace(name, "tblk", "", RegexOptions.IgnoreCase); // TunnelBlick
                    name = Regex.Replace(name, "ovpn", "", RegexOptions.IgnoreCase); // OpenVPN

                    foreach (string countryName in CountriesManager.Name2Code.Keys)
                    {
                        if (name.IndexOf(countryName) != -1)
                        {
                            dictInfo["country"] = CountriesManager.Name2Code[countryName];
                        }
                    }

                    // Cleaning
                    name = name.Replace("-", " - ").Trim();
                    name = name.Replace("_", " - ").Trim();
                    name = name.Replace(".", " - ").Trim();

                    name = name.Replace("\\", " - ").Trim();
                    name = name.Replace("/", " - ").Trim();

                    for (; ; )
                    {
                        string orig = name;

                        name = name.Replace("  ", " ");
                        name = name.Replace("\t", " ");
                        name = name.Replace("- -", "-");

                        name = name.Trim(" -".ToCharArray());

                        if (name == orig)
                            break;
                    }

                    if (dictInfo.ContainsKey("protocol"))
                        name += " - " + dictInfo["protocol"].ToUpperInvariant();

                    dictInfo["name"] = name;
                }

                return dictInfo;
            }
            catch (System.Exception e)
            {
                string message = MessagesFormatter.Format("Profiles scan, {1} (in profile '{1}')", e.Message, file.FullName); // TOTRANSLATE
                if (interactive)
                    Engine.Instance.Logs.Log(LogType.Fatal, message);
                else
                    Engine.Instance.Logs.Log(LogType.Warning, message);
                return null;
            }
        }