...

Source file src/k8s.io/kubernetes/build/pause/windows/wincat/wincat.go

Documentation: k8s.io/kubernetes/build/pause/windows/wincat

     1  /*
     2  Copyright 2020 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 main connects to the given host and port and redirects its stdin to the connection and
    18  // the connection's output to stdout. This is currently being used for port-forwarding for Windows Pods.
    19  package main
    20  
    21  import (
    22  	"fmt"
    23  	"io"
    24  	"log"
    25  	"net"
    26  	"os"
    27  	"sync"
    28  )
    29  
    30  func main() {
    31  	if len(os.Args) != 3 {
    32  		log.Fatalln("usage: wincat <host> <port>")
    33  	}
    34  	host := os.Args[1]
    35  	port := os.Args[2]
    36  
    37  	addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%s", host, port))
    38  	if err != nil {
    39  		log.Fatalf("Failed to resolve TCP addr %v %v", host, port)
    40  	}
    41  
    42  	conn, err := net.DialTCP("tcp", nil, addr)
    43  	if err != nil {
    44  		log.Fatalf("Failed to connect to %s:%s because %s", host, port, err)
    45  	}
    46  	defer conn.Close()
    47  
    48  	var wg sync.WaitGroup
    49  	wg.Add(2)
    50  
    51  	go func() {
    52  		defer func() {
    53  			os.Stdout.Close()
    54  			os.Stdin.Close()
    55  			conn.CloseRead()
    56  			wg.Done()
    57  		}()
    58  
    59  		_, err := io.Copy(os.Stdout, conn)
    60  		if err != nil {
    61  			log.Printf("error while copying stream to stdout: %v", err)
    62  		}
    63  	}()
    64  
    65  	go func() {
    66  		defer func() {
    67  			conn.CloseWrite()
    68  			wg.Done()
    69  		}()
    70  
    71  		_, err := io.Copy(conn, os.Stdin)
    72  		if err != nil {
    73  			log.Printf("error while copying stream from stdin: %v", err)
    74  		}
    75  	}()
    76  
    77  	wg.Wait()
    78  }
    79  

View as plain text