mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-26 04:03:11 +00:00
proxy: create a udpListener
interface
This represents what is needed from the frontend side of the proxy: - the ability to receive a UDP datagram and know who it is from - the ability to send a UDP datagram to a particular destination - the ability to close Signed-off-by: David Scott <dave.scott@docker.com>
This commit is contained in:
parent
58ad1b7161
commit
abbafd82f1
@ -31,7 +31,11 @@ type Proxy interface {
|
|||||||
func NewProxy(frontendAddr, backendAddr net.Addr) (Proxy, error) {
|
func NewProxy(frontendAddr, backendAddr net.Addr) (Proxy, error) {
|
||||||
switch frontendAddr.(type) {
|
switch frontendAddr.(type) {
|
||||||
case *net.UDPAddr:
|
case *net.UDPAddr:
|
||||||
return NewUDPProxy(frontendAddr.(*net.UDPAddr), backendAddr.(*net.UDPAddr))
|
listener, err := net.ListenUDP("udp", frontendAddr.(*net.UDPAddr))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return NewUDPProxy(frontendAddr, listener, backendAddr.(*net.UDPAddr))
|
||||||
case *net.TCPAddr:
|
case *net.TCPAddr:
|
||||||
listener, err := net.Listen("tcp", frontendAddr.String())
|
listener, err := net.Listen("tcp", frontendAddr.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -18,6 +18,12 @@ const (
|
|||||||
UDPBufSize = 65507
|
UDPBufSize = 65507
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type udpListener interface {
|
||||||
|
ReadFromUDP(b []byte) (int, *net.UDPAddr, error)
|
||||||
|
WriteToUDP(b []byte, addr *net.UDPAddr) (int, error)
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
// A net.Addr where the IP is split into two fields so you can use it as a key
|
// A net.Addr where the IP is split into two fields so you can use it as a key
|
||||||
// in a map:
|
// in a map:
|
||||||
type connTrackKey struct {
|
type connTrackKey struct {
|
||||||
@ -47,22 +53,19 @@ type connTrackMap map[connTrackKey]*net.UDPConn
|
|||||||
// interface to handle UDP traffic forwarding between the frontend and backend
|
// interface to handle UDP traffic forwarding between the frontend and backend
|
||||||
// addresses.
|
// addresses.
|
||||||
type UDPProxy struct {
|
type UDPProxy struct {
|
||||||
listener *net.UDPConn
|
listener udpListener
|
||||||
frontendAddr *net.UDPAddr
|
frontendAddr net.Addr
|
||||||
backendAddr *net.UDPAddr
|
backendAddr *net.UDPAddr
|
||||||
connTrackTable connTrackMap
|
connTrackTable connTrackMap
|
||||||
connTrackLock sync.Mutex
|
connTrackLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUDPProxy creates a new UDPProxy.
|
// NewUDPProxy creates a new UDPProxy.
|
||||||
func NewUDPProxy(frontendAddr, backendAddr *net.UDPAddr) (*UDPProxy, error) {
|
func NewUDPProxy(frontendAddr net.Addr, listener udpListener, backendAddr *net.UDPAddr) (*UDPProxy, error) {
|
||||||
listener, err := net.ListenUDP("udp", frontendAddr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &UDPProxy{
|
return &UDPProxy{
|
||||||
listener: listener,
|
listener: listener,
|
||||||
frontendAddr: listener.LocalAddr().(*net.UDPAddr),
|
frontendAddr: frontendAddr,
|
||||||
backendAddr: backendAddr,
|
backendAddr: backendAddr,
|
||||||
connTrackTable: make(connTrackMap),
|
connTrackTable: make(connTrackMap),
|
||||||
}, nil
|
}, nil
|
||||||
@ -112,7 +115,7 @@ func (proxy *UDPProxy) Run() {
|
|||||||
// ECONNREFUSED like Read do (see comment in
|
// ECONNREFUSED like Read do (see comment in
|
||||||
// UDPProxy.replyLoop)
|
// UDPProxy.replyLoop)
|
||||||
if !isClosedError(err) {
|
if !isClosedError(err) {
|
||||||
logrus.Printf("Stopping proxy on udp/%v for udp/%v (%s)", proxy.frontendAddr, proxy.backendAddr, err)
|
logrus.Printf("Stopping proxy on %v for udp/%v (%s)", proxy.frontendAddr, proxy.backendAddr, err)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user