1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package tunnelssh
import (
"bytes"
"context"
"fmt"
"io"
"log"
"math"
"net"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/microsoft/dev-tunnels/go/tunnels/ssh/messages"
"golang.org/x/crypto/ssh"
)
type portForwardingManager interface {
Add(port uint16)
}
type ClientSSHSession struct {
*SSHSession
pf portForwardingManager
listenersMu sync.Mutex
listeners []net.Listener
channels uint32
acceptLocalConn bool
forwardedPorts map[uint16]uint16
}
func NewClientSSHSession(socket net.Conn, pf portForwardingManager, acceptLocalConn bool, logger *log.Logger) *ClientSSHSession {
return &ClientSSHSession{
SSHSession: &SSHSession{
socket: socket,
logger: logger,
},
pf: pf,
acceptLocalConn: acceptLocalConn,
listeners: make([]net.Listener, 0),
forwardedPorts: make(map[uint16]uint16),
}
}
func (s *ClientSSHSession) Connect(ctx context.Context) error {
clientConfig := ssh.ClientConfig{
// For now, the client is allowed to skip SSH authentication;
// they must have a valid tunnel access token already to get this far.
User: "tunnel",
Timeout: 10 * time.Second,
// TODO: Validate host public keys match those published to the service?
// For now, the assumption is only a host with access to the tunnel can get a token
// that enables listening for tunnel connections.
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
sshClientConn, chans, reqs, err := ssh.NewClientConn(s.socket, "", &clientConfig)
if err != nil {
return fmt.Errorf("error creating ssh client connection: %w", err)
}
s.conn = sshClientConn
go s.handleGlobalRequests(reqs)
sshClient := ssh.NewClient(sshClientConn, chans, nil)
s.Session, err = sshClient.NewSession()
if err != nil {
return fmt.Errorf("error creating ssh client session: %w", err)
}
s.reader, err = s.Session.StdoutPipe()
if err != nil {
return fmt.Errorf("error creating ssh session reader: %w", err)
}
s.writer, err = s.Session.StdinPipe()
if err != nil {
return fmt.Errorf("error creating ssh session writer: %w", err)
}
return nil
}
func (s *ClientSSHSession) handleGlobalRequests(incoming <-chan *ssh.Request) {
for r := range incoming {
switch r.Type {
case messages.PortForwardRequestType:
s.handlePortForwardRequest(r)
default:
// This handles keepalive messages and matches
// the behaviour of OpenSSH.
r.Reply(false, nil)
}
}
}
func (s *ClientSSHSession) handlePortForwardRequest(r *ssh.Request) {
req := new(messages.PortForwardRequest)
buf := bytes.NewReader(r.Payload)
if err := req.Unmarshal(buf); err != nil {
s.logger.Printf(fmt.Sprintf("error unmarshalling port forward request: %s", err))
r.Reply(false, nil)
return
}
s.pf.Add(uint16(req.Port()))
if s.acceptLocalConn {
go s.forwardPort(context.Background(), uint16(req.Port()))
}
reply := messages.NewPortForwardSuccess(req.Port())
b, err := reply.Marshal()
if err != nil {
s.logger.Printf(fmt.Sprintf("error marshaling port forward success response: %s", err))
r.Reply(false, nil)
return
}
r.Reply(true, b)
}
func (s *ClientSSHSession) OpenChannel(ctx context.Context, channelType string, data []byte) (ssh.Channel, error) {
channel, reqs, err := s.conn.OpenChannel(channelType, data)
if err != nil {
return nil, fmt.Errorf("failed to open channel: %w", err)
}
go ssh.DiscardRequests(reqs)
return channel, nil
}
func (s *ClientSSHSession) forwardPort(ctx context.Context, port uint16) error {
var listener net.Listener
var i uint16 = 0
for i < 10 {
portNum := port + i
innerListener, err := net.Listen("tcp", fmt.Sprintf(":%d", portNum))
if err == nil {
listener = innerListener
break
}
i++
}
if listener == nil {
innerListener, err := net.Listen("tcp", ":0")
if err != nil {
return fmt.Errorf("error creating listener: %w", err)
}
listener = innerListener
}
addressSlice := strings.Split(listener.Addr().String(), ":")
portNum, err := strconv.ParseUint(addressSlice[len(addressSlice)-1], 10, 16)
if err != nil {
return fmt.Errorf("error getting port number: %w", err)
}
if portNum > 0 && portNum <= math.MaxUint16 {
s.forwardedPorts[port] = uint16(portNum)
} else {
return fmt.Errorf("port number %d is out of bounds", portNum)
}
errc := make(chan error, 1)
sendError := func(err error) {
// Use non-blocking send, to avoid goroutines getting
// stuck in case of concurrent or sequential errors.
select {
case errc <- err:
default:
}
}
fmt.Printf("Client connected at %v to host port %v\n", listener.Addr(), port)
go func() {
for {
conn, err := listener.Accept()
if err != nil {
sendError(err)
return
}
s.listenersMu.Lock()
s.listeners = append(s.listeners, listener)
s.listenersMu.Unlock()
go func() {
if err := s.handleConnection(ctx, conn, port); err != nil {
sendError(err)
}
}()
}
}()
return awaitError(ctx, errc)
}
func (s *ClientSSHSession) handleConnection(ctx context.Context, conn io.ReadWriteCloser, port uint16) (err error) {
defer safeClose(conn, &err)
channel, err := s.openStreamingChannel(ctx, port)
if err != nil {
return fmt.Errorf("failed to open streaming channel: %w", err)
}
// Ideally we would call safeClose again, but (*ssh.channel).Close
// appears to have a bug that causes it return io.EOF spuriously
// if its peer closed first; see github.com/golang/go/issues/38115.
defer func() {
closeErr := channel.Close()
if err == nil && closeErr != io.EOF {
err = closeErr
}
}()
errs := make(chan error, 2)
copyConn := func(w io.Writer, r io.Reader) {
_, err := io.Copy(w, r)
errs <- err
}
go copyConn(conn, channel)
go copyConn(channel, conn)
// Wait until context is cancelled or both copies are done.
// Discard errors from io.Copy; they should not cause (e.g.) failures.
for i := 0; ; {
select {
case <-ctx.Done():
return ctx.Err()
case <-errs:
i++
if i == 2 {
return nil
}
}
}
}
func (s *ClientSSHSession) NextChannelID() uint32 {
return atomic.AddUint32(&s.channels, 1)
}
func (s *ClientSSHSession) openStreamingChannel(ctx context.Context, port uint16) (ssh.Channel, error) {
portForwardChannel := messages.NewPortForwardChannel(
s.NextChannelID(),
"127.0.0.1",
uint32(port),
"",
0,
)
data, err := portForwardChannel.Marshal()
if err != nil {
return nil, fmt.Errorf("failed to marshal port forward channel open message: %w", err)
}
channel, err := s.OpenChannel(ctx, portForwardChannel.Type(), data)
if err != nil {
return nil, fmt.Errorf("failed to open port forward channel: %w", err)
}
return channel, nil
}
func (s *ClientSSHSession) Close() error {
if s.Session != nil {
s.Session.Close()
}
if s.conn != nil {
s.conn.Close()
}
if s.socket != nil {
s.socket.Close()
}
for _, listener := range s.listeners {
listener.Close()
}
return nil
}
|