11import * as ssh2 from 'ssh2' ;
2- import * as fs from 'fs' ;
32import * as bcrypt from 'bcryptjs' ;
43import { getSSHConfig , getMaxPackSizeBytes , getDomains } from '../../config' ;
54import { serverConfig } from '../../config/env' ;
@@ -15,6 +14,7 @@ import {
1514import { ClientWithUser } from './types' ;
1615import { createMockResponse } from './sshHelpers' ;
1716import { processGitUrl } from '../routes/helper' ;
17+ import { ensureHostKey } from './hostKeyManager' ;
1818
1919export class SSHServer {
2020 private server : ssh2 . Server ;
@@ -23,16 +23,22 @@ export class SSHServer {
2323 const sshConfig = getSSHConfig ( ) ;
2424 const privateKeys : Buffer [ ] = [ ] ;
2525
26+ // Ensure the SSH host key exists (generates automatically if needed)
27+ // This key identifies the PROXY SERVER to connecting clients, similar to an SSL certificate.
28+ // It is NOT used for authenticating to remote Git servers - agent forwarding handles that.
2629 try {
27- privateKeys . push ( fs . readFileSync ( sshConfig . hostKey . privateKeyPath ) ) ;
30+ const hostKey = ensureHostKey ( sshConfig . hostKey ) ;
31+ privateKeys . push ( hostKey ) ;
2832 } catch ( error ) {
33+ console . error ( '[SSH] Failed to initialize proxy host key' ) ;
2934 console . error (
30- `Error reading private key at ${ sshConfig . hostKey . privateKeyPath } . Check your SSH host key configuration or disbale SSH. ` ,
35+ `[SSH] ${ error instanceof Error ? error . message : String ( error ) } ` ,
3136 ) ;
37+ console . error ( '[SSH] Cannot start SSH server without a valid host key.' ) ;
3238 process . exit ( 1 ) ;
3339 }
3440
35- // TODO: Server config could go to config file
41+ // Initialize SSH server with secure defaults
3642 this . server = new ssh2 . Server (
3743 {
3844 hostKeys : privateKeys ,
0 commit comments