2023-11-17 20:39:00 +03:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-11-18 16:14:39 +03:00
|
|
|
"path"
|
2023-11-17 20:39:00 +03:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/Neur0toxine/sshpoke/internal/config"
|
2023-11-18 16:14:39 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/internal/server/driver/base"
|
2023-11-18 17:51:04 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/internal/server/driver/ssh/sshproto"
|
2023-11-18 16:14:39 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/internal/server/driver/ssh/types"
|
2023-11-17 20:39:00 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/internal/server/driver/util"
|
2023-11-18 12:36:17 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/pkg/dto"
|
2023-11-18 16:14:39 +03:00
|
|
|
"golang.org/x/crypto/ssh"
|
2023-11-17 20:39:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type SSH struct {
|
2023-11-18 16:14:39 +03:00
|
|
|
base.Base
|
2023-11-18 17:51:04 +03:00
|
|
|
params Params
|
|
|
|
proto *sshproto.Client
|
|
|
|
wg sync.WaitGroup
|
2023-11-17 20:39:00 +03:00
|
|
|
}
|
|
|
|
|
2023-11-18 16:14:39 +03:00
|
|
|
func New(ctx context.Context, name string, params config.DriverParams) (base.Driver, error) {
|
|
|
|
drv := &SSH{
|
2023-11-18 17:51:04 +03:00
|
|
|
Base: base.New(ctx, name),
|
2023-11-18 16:14:39 +03:00
|
|
|
}
|
2023-11-17 20:39:00 +03:00
|
|
|
if err := util.UnmarshalParams(params, &drv.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-18 16:14:39 +03:00
|
|
|
drv.populateFromSSHConfig()
|
2023-11-18 17:51:04 +03:00
|
|
|
drv.proto = sshproto.New(drv.params.Address, drv.params.Auth.User, drv.authenticators(), drv.Log())
|
|
|
|
go drv.proto.Connect(drv.Context())
|
2023-11-17 20:39:00 +03:00
|
|
|
return drv, nil
|
|
|
|
}
|
|
|
|
|
2023-11-18 16:14:39 +03:00
|
|
|
func (d *SSH) populateFromSSHConfig() {
|
|
|
|
if d.params.Auth.Directory == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cfg, err := parseSSHConfig(types.SmartPath(path.Join(string(d.params.Auth.Directory), "config")))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if user, err := cfg.Get(d.params.Address, "User"); err == nil && user != "" {
|
|
|
|
d.params.Auth.User = user
|
|
|
|
}
|
|
|
|
if usePass, err := cfg.Get(d.params.Address, "PasswordAuthentication"); err == nil && usePass == "yes" {
|
|
|
|
d.params.Auth.Type = types.AuthTypePassword
|
|
|
|
}
|
|
|
|
if keyfile, err := cfg.Get(d.params.Address, "IdentityFile"); err == nil && keyfile != "" {
|
|
|
|
resolvedKeyFile, err := types.SmartPath(keyfile).Resolve(false)
|
|
|
|
if err == nil {
|
|
|
|
d.params.Auth.Type = types.AuthTypeKey
|
|
|
|
d.params.Auth.Keyfile = resolvedKeyFile
|
|
|
|
}
|
|
|
|
}
|
2023-11-17 20:39:00 +03:00
|
|
|
}
|
|
|
|
|
2023-11-18 16:14:39 +03:00
|
|
|
func (d *SSH) Handle(event dto.Event) error {
|
|
|
|
// TODO: Implement event handling & connections management.
|
|
|
|
return errors.New("server handler is not implemented yet")
|
2023-11-18 12:36:17 +03:00
|
|
|
}
|
|
|
|
|
2023-11-17 20:39:00 +03:00
|
|
|
func (d *SSH) Driver() config.DriverType {
|
|
|
|
return config.DriverSSH
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SSH) WaitForShutdown() {
|
|
|
|
d.wg.Wait()
|
|
|
|
}
|
2023-11-18 16:14:39 +03:00
|
|
|
|
2023-11-18 17:51:04 +03:00
|
|
|
func (d *SSH) authenticators() []ssh.AuthMethod {
|
|
|
|
auth := d.authenticator()
|
|
|
|
if auth == nil {
|
2023-11-18 16:14:39 +03:00
|
|
|
return nil
|
|
|
|
}
|
2023-11-18 17:51:04 +03:00
|
|
|
return []ssh.AuthMethod{auth}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SSH) authenticator() ssh.AuthMethod {
|
|
|
|
switch d.params.Auth.Type {
|
|
|
|
case types.AuthTypePasswordless:
|
|
|
|
return sshproto.AuthPassword("")
|
|
|
|
case types.AuthTypePassword:
|
|
|
|
return sshproto.AuthPassword(d.params.Auth.Password)
|
|
|
|
case types.AuthTypeKey:
|
|
|
|
if d.params.Auth.Keyfile != "" {
|
|
|
|
keyAuth, err := sshproto.AuthKeyFile(
|
|
|
|
types.SmartPath(path.Join(d.params.Auth.Directory.String(), d.params.Auth.Keyfile)))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return keyAuth
|
2023-11-18 16:14:39 +03:00
|
|
|
}
|
2023-11-18 17:51:04 +03:00
|
|
|
dirAuth, err := sshproto.AuthKeyDir(d.params.Auth.Directory)
|
2023-11-18 16:14:39 +03:00
|
|
|
if err != nil {
|
2023-11-18 17:51:04 +03:00
|
|
|
return nil
|
2023-11-18 16:14:39 +03:00
|
|
|
}
|
2023-11-18 17:51:04 +03:00
|
|
|
return dirAuth
|
2023-11-18 16:14:39 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|