mirror of
https://github.com/xor-gate/go-socks5-ssh-proxy
synced 2026-03-23 06:16:35 +01:00
Initial working version
This commit is contained in:
49
vendor/github.com/cloudfoundry/socks5-proxy/host_key.go
generated
vendored
Normal file
49
vendor/github.com/cloudfoundry/socks5-proxy/host_key.go
generated
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
type HostKey struct{}
|
||||
|
||||
func NewHostKey() HostKey {
|
||||
return HostKey{}
|
||||
}
|
||||
|
||||
func (h HostKey) Get(username, privateKey, serverURL string) (ssh.PublicKey, error) {
|
||||
publicKeyChannel := make(chan ssh.PublicKey, 1)
|
||||
dialErrorChannel := make(chan error)
|
||||
|
||||
if username == "" {
|
||||
username = "jumpbox"
|
||||
}
|
||||
|
||||
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
clientConfig := NewSSHClientConfig(username, keyScanCallback(publicKeyChannel), ssh.PublicKeys(signer))
|
||||
|
||||
go func() {
|
||||
conn, err := ssh.Dial("tcp", serverURL, clientConfig)
|
||||
if err != nil {
|
||||
publicKeyChannel <- nil
|
||||
dialErrorChannel <- err
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
dialErrorChannel <- nil
|
||||
}()
|
||||
|
||||
return <-publicKeyChannel, <-dialErrorChannel
|
||||
}
|
||||
|
||||
func keyScanCallback(publicKeyChannel chan ssh.PublicKey) ssh.HostKeyCallback {
|
||||
return func(_ string, _ net.Addr, key ssh.PublicKey) error {
|
||||
publicKeyChannel <- key
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user