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.
(l net.Listener, s Session)
| 51 | // session on the OpenSSH channel for agent connections. It blocks and services |
| 52 | // connections until the listener stop accepting. |
| 53 | func ForwardAgentConnections(l net.Listener, s Session) { |
| 54 | sshConn := s.Context().Value(ContextKeyConn).(gossh.Conn) |
| 55 | for { |
| 56 | conn, err := l.Accept() |
| 57 | if err != nil { |
| 58 | return |
| 59 | } |
| 60 | go func(conn net.Conn) { |
| 61 | defer conn.Close() |
| 62 | channel, reqs, err := sshConn.OpenChannel(agentChannelType, nil) |
| 63 | if err != nil { |
| 64 | return |
| 65 | } |
| 66 | defer channel.Close() |
| 67 | go gossh.DiscardRequests(reqs) |
| 68 | var wg sync.WaitGroup |
| 69 | wg.Add(2) |
| 70 | go func() { |
| 71 | io.Copy(conn, channel) |
| 72 | conn.(*net.UnixConn).CloseWrite() |
| 73 | wg.Done() |
| 74 | }() |
| 75 | go func() { |
| 76 | io.Copy(channel, conn) |
| 77 | channel.CloseWrite() |
| 78 | wg.Done() |
| 79 | }() |
| 80 | wg.Wait() |
| 81 | }(conn) |
| 82 | } |
| 83 | } |