コード例 #1
0
ファイル: Host.cs プロジェクト: gitrab/ssh-tunnel-manager
 public Host(HostInfo info, Config config, PuttyProfile profile)
 {
     Info = info;
     _config = config;
     _profile = profile;
     _puttyLink = new PuttyLink(Info, config, profile);
 }
コード例 #2
0
        public static void StartPutty(HostInfo host, PuttyProfile profile)
        {
            var fileName = Path.Combine(Util.Helper.StartupPath, PuttyLocation);
            var args     = PuttyArguments(host, profile, host.AuthType);

            Process.Start(fileName, args);
        }
コード例 #3
0
        public static PuttyProfile ReadProfile(string profileName)
        {
            if (profileName == null)
            {
                throw new ArgumentNullException("profileName");
            }
            try
            {
                using (RegistryKey profileKey = Registry.CurrentUser.OpenSubKey(@"Software\SimonTatham\PuTTY\Sessions\" + profileName, false))
                {
                    if (profileKey == null)
                    {
                        Logger.Log.Info(Resources.PuttyProfile_ProfileNotFound);
                        return(null);
                    }

                    if (_defaultProfileProperties.Select(v => v.Key).Except(profileKey.GetValueNames()).Any())
                    {
                        Logger.Log.Info(Resources.PuttyProfile_NotAllPropertiesSet);
                        return(null);
                    }

                    var profile = new PuttyProfile {
                        Name = profileName
                    };
                    foreach (var name in profileKey.GetValueNames())
                    {
                        var          value = profileKey.GetValue(name);
                        var          kind  = profileKey.GetValueKind(name);
                        PropertyType type;
                        switch (kind)
                        {
                        case RegistryValueKind.String:
                            type = PropertyType.String;
                            break;

                        case RegistryValueKind.DWord:
                            type = PropertyType.Int32;
                            break;

                        default:
                            throw new NotSupportedException(Resources.PuttyProfile_Error_RegistryValueKindNotSupported);
                        }
                        profile.Properties[name] = new PuttyProfileProperty(name)
                        {
                            Value = value, Type = type
                        };
                    }
                    return(profile);
                }
            }
            catch (SecurityException e)
            {
                throw new SSHTunnelManagerException(
                          string.Format(Resources.PuttyProfile_Error_SecurityException2, profileName, e.Message), e);
            }
        }
コード例 #4
0
        public static void StartPutty(HostInfo host, PuttyProfile profile, bool addTunnels)
        {
            var fileName = new FileInfo(Path.Combine(Util.Helper.StartupPath, PuttyLocation));
            var args     = PuttyArguments(host, profile, host.AuthType, addTunnels);

            Process.Start(new ProcessStartInfo(fileName.FullName, args)
            {
                WorkingDirectory = fileName.Directory.FullName
            });
        }
コード例 #5
0
        public static string PuttyArguments(HostInfo host, PuttyProfile profile, AuthenticationType authType)
        {
            // example: -ssh -load _stm_preset_ username@domainName -P 22 -pw password -D 5000 -L 44333:username.dyndns.org:44333

            string profileArg = "";

            if (profile != null)
            {
                profileArg = @" -load " + profile.Name;
            }

            var startShellOption = "";

            if (string.IsNullOrWhiteSpace(host.RemoteCommand))
            {
                startShellOption = " -N";
            }

            string args;

            switch (authType)
            {
            case AuthenticationType.None:
                args = String.Format(@"-ssh{0} {1}@{2} -P {3} -v{4}", profileArg, host.Username, host.Hostname,
                                     host.Port, startShellOption);
                Logger.Log.DebugFormat(@"plink.exe {0}", args);
                break;

            case AuthenticationType.Password:
                args = String.Format(@"-ssh{0} {1}@{2} -P {3} -pw {4} -v{5}", profileArg, host.Username, host.Hostname,
                                     host.Port, host.Password, startShellOption);
                Logger.Log.DebugFormat(@"plink.exe -ssh{0} {1}@{2} -P {3} -pw ******** -v -N", profileArg, host.Username,
                                       host.Hostname, host.Port);
                break;

            case AuthenticationType.PrivateKey:
                args = String.Format(@"-ssh{0} {1}@{2} -P {3} -i {4} -v{5}", profileArg, host.Username, host.Hostname,
                                     host.Port, PrivateKeysStorage.CreatePrivateKey(host).Filename, startShellOption);
                Logger.Log.DebugFormat(@"plink.exe {0}", args);
                break;

            default:
                throw new ArgumentOutOfRangeException("authType");
            }
            var sb = new StringBuilder(args);

            foreach (var tunnelArg in host.Tunnels.Select(tunnelArguments))
            {
                sb.Append(tunnelArg);
            }

            args = sb.ToString();
            return(args);
        }
コード例 #6
0
        public static string PuttyArguments(HostInfo host, PuttyProfile profile, AuthenticationType authType)
        {
            // example: -ssh -load _stm_preset_ username@domainName -P 22 -pw password -D 5000 -L 44333:username.dyndns.org:44333

            string profileArg = "";
            if (profile != null)
            {
                profileArg = @" -load " + profile.Name;
            }

            var startShellOption = "";
            if (string.IsNullOrWhiteSpace(host.RemoteCommand))
            {
                startShellOption = " -N";
            }

            string args;
            switch (authType)
            {
            case AuthenticationType.None:
                args = String.Format(@"-ssh{0} {1}@{2} -P {3} -v{4}", profileArg, host.Username, host.Hostname,
                                     host.Port, startShellOption);
                Logger.Log.DebugFormat(@"plink.exe {0}", args);
                break;
            case AuthenticationType.Password:
                args = String.Format(@"-ssh{0} {1}@{2} -P {3} -pw {4} -v{5}", profileArg, host.Username, host.Hostname,
                                     host.Port, host.Password, startShellOption);
                Logger.Log.DebugFormat(@"plink.exe -ssh{0} {1}@{2} -P {3} -pw ******** -v -N", profileArg, host.Username,
                                       host.Hostname, host.Port);
                break;
            case AuthenticationType.PrivateKey:
                args = String.Format(@"-ssh{0} {1}@{2} -P {3} -i {4} -v{5}", profileArg, host.Username, host.Hostname,
                                     host.Port, PrivateKeysStorage.CreatePrivateKey(host).Filename, startShellOption);
                Logger.Log.DebugFormat(@"plink.exe {0}", args);
                break;
            default:
                throw new ArgumentOutOfRangeException("authType");
            }
            var sb = new StringBuilder(args);
            foreach (var tunnelArg in host.Tunnels.Select(tunnelArguments))
            {
                sb.Append(tunnelArg);
            }

            args = sb.ToString();
            return args;
        }
コード例 #7
0
        public HostsManager(Config config, EncryptedStorage storage, string filename, string password)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            if (storage == null)
            {
                throw new ArgumentNullException("storage");
            }
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }

            Filename = filename;
            Password = password;
            Config   = config;
            Storage  = storage;

            try
            {
                PuttyProfile = PuttyProfile.ReadOrCreate(PuttyProfileName);
            }
            catch (SSHTunnelManagerException e)
            {
                Logger.Log.Error(Helper.JoinExceptionMessages(e.Message, Resources.HostsManager_Error_LoadPuttyProfileError));
            }

            var hosts = storage.Data.Hosts.Select(delegate(HostInfo h)
            {
                var viewModel   = new THostViewModel();
                viewModel.Model = new Host(h, Config, PuttyProfile)
                {
                    ViewModel = viewModel
                };
                return(viewModel);
            }).ToList();

            Hosts            = new BindingListView <THostViewModel>(hosts);
            Hosts.AddingNew += (o, e) => { e.NewObject = _addingNewHost; };
        }
