@@ -163,8 +163,24 @@ func Listen(host string, port int, ciphers []string, keyExchanges []string, macs
163
163
},
164
164
}
165
165
166
- keyPath := filepath .Join (setting .AppDataPath , "ssh/gogs.rsa" )
167
- if ! com .IsExist (keyPath ) {
166
+ // look for all supported ssh_host_*_key formats
167
+ keyFiles := make ([]string , 0 , 1 )
168
+ for _ , keyType := range [... ]string {"rsa" , "dsa" , "ecdsa" , "ed25519" } {
169
+ keyPath := filepath .Join (setting .AppDataPath , "ssh/ssh_host_" + keyType + "_key" )
170
+ if com .IsExist (keyPath ) {
171
+ keyFiles = append (keyFiles , keyPath )
172
+ }
173
+ }
174
+
175
+ // also check for legacy gogs.rsa, only if no openssh-named keys were found
176
+ oldKeyFile := filepath .Join (setting .AppDataPath , "ssh/gogs.rsa" )
177
+ if len (keyFiles ) == 0 && com .IsExist (oldKeyFile ) {
178
+ keyFiles = append (keyFiles , oldKeyFile )
179
+ }
180
+
181
+ // if no keys found, create an RSA key
182
+ if len (keyFiles ) == 0 {
183
+ keyPath := filepath .Join (setting .AppDataPath , "ssh/ssh_host_rsa_key" )
168
184
filePath := filepath .Dir (keyPath )
169
185
170
186
if err := os .MkdirAll (filePath , os .ModePerm ); err != nil {
@@ -175,12 +191,17 @@ func Listen(host string, port int, ciphers []string, keyExchanges []string, macs
175
191
if err != nil {
176
192
log .Fatal ("Failed to generate private key: %v" , err )
177
193
}
178
- log .Trace ("New private key is generated: %s" , keyPath )
194
+ log .Trace ("SSH: New private key is generateed: %s" , keyPath )
195
+ keyFiles = append (keyFiles , keyPath )
179
196
}
180
197
181
- err := srv .SetOption (ssh .HostKeyFile (keyPath ))
182
- if err != nil {
183
- log .Error ("Failed to set Host Key. %s" , err )
198
+ for _ , keyPath := range keyFiles {
199
+ err := srv .SetOption (ssh .HostKeyFile (keyPath ))
200
+ if err != nil {
201
+ log .Error ("Failed to set SSH Host Key %s: %s" , keyPath , err )
202
+ } else {
203
+ log .Trace ("SSH: loaded host key %s" , keyPath )
204
+ }
184
205
}
185
206
186
207
go func () {
0 commit comments