ziggs/internal/sshui/server.go

45 lines
1014 B
Go
Raw Normal View History

2023-01-08 01:28:39 +00:00
package sshui
import (
"crypto/rand"
"crypto/rsa"
"os"
"path/filepath"
2023-01-08 01:28:39 +00:00
"github.com/gliderlabs/ssh"
"git.tcp.direct/kayos/ziggs/internal/config"
"git.tcp.direct/kayos/ziggs/internal/data"
2023-01-08 01:28:39 +00:00
)
func ServeSSH() error {
2023-01-08 01:28:39 +00:00
var opts []ssh.Option
switch config.SSHHostKey {
case "":
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return err
}
if err = privateKey.Validate(); err != nil {
return err
}
dir, _ := filepath.Split(config.Filename)
newFile := filepath.Join(dir, "host_rsa")
if err = os.WriteFile(newFile, encodePrivateKeyToPEM(privateKey), 0600); err != nil {
return err
}
config.Snek.Set("ssh.host_key", newFile)
default:
2023-01-08 01:28:39 +00:00
opts = append(opts, ssh.HostKeyFile(config.SSHHostKey))
}
opts = append(opts, ssh.PasswordAuth(func(ctx ssh.Context, password string) bool {
attempt := data.NewUserPass(false, ctx.User(), password)
err := attempt.Authenticate()
return err == nil
2023-01-08 01:28:39 +00:00
}))
return ssh.ListenAndServe(config.SSHListen, nil, opts...)
2023-01-08 01:28:39 +00:00
}