Beispiel #1
0
        public void InitStart()
        {
            OvpnFile = new TemporaryFile("ovpn");

            Platform.Instance.FileContentsWriteText(OvpnFile.Path, OpenVpnProfileStartup.Get());
            Platform.Instance.FileEnsurePermission(OvpnFile.Path, "600");
        }
Beispiel #2
0
        public void InitStart()
        {
            OvpnFile = new TemporaryFile("ovpn");

            Platform.Instance.FileContentsWriteText(OvpnFile.Path, OpenVpnProfileStartup.Get(), Encoding.UTF8);
        }
Beispiel #3
0
        public OvpnBuilder BuildOVPN(bool preview)
        {
            // If preview, no physical additional files are created.

            Storage s = Engine.Instance.Storage;

            OvpnBuilder ovpn = new OvpnBuilder();

            if (s.GetBool("openvpn.skip_defaults") == false)
            {
                ovpn.AppendDirectives(Engine.Instance.Storage.Get("openvpn.directives"), "Client level");
                string directivesPath = Engine.Instance.Storage.Get("openvpn.directives.path");
                if (directivesPath.Trim() != "")
                {
                    try
                    {
                        if (Platform.Instance.FileExists(directivesPath))
                        {
                            string text = Platform.Instance.FileContentsReadText(directivesPath);
                            ovpn.AppendDirectives(text, "Client level");
                        }
                        else
                        {
                            Engine.Instance.Logs.Log(LogType.Warning, MessagesFormatter.Format(Messages.FileNotFound, directivesPath));
                        }
                    }
                    catch (Exception ex)
                    {
                        Engine.Instance.Logs.Log(LogType.Warning, MessagesFormatter.Format(Messages.FileErrorRead, directivesPath, ex.Message));
                    }
                }
                Provider.OnBuildOvpnDefaults(ovpn);

                ovpn.AppendDirectives(OvpnDirectives, "Server level");

                if (Path != "")
                {
                    if (Platform.Instance.FileExists(Path))
                    {
                        string text = Platform.Instance.FileContentsReadText(Path);
                        ovpn.AppendDirectives(text, "Config file");

                        string dirPath = Platform.Instance.FileGetDirectoryPath(Path);
                        ovpn.NormalizeRelativePath(dirPath);
                    }
                }
            }

            if (s.Get("openvpn.dev_node") != "")
            {
                ovpn.AppendDirective("dev-node", s.Get("openvpn.dev_node"), "");
            }

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

            if (rcvbuf == -2)
            {
                rcvbuf = Platform.Instance.GetRecommendedRcvBufDirective();
            }
            if (rcvbuf == -2)
            {
                rcvbuf = -1;
            }
            if (rcvbuf != -1)
            {
                ovpn.AppendDirective("rcvbuf", rcvbuf.ToString(), "");
            }

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

            if (sndbuf == -2)
            {
                sndbuf = Platform.Instance.GetRecommendedSndBufDirective();
            }
            if (sndbuf == -2)
            {
                sndbuf = -1;
            }
            if (sndbuf != -1)
            {
                ovpn.AppendDirective("sndbuf", sndbuf.ToString(), "");
            }

            string proxyDirectiveName = "";
            string proxyDirectiveArgs = "";

            string proxyMode = s.GetLower("proxy.mode");
            string proxyWhen = s.GetLower("proxy.when");

            if ((proxyWhen == "none") || (proxyWhen == "web"))
            {
                proxyMode = "none";
            }
            if (proxyMode == "tor")
            {
                proxyDirectiveName = "socks-proxy";
            }
            else if (proxyMode == "http")
            {
                proxyDirectiveName = "http-proxy";
            }
            else if (proxyMode == "socks")
            {
                proxyDirectiveName = "socks-proxy";
            }

            if (proxyDirectiveName != "")
            {
                proxyDirectiveArgs += s.Get("proxy.host") + " " + s.Get("proxy.port");

                if ((s.GetLower("proxy.mode") != "none") && (s.GetLower("proxy.mode") != "tor"))
                {
                    if (s.Get("proxy.auth") != "None")
                    {
                        string fileNameAuthOvpn = "";
                        if (preview)
                        {
                            fileNameAuthOvpn = "dummy.ppw";
                        }
                        else
                        {
                            ovpn.FileProxyAuth = new TemporaryFile("ppw");
                            fileNameAuthOvpn   = ovpn.FileProxyAuth.Path.Replace("\\", "\\\\");                           // 2.6, Escaping for Windows
                            string fileNameData = s.Get("proxy.login") + "\n" + s.Get("proxy.password") + "\n";
                            Platform.Instance.FileContentsWriteText(ovpn.FileProxyAuth.Path, fileNameData);
                            Platform.Instance.FileEnsurePermission(ovpn.FileProxyAuth.Path, "600");
                        }
                        proxyDirectiveArgs += " \"" + fileNameAuthOvpn + "\" " + s.Get("proxy.auth").ToLowerInvariant();                         // 2.6 Auth Fix
                    }
                }

                ovpn.AppendDirective(proxyDirectiveName, proxyDirectiveArgs, "");
            }

            if (Lib.Common.Constants.AlphaFeatures)
            {
                if (Software.GetTool("openvpn").VersionAboveOrEqual("2.4"))
                {
                    // IP Layer routes

                    ovpn.AppendDirective("pull-filter", "ignore \"redirect-gateway\"", "Forced at client side");

                    bool ipv4In = true;
                    bool ipv6In = true;

                    if (s.GetLower("protocol.ipv4.route") == "in-always")
                    {
                        ipv4In = true;
                    }
                    else if (s.GetLower("protocol.ipv4.route") == "in-out")
                    {
                        if (SupportIPv4)
                        {
                            ipv4In = true;
                        }
                        else
                        {
                            ipv4In = false;
                        }
                    }
                    else if (s.GetLower("protocol.ipv4.route") == "in-block")
                    {
                        if (SupportIPv4)
                        {
                            ipv4In = true;
                        }
                        else
                        {
                            ipv4In = false;                             // Out, but doesn't matter, will be blocked.
                        }
                    }
                    else if (s.GetLower("protocol.ipv4.route") == "out")
                    {
                        ipv4In = false;
                    }
                    else if (s.GetLower("protocol.ipv4.route") == "block")
                    {
                        ipv4In = false;                         // Out, but doesn't matter, will be blocked.
                    }

                    if (s.GetLower("protocol.ipv6.route") == "in-always")
                    {
                        ipv6In = true;
                    }
                    else if (s.GetLower("protocol.ipv6.route") == "in-out")
                    {
                        if (SupportIPv4)
                        {
                            ipv6In = true;
                        }
                        else
                        {
                            ipv6In = false;
                        }
                    }
                    else if (s.GetLower("protocol.ipv6.route") == "in-block")
                    {
                        if (SupportIPv6)
                        {
                            ipv6In = true;
                        }
                        else
                        {
                            ipv6In = false;                             // Out, but doesn't matter, will be blocked.
                        }
                    }
                    else if (s.GetLower("protocol.ipv6.route") == "out")
                    {
                        ipv6In = false;
                    }
                    else if (s.GetLower("protocol.ipv6.route") == "block")
                    {
                        ipv6In = false;                         // Out, but doesn't matter, will be blocked.
                    }

                    if ((ipv4In == false) && (ipv6In == false))
                    {
                        // no redirect-gateway
                    }
                    else if ((ipv4In == true) && (ipv6In == false))
                    {
                        ovpn.AppendDirective("redirect-gateway", "def1 bypass-dhcp", "");
                    }
                    else if ((ipv4In == false) && (ipv6In == true))
                    {
                        ovpn.AppendDirective("redirect-gateway", "ipv6 !ipv4 def1 bypass-dhcp", "");
                    }
                    else
                    {
                        ovpn.AppendDirective("redirect-gateway", "ipv6 def1 bypass-dhcp", "");
                    }
                }
                else
                {
                    // ClodoTemp: If <2.4 ? Ipv6 are anyway non managed well.
                }
            }
            else
            {
            }

            string routesDefault = s.Get("routes.default");

            if (routesDefault == "out")
            {
                if (Software.GetTool("openvpn").VersionAboveOrEqual("2.4"))
                {
                    ovpn.RemoveDirective("redirect-gateway");                     // Remove if exists
                    ovpn.AppendDirective("pull-filter", "ignore \"redirect-gateway\"", "For Routes Out");
                }
                else                 // Compatibility <2.4
                {
                    ovpn.AppendDirective("route-nopull", "", "For Routes Out");

                    // For DNS
                    // < 2.9. route directive useless, and DNS are forced manually in every supported platform. // TOCLEAN

                    /*
                     * ovpn += "dhcp-option DNS " + Constants.DnsVpn + "\n"; // Manually because route-nopull skip it
                     * ovpn += "route 10.4.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                     * ovpn += "route 10.5.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                     * ovpn += "route 10.6.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                     * ovpn += "route 10.7.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                     * ovpn += "route 10.8.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                     * ovpn += "route 10.9.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                     * ovpn += "route 10.30.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                     * ovpn += "route 10.50.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                     */

                    // 2.9, Can be removed when resolv-conf method it's not binded anymore in up/down ovpn directive // TOFIX
                    ovpn.AppendDirective("dhcp-option", "DNS " + Lib.Common.Constants.DnsVpn, "");
                }

                // For Checking
                foreach (IpAddress ip in IpsExit.IPs)
                {
                    if (ip.IsV4)                    // TOFIX IPv6
                    {
                        ovpn.AppendDirective("route", ip.ToOpenVPN() + " vpn_gateway", "For Checking Route");
                    }
                }
            }

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

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

                string      ipCustomRoute  = routeEntries[0];
                IpAddresses ipsCustomRoute = new IpAddresses(ipCustomRoute);

                if (ipsCustomRoute.Count == 0)
                {
                    Engine.Instance.Logs.Log(LogType.Verbose, MessagesFormatter.Format(Messages.CustomRouteInvalid, ipCustomRoute.ToString()));
                }
                else
                {
                    string action = routeEntries[1];
                    string notes  = routeEntries[2];

                    string gateway = "";

                    if ((routesDefault == "out") && (action == "in"))
                    {
                        gateway = "vpn_gateway";
                    }
                    if ((routesDefault == "in") && (action == "out"))
                    {
                        gateway = "net_gateway";
                    }


                    if (gateway != "")
                    {
                        foreach (IpAddress ip in ipsCustomRoute.IPs)
                        {
                            if (ip.IsV4)
                            {
                                ovpn.AppendDirective("route", ip.ToOpenVPN() + " " + gateway, (notes != "") ? Utils.StringSafe(notes) : ipCustomRoute);
                            }
                            // TOFIX IPv6

                            /*
                             * else if(ipCustomRoute.IsV6)
                             *      ovpn.AppendDirective("route-ipv6", ipCustomRoute.ToOpenVPN() + " " + gateway + "_ipv6", Utils.StringSafe(notes));
                             */
                        }
                    }
                }
            }

            if (routesDefault == "in")
            {
                if (proxyMode == "tor")
                {
                    IpAddresses torNodeIps = TorControl.GetGuardIps();
                    foreach (IpAddress torNodeIp in torNodeIps.IPs)
                    {
                        if (torNodeIp.IsV4)
                        {
                            ovpn.AppendDirective("route", torNodeIp.ToOpenVPN() + " net_gateway", "Tor Circuit");
                        }
                        // TOFIX IPv6

                        /*
                         * else if(torNodeIp.IsV6)
                         *      ovpn.AppendDirective("route-ipv6", torNodeIp.ToOpenVPN() + " net_gateway_ipv6", "Tor Circuit");
                         */
                    }
                }
            }

            ovpn.AppendDirective("management", "127.0.0.1 " + Engine.Instance.Storage.Get("openvpn.management_port"), "");

            ovpn.AppendDirectives(Engine.Instance.Storage.Get("openvpn.custom"), "Custom level");

            // Experimental - Allow identification as Public Network in Windows. Advanced Option?
            // ovpn.Append("route-metric 512");
            // ovpn.Append("route 0.0.0.0 0.0.0.0");

            Provider.OnBuildOvpn(this, ovpn);

            Provider.OnBuildOvpnAuth(ovpn);

            Platform.Instance.OnBuildOvpn(ovpn);

            ovpn.Normalize();

            string ovpnText = ovpn.Get();

            Provider.OnBuildOvpnPost(ref ovpnText);

            return(ovpn);
        }
