changes to ssh configuration
This commit is contained in:
		@@ -11,11 +11,12 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Host defines a host to which to connect
 | 
					// Host defines a host to which to connect
 | 
				
			||||||
// If not provided, the values will be looked up in the default ssh config file
 | 
					// If not provided, the values will be looked up in the default ssh config files
 | 
				
			||||||
type Host struct {
 | 
					type Host struct {
 | 
				
			||||||
 | 
						ConfigFilePath     string
 | 
				
			||||||
	Empty              bool
 | 
						Empty              bool
 | 
				
			||||||
	Host               string
 | 
						Host               string
 | 
				
			||||||
	HostName           string
 | 
						HostName           []string
 | 
				
			||||||
	Port               uint16
 | 
						Port               uint16
 | 
				
			||||||
	PrivateKeyPath     string
 | 
						PrivateKeyPath     string
 | 
				
			||||||
	PrivateKeyPassword string
 | 
						PrivateKeyPassword string
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,25 +13,36 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type SshConfig struct {
 | 
					type SshConfig struct {
 | 
				
			||||||
	PrivateKey string
 | 
						// Config file to open
 | 
				
			||||||
	Port       uint
 | 
						configFile string
 | 
				
			||||||
	HostName   string
 | 
					
 | 
				
			||||||
	User       string
 | 
						// Private key path
 | 
				
			||||||
 | 
						privateKey string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Port to connect to
 | 
				
			||||||
 | 
						port uint16
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// host to check
 | 
				
			||||||
 | 
						host string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// host name to connect to
 | 
				
			||||||
 | 
						hostName []string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						user string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetSSHConfig(host string) (SshConfig, error) {
 | 
					func (config SshConfig) GetSSHConfig() (SshConfig, error) {
 | 
				
			||||||
	var config SshConfig
 | 
						hostNames := ssh_config.GetAll(config.host, "HostName")
 | 
				
			||||||
	hostName := ssh_config.Get(host, "HostName")
 | 
						if hostNames == nil {
 | 
				
			||||||
	if hostName == "" {
 | 
					 | 
				
			||||||
		return SshConfig{}, errors.New("hostname not found")
 | 
							return SshConfig{}, errors.New("hostname not found")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	config.HostName = hostName
 | 
						config.hostName = hostNames
 | 
				
			||||||
	privKey, err := ssh_config.GetStrict(host, "IdentityFile")
 | 
						privKey, err := ssh_config.GetStrict(config.host, "IdentityFile")
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return SshConfig{}, err
 | 
							return SshConfig{}, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	config.PrivateKey = privKey
 | 
						config.privateKey = privKey
 | 
				
			||||||
	User := ssh_config.Get(host, "User")
 | 
						User := ssh_config.Get(config.host, "User")
 | 
				
			||||||
	if User == "" {
 | 
						if User == "" {
 | 
				
			||||||
		return SshConfig{}, errors.New("user not found")
 | 
							return SshConfig{}, errors.New("user not found")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -39,6 +50,7 @@ func GetSSHConfig(host string) (SshConfig, error) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (remoteConfig *Host) ConnectToSSHHost() (*ssh.Client, error) {
 | 
					func (remoteConfig *Host) ConnectToSSHHost() (*ssh.Client, error) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var sshc *ssh.Client
 | 
						var sshc *ssh.Client
 | 
				
			||||||
	var connectErr error
 | 
						var connectErr error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -61,16 +73,24 @@ func (remoteConfig *Host) ConnectToSSHHost() (*ssh.Client, error) {
 | 
				
			|||||||
				}
 | 
									}
 | 
				
			||||||
				remoteConfig.PrivateKeyPath = filepath.Join(identityFile)
 | 
									remoteConfig.PrivateKeyPath = filepath.Join(identityFile)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			remoteConfig.HostName, _ = cfg.Get(remoteConfig.Host, "HostName")
 | 
								remoteConfig.HostName, _ = cfg.GetAll(remoteConfig.Host, "HostName")
 | 
				
			||||||
			if remoteConfig.HostName == "" {
 | 
								if remoteConfig.HostName == nil {
 | 
				
			||||||
				remoteConfig.HostName = remoteConfig.Host
 | 
									remoteConfig.HostName[0] = remoteConfig.Host
 | 
				
			||||||
			}
 | 
									port, _ := cfg.Get(remoteConfig.Host, "Port")
 | 
				
			||||||
			port, _ := cfg.Get(remoteConfig.Host, "Port")
 | 
									if port == "" {
 | 
				
			||||||
			if port == "" {
 | 
										port = "22"
 | 
				
			||||||
				port = "22"
 | 
									}
 | 
				
			||||||
 | 
									remoteConfig.HostName[0] = remoteConfig.HostName[0] + ":" + port
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									for index, hostName := range remoteConfig.HostName {
 | 
				
			||||||
 | 
										port, _ := cfg.Get(remoteConfig.Host, "Port")
 | 
				
			||||||
 | 
										if port == "" {
 | 
				
			||||||
 | 
											port = "22"
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										remoteConfig.HostName[index] = hostName + ":" + port
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			privateKey, err := os.ReadFile(remoteConfig.PrivateKeyPath)
 | 
								privateKey, err := os.ReadFile(remoteConfig.PrivateKeyPath)
 | 
				
			||||||
			remoteConfig.HostName = remoteConfig.HostName + ":" + port
 | 
					 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return nil, fmt.Errorf("read private key error: %w", err)
 | 
									return nil, fmt.Errorf("read private key error: %w", err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@@ -83,7 +103,12 @@ func (remoteConfig *Host) ConnectToSSHHost() (*ssh.Client, error) {
 | 
				
			|||||||
				Auth:            []ssh.AuthMethod{ssh.PublicKeys(signer)},
 | 
									Auth:            []ssh.AuthMethod{ssh.PublicKeys(signer)},
 | 
				
			||||||
				HostKeyCallback: ssh.InsecureIgnoreHostKey(),
 | 
									HostKeyCallback: ssh.InsecureIgnoreHostKey(),
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			sshc, connectErr = ssh.Dial("tcp", remoteConfig.HostName, sshConfig)
 | 
								for _, host := range remoteConfig.HostName {
 | 
				
			||||||
 | 
									sshc, connectErr = ssh.Dial("tcp", host, sshConfig)
 | 
				
			||||||
 | 
									if connectErr != nil {
 | 
				
			||||||
 | 
										continue
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			break
 | 
								break
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user