make known_host persistent
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 20s
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 20s
This commit is contained in:
@@ -7,4 +7,9 @@ services:
|
||||
- SYS_CHROOT
|
||||
environment:
|
||||
- COMMAND=/app/tui
|
||||
volumes:
|
||||
- ssh_host_key:/app/host_key
|
||||
restart: unless-stopped
|
||||
|
||||
volumes:
|
||||
ssh_host_key:
|
||||
|
||||
20
main.go
20
main.go
@@ -4,6 +4,7 @@ import (
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"encoding/pem"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
@@ -23,14 +24,31 @@ func main() {
|
||||
},
|
||||
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)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
signer, err := ssh.NewSignerFromKey(key)
|
||||
signer, err = ssh.NewSignerFromKey(key)
|
||||
if err != nil {
|
||||
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)
|
||||
listener, err := net.Listen("tcp", ":22")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user