mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-05-11 06:26:35 +00:00

Creates an abstraction over UDP Conn for origin "connection" which can be useful for future support of complex protocols that may require changing ports during protocol negotiation (eg. SIP, TFTP) In addition, it removes a dependency from ingress on connection package.
28 lines
564 B
Go
28 lines
564 B
Go
package ingress
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
)
|
|
|
|
type UDPProxy struct {
|
|
io.ReadWriteCloser
|
|
}
|
|
|
|
func DialUDP(dstIP net.IP, dstPort uint16) (*UDPProxy, error) {
|
|
dstAddr := &net.UDPAddr{
|
|
IP: dstIP,
|
|
Port: int(dstPort),
|
|
}
|
|
|
|
// We use nil as local addr to force runtime to find the best suitable local address IP given the destination
|
|
// address as context.
|
|
udpConn, err := net.DialUDP("udp", nil, dstAddr)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to create UDP proxy to origin (%v:%v): %w", dstIP, dstPort, err)
|
|
}
|
|
|
|
return &UDPProxy{udpConn}, nil
|
|
}
|