Beispiel #1
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();
            }
        }