package sshimport (gossh)const ( agentRequestType = "auth-agent-req@openssh.com" agentChannelType = "auth-agent@openssh.com" agentTempDir = "auth-agent" agentListenFile = "listener.sock")// contextKeyAgentRequest is an internal context key for storing if the// client requested agent forwardingvar contextKeyAgentRequest = &contextKey{"auth-agent-req"}// SetAgentRequested sets up the session context so that AgentRequested// returns true.func ( Context) { .SetValue(contextKeyAgentRequest, true)}// AgentRequested returns true if the client requested agent forwarding.func ( Session) bool {return .Context().Value(contextKeyAgentRequest) == true}// NewAgentListener sets up a temporary Unix socket that can be communicated// to the session environment and used for forwarding connections.func () (net.Listener, error) { , := os.MkdirTemp("", agentTempDir)if != nil {returnnil, } , := net.Listen("unix", path.Join(, agentListenFile))if != nil {returnnil, }return , nil}// ForwardAgentConnections takes connections from a listener to proxy into the// session on the OpenSSH channel for agent connections. It blocks and services// connections until the listener stop accepting.func ( net.Listener, Session) { := .Context().Value(ContextKeyConn).(gossh.Conn)for { , := .Accept()if != nil {return }gofunc( net.Conn) {defer .Close() , , := .OpenChannel(agentChannelType, nil)if != nil {return }defer .Close()gogossh.DiscardRequests()varsync.WaitGroup .Add(2)gofunc() {io.Copy(, ) .(*net.UnixConn).CloseWrite() .Done() }()gofunc() {io.Copy(, ) .CloseWrite() .Done() }() .Wait() }() }}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.