Пример #1
0
        protected void PrepareRequiredConnection(SSHConnectionInfo requiredConnectionInfo)
        {
            if (requiredConnectionInfo == null)
            {
                return;
            }
            var additionalPF = new PortForwarding
            {
                Type       = "Local",
                RemoteHost = ConnectionInfo.Host,
                RemotePort = ConnectionInfo.Port,
            };
            // Try to re-use recently used port.
            var recentlyUsedPort = ConnectionInfo.GetAvailableCachedPort();

            if (recentlyUsedPort > 0)
            {
                additionalPF.LocalPort = recentlyUsedPort;
            }
            var conn = new SSHConnectionViewModel(requiredConnectionInfo, false, additionalPF)
            {
                Parent = this
            };

            conn.IsRoot.Value  = false;
            RequiredConnection = conn;
            Children.Add(conn);
        }
Пример #2
0
    private PortForwarding AddPortForwarding(PortForwardingData portForwardingData)
    {
        var portForwarding = new PortForwarding(_currentId.ToString(), portForwardingData);

        _portForwardingConfigurations[portForwarding.Id] = portForwarding;
        _currentId++;
        return(portForwarding);
    }
 public PortForwardingForm(PortForwarding portForwarding)
 {
     Name            = portForwarding.Name;
     Protocol        = portForwarding.Protocol;
     SourceIp        = portForwarding.SourceIp?.ToString();
     SourcePort      = portForwarding.SourcePort;
     DestinationIp   = portForwarding.DestinationIp.ToString();
     DestinationPort = portForwarding.DestinationPort;
 }
Пример #4
0
    public Task <PortForwarding> GetById(string id)
    {
        PortForwarding portForwarding = null;

        if (_portForwardingConfigurations.ContainsKey(id))
        {
            portForwarding = _portForwardingConfigurations[id];
        }
        return(Task.FromResult(portForwarding));
    }
Пример #5
0
 /// <summary>
 /// Create a <see cref="ForwardedPort"/> from <see cref="PortForwarding"/> setting.
 /// </summary>
 /// <param name="pf"></param>
 /// <returns></returns>
 private ForwardedPort CreateForwardedPort(PortForwarding pf)
 {
     if (pf.Type == "Remote")
     {
         if (pf.RemotePort == null)
         {
             throw new InvalidOperationException(Resources.SSHConnection_Exception_RemotePortNotAssigned);
         }
         if (!pf.RemotePort.HasValue || !pf.RemotePort.Value.IsValidIPPort() ||
             !pf.LocalPort.HasValue || !pf.LocalPort.Value.IsValidIPPort())
         {
             throw new InvalidOperationException(Resources.SSHConnection_Exception_PortNumberOutOfRange);
         }
         // LocalHost and LocalPort are the information of the SSH server.
         return((string.IsNullOrWhiteSpace(pf.LocalHost))
             ? new ForwardedPortRemote((uint)pf.LocalPort, pf.RemoteHost, (uint)pf.RemotePort)
             : new ForwardedPortRemote(pf.LocalHost, (uint)pf.LocalPort, pf.RemoteHost, (uint)pf.RemotePort));
     }
     else if (pf.Type == "Dynamic")
     {
         // RemoteHost and RemotePort don't care.
         if (!pf.LocalPort.HasValue || !pf.LocalPort.Value.IsValidIPPort())
         {
             throw new InvalidOperationException(Resources.SSHConnection_Exception_PortNumberOutOfRange);
         }
         var boundHost = string.IsNullOrWhiteSpace(pf.LocalHost) ? "127.0.0.1" : pf.LocalHost;
         return((string.IsNullOrWhiteSpace(pf.LocalHost))
             ? new ForwardedPortDynamic(boundHost, (uint)pf.LocalPort.Value)
             : new ForwardedPortDynamic((uint)pf.LocalPort.Value));
     }
     else // Type not assgined / Type == "Local"
     {
         if (string.IsNullOrWhiteSpace(pf.RemoteHost) || pf.RemotePort == null)
         {
             throw new InvalidOperationException(Resources.SSHConnection_Exception_RemoteHostPortNotAssigned);
         }
         if (!pf.RemotePort.Value.IsValidIPPort() ||
             (pf.LocalPort.HasValue && !pf.LocalPort.Value.IsValidIPPort()))
         {
             throw new InvalidOperationException(Resources.SSHConnection_Exception_PortNumberOutOfRange);
         }
         var boundHost = string.IsNullOrWhiteSpace(pf.LocalHost) ? "127.0.0.1" : pf.LocalHost;
         return((pf.LocalPort == null)
             ? new ForwardedPortLocal(boundHost, pf.RemoteHost, (uint)pf.RemotePort.Value)
             : new ForwardedPortLocal(boundHost, (uint)pf.LocalPort.Value, pf.RemoteHost, (uint)pf.RemotePort.Value));
     }
 }
Пример #6
0
 /// <summary>
 /// Instantiates <see cref="SSHConnectionViewModel"/> from <see cref="SSHConnectionInfo"/> and returns the root connection.
 /// </summary>
 /// <param name="info"></param>
 /// <param name="asSubordinate">Whether invoked from another connection or not.</param>
 /// <param name="additionalPortForwarding"><see cref="PortForwarding"/> which will be additionally established.</param>
 public SSHConnectionViewModel(SSHConnectionInfo info, bool asSubordinate = false, PortForwarding additionalPortForwarding = null) : base(info)
 {
     if (string.IsNullOrWhiteSpace(info.Username))
     {
         throw new InvalidOperationException(Resources.SSHConnection_Exception_UsernameNotAssigned);
     }
     if (!string.IsNullOrWhiteSpace(info.PrivateKeyFilePath) && !File.Exists(info.PrivateKeyFilePath))
     {
         throw new FileNotFoundException(
                   string.Format(Resources.SSHConnection_Exception_PrivateKeyFileNotExists, info.PrivateKeyFilePath),
                   info.PrivateKeyFilePath);
     }
     if (info.PortForwardingCollection != null &&
         ((info.AlwaysForwardPorts ?? false) || !asSubordinate))
     {
         CreateForwardedPorts(info.PortForwardingCollection);
     }
     if (additionalPortForwarding != null)
     {
         CreateForwardedPorts(new[] { additionalPortForwarding });
     }
     PrepareRequiredConnection(info.RequiredConnection);
 }