MCPcopy Create free account
hub / github.com/tailscale/tailcat / hostKeyFileOrCreate

Function hostKeyFileOrCreate

tailcat_ssh.go:349–375  ·  view source on GitHub ↗
(keyDir string)

Source from the content-addressed store, hash-verified

347var hostKeyMu sync.Mutex
348
349func hostKeyFileOrCreate(keyDir string) ([]byte, error) {
350 hostKeyMu.Lock()
351 defer hostKeyMu.Unlock()
352
353 path := filepath.Join(keyDir, "ssh_host_ed25519_key")
354 v, err := os.ReadFile(path)
355 if err == nil {
356 return v, nil
357 }
358 if !os.IsNotExist(err) {
359 return nil, err
360 }
361
362 _, priv, err := ed25519.GenerateKey(rand.Reader)
363 if err != nil {
364 return nil, err
365 }
366 mk, err := x509.MarshalPKCS8PrivateKey(priv)
367 if err != nil {
368 return nil, err
369 }
370 pemData := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: mk})
371 if err := os.WriteFile(path, pemData, 0600); err != nil {
372 return nil, err
373 }
374 return pemData, nil
375}
376
377// opcodeShortName maps SSH terminal mode opcodes to mnemonic names
378// expected by the termios package.

Callers 1

getHostKeysFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected