add --allow-private-dst support for docker, bot and creator app
This commit is contained in:
1 parent
ca0966bc33
commit
d171a757aa
16 files changed
+129
-32
No files matched your search
@@ -3,22 +3,50 @@ package common
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
const blockedDstLogInterval = 10 * time.Second
|
||||
|
||||
var AllowPrivateDst bool
|
||||
|
||||
var ErrPrivateDst = errors.New("destination not allowed")
|
||||
|
||||
var (
|
||||
blockedDstMu sync.Mutex
|
||||
blockedDstLastLog time.Time
|
||||
blockedDstSuppressed int
|
||||
)
|
||||
|
||||
func ClaimBlockedDstLog() (int, bool) {
|
||||
blockedDstMu.Lock()
|
||||
defer blockedDstMu.Unlock()
|
||||
now := time.Now()
|
||||
if !blockedDstLastLog.IsZero() && now.Sub(blockedDstLastLog) < blockedDstLogInterval {
|
||||
blockedDstSuppressed++
|
||||
return 0, false
|
||||
}
|
||||
suppressed := blockedDstSuppressed
|
||||
blockedDstSuppressed = 0
|
||||
blockedDstLastLog = now
|
||||
return suppressed, true
|
||||
}
|
||||
|
||||
// cgnat is caught by neither IsGlobalUnicast nor IsPrivate
|
||||
var _, cgnat, _ = net.ParseCIDR("100.64.0.0/10")
|
||||
|
||||
func IsPrivateDst(addr string) bool {
|
||||
func hostFromAddr(addr string) string {
|
||||
host, _, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
host = addr
|
||||
return addr
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
func IsPrivateDst(addr string) bool {
|
||||
host := hostFromAddr(addr)
|
||||
if host == "" {
|
||||
// ":80" dials the local machine
|
||||
return true
|
||||
@@ -30,7 +58,29 @@ func IsPrivateDst(addr string) bool {
|
||||
return isPrivateIP(ip)
|
||||
}
|
||||
|
||||
func DstBlocked(addr string) bool { return !AllowPrivateDst && IsPrivateDst(addr) }
|
||||
func DstBlocked(addr string) bool {
|
||||
host := hostFromAddr(addr)
|
||||
if host == "" {
|
||||
return true
|
||||
}
|
||||
ip := net.ParseIP(host)
|
||||
if ip == nil {
|
||||
return false
|
||||
}
|
||||
return ipBlocked(ip)
|
||||
}
|
||||
|
||||
func ipBlocked(ip net.IP) bool {
|
||||
if isNeverAllowedIP(ip) {
|
||||
return true
|
||||
}
|
||||
return !AllowPrivateDst && isPrivateIP(ip)
|
||||
}
|
||||
|
||||
// AllowPrivateDst unlocks rfc1918 and cgnat only, the metadata ip and the creator's own loopback stay closed
|
||||
func isNeverAllowedIP(ip net.IP) bool {
|
||||
return ip.IsLoopback() || ip.IsUnspecified() || ip.IsLinkLocalUnicast() || ip.IsMulticast()
|
||||
}
|
||||
|
||||
func isPrivateIP(ip net.IP) bool {
|
||||
return !ip.IsGlobalUnicast() || ip.IsPrivate() || cgnat.Contains(ip)
|
||||
@@ -61,7 +111,7 @@ func DialUDP(addr string) (*net.UDPConn, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !AllowPrivateDst && isPrivateIP(udpAddr.IP) {
|
||||
if ipBlocked(udpAddr.IP) {
|
||||
return nil, ErrPrivateDst
|
||||
}
|
||||
return net.DialUDP("udp", nil, udpAddr)
|
||||
|
||||
+6
-1
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
@@ -171,7 +172,11 @@ func (c *dcCreatorRelay) handleUDP(connID uint32, payload []byte) {
|
||||
|
||||
conn, err := common.DialUDP(addr)
|
||||
if err != nil {
|
||||
if common.Debug {
|
||||
if errors.Is(err, common.ErrPrivateDst) {
|
||||
if suppressed, ok := common.ClaimBlockedDstLog(); ok {
|
||||
log.Printf("dc-creator: UDP %d blocked %s, private destinations need --allow-private-dst, suppressed=%d", connID, common.MaskAddr(addr), suppressed)
|
||||
}
|
||||
} else if common.Debug {
|
||||
log.Printf("dc-creator: UDP dial %s failed: %s", common.MaskAddr(addr), common.MaskError(err))
|
||||
}
|
||||
return
|
||||
|
||||
@@ -388,8 +388,8 @@ func (rb *RelayBridge) handleUDP(connID uint32, payload []byte) {
|
||||
|
||||
// per datagram, an already open session carries a fresh dst every time
|
||||
if common.DstBlocked(addr) {
|
||||
if common.Debug {
|
||||
rb.logFn("relay[creator]: UDP %d blocked %s", connID, common.MaskAddr(addr))
|
||||
if suppressed, ok := common.ClaimBlockedDstLog(); ok {
|
||||
rb.logFn("relay[creator]: UDP %d blocked %s, private destinations need --allow-private-dst, suppressed=%d", connID, common.MaskAddr(addr), suppressed)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -471,7 +471,7 @@ func (rb *RelayBridge) dialCreatorUDP(addr string) (*creatorUDP, error) {
|
||||
func (rb *RelayBridge) connectTCP(connID uint32, addr string) {
|
||||
rb.logFn("relay: CONNECT %d -> %s", connID, common.MaskAddr(addr))
|
||||
if common.DstBlocked(addr) {
|
||||
rb.logFn("relay: CONNECT %d blocked %s", connID, common.MaskAddr(addr))
|
||||
rb.logFn("relay: CONNECT %d blocked %s, private destinations need --allow-private-dst", connID, common.MaskAddr(addr))
|
||||
rb.send(connID, MsgConnectErr, []byte(common.ErrPrivateDst.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user