2016-06-09 07:31:24 -04:00
|
|
|
// docker-proxy provides a network Proxy interface and implementations for TCP
|
|
|
|
// and UDP.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2017-06-13 01:29:56 -04:00
|
|
|
|
|
|
|
"github.com/ishidawataru/sctp"
|
2016-06-09 07:31:24 -04:00
|
|
|
)
|
|
|
|
|
2020-12-13 18:56:30 -05:00
|
|
|
// ipVersion refers to IP version - v4 or v6
|
|
|
|
type ipVersion string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// IPv4 is version 4
|
|
|
|
ipv4 ipVersion = "4"
|
|
|
|
// IPv4 is version 6
|
|
|
|
ipv6 ipVersion = "6"
|
|
|
|
)
|
|
|
|
|
2016-06-09 07:31:24 -04:00
|
|
|
// Proxy defines the behavior of a proxy. It forwards traffic back and forth
|
|
|
|
// between two endpoints : the frontend and the backend.
|
|
|
|
// It can be used to do software port-mapping between two addresses.
|
|
|
|
// e.g. forward all traffic between the frontend (host) 127.0.0.1:3000
|
|
|
|
// to the backend (container) at 172.17.42.108:4000.
|
|
|
|
type Proxy interface {
|
|
|
|
// Run starts forwarding traffic back and forth between the front
|
|
|
|
// and back-end addresses.
|
|
|
|
Run()
|
|
|
|
// Close stops forwarding traffic and close both ends of the Proxy.
|
|
|
|
Close()
|
|
|
|
// FrontendAddr returns the address on which the proxy is listening.
|
|
|
|
FrontendAddr() net.Addr
|
|
|
|
// BackendAddr returns the proxied address.
|
|
|
|
BackendAddr() net.Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProxy creates a Proxy according to the specified frontendAddr and backendAddr.
|
|
|
|
func NewProxy(frontendAddr, backendAddr net.Addr) (Proxy, error) {
|
|
|
|
switch frontendAddr.(type) {
|
|
|
|
case *net.UDPAddr:
|
|
|
|
return NewUDPProxy(frontendAddr.(*net.UDPAddr), backendAddr.(*net.UDPAddr))
|
|
|
|
case *net.TCPAddr:
|
|
|
|
return NewTCPProxy(frontendAddr.(*net.TCPAddr), backendAddr.(*net.TCPAddr))
|
2017-06-13 01:29:56 -04:00
|
|
|
case *sctp.SCTPAddr:
|
|
|
|
return NewSCTPProxy(frontendAddr.(*sctp.SCTPAddr), backendAddr.(*sctp.SCTPAddr))
|
2016-06-09 07:31:24 -04:00
|
|
|
default:
|
2016-12-03 00:22:42 -05:00
|
|
|
panic("Unsupported protocol")
|
2016-06-09 07:31:24 -04:00
|
|
|
}
|
|
|
|
}
|