2015-07-25 04:35:07 -04:00
|
|
|
// Package proxy provides a network Proxy interface and implementations for TCP
|
|
|
|
// and UDP.
|
2013-10-04 22:25:15 -04:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2015-07-25 04:35:07 -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.
|
2013-10-04 22:25:15 -04:00
|
|
|
type Proxy interface {
|
2015-07-25 04:35:07 -04:00
|
|
|
// Run starts forwarding traffic back and forth between the front
|
|
|
|
// and back-end addresses.
|
2013-10-04 22:25:15 -04:00
|
|
|
Run()
|
2015-07-25 04:35:07 -04:00
|
|
|
// Close stops forwarding traffic and close both ends of the Proxy.
|
2013-10-04 22:25:15 -04:00
|
|
|
Close()
|
2015-07-25 04:35:07 -04:00
|
|
|
// FrontendAddr returns the address on which the proxy is listening.
|
2013-10-04 22:25:15 -04:00
|
|
|
FrontendAddr() net.Addr
|
2015-07-25 04:35:07 -04:00
|
|
|
// BackendAddr returns the proxied address.
|
2013-10-04 22:25:15 -04:00
|
|
|
BackendAddr() net.Addr
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// NewProxy creates a Proxy according to the specified frontendAddr and backendAddr.
|
2013-10-04 22:25:15 -04:00
|
|
|
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))
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("Unsupported protocol"))
|
|
|
|
}
|
|
|
|
}
|