// 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); } }
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(); } }
// 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; } }