Skip to content

Commit

Permalink
proxy: imp code, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Feb 15, 2022
1 parent 7f48aa3 commit 99ec30d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
30 changes: 15 additions & 15 deletions proxy/dns_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,39 +80,39 @@ type DNSContext struct {
}

// calcFlagsAndSize lazily calculates some values required for Resolve method.
func (ctx *DNSContext) calcFlagsAndSize() {
if ctx.udpSize != 0 || ctx.Req == nil {
func (dctx *DNSContext) calcFlagsAndSize() {
if dctx.udpSize != 0 || dctx.Req == nil {
return
}

ctx.adBit = ctx.Req.AuthenticatedData
ctx.udpSize = defaultUDPBufSize
if o := ctx.Req.IsEdns0(); o != nil {
ctx.hasEDNS0 = true
ctx.doBit = o.Do()
ctx.udpSize = o.UDPSize()
dctx.adBit = dctx.Req.AuthenticatedData
dctx.udpSize = defaultUDPBufSize
if o := dctx.Req.IsEdns0(); o != nil {
dctx.hasEDNS0 = true
dctx.doBit = o.Do()
dctx.udpSize = o.UDPSize()
}
}

// scrub prepares the d.Res to be written. Truncation is applied as well if
// necessary.
func (ctx *DNSContext) scrub() {
if ctx.Res == nil || ctx.Req == nil {
func (dctx *DNSContext) scrub() {
if dctx.Res == nil || dctx.Req == nil {
return
}

// We should guarantee that all the values we need are calculated.
ctx.calcFlagsAndSize()
dctx.calcFlagsAndSize()

// RFC-6891 (https://tools.ietf.org/html/rfc6891) states that response
// mustn't contain an EDNS0 RR if the request doesn't include it.
//
// See https://github.com/AdguardTeam/dnsproxy/issues/132.
if ctx.hasEDNS0 && ctx.Res.IsEdns0() == nil {
ctx.Res.SetEdns0(ctx.udpSize, ctx.doBit)
if dctx.hasEDNS0 && dctx.Res.IsEdns0() == nil {
dctx.Res.SetEdns0(dctx.udpSize, dctx.doBit)
}

ctx.Res.Truncate(proxyutil.DNSSize(ctx.Proto == ProtoUDP, ctx.Req))
dctx.Res.Truncate(proxyutil.DNSSize(dctx.Proto == ProtoUDP, dctx.Req))
// Some devices require DNS message compression.
ctx.Res.Compress = true
dctx.Res.Compress = true
}
16 changes: 15 additions & 1 deletion proxy/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ func setECS(m *dns.Msg, ip net.IP, scope uint8) (net.IP, uint8) {
return e.Address, e.SourceNetmask
}

// Return TRUE if IP is within public Internet IP range
// isPublicIP returns true if ip is within public Internet IP range.
//
// TODO(e.burkov, a.garipov): Add an interface-based subnet detector to the
// netutil.
//
// nolint (gocyclo)
func isPublicIP(ip net.IP) (ok bool) {
if ip = ip.To4(); ip == nil {
Expand All @@ -153,29 +157,39 @@ func isPublicIP(ip net.IP) (ok bool) {

switch ip[0] {
case 0, 10, 127:
// Software, private network, loopback.
return false
case 169:
// Link-local.
return ip[1] != 254
case 172:
// Private network.
return ip[1] < 16 || ip[1] > 31
case 192:
switch ip[1] {
case 0:
// Private network, documentation.
return ip[2] != 0 && ip[2] != 2
case 88:
// Reserved.
return ip[2] != 99
case 168:
// Private network.
return false
default:
return true
}
case 198:
// Private network, documentation.
return (ip[1] != 18 && ip[2] != 19) && (ip[1] != 51 && ip[2] != 100)
case 203:
// Documentation.
return ip[1] != 0 || ip[2] != 113
case 224:
// Multicast.
return ip[1] != 0 || ip[2] != 0
case 255:
// Broadcast.
return ip[1] != 255 || ip[2] != 255 || ip[3] != 255
default:
return true
Expand Down
16 changes: 8 additions & 8 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,28 +507,28 @@ func (p *Proxy) Resolve(d *DNSContext) (err error) {
}

// processECS adds EDNS Client-Subnet data into the request from d.
func (d *DNSContext) processECS(cliIP net.IP) {
d.ECSReqIP, d.ECSReqMask, _ = parseECS(d.Req)
if d.ECSReqMask != 0 {
log.Debug("passing through ecs: %s/%d", d.ECSReqIP, d.ECSReqMask)
func (dctx *DNSContext) processECS(cliIP net.IP) {
dctx.ECSReqIP, dctx.ECSReqMask, _ = parseECS(dctx.Req)
if dctx.ECSReqMask != 0 {
log.Debug("passing through ecs: %s/%d", dctx.ECSReqIP, dctx.ECSReqMask)

return
}

// Set ECS.
d.ECSReqIP = nil
dctx.ECSReqIP = nil

if cliIP == nil {
cliIP, _ = netutil.IPAndPortFromAddr(d.Addr)
cliIP, _ = netutil.IPAndPortFromAddr(dctx.Addr)
if cliIP == nil {
return
}
}

if isPublicIP(cliIP) {
d.ECSReqIP, d.ECSReqMask = setECS(d.Req, cliIP, 0)
dctx.ECSReqIP, dctx.ECSReqMask = setECS(dctx.Req, cliIP, 0)

log.Debug("setting ecs: %s/%d", d.ECSReqIP, d.ECSReqMask)
log.Debug("setting ecs: %s/%d", dctx.ECSReqIP, dctx.ECSReqMask)
}
}

Expand Down

0 comments on commit 99ec30d

Please sign in to comment.