Beispiel #4
0
        public void BuildOVPN()
        {
            ServerInfo CurrentServer = Engine.Instance.CurrentServer;

            Storage s = Engine.Instance.Storage;

            OvpnBuilder ovpn = new OvpnBuilder();

            if (s.GetBool("openvpn.skip_defaults") == false)
            {
                ovpn.AppendDirectives(Engine.Instance.Storage.Get("openvpn.directives"), "Client level");
                CurrentServer.Provider.OnBuildOvpnDefaults(ovpn);

                ovpn.AppendDirectives(CurrentServer.OvpnDirectives, "Server level");
            }

            if (s.Get("openvpn.dev_node") != "")
                ovpn.AppendDirective("dev-node", s.Get("openvpn.dev_node"), "");

            int rcvbuf = s.GetInt("openvpn.rcvbuf");
            if ((rcvbuf == -2) && (Platform.IsWindows())) rcvbuf = (256 * 1024);
            if (rcvbuf == -2) rcvbuf = -1;
            if (rcvbuf != -1)
                ovpn.AppendDirective("rcvbuf", rcvbuf.ToString(), "");

            int sndbuf = s.GetInt("openvpn.sndbuf");
            if ((sndbuf == -2) && (Platform.IsWindows())) sndbuf = (256 * 1024);
            if (sndbuf == -2) sndbuf = -1;
            if (sndbuf != -1)
                ovpn.AppendDirective("sndbuf", sndbuf.ToString(), "");

            string proxyDirectiveName = "";
            string proxyDirectiveArgs = "";

            string proxyMode = s.GetLower("proxy.mode");
            if (proxyMode == "tor")
            {
                proxyDirectiveName = "socks-proxy";
            }
            else if (proxyMode == "http")
            {
                proxyDirectiveName = "http-proxy";

            }
            else if (proxyMode == "socks")
            {
                proxyDirectiveName = "socks-proxy";
            }

            if (proxyDirectiveName != "")
            {
                proxyDirectiveArgs += s.Get("proxy.host") + " " + s.Get("proxy.port");

                if ((s.GetLower("proxy.mode") != "none") && (s.GetLower("proxy.mode") != "tor"))
                {
                    if (s.Get("proxy.auth") != "None")
                    {
                        m_fileProxyAuth = new TemporaryFile("ppw");
                        string fileNameAuthOvpn = m_fileProxyAuth.Path.Replace("\\", "\\\\"); // 2.6, Escaping for Windows
                        string fileNameData = s.Get("proxy.login") + "\n" + s.Get("proxy.password") + "\n";
                        Platform.Instance.FileContentsWriteText(m_fileProxyAuth.Path, fileNameData);
                        proxyDirectiveArgs += " \"" + fileNameAuthOvpn + "\" " + s.Get("proxy.auth").ToLowerInvariant(); // 2.6 Auth Fix
                    }
                }

                ovpn.AppendDirective(proxyDirectiveName, proxyDirectiveArgs, "");
            }

            string routesDefault = s.Get("routes.default");
            if (routesDefault == "out")
            {
                ovpn.AppendDirective("route-nopull", "", "For Routes Out");

                // For Checking
                if(CurrentServer.IpExit != "")
                    ovpn.AppendDirective("route", CurrentServer.IpExit + " 255.255.255.255 vpn_gateway", "For Checking Route");

                // For DNS
                // < 2.9. route directive useless, and DNS are forced manually in every supported platform. // TOCLEAN
                /*
                ovpn += "dhcp-option DNS " + Constants.DnsVpn + "\n"; // Manually because route-nopull skip it
                ovpn += "route 10.4.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                ovpn += "route 10.5.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                ovpn += "route 10.6.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                ovpn += "route 10.7.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                ovpn += "route 10.8.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                ovpn += "route 10.9.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                ovpn += "route 10.30.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                ovpn += "route 10.50.0.1 255.255.255.255 vpn_gateway # AirDNS\n";
                */

                // 2.9, Can be removed when resolv-conf method it's not binded anymore in up/down ovpn directive // TOFIX
                ovpn.AppendDirective("dhcp-option", "DNS " + Constants.DnsVpn, "");
            }
            string routes = s.Get("routes.custom");
            string[] routes2 = routes.Split(';');
            foreach (string route in routes2)
            {
                string[] routeEntries = route.Split(',');
                if (routeEntries.Length != 3)
                    continue;

                IpAddressRange ipCustomRoute = new IpAddressRange(routeEntries[0]);

                if (ipCustomRoute.Valid == false)
                    Engine.Instance.Logs.Log(LogType.Warning, MessagesFormatter.Format(Messages.CustomRouteInvalid, ipCustomRoute.ToString()));
                else
                {
                    string action = routeEntries[1];
                    string notes = routeEntries[2];

                    if ((routesDefault == "out") && (action == "in"))
                        ovpn.AppendDirective("route", ipCustomRoute.ToOpenVPN() + " vpn_gateway", Utils.SafeString(notes));
                    if ((routesDefault == "in") && (action == "out"))
                        ovpn.AppendDirective("route", ipCustomRoute.ToOpenVPN() + " net_gateway", Utils.SafeString(notes));
                }
            }

            if (routesDefault == "in")
            {
                if (proxyMode == "tor")
                {
                    List<string> torNodeIps = TorControl.GetGuardIps();
                    foreach (string torNodeIp in torNodeIps)
                    {
                        ovpn.AppendDirective("route", torNodeIp + " 255.255.255.255 net_gateway", "Tor Circuit");
                    }
                }
            }

            ovpn.AppendDirective("management", "127.0.0.1 " + Engine.Instance.Storage.Get("openvpn.management_port"), "");

            ovpn.AppendDirectives(Engine.Instance.Storage.Get("openvpn.custom"), "Custom level");

            // Experimental - Allow identification as Public Network in Windows. Advanced Option?
            // ovpn.Append("route-metric 512");
            // ovpn.Append("route 0.0.0.0 0.0.0.0");

            // Used by OpenVPN provider
            if (ovpn.ExistsDirective("auth-user-pass"))
            {
                m_filePasswordAuth = new TemporaryFile("ppw");
                string fileNameAuthOvpn = m_filePasswordAuth.Path.Replace("\\", "\\\\");
                string login = CurrentServer.Provider.GetLogin();
                string password = CurrentServer.Provider.GetPassword();
                string fileNameData = login + "\n" + password + "\n";

                Platform.Instance.FileContentsWriteText(m_filePasswordAuth.Path, fileNameData);
                proxyDirectiveArgs += " \"" + fileNameAuthOvpn + "\" " + s.Get("proxy.auth").ToLowerInvariant();

                ovpn.AppendDirective("auth-user-pass", "\"" + fileNameAuthOvpn + "\"", "Auth");
            }

            CurrentServer.Provider.OnBuildOvpn(ovpn);

            CurrentServer.Provider.OnBuildOvpnAuth(ovpn);

            Platform.Instance.OnBuildOvpn(ovpn);

            ovpn.Normalize();

            string ovpnText = ovpn.Get();

            CurrentServer.Provider.OnBuildOvpnPost(ref ovpnText);

            Engine.Instance.ConnectedOVPN = ovpnText;

            m_ovpn = ovpn;
        }