sshpoke/internal/server/driver/ssh/driver.go

105 lines
2.7 KiB
Go
Raw Normal View History

2023-11-17 20:39:00 +03:00
package ssh
import (
"context"
"errors"
"path"
2023-11-17 20:39:00 +03:00
"sync"
"github.com/Neur0toxine/sshpoke/internal/config"
"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"
"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"
"github.com/Neur0toxine/sshpoke/pkg/dto"
"golang.org/x/crypto/ssh"
2023-11-17 20:39:00 +03:00
)
type SSH struct {
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
}
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-17 20:39:00 +03:00
if err := util.UnmarshalParams(params, &drv.params); err != nil {
return nil, err
}
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
}
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
}
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-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 17:51:04 +03:00
func (d *SSH) authenticators() []ssh.AuthMethod {
auth := d.authenticator()
if auth == nil {
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 17:51:04 +03:00
dirAuth, err := sshproto.AuthKeyDir(d.params.Auth.Directory)
if err != nil {
2023-11-18 17:51:04 +03:00
return nil
}
2023-11-18 17:51:04 +03:00
return dirAuth
}
return nil
}