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 forwarding.var 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) {deferrecoverAndLog("panic forwarding agent connection", nil, nil)deferfunc() { _ = .Close() }() , , := .OpenChannel(agentChannelType, nil)if != nil {return }deferfunc() { _ = .Close() }()gogossh.DiscardRequests()varsync.WaitGroup .Add(2)gofunc() {// Done is deferred ahead of the recover so that it runs even // if the copy panics. Otherwise the recover would turn a crash // into a permanently blocked Wait below, leaking this // goroutine along with the socket and the channel.defer .Done()deferrecoverAndLog("panic proxying agent connection", nil, nil) _, _ = io.Copy(, ) _ = .(*net.UnixConn).CloseWrite() }()gofunc() {defer .Done()deferrecoverAndLog("panic proxying agent connection", nil, nil) _, _ = io.Copy(, ) _ = .CloseWrite() }() .Wait() }() }}
The pages are generated with Goldsv0.8.7. (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.