<commit_before>package remote

import (
	"bytes"
	"crypto/x509"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"path"
	"syscall"
	"time"

	"github.com/usi-lfkeitel/saturn/src/utils"

	"golang.org/x/crypto/ssh"
	"golang.org/x/crypto/ssh/terminal"
)

var sshClientConfig *ssh.ClientConfig

func LoadPrivateKey(config *utils.Config) error {
	authMethods := make([]ssh.AuthMethod, 0, 1)

	if config.SSH.PrivateKey != "" {
		sshPrivateKey, err := ioutil.ReadFile(config.SSH.PrivateKey)
		if err != nil {
			return err
		}

		signer, err := ssh.ParsePrivateKeyWithPassphrase(sshPrivateKey, []byte{})
		if err == x509.IncorrectPasswordError {
			signer, err = loadPrivateKeyPromptPassphrase(sshPrivateKey)
		}
		if err != nil {
			return err
		}
		authMethods = append(authMethods, ssh.PublicKeys(signer))
	}

	if config.SSH.Password != "" {
		authMethods = append(authMethods, ssh.Password(config.SSH.Password))
	}

	t, _ := time.ParseDuration(config.SSH.Timeout)

	sshClientConfig = &ssh.ClientConfig{
		User:            config.SSH.Username,
		Auth:            authMethods,
		Timeout:         t,
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	}
	return nil
}

func loadPrivateKeyPromptPassphrase(key []byte) (ssh.Signer, error) {
	return ssh.ParsePrivateKeyWithPassphrase(key, getPassword())
}

func getPassword() []byte {
	fmt.Print("SSH Key Password: ")
	bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
	if err != nil {
		log.Println(err.Error())
	}
	fmt.Println("")

	return bytes.TrimSpace(bytePassword)
}

func UploadScript(config *utils.Config, hosts map[string]*utils.ConfigHost, genFilename string) error {
	f, err := os.Open(genFilename)
	if err != nil {
		return err
	}
	defer f.Close()

	s, err := f.Stat()
	if err != nil {
		return err
	}

	for _, host := range hosts {
		if host.Disable {
			continue
		}

		_, err = f.Seek(0, 0) // rewind file reader
		if err != nil {<commit_msg>Prompt for SSH password if no other authentication is given<commit_after>package remote

import (
	"bytes"
	"crypto/x509"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"path"
	"syscall"
	"time"

	"github.com/usi-lfkeitel/saturn/src/utils"

	"golang.org/x/crypto/ssh"
	"golang.org/x/crypto/ssh/terminal"
)

var sshClientConfig *ssh.ClientConfig

func LoadPrivateKey(config *utils.Config) error {
	authMethods := make([]ssh.AuthMethod, 0, 1)

	if config.SSH.PrivateKey != "" {
		sshPrivateKey, err := ioutil.ReadFile(config.SSH.PrivateKey)
		if err != nil {
			return err
		}

		signer, err := ssh.ParsePrivateKeyWithPassphrase(sshPrivateKey, []byte{})
		if err == x509.IncorrectPasswordError {
			signer, err = loadPrivateKeyPromptPassphrase(sshPrivateKey)
		}
		if err != nil {
			return err
		}
		authMethods = append(authMethods, ssh.PublicKeys(signer))
	}

	if config.SSH.Password != "" {
		authMethods = append(authMethods, ssh.Password(config.SSH.Password))
	}

	if len(authMethods) == 0 {
		authMethods = append(authMethods, ssh.Password(string(getPassword("SSH Password: "))))
	}

	t, _ := time.ParseDuration(config.SSH.Timeout)

	sshClientConfig = &ssh.ClientConfig{
		User:            config.SSH.Username,
		Auth:            authMethods,
		Timeout:         t,
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	}
	return nil
}

func loadPrivateKeyPromptPassphrase(key []byte) (ssh.Signer, error) {
	return ssh.ParsePrivateKeyWithPassphrase(key, getPassword("SSH Key Password: "))
}

func getPassword(prompt string) []byte {
	fmt.Print(prompt)
	bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
	if err != nil {
		log.Println(err.Error())
	}
	fmt.Println("")

	return bytes.TrimSpace(bytePassword)
}

func UploadScript(config *utils.Config, hosts map[string]*utils.ConfigHost, genFilename string) error {
	f, err := os.Open(genFilename)
	if err != nil {
		return err
	}
	defer f.Close()

	s, err := f.Stat()
	if err != nil {
		return err
	}

	for _, host := range hosts {
		if host.Disable {
			continue
		}

		_, err = f.Seek(0, 0) // rewind file reader
		if err != nil {
{'fraction_non_alphanumeric': 0.13887506222000995, 'fraction_numerical': 0.005226480836236934, 'mean_word_length': 3.8833535844471445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6228366', 'n_tokens_mistral': 1594, 'n_tokens_neox': 1433, 'n_words': 299}