agent now uses a fixed host key using the go embed package.

Printing welcome message when user logs in.
This commit is contained in:
Erik Brakkee 2024-07-21 11:22:05 +02:00
parent 8981efd0b5
commit c25129a9bf
2 changed files with 49 additions and 34 deletions

View File

@ -4,10 +4,6 @@ import (
"bufio"
"cidebug/pkg/iowrappers"
"cidebug/pkg/websocketutil"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/gorilla/websocket"
"io"
@ -22,8 +18,13 @@ import (
"github.com/gliderlabs/ssh"
"github.com/hashicorp/yamux"
"github.com/pkg/sftp"
_ "embed"
)
//go:embed hostkey.pem
var hostPrivateKey []byte
func SftpHandler(sess ssh.Session) {
debugStream := io.Discard
serverOptions := []sftp.ServerOption{
@ -57,6 +58,8 @@ func setWinsize(f *os.File, w, h int) {
func sshServer(hostKeyFile string) *ssh.Server {
ssh.Handle(func(s ssh.Session) {
hostname, _ := os.Hostname()
io.WriteString(s, fmt.Sprintf("Your are now on %s\n\n", hostname))
cmd := exec.Command("bash")
ptyReq, winCh, isPty := s.Pty()
if isPty {
@ -88,11 +91,13 @@ func sshServer(hostKeyFile string) *ssh.Server {
"sftp": SftpHandler,
},
}
err := generateHostKey(hostKeyFile, 2048)
if err != nil {
log.Printf("Could not create host key file '%s': %v", hostKeyFile, err)
}
option := ssh.HostKeyFile(hostKeyFile)
//err := generateHostKey(hostKeyFile, 2048)
//if err != nil {
// log.Printf("Could not create host key file '%s': %v", hostKeyFile, err)
//}
//option := ssh.HostKeyFile(hostKeyFile)
option := ssh.HostKeyPEM(hostPrivateKey)
option(&server)
return &server
@ -138,31 +143,6 @@ func (f ReaderFunc) Read(p []byte) (n int, err error) {
return f(p)
}
func generateHostKey(filename string, bitSize int) error {
if _, err := os.Stat(filename); !os.IsNotExist(err) {
log.Printf("Reusing key file '%s'", filename)
return nil
}
privateKey, err := rsa.GenerateKey(rand.Reader, bitSize)
if err != nil {
return err
}
privateKeyPEM := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
privateKeyFile, err := os.Create(filename)
if err != nil {
return err
}
defer privateKeyFile.Close()
log.Printf("Generating key '%s'", filename)
return pem.Encode(privateKeyFile, privateKeyPEM)
}
func main() {
wsURL := os.Args[1]
conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)

35
pkg/sshutils/ssh.go Normal file
View File

@ -0,0 +1,35 @@
package sshutils
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"log"
"os"
)
func generateHostKey(filename string, bitSize int) error {
if _, err := os.Stat(filename); !os.IsNotExist(err) {
log.Printf("Reusing key file '%s'", filename)
return nil
}
privateKey, err := rsa.GenerateKey(rand.Reader, bitSize)
if err != nil {
return err
}
privateKeyPEM := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
privateKeyFile, err := os.Create(filename)
if err != nil {
return err
}
defer privateKeyFile.Close()
log.Printf("Generating key '%s'", filename)
return pem.Encode(privateKeyFile, privateKeyPEM)
}