コード例 #8
0
 public PuttyLink(HostInfo host, Config config, PuttyProfile profile)
 {
     if (host == null)
     {
         throw new ArgumentNullException("host");
     }
     if (config == null)
     {
         throw new ArgumentNullException("config");
     }
     if (profile == null)
     {
         throw new ArgumentNullException("profile");
     }
     Host     = host;
     _config  = config;
     _profile = profile;
 }
コード例 #9
0
        public static PuttyProfile ReadProfile(string profileName)
        {
            if (profileName == null) throw new ArgumentNullException("profileName");
            try
            {
                using (RegistryKey profileKey = Registry.CurrentUser.OpenSubKey(@"Software\SimonTatham\PuTTY\Sessions\" + profileName, false))
                {
                    if (profileKey == null)
                    {
                        Logger.Log.Info(Resources.PuttyProfile_ProfileNotFound);
                        return null;
                    }

                    if (_defaultProfileProperties.Select(v => v.Key).Except(profileKey.GetValueNames()).Any())
                    {
                        Logger.Log.Info(Resources.PuttyProfile_NotAllPropertiesSet);
                        return null;
                    }

                    var profile = new PuttyProfile {Name = profileName};
                    foreach (var name in profileKey.GetValueNames())
                    {
                        var value = profileKey.GetValue(name);
                        var kind = profileKey.GetValueKind(name);
                        PropertyType type;
                        switch (kind)
                        {
                            case RegistryValueKind.String:
                                type = PropertyType.String;
                                break;
                            case RegistryValueKind.DWord:
                                type = PropertyType.Int32;
                                break;
                            default:
                                throw new NotSupportedException(Resources.PuttyProfile_Error_RegistryValueKindNotSupported);
                        }
                        profile.Properties[name] = new PuttyProfileProperty(name) { Value = value, Type = type };
                    }
                    return profile;
                }
            }
            catch (SecurityException e)
            {
                throw new SSHTunnelManagerException(
                    string.Format(Resources.PuttyProfile_Error_SecurityException2, profileName, e.Message), e);
            }
        }
コード例 #10
0
 public PuttyLink(HostInfo host, Config config, PuttyProfile profile)
 {
     if (host == null) throw new ArgumentNullException("host");
     if (config == null) throw new ArgumentNullException("config");
     if (profile == null) throw new ArgumentNullException("profile");
     Host = host;
     _config = config;
     _profile = profile;
 }
コード例 #11
0
 public static void StartPutty(HostInfo host, PuttyProfile profile)
 {
     var fileName = Path.Combine(Util.Helper.StartupPath, PuttyLocation);
     var args = PuttyArguments(host, profile, host.AuthType);
     Process.Start(fileName, args);
 }
コード例 #12
0
        public OptionsDialog(PuttyProfile puttyProfile)
        {
            InitializeComponent();

            PuttyProfile = puttyProfile;

            buttonApply.Enabled = false;

            checkGroupBoxAutoRestart.Checked = Settings.Default.Config_RestartEnabled;
            numericUpDownRestartDelay.Value = Settings.Default.Config_RestartDelay;
            numericUpDownMaxAttemptsCount.Value = Settings.Default.Config_MaxAttemptsCount;
            radioButtonMakeDelay.Checked = Settings.Default.Config_AfterMaxAttemptsMakeDelay;
            checkBoxTraceDebug.Checked = Settings.Default.Config_TraceDebug;
            checkGroupBoxRestartHostsWithWarns.Checked = Settings.Default.Config_RestartHostsWithWarnings;
            numericUpDownRestartHWWInterval.Value = Settings.Default.Config_RestartHostsWithWarningsInterval;
            chbxRunAtWindowsStartup.Checked = Settings.Default.Config_RunAtWindowsStartup;
            chbxStartHostsBeingActiveLastTime.Checked = Settings.Default.Config_StartHostsBeingActiveLastTime;
            if (PuttyProfile != null)
            {
                checkBoxLocalPortAcceptAll.Checked = PuttyProfile.LocalPortAcceptAll;
                checkBoxRemotePortAcceptAll.Checked = PuttyProfile.RemotePortAcceptAll;
                checkGroupBoxProxy.Checked = PuttyProfile.ProxyMethod != PuttyProfile.ProxyType.None;
                comboBoxProxyType.SelectedIndex = PuttyProfile.ProxyMethod == PuttyProfile.ProxyType.Http ? 0 
                    : PuttyProfile.ProxyMethod == PuttyProfile.ProxyType.Socks4 ? 1
                    : PuttyProfile.ProxyMethod == PuttyProfile.ProxyType.Socks5 ? 2
                    : 0;
                textBoxHostname.Text = PuttyProfile.ProxyHost;
                textBoxPort.Text = PuttyProfile.ProxyPort.ToString();
                textBoxUsername.Text = PuttyProfile.ProxyUsername;
                textBoxPassword.Text = PuttyProfile.ProxyPassword;
                checkBoxAuthReq.Checked = !string.IsNullOrEmpty(PuttyProfile.ProxyUsername);
                checkBoxProxyLocalhost.Checked = PuttyProfile.ProxyLocalhost;
                textBoxProxyExcludes.Text = PuttyProfile.ProxyExcludeList;
            } else
            {
                checkBoxLocalPortAcceptAll.Enabled = false;
                checkBoxRemotePortAcceptAll.Enabled = false;
                checkGroupBoxProxy.Enabled = false;
            }

            _validator = new Validator(theErrorProvider);
            _validator.AddControl(textBoxHostname, validateHostname);
            _validator.AddControl(textBoxPort, validatePort);
            _validator.AddControl(textBoxUsername, validateUsername);
            _validator.AddControl(textBoxPassword, validatePassword);

            var controls = new List<Control>();
            collectControls(Controls, ref controls);
            foreach (var textBox in controls.OfType<TextBox>())
            {
                textBox.TextChanged += controlChanged;
            }
            foreach (var textBox in controls.OfType<CheckBox>())
            {
                textBox.CheckedChanged += controlChanged;
            }
            foreach (var textBox in controls.OfType<RadioButton>())
            {
                textBox.CheckedChanged += controlChanged;
            }
            foreach (var textBox in controls.OfType<ComboBox>())
            {
                textBox.SelectedIndexChanged += controlChanged;
            }
            foreach (var textBox in controls.OfType<CheckGroupBox>())
            {
                textBox.CheckedChanged += controlChanged;
            }
            foreach (var textBox in controls.OfType<NumericUpDown>())
            {
                textBox.ValueChanged += controlChanged;
            }
        }
コード例 #13
0
        public static string PuttyArguments(HostInfo host, PuttyProfile profile, AuthenticationType authType, bool addTunnels)
        {
            // example: -ssh -load _stm_preset_ username@domainName -P 22 -pw password -D 5000 -L 44333:username.dyndns.org:44333

            string profileArg = "";

            if (profile != null)
            {
                profileArg = @" -load " + profile.Name;
            }

            var startShellOption = "";

            if (string.IsNullOrWhiteSpace(host.RemoteCommand) && addTunnels)
            {
                startShellOption = " -N";
            }

            string args;

            switch (authType)
            {
            case AuthenticationType.None:
                args = String.Format(@"-ssh{0} {1}@{2} -P {3} -v{4}", profileArg, host.Username, host.CurrentHostAndPort.Hostname,
                                     host.CurrentHostAndPort.Port, startShellOption);
                //Logger.Log.DebugFormat(@"plink.exe {0}", args);
                break;

            case AuthenticationType.Password:
                args = String.Format(@"-ssh{0} {1}@{2} -P {3} -pw {4} -v{5}", profileArg, host.Username, host.CurrentHostAndPort.Hostname,
                                     host.CurrentHostAndPort.Port, host.Password, startShellOption);
                //Logger.Log.DebugFormat(@"plink.exe -ssh{0} {1}@{2} -P {3} -pw ******** -v -N", profileArg, host.Username,host.Hostname, host.Port);
                break;

            case AuthenticationType.PrivateKey:
                args = String.Format(@"-ssh{0} {1}@{2} -P {3} -i ""{4}"" -v{5}", profileArg, host.Username, host.CurrentHostAndPort.Hostname,
                                     host.CurrentHostAndPort.Port, PrivateKeysStorage.CreatePrivateKey(host).Filename, startShellOption);
                //Logger.Log.DebugFormat(@"plink.exe {0}", args);
                break;

            default:
                throw new ArgumentOutOfRangeException("authType");
            }

            if (addTunnels)
            {
                var sb = new StringBuilder(args);
                foreach (var tunnelArg in host.Tunnels.Select(tunnelArguments))
                {
                    sb.Append(tunnelArg);
                }

                args = sb.ToString();
            }

            Logger.Log.DebugFormat(@"plink.exe {0}", args.Replace("-pw " + host.Password + " ", "-pw ********* "));

            if (addTunnels && host.Tunnels.Any())
            {
                var ports     = host.Tunnels.Select(x => x.LocalPort).ToList();
                var ipGP      = IPGlobalProperties.GetIPGlobalProperties();
                var endpoints = ipGP.GetActiveTcpListeners();
                var portUsed  = endpoints.Select(x => x.Port.ToString()).Distinct().Where(x => ports.Contains(x)).ToList();
                if (portUsed.Any())
                {
                    var portsDetail = NetStatPortsAndProcessNames.GetNetStatPorts().ToLookup(x => x.port_number);
                    if (portUsed.Count == 1)
                    {
                        throw new Exception("La porta " + portUsed[0] + " è già in uso da '" + portsDetail[portUsed[0]].Select(p => p.process_name).FirstOrDefault() + "'.");
                    }
                    portUsed = portUsed.Select(x =>
                                               x + " (" + portsDetail[x].Select(p => p.process_name).FirstOrDefault() + ")").ToList();
                    throw new Exception("Le seguenti porto sono già in uso: " + string.Join(", ", portUsed));
                }
            }

            return(args);
        }