| 1 |
/* |
| 2 |
Copyright 2015 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package portforward |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"net" |
| 24 |
"net/http" |
| 25 |
"sort" |
| 26 |
"strconv" |
| 27 |
"strings" |
| 28 |
"sync" |
| 29 |
|
| 30 |
v1 "k8s.io/api/core/v1" |
| 31 |
"k8s.io/apimachinery/pkg/util/httpstream" |
| 32 |
"k8s.io/apimachinery/pkg/util/runtime" |
| 33 |
netutils "k8s.io/utils/net" |
| 34 |
) |
| 35 |
|
| 36 |
// PortForwardProtocolV1Name is the subprotocol used for port forwarding. |
| 37 |
// TODO move to API machinery and re-unify with kubelet/server/portfoward |
| 38 |
const PortForwardProtocolV1Name = "portforward.k8s.io" |
| 39 |
|
| 40 |
var ( |
| 41 |
// error returned whenever we lost connection to a pod |
| 42 |
ErrLostConnectionToPod = errors.New("lost connection to pod") |
| 43 |
|
| 44 |
// set of error we're expecting during port-forwarding |
| 45 |
networkClosedError = "use of closed network connection" |
| 46 |
) |
| 47 |
|
| 48 |
// PortForwarder knows how to listen for local connections and forward them to |
| 49 |
// a remote pod via an upgraded HTTP request. |
| 50 |
type PortForwarder struct { |
| 51 |
addresses []listenAddress |
| 52 |
ports []ForwardedPort |
| 53 |
stopChan <-chan struct{} |
| 54 |
|
| 55 |
dialer httpstream.Dialer |
| 56 |
streamConn httpstream.Connection |
| 57 |
listeners []io.Closer |
| 58 |
Ready chan struct{} |
| 59 |
requestIDLock sync.Mutex |
| 60 |
requestID int |
| 61 |
out io.Writer |
| 62 |
errOut io.Writer |
| 63 |
} |
| 64 |
|
| 65 |
// ForwardedPort contains a Local:Remote port pairing. |
| 66 |
type ForwardedPort struct { |
| 67 |
Local uint16 |
| 68 |
Remote uint16 |
| 69 |
} |
| 70 |
|
| 71 |
/* |
| 72 |
valid port specifications: |
| 73 |
|
| 74 |
5000 |
| 75 |
- forwards from localhost:5000 to pod:5000 |
| 76 |
|
| 77 |
8888:5000 |
| 78 |
- forwards from localhost:8888 to pod:5000 |
| 79 |
|
| 80 |
0:5000 |
| 81 |
:5000 |
| 82 |
- selects a random available local port, |
| 83 |
forwards from localhost:<random port> to pod:5000 |
| 84 |
*/ |
| 85 |
func parsePorts(ports []string) ([]ForwardedPort, error) { |
| 86 |
var forwards []ForwardedPort |
| 87 |
for _, portString := range ports { |
| 88 |
parts := strings.Split(portString, ":") |
| 89 |
var localString, remoteString string |
| 90 |
if len(parts) == 1 { |
| 91 |
localString = parts[0] |
| 92 |
remoteString = parts[0] |
| 93 |
} else if len(parts) == 2 { |
| 94 |
localString = parts[0] |
| 95 |
if localString == "" { |
| 96 |
// support :5000 |
| 97 |
localString = "0" |
| 98 |
} |
| 99 |
remoteString = parts[1] |
| 100 |
} else { |
| 101 |
return nil, fmt.Errorf("invalid port format '%s'", portString) |
| 102 |
} |
| 103 |
|
| 104 |
localPort, err := strconv.ParseUint(localString, 10, 16) |
| 105 |
if err != nil { |
| 106 |
return nil, fmt.Errorf("error parsing local port '%s': %s", localString, err) |
| 107 |
} |
| 108 |
|
| 109 |
remotePort, err := strconv.ParseUint(remoteString, 10, 16) |
| 110 |
if err != nil { |
| 111 |
return nil, fmt.Errorf("error parsing remote port '%s': %s", remoteString, err) |
| 112 |
} |
| 113 |
if remotePort == 0 { |
| 114 |
return nil, fmt.Errorf("remote port must be > 0") |
| 115 |
} |
| 116 |
|
| 117 |
forwards = append(forwards, ForwardedPort{uint16(localPort), uint16(remotePort)}) |
| 118 |
} |
| 119 |
|
| 120 |
return forwards, nil |
| 121 |
} |
| 122 |
|
| 123 |
type listenAddress struct { |
| 124 |
address string |
| 125 |
protocol string |
| 126 |
failureMode string |
| 127 |
} |
| 128 |
|
| 129 |
func parseAddresses(addressesToParse []string) ([]listenAddress, error) { |
| 130 |
var addresses []listenAddress |
| 131 |
parsed := make(map[string]listenAddress) |
| 132 |
for _, address := range addressesToParse { |
| 133 |
if address == "localhost" { |
| 134 |
if _, exists := parsed["127.0.0.1"]; !exists { |
| 135 |
ip := listenAddress{address: "127.0.0.1", protocol: "tcp4", failureMode: "all"} |
| 136 |
parsed[ip.address] = ip |
| 137 |
} |
| 138 |
if _, exists := parsed["::1"]; !exists { |
| 139 |
ip := listenAddress{address: "::1", protocol: "tcp6", failureMode: "all"} |
| 140 |
parsed[ip.address] = ip |
| 141 |
} |
| 142 |
} else if netutils.ParseIPSloppy(address).To4() != nil { |
| 143 |
parsed[address] = listenAddress{address: address, protocol: "tcp4", failureMode: "any"} |
| 144 |
} else if netutils.ParseIPSloppy(address) != nil { |
| 145 |
parsed[address] = listenAddress{address: address, protocol: "tcp6", failureMode: "any"} |
| 146 |
} else { |
| 147 |
return nil, fmt.Errorf("%s is not a valid IP", address) |
| 148 |
} |
| 149 |
} |
| 150 |
addresses = make([]listenAddress, len(parsed)) |
| 151 |
id := 0 |
| 152 |
for _, v := range parsed { |
| 153 |
addresses[id] = v |
| 154 |
id++ |
| 155 |
} |
| 156 |
// Sort addresses before returning to get a stable order |
| 157 |
sort.Slice(addresses, func(i, j int) bool { return addresses[i].address < addresses[j].address }) |
| 158 |
|
| 159 |
return addresses, nil |
| 160 |
} |
| 161 |
|
| 162 |
// New creates a new PortForwarder with localhost listen addresses. |
| 163 |
func New(dialer httpstream.Dialer, ports []string, stopChan <-chan struct{}, readyChan chan struct{}, out, errOut io.Writer) (*PortForwarder, error) { |
| 164 |
return NewOnAddresses(dialer, []string{"localhost"}, ports, stopChan, readyChan, out, errOut) |
| 165 |
} |
| 166 |
|
| 167 |
// NewOnAddresses creates a new PortForwarder with custom listen addresses. |
| 168 |
func NewOnAddresses(dialer httpstream.Dialer, addresses []string, ports []string, stopChan <-chan struct{}, readyChan chan struct{}, out, errOut io.Writer) (*PortForwarder, error) { |
| 169 |
if len(addresses) == 0 { |
| 170 |
return nil, errors.New("you must specify at least 1 address") |
| 171 |
} |
| 172 |
parsedAddresses, err := parseAddresses(addresses) |
| 173 |
if err != nil { |
| 174 |
return nil, err |
| 175 |
} |
| 176 |
if len(ports) == 0 { |
| 177 |
return nil, errors.New("you must specify at least 1 port") |
| 178 |
} |
| 179 |
parsedPorts, err := parsePorts(ports) |
| 180 |
if err != nil { |
| 181 |
return nil, err |
| 182 |
} |
| 183 |
return &PortForwarder{ |
| 184 |
dialer: dialer, |
| 185 |
addresses: parsedAddresses, |
| 186 |
ports: parsedPorts, |
| 187 |
stopChan: stopChan, |
| 188 |
Ready: readyChan, |
| 189 |
out: out, |
| 190 |
errOut: errOut, |
| 191 |
}, nil |
| 192 |
} |
| 193 |
|
| 194 |
// ForwardPorts formats and executes a port forwarding request. The connection will remain |
| 195 |
// open until stopChan is closed. |
| 196 |
func (pf *PortForwarder) ForwardPorts() error { |
| 197 |
defer pf.Close() |
| 198 |
|
| 199 |
var err error |
| 200 |
var protocol string |
| 201 |
pf.streamConn, protocol, err = pf.dialer.Dial(PortForwardProtocolV1Name) |
| 202 |
if err != nil { |
| 203 |
return fmt.Errorf("error upgrading connection: %s", err) |
| 204 |
} |
| 205 |
defer pf.streamConn.Close() |
| 206 |
if protocol != PortForwardProtocolV1Name { |
| 207 |
return fmt.Errorf("unable to negotiate protocol: client supports %q, server returned %q", PortForwardProtocolV1Name, protocol) |
| 208 |
} |
| 209 |
|
| 210 |
return pf.forward() |
| 211 |
} |
| 212 |
|
| 213 |
// forward dials the remote host specific in req, upgrades the request, starts |
| 214 |
// listeners for each port specified in ports, and forwards local connections |
| 215 |
// to the remote host via streams. |
| 216 |
func (pf *PortForwarder) forward() error { |
| 217 |
var err error |
| 218 |
|
| 219 |
listenSuccess := false |
| 220 |
for i := range pf.ports { |
| 221 |
port := &pf.ports[i] |
| 222 |
err = pf.listenOnPort(port) |
| 223 |
switch { |
| 224 |
case err == nil: |
| 225 |
listenSuccess = true |
| 226 |
default: |
| 227 |
if pf.errOut != nil { |
| 228 |
fmt.Fprintf(pf.errOut, "Unable to listen on port %d: %v\n", port.Local, err) |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
if !listenSuccess { |
| 234 |
return fmt.Errorf("unable to listen on any of the requested ports: %v", pf.ports) |
| 235 |
} |
| 236 |
|
| 237 |
if pf.Ready != nil { |
| 238 |
close(pf.Ready) |
| 239 |
} |
| 240 |
|
| 241 |
// wait for interrupt or conn closure |
| 242 |
select { |
| 243 |
case <-pf.stopChan: |
| 244 |
case <-pf.streamConn.CloseChan(): |
| 245 |
return ErrLostConnectionToPod |
| 246 |
} |
| 247 |
|
| 248 |
return nil |
| 249 |
} |
| 250 |
|
| 251 |
// listenOnPort delegates listener creation and waits for connections on requested bind addresses. |
| 252 |
// An error is raised based on address groups (default and localhost) and their failure modes |
| 253 |
func (pf *PortForwarder) listenOnPort(port *ForwardedPort) error { |
| 254 |
var errors []error |
| 255 |
failCounters := make(map[string]int, 2) |
| 256 |
successCounters := make(map[string]int, 2) |
| 257 |
for _, addr := range pf.addresses { |
| 258 |
err := pf.listenOnPortAndAddress(port, addr.protocol, addr.address) |
| 259 |
if err != nil { |
| 260 |
errors = append(errors, err) |
| 261 |
failCounters[addr.failureMode]++ |
| 262 |
} else { |
| 263 |
successCounters[addr.failureMode]++ |
| 264 |
} |
| 265 |
} |
| 266 |
if successCounters["all"] == 0 && failCounters["all"] > 0 { |
| 267 |
return fmt.Errorf("%s: %v", "Listeners failed to create with the following errors", errors) |
| 268 |
} |
| 269 |
if failCounters["any"] > 0 { |
| 270 |
return fmt.Errorf("%s: %v", "Listeners failed to create with the following errors", errors) |
| 271 |
} |
| 272 |
return nil |
| 273 |
} |
| 274 |
|
| 275 |
// listenOnPortAndAddress delegates listener creation and waits for new connections |
| 276 |
// in the background f |
| 277 |
func (pf *PortForwarder) listenOnPortAndAddress(port *ForwardedPort, protocol string, address string) error { |
| 278 |
listener, err := pf.getListener(protocol, address, port) |
| 279 |
if err != nil { |
| 280 |
return err |
| 281 |
} |
| 282 |
pf.listeners = append(pf.listeners, listener) |
| 283 |
go pf.waitForConnection(listener, *port) |
| 284 |
return nil |
| 285 |
} |
| 286 |
|
| 287 |
// getListener creates a listener on the interface targeted by the given hostname on the given port with |
| 288 |
// the given protocol. protocol is in net.Listen style which basically admits values like tcp, tcp4, tcp6 |
| 289 |
func (pf *PortForwarder) getListener(protocol string, hostname string, port *ForwardedPort) (net.Listener, error) { |
| 290 |
listener, err := net.Listen(protocol, net.JoinHostPort(hostname, strconv.Itoa(int(port.Local)))) |
| 291 |
if err != nil { |
| 292 |
return nil, fmt.Errorf("unable to create listener: Error %s", err) |
| 293 |
} |
| 294 |
listenerAddress := listener.Addr().String() |
| 295 |
host, localPort, _ := net.SplitHostPort(listenerAddress) |
| 296 |
localPortUInt, err := strconv.ParseUint(localPort, 10, 16) |
| 297 |
|
| 298 |
if err != nil { |
| 299 |
fmt.Fprintf(pf.out, "Failed to forward from %s:%d -> %d\n", hostname, localPortUInt, port.Remote) |
| 300 |
return nil, fmt.Errorf("error parsing local port: %s from %s (%s)", err, listenerAddress, host) |
| 301 |
} |
| 302 |
port.Local = uint16(localPortUInt) |
| 303 |
if pf.out != nil { |
| 304 |
fmt.Fprintf(pf.out, "Forwarding from %s -> %d\n", net.JoinHostPort(hostname, strconv.Itoa(int(localPortUInt))), port.Remote) |
| 305 |
} |
| 306 |
|
| 307 |
return listener, nil |
| 308 |
} |
| 309 |
|
| 310 |
// waitForConnection waits for new connections to listener and handles them in |
| 311 |
// the background. |
| 312 |
func (pf *PortForwarder) waitForConnection(listener net.Listener, port ForwardedPort) { |
| 313 |
for { |
| 314 |
select { |
| 315 |
case <-pf.streamConn.CloseChan(): |
| 316 |
return |
| 317 |
default: |
| 318 |
conn, err := listener.Accept() |
| 319 |
if err != nil { |
| 320 |
// TODO consider using something like https://github.com/hydrogen18/stoppableListener? |
| 321 |
if !strings.Contains(strings.ToLower(err.Error()), networkClosedError) { |
| 322 |
runtime.HandleError(fmt.Errorf("error accepting connection on port %d: %v", port.Local, err)) |
| 323 |
} |
| 324 |
return |
| 325 |
} |
| 326 |
go pf.handleConnection(conn, port) |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
func (pf *PortForwarder) nextRequestID() int { |
| 332 |
pf.requestIDLock.Lock() |
| 333 |
defer pf.requestIDLock.Unlock() |
| 334 |
id := pf.requestID |
| 335 |
pf.requestID++ |
| 336 |
return id |
| 337 |
} |
| 338 |
|
| 339 |
// handleConnection copies data between the local connection and the stream to |
| 340 |
// the remote server. |
| 341 |
func (pf *PortForwarder) handleConnection(conn net.Conn, port ForwardedPort) { |
| 342 |
defer conn.Close() |
| 343 |
|
| 344 |
if pf.out != nil { |
| 345 |
fmt.Fprintf(pf.out, "Handling connection for %d\n", port.Local) |
| 346 |
} |
| 347 |
|
| 348 |
requestID := pf.nextRequestID() |
| 349 |
|
| 350 |
// create error stream |
| 351 |
headers := http.Header{} |
| 352 |
headers.Set(v1.StreamType, v1.StreamTypeError) |
| 353 |
headers.Set(v1.PortHeader, fmt.Sprintf("%d", port.Remote)) |
| 354 |
headers.Set(v1.PortForwardRequestIDHeader, strconv.Itoa(requestID)) |
| 355 |
errorStream, err := pf.streamConn.CreateStream(headers) |
| 356 |
if err != nil { |
| 357 |
runtime.HandleError(fmt.Errorf("error creating error stream for port %d -> %d: %v", port.Local, port.Remote, err)) |
| 358 |
return |
| 359 |
} |
| 360 |
// we're not writing to this stream |
| 361 |
errorStream.Close() |
| 362 |
defer pf.streamConn.RemoveStreams(errorStream) |
| 363 |
|
| 364 |
errorChan := make(chan error) |
| 365 |
go func() { |
| 366 |
message, err := io.ReadAll(errorStream) |
| 367 |
switch { |
| 368 |
case err != nil: |
| 369 |
errorChan <- fmt.Errorf("error reading from error stream for port %d -> %d: %v", port.Local, port.Remote, err) |
| 370 |
case len(message) > 0: |
| 371 |
errorChan <- fmt.Errorf("an error occurred forwarding %d -> %d: %v", port.Local, port.Remote, string(message)) |
| 372 |
} |
| 373 |
close(errorChan) |
| 374 |
}() |
| 375 |
|
| 376 |
// create data stream |
| 377 |
headers.Set(v1.StreamType, v1.StreamTypeData) |
| 378 |
dataStream, err := pf.streamConn.CreateStream(headers) |
| 379 |
if err != nil { |
| 380 |
runtime.HandleError(fmt.Errorf("error creating forwarding stream for port %d -> %d: %v", port.Local, port.Remote, err)) |
| 381 |
return |
| 382 |
} |
| 383 |
defer pf.streamConn.RemoveStreams(dataStream) |
| 384 |
|
| 385 |
localError := make(chan struct{}) |
| 386 |
remoteDone := make(chan struct{}) |
| 387 |
|
| 388 |
go func() { |
| 389 |
// Copy from the remote side to the local port. |
| 390 |
if _, err := io.Copy(conn, dataStream); err != nil && !strings.Contains(strings.ToLower(err.Error()), networkClosedError) { |
| 391 |
runtime.HandleError(fmt.Errorf("error copying from remote stream to local connection: %v", err)) |
| 392 |
} |
| 393 |
|
| 394 |
// inform the select below that the remote copy is done |
| 395 |
close(remoteDone) |
| 396 |
}() |
| 397 |
|
| 398 |
go func() { |
| 399 |
// inform server we're not sending any more data after copy unblocks |
| 400 |
defer dataStream.Close() |
| 401 |
|
| 402 |
// Copy from the local port to the remote side. |
| 403 |
if _, err := io.Copy(dataStream, conn); err != nil && !strings.Contains(strings.ToLower(err.Error()), networkClosedError) { |
| 404 |
runtime.HandleError(fmt.Errorf("error copying from local connection to remote stream: %v", err)) |
| 405 |
// break out of the select below without waiting for the other copy to finish |
| 406 |
close(localError) |
| 407 |
} |
| 408 |
}() |
| 409 |
|
| 410 |
// wait for either a local->remote error or for copying from remote->local to finish |
| 411 |
select { |
| 412 |
case <-remoteDone: |
| 413 |
case <-localError: |
| 414 |
} |
| 415 |
|
| 416 |
// reset dataStream to discard any unsent data, preventing port forwarding from being blocked. |
| 417 |
// we must reset dataStream before waiting on errorChan, otherwise, |
| 418 |
// the blocking data will affect errorStream and cause <-errorChan to block indefinitely. |
| 419 |
_ = dataStream.Reset() |
| 420 |
|
| 421 |
// always expect something on errorChan (it may be nil) |
| 422 |
err = <-errorChan |
| 423 |
if err != nil { |
| 424 |
runtime.HandleError(err) |
| 425 |
pf.streamConn.Close() |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
// Close stops all listeners of PortForwarder. |
| 430 |
func (pf *PortForwarder) Close() { |
| 431 |
// stop all listeners |
| 432 |
for _, l := range pf.listeners { |
| 433 |
if err := l.Close(); err != nil { |
| 434 |
runtime.HandleError(fmt.Errorf("error closing listener: %v", err)) |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
// GetPorts will return the ports that were forwarded; this can be used to |
| 440 |
// retrieve the locally-bound port in cases where the input was port 0. This |
| 441 |
// function will signal an error if the Ready channel is nil or if the |
| 442 |
// listeners are not ready yet; this function will succeed after the Ready |
| 443 |
// channel has been closed. |
| 444 |
func (pf *PortForwarder) GetPorts() ([]ForwardedPort, error) { |
| 445 |
if pf.Ready == nil { |
| 446 |
return nil, fmt.Errorf("no Ready channel provided") |
| 447 |
} |
| 448 |
select { |
| 449 |
case <-pf.Ready: |
| 450 |
return pf.ports, nil |
| 451 |
default: |
| 452 |
return nil, fmt.Errorf("listeners not ready") |
| 453 |
} |
| 454 |
} |