Browse Source

En cours de développement

master
mls-361 7 months ago
parent
commit
7fd6649059
  1. 41
      client.go
  2. 108
      config.go
  3. 11
      connection.go
  4. 11
      session.go
  5. 68
      ssh.go
  6. 11
      stream.go

41
client.go

@ -0,0 +1,41 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### ssh ####### Copyright (c) 2021 mls-361 ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package ssh
import (
"github.com/mls-361/logger"
"golang.org/x/crypto/ssh"
)
// Client AFAIRE.
type Client struct {
host string
port int
username string
addr string
config *ssh.ClientConfig
}
// Connect AFAIRE.
func (c *Client) Connect(logger logger.Logger) (*Connection, error) {
ssh, err := ssh.Dial("tcp", c.addr, c.config)
if err != nil {
return nil, err
}
conn := &Connection{
client: c,
logger: logger,
ssh: ssh,
}
return conn, nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

108
config.go

@ -0,0 +1,108 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### ssh ####### Copyright (c) 2021 mls-361 ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package ssh
import (
"fmt"
"io/ioutil"
"time"
"forge.chapril.org/mls-361/errors"
"golang.org/x/crypto/ssh"
)
// Config AFAIRE.
type Config struct {
Host string
Port int
Username string
Password string
KeyFile string
Passphrase string
Timeout time.Duration
}
func (cfg *Config) signer() (ssh.Signer, error) {
buf, err := ioutil.ReadFile(cfg.KeyFile)
if err != nil {
return nil, err
}
if cfg.Passphrase != "" {
key, err := ssh.ParseRawPrivateKeyWithPassphrase(buf, []byte(cfg.Passphrase))
if err != nil {
return nil, err
}
return ssh.NewSignerFromKey(key)
}
return ssh.ParsePrivateKey(buf)
}
func (cfg *Config) configure() (*ssh.ClientConfig, error) {
auths := []ssh.AuthMethod{}
if cfg.KeyFile != "" {
key, err := cfg.signer()
if err != nil {
return nil, err
}
auths = append(auths, ssh.PublicKeys(key))
}
// Nécessite "PasswordAuthentication yes" in /etc/ssh/sshd_config
if cfg.Password != "" {
auths = append(auths, ssh.Password(cfg.Password))
}
scc := &ssh.ClientConfig{
User: cfg.Username,
Auth: auths,
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // AFINIR
Timeout: cfg.Timeout,
}
return scc, nil
}
// NewClient AFAIRE.
func (cfg *Config) NewClient() (*Client, error) {
if cfg.Host == "" {
return nil, errors.New("the Host field must not be empty") /////////////////////////////////////////////////////
}
if cfg.Username == "" {
return nil, errors.New("the Username field must not be empty") /////////////////////////////////////////////////
}
config, err := cfg.configure()
if err != nil {
return nil, err
}
port := cfg.Port
if port == 0 {
port = 22
}
c := &Client{
host: cfg.Host,
port: port,
username: cfg.Username,
addr: fmt.Sprintf("%s:%d", cfg.Host, port),
config: config,
}
return c, nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

11
connection.go

@ -0,0 +1,11 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### ssh ####### Copyright (c) 2021 mls-361 ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package ssh
/*
######################################################################################################## @(°_°)@ #######
*/

11
session.go

@ -0,0 +1,11 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### ssh ####### Copyright (c) 2021 mls-361 ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package ssh
/*
######################################################################################################## @(°_°)@ #######
*/

68
ssh.go

@ -6,6 +6,74 @@
package ssh
import (
"forge.chapril.org/mls-361/errors"
"forge.chapril.org/mls-361/logger"
)
// Crypto AFAIRE.
type Crypto interface {
DecryptString(text string) (string, error)
}
// 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.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)
}
/*
######################################################################################################## @(°_°)@ #######
*/

11
stream.go

@ -0,0 +1,11 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### ssh ####### Copyright (c) 2021 mls-361 ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package ssh
/*
######################################################################################################## @(°_°)@ #######
*/
Loading…
Cancel
Save