1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
package wish
import (
"github.com/charmbracelet/keygen"
"github.com/gliderlabs/ssh"
)
// Middleware is a function that takes an ssh.Handler and returns an
// ssh.Handler. Implementations should call the provided handler argument.
type Middleware func(ssh.Handler) ssh.Handler
// NewServer is returns a default SSH server with the provided Middleware. A
// new SSH key pair of type ed25519 will be created if one does not exist. By
// default this server will accept all incoming connections, password and
// public key.
func NewServer(ops ...ssh.Option) (*ssh.Server, error) {
s := &ssh.Server{}
// Some sensible defaults
s.Version = "OpenSSH_7.6p1"
for _, op := range ops {
if err := s.SetOption(op); err != nil {
return nil, err
}
}
if len(s.HostSigners) == 0 {
k, err := keygen.New("", "", nil, keygen.Ed25519)
if err != nil {
return nil, err
}
err = s.SetOption(WithHostKeyPEM(k.PrivateKeyPEM))
if err != nil {
return nil, err
}
}
return s, nil
}
|