Files
whitelist-bypass/relay/common/privatedst.go
T
Sbenazar 747f8f2312 stop the creator dialing private/internal destinations (#175)
connectTCP and dialCreatorUDP dialed whatever address came over the tunnel, so a joiner could make the creator reach its own LAN, loopback or the metadata ip at 169.254.169.254. dc mode had the same hole so the check lives in common now, all three paths call it. i block everything that isn't global unicast plus RFC1918 and cgnat, and ":80" too - empty host dials the local machine. names get checked inside Dialer.Control, per resolved address right before connect, so a re-resolve can't rebind past it and the dialer keeps its address fallback. with an upstream socks the name is resolved proxy-side, so there only the literal is mine to check. --allow-private-dst brings the old behaviour back.
2026-09-03 20:20:52 +02:00

69 lines
1.4 KiB
Go

package common
import (
"errors"
"net"
"syscall"
"time"
)
var AllowPrivateDst bool
var ErrPrivateDst = errors.New("destination not allowed")
// cgnat is caught by neither IsGlobalUnicast nor IsPrivate
var _, cgnat, _ = net.ParseCIDR("100.64.0.0/10")
func IsPrivateDst(addr string) bool {
host, _, err := net.SplitHostPort(addr)
if err != nil {
host = addr
}
if host == "" {
// ":80" dials the local machine
return true
}
ip := net.ParseIP(host)
if ip == nil {
return false
}
return isPrivateIP(ip)
}
func DstBlocked(addr string) bool { return !AllowPrivateDst && IsPrivateDst(addr) }
func isPrivateIP(ip net.IP) bool {
return !ip.IsGlobalUnicast() || ip.IsPrivate() || cgnat.Contains(ip)
}
func DialTCP(addr string, timeout time.Duration) (net.Conn, error) {
if DstBlocked(addr) {
return nil, ErrPrivateDst
}
// Control runs per resolved address right before connect, a re-resolve can't rebind past it
d := net.Dialer{
Timeout: timeout,
Control: func(_, address string, _ syscall.RawConn) error {
if DstBlocked(address) {
return ErrPrivateDst
}
return nil
},
}
return d.Dial("tcp", addr)
}
func DialUDP(addr string) (*net.UDPConn, error) {
if DstBlocked(addr) {
return nil, ErrPrivateDst
}
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
if !AllowPrivateDst && isPrivateIP(udpAddr.IP) {
return nil, ErrPrivateDst
}
return net.DialUDP("udp", nil, udpAddr)
}