-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ednsopt #91
Open
klyr
wants to merge
2
commits into
AdguardTeam:master
Choose a base branch
from
klyr:ednsopt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ednsopt #91
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,14 @@ package main | |
|
||
import ( | ||
"crypto/tls" | ||
"encoding/base64" | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"os" | ||
"os/signal" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
|
@@ -100,6 +103,9 @@ type Options struct { | |
// Use Custom EDNS Client Address | ||
EDNSAddr string `long:"edns-addr" description:"Send EDNS Client Address"` | ||
|
||
// Add user-specific EDNS exstension | ||
EDNSOpt []string `long:"ednsopt" description:"List of EDNS extensions to send along with the DNS query (ex: 8:deadbeaf)"` | ||
|
||
// Other settings and options | ||
// -- | ||
|
||
|
@@ -218,6 +224,27 @@ func createProxyConfig(options Options) proxy.Config { | |
} | ||
} | ||
|
||
if len(options.EDNSOpt) > 0 { | ||
config.EDNSOpts = make(map[uint16][]byte) | ||
for _, s := range options.EDNSOpt { | ||
kv := strings.Split(s, ":") | ||
if len(kv) != 2 { | ||
log.Fatalf("parse error for '%s', --ednsopt must be of the form: option-code:base64encodeddata (ex: --ednsopt 8:SGVsbG8gd29ybGQK)", s) | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
r, err := strconv.ParseUint(kv[0], 10, 16) | ||
if err != nil { | ||
log.Fatalf("parse error for '%s', --ednsopt option-code must be a valid 16 bits numbers (between 0 and 65535)", kv[0]) | ||
} | ||
optionCode := uint16(r) | ||
data, err := base64.StdEncoding.DecodeString(kv[1]) | ||
if err != nil { | ||
log.Fatalf("parse error for '%s', --ednsopt data must be a valid base64 encoded string: (%s)", kv[1], err) | ||
} | ||
config.EDNSOpts[optionCode] = data | ||
} | ||
} | ||
} | ||
|
||
if options.Fallbacks != nil { | ||
fallbacks := []upstream.Upstream{} | ||
for i, f := range options.Fallbacks { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -258,6 +258,10 @@ func (p *Proxy) Resolve(d *DNSContext) error { | |
p.processECS(d) | ||
} | ||
|
||
if p.Config.EDNSOpts != nil { | ||
p.processEDNSOpts(d) | ||
} | ||
|
||
if p.replyFromCache(d) { | ||
return nil | ||
} | ||
|
@@ -349,3 +353,29 @@ func (p *Proxy) processECS(d *DNSContext) { | |
d.ecsReqIP = ip | ||
d.ecsReqMask = mask | ||
} | ||
|
||
// Set EDNSOpts | ||
func (p *Proxy) processEDNSOpts(d *DNSContext) { | ||
var o (*dns.OPT) = nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
for _, ex := range d.Req.Extra { | ||
if ex.Header().Rrtype == dns.TypeOPT { | ||
o = ex.(*dns.OPT) | ||
break | ||
} | ||
} | ||
|
||
if o == nil { // OPT Record does not already exist, create it | ||
o = new(dns.OPT) | ||
o.SetUDPSize(4096) | ||
o.Hdr.Name = "." | ||
o.Hdr.Rrtype = dns.TypeOPT | ||
d.Req.Extra = append(d.Req.Extra, o) | ||
} | ||
|
||
for k, v := range p.Config.EDNSOpts { | ||
e := new(dns.EDNS0_LOCAL) | ||
e.Code = k | ||
e.Data = v | ||
o.Option = append(o.Option, e) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please move this code to a separate function and also add a unit-test for it?