mirror of
https://github.com/moul/sshportal.git
synced 2024-11-10 17:26:41 +08:00
36 lines
831 B
Go
36 lines
831 B
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
gossh "golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
type dynamicHostKey struct {
|
|
db *gorm.DB
|
|
host *Host
|
|
}
|
|
|
|
func (d *dynamicHostKey) check(hostname string, remote net.Addr, key gossh.PublicKey) error {
|
|
if len(d.host.HostKey) == 0 {
|
|
log.Println("Discovering host fingerprint...")
|
|
return d.db.Model(d.host).Update("HostKey", key.Marshal()).Error
|
|
}
|
|
|
|
if !bytes.Equal(d.host.HostKey, key.Marshal()) {
|
|
return fmt.Errorf("ssh: host key mismatch")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DynamicHostKey returns a function for use in
|
|
// ClientConfig.HostKeyCallback to dynamically learn or accept host key.
|
|
func DynamicHostKey(db *gorm.DB, host *Host) gossh.HostKeyCallback {
|
|
// FIXME: forward interactively the host key checking
|
|
hk := &dynamicHostKey{db, host}
|
|
return hk.check
|
|
}
|