package bridge

import (
	
	
	

	
	
	
)

// InitShellWithPassword creates a SSH shell with password
func (, , ,  string) (*ssh.Client, error) {
	 := &ssh.ClientConfig{
		User: ,
		Auth: []ssh.AuthMethod{
			ssh.Password(),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
		Timeout:         time.Second * 5,
	}

	,  := helpers.ResolveIP()
	if  != nil {
		return nil, 
	}

	var  *ssh.Client
	 = retry.Do(
		func() error {
			,  = ssh.Dial("tcp", net.JoinHostPort(, ), )
			if  != nil {
				if strings.Contains(.Error(), "unable to authenticate") {
					return retry.Unrecoverable()
				}
				return 
			}
			return nil
		},
		retry.Attempts(5),
		retry.Delay(1*time.Second),
	)

	if  != nil {
		return nil, 
	}

	return , nil
}

// InitShellWithCert creates a SSH shell with certificate
func (, , ,  string) (*ssh.Client, error) {
	,  := ssh.ParsePrivateKey([]byte())
	if  != nil {
		return nil, 
	}

	 := &ssh.ClientConfig{
		User: ,
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
		Timeout:         time.Second * 5,
	}

	,  := helpers.ResolveIP()
	if  != nil {
		return nil, 
	}

	var  *ssh.Client
	 = retry.Do(
		func() error {
			,  = ssh.Dial("tcp", net.JoinHostPort(, ), )
			if  != nil {
				if strings.Contains(.Error(), "unable to authenticate") {
					return retry.Unrecoverable()
				}
				return 
			}
			return nil
		},
		retry.Attempts(5),
		retry.Delay(1*time.Second),
	)

	return , nil
}

// VerifySSH checks if remote end is active or not
func (, , ,  string) bool {
	 := &ssh.ClientConfig{
		User: ,
		Auth: []ssh.AuthMethod{
			ssh.Password(),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
		Timeout:         time.Second * 5,
	}

	,  := helpers.ResolveIP()
	if  != nil {
		return false
	}

	var  *ssh.Client
	 = retry.Do(
		func() error {
			,  = ssh.Dial("tcp", net.JoinHostPort(, ), )
			if  != nil {
				if strings.Contains(.Error(), "unable to authenticate") {
					return retry.Unrecoverable()
				}
				return 
			}
			return nil
		},
		retry.Attempts(5),
		retry.Delay(1*time.Second),
	)

	defer .Close()
	return true
}

// VerifySSHCertificate checks if remote end is active or not
func (, , ,  string) bool {
	,  := ssh.ParsePrivateKey([]byte())
	if  != nil {
		return false
	}

	 := &ssh.ClientConfig{
		User: ,
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
		Timeout:         time.Second * 5,
	}

	,  := helpers.ResolveIP()
	if  != nil {
		return false
	}

	var  *ssh.Client
	 = retry.Do(
		func() error {
			,  = ssh.Dial("tcp", net.JoinHostPort(, ), )
			if  != nil {
				if strings.Contains(.Error(), "unable to authenticate") {
					return retry.Unrecoverable()
				}
				return 
			}
			return nil
		},
		retry.Attempts(5),
		retry.Delay(1*time.Second),
	)
	if  != nil {
		return false
	}

	defer .Close()
	return true
}