-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Finish a plain DNS proxy demo.
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
|
||
"github.com/miekg/dns" | ||
"github.com/sirupsen/logrus" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
var udpCli = &dns.Client{SingleInflight: true} | ||
var tcpCli = &dns.Client{SingleInflight: true, Net: "tcp"} | ||
|
||
var ( | ||
flagListen = flag.String("listen", "[::]:5553", "Listening address") | ||
flagUDPMaxBytes = flag.Int("udpMaxBytes", 1410, "Default DNS max message size on UDP.") | ||
) | ||
|
||
// DNS Proxy Implementation Guidelines: https://tools.ietf.org/html/rfc5625 | ||
// DNS query processing: https://tools.ietf.org/html/rfc1034#section-3.7 | ||
// Happy Eyeballs: https://tools.ietf.org/html/rfc6555#section-5.4 and #section-6 | ||
func handle(w dns.ResponseWriter, req *dns.Msg) { | ||
// defer w.Close() | ||
logrus.Infoln("Question:", &req.Question[0]) | ||
|
||
req.RecursionDesired = true | ||
// https://tools.ietf.org/html/rfc6891#section-6.2.5 | ||
if e := req.IsEdns0(); e != nil { | ||
if e.UDPSize() < uint16(*flagUDPMaxBytes) { | ||
e.SetUDPSize(uint16(*flagUDPMaxBytes)) | ||
} | ||
} else { | ||
req.SetEdns0(uint16(*flagUDPMaxBytes), false) | ||
} | ||
|
||
reply, rtt, err := udpCli.Exchange(req, "119.29.29.29:53") | ||
if err != nil { | ||
logrus.WithError(err).Error("UDP query failed.") | ||
reply, rtt, err = tcpCli.Exchange(req, "119.29.29.29:53") | ||
if err != nil { | ||
logrus.WithError(err).Error("TCP query failed.") | ||
} | ||
} | ||
if reply != nil { | ||
logrus.Infof("Query RTT: %s", rtt) | ||
// https://github.com/miekg/dns/issues/216 | ||
reply.Compress = true | ||
} else { | ||
reply = new(dns.Msg) | ||
reply.SetReply(req) | ||
} | ||
|
||
w.WriteMsg(reply) | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
dns.HandleFunc(".", handle) | ||
udpServer := &dns.Server{Addr: *flagListen, Net: "udp", ReusePort: true} | ||
tcpServer := &dns.Server{Addr: *flagListen, Net: "tcp", ReusePort: true} | ||
|
||
eg, _ := errgroup.WithContext(context.Background()) | ||
eg.Go(udpServer.ListenAndServe) | ||
eg.Go(tcpServer.ListenAndServe) | ||
eg.Wait() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module github.com/cherrot/gochinadns | ||
|
||
require ( | ||
github.com/miekg/dns v1.1.3 | ||
github.com/sirupsen/logrus v1.3.0 | ||
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc // indirect | ||
golang.org/x/net v0.0.0-20190110200230-915654e7eabc // indirect | ||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 | ||
golang.org/x/sys v0.0.0-20190116161447-11f53e031339 // indirect | ||
) |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= | ||
github.com/miekg/dns v1.1.3 h1:1g0r1IvskvgL8rR+AcHzUA+oFmGcQlaIm4IqakufeMM= | ||
github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME= | ||
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= | ||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | ||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= | ||
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc h1:F5tKCVGp+MUAHhKp5MZtGqAlGX3+oCsiL1Q629FL90M= | ||
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= | ||
golang.org/x/net v0.0.0-20190110200230-915654e7eabc h1:Yx9JGxI1SBhVLFjpAkWMaO1TF+xyqtHLjZpvQboJGiM= | ||
golang.org/x/net v0.0.0-20190110200230-915654e7eabc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= | ||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw= | ||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190116161447-11f53e031339 h1:g/Jesu8+QLnA0CPzF3E1pURg0Byr7i6jLoX5sqjcAh0= | ||
golang.org/x/sys v0.0.0-20190116161447-11f53e031339/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |