A FAIRE.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.9 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### ssh ####### Copyright (c) 2021 mls-361 ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package ssh
import "forge.chapril.org/mls-361/errors"
// Crypto AFAIRE.
type Crypto interface {
DecryptString(text string) (string, error)
}
// Logger AFAIRE.
type Logger interface {
Debug(msg string, kv ...interface{})
}
// Clients AFAIRE.
type Clients map[string]map[string]*Client
// NewClients AFAIRE.
func NewClients(config []*Config, crypto Crypto) (Clients, error) {
clients := make(Clients)
for _, cfg := range config {
if crypto != nil {
if cfg.Password != "" {
p, err := crypto.DecryptString(cfg.Password)
if err != nil {
return nil, err
}
cfg.Password = p
}
if cfg.Passphrase != "" {
p, err := crypto.DecryptString(cfg.Passphrase)
if err != nil {
return nil, err
}
cfg.Passphrase = p
}
}
_, ok := clients[cfg.Host]
if !ok {
clients[cfg.Host] = make(map[string]*Client)
}
client, err := cfg.NewClient()
if err != nil {
return nil, err
}
clients[cfg.Host][cfg.Username] = client
}
return clients, nil
}
// Connect AFAIRE.
func (c Clients) Connect(host, username string, logger Logger) (*Connection, error) {
client, ok := c[host][username]
if !ok {
return nil, errors.New( ////////////////////////////////////////////////////////////////////////////////////////
"this SSH server or user does not exist",
"server", host,
"user", username,
)
}
return client.Connect(logger)
}
/*
######################################################################################################## @(°_°)@ #######
*/