add auth agent forward

ws
dingjun 4 years ago
parent 93ee69ff82
commit 1cb9f751c1

@ -18,6 +18,7 @@ import (
"github.com/fangdingjun/go-log/v5" "github.com/fangdingjun/go-log/v5"
socks "github.com/fangdingjun/socks-go" socks "github.com/fangdingjun/socks-go"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
) )
// Client is ssh client connection // Client is ssh client connection
@ -29,6 +30,7 @@ type Client struct {
err error err error
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
authAgent agent.ExtendedAgent
} }
// NewClient create a new ssh Client // NewClient create a new ssh Client
@ -54,6 +56,11 @@ func NewClient(c net.Conn, config *ssh.ClientConfig, addr string, conf *Conf) (*
return client, nil return client, nil
} }
// SetAuthAgent set auth agent
func (cc *Client) SetAuthAgent(_agent agent.ExtendedAgent) {
cc.authAgent = _agent
}
// Client return *ssh.Client // Client return *ssh.Client
func (cc *Client) Client() *ssh.Client { func (cc *Client) Client() *ssh.Client {
return cc.client return cc.client
@ -168,6 +175,17 @@ func (cc *Client) Shell() error {
return err return err
} }
if cc.authAgent != nil {
log.Debugln("request auth agent forwarding")
if err = agent.RequestAgentForwarding(session); err == nil {
if err1 := agent.ForwardToAgent(cc.client, cc.authAgent); err1 != nil {
log.Debugln(err)
}
} else {
log.Debugln(err)
}
}
// register console change signal // register console change signal
consoleChange(_console, session) consoleChange(_console, session)

@ -86,6 +86,7 @@ func main() {
var agentConn net.Conn var agentConn net.Conn
var err error var err error
var agentClient agent.ExtendedAgent
// read ssh agent and default auth key // read ssh agent and default auth key
if cfg.Password == "" && cfg.PrivateKey == "" { if cfg.Password == "" && cfg.PrivateKey == "" {
@ -115,7 +116,7 @@ func main() {
if err == nil { if err == nil {
defer agentConn.Close() defer agentConn.Close()
log.Debugf("add auth method with agent %s", os.Getenv("SSH_AUTH_SOCK")) log.Debugf("add auth method with agent %s", os.Getenv("SSH_AUTH_SOCK"))
agentClient := agent.NewClient(agentConn) agentClient = agent.NewClient(agentConn)
//auth = append(auth, ssh.PublicKeysCallback(agentClient.Signers)) //auth = append(auth, ssh.PublicKeysCallback(agentClient.Signers))
signers, err := agentClient.Signers() signers, err := agentClient.Signers()
if err == nil { if err == nil {
@ -268,6 +269,10 @@ func main() {
log.Debugf("ssh negotation success") log.Debugf("ssh negotation success")
if agentClient != nil {
client.SetAuthAgent(agentClient)
}
var local, remote string var local, remote string
// process port forward // process port forward

Loading…
Cancel
Save