make known_host persistent
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 20s

This commit is contained in:
2025-11-06 20:12:28 +01:00
parent d9df32f7cf
commit 4d5d14bb26
2 changed files with 30 additions and 7 deletions

View File

@@ -7,4 +7,9 @@ services:
- SYS_CHROOT - SYS_CHROOT
environment: environment:
- COMMAND=/app/tui - COMMAND=/app/tui
volumes:
- ssh_host_key:/app/host_key
restart: unless-stopped restart: unless-stopped
volumes:
ssh_host_key:

20
main.go
View File

@@ -4,6 +4,7 @@ import (
"crypto/ed25519" "crypto/ed25519"
"crypto/rand" "crypto/rand"
"encoding/binary" "encoding/binary"
"encoding/pem"
"io" "io"
"log" "log"
"net" "net"
@@ -23,14 +24,31 @@ func main() {
}, },
NoClientAuth: true, NoClientAuth: true,
} }
var signer ssh.Signer
keyFile := "/app/host_key"
if data, err := os.ReadFile(keyFile); err == nil {
signer, err = ssh.ParsePrivateKey(data)
if err != nil {
log.Fatal("Failed to parse existing host key:", err)
}
} else {
_, key, err := ed25519.GenerateKey(rand.Reader) _, key, err := ed25519.GenerateKey(rand.Reader)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
signer, err := ssh.NewSignerFromKey(key) signer, err = ssh.NewSignerFromKey(key)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
block, err := ssh.MarshalPrivateKey(signer, "")
if err != nil {
log.Fatal("Failed to marshal host key:", err)
}
privateKeyBytes := pem.EncodeToMemory(block)
if err := os.WriteFile(keyFile, privateKeyBytes, 0600); err != nil {
log.Fatal("Failed to save host key:", err)
}
}
config.AddHostKey(signer) config.AddHostKey(signer)
listener, err := net.Listen("tcp", ":22") listener, err := net.Listen("tcp", ":22")
if err != nil { if err != nil {