github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/libnetwork/cmd/proxy/proxy.go (about)

     1  // docker-proxy provides a network Proxy interface and implementations for TCP
     2  // and UDP.
     3  package main
     4  
     5  import (
     6  	"net"
     7  
     8  	"github.com/ishidawataru/sctp"
     9  )
    10  
    11  // ipVersion refers to IP version - v4 or v6
    12  type ipVersion string
    13  
    14  const (
    15  	// IPv4 is version 4
    16  	ipv4 ipVersion = "4"
    17  	// IPv4 is version 6
    18  	ipv6 ipVersion = "6"
    19  )
    20  
    21  // Proxy defines the behavior of a proxy. It forwards traffic back and forth
    22  // between two endpoints : the frontend and the backend.
    23  // It can be used to do software port-mapping between two addresses.
    24  // e.g. forward all traffic between the frontend (host) 127.0.0.1:3000
    25  // to the backend (container) at 172.17.42.108:4000.
    26  type Proxy interface {
    27  	// Run starts forwarding traffic back and forth between the front
    28  	// and back-end addresses.
    29  	Run()
    30  	// Close stops forwarding traffic and close both ends of the Proxy.
    31  	Close()
    32  	// FrontendAddr returns the address on which the proxy is listening.
    33  	FrontendAddr() net.Addr
    34  	// BackendAddr returns the proxied address.
    35  	BackendAddr() net.Addr
    36  }
    37  
    38  // NewProxy creates a Proxy according to the specified frontendAddr and backendAddr.
    39  func NewProxy(frontendAddr, backendAddr net.Addr) (Proxy, error) {
    40  	switch frontendAddr.(type) {
    41  	case *net.UDPAddr:
    42  		return NewUDPProxy(frontendAddr.(*net.UDPAddr), backendAddr.(*net.UDPAddr))
    43  	case *net.TCPAddr:
    44  		return NewTCPProxy(frontendAddr.(*net.TCPAddr), backendAddr.(*net.TCPAddr))
    45  	case *sctp.SCTPAddr:
    46  		return NewSCTPProxy(frontendAddr.(*sctp.SCTPAddr), backendAddr.(*sctp.SCTPAddr))
    47  	default:
    48  		panic("Unsupported protocol")
    49  	}
    50  }