@@ -3,6 +3,7 @@ package simulator
33import (
44 "context"
55 "fmt"
6+ "net"
67 "strings"
78 "time"
89
@@ -28,23 +29,42 @@ func NewEncryptedDNS() *EncryptedDNS {
2829}
2930
3031func (s * EncryptedDNS ) Init (bind BindAddr ) error {
31- // TODO: along with issues/39, bind if iface specififed.
3232 s .bind = bind
3333 return nil
3434}
3535
3636func (EncryptedDNS ) Cleanup () {
3737}
3838
39+ // HostMsg implements the HostMsgFormatter interface, returning a custom host message
40+ // string to be output by the run command.
41+ func (s * EncryptedDNS ) HostMsg (host string ) string {
42+ var protoStr string
43+ switch s .Proto {
44+ case encdns .DoH :
45+ protoStr = "DNS-over-HTTPS"
46+ case encdns .DoT :
47+ protoStr = "DNS-over-TLS"
48+ case encdns .DNSCrypt :
49+ protoStr = "DNSCrypt"
50+ }
51+ return fmt .Sprintf ("Simulating Encrypted DNS (%s) via *.%s" , protoStr , host )
52+ }
53+
3954// randomProvider returns a random Protocol p Provider.
40- func randomProvider (ctx context.Context , p encdns.Protocol ) encdns.Queryable {
41- switch p {
55+ func (s * EncryptedDNS ) randomProvider (ctx context.Context ) encdns.Queryable {
56+ // If the user has set a bind interface via the -iface flag, have providers use it.
57+ var bindIP net.IP
58+ if s .bind .UserSet {
59+ bindIP = s .bind .Addr
60+ }
61+ switch s .Proto {
4262 case encdns .DoH :
43- return dohproviders .NewRandom (ctx )
63+ return dohproviders .NewRandom (ctx , bindIP )
4464 case encdns .DoT :
45- return dotproviders .NewRandom (ctx )
65+ return dotproviders .NewRandom (ctx , bindIP )
4666 case encdns .DNSCrypt :
47- return dnscryptproviders .NewRandom (ctx )
67+ return dnscryptproviders .NewRandom (ctx , bindIP )
4868 default :
4969 return nil
5070 }
@@ -53,12 +73,8 @@ func randomProvider(ctx context.Context, p encdns.Protocol) encdns.Queryable {
5373// Simulate lookups for txt records for give host.
5474func (s * EncryptedDNS ) Simulate (ctx context.Context , host string ) error {
5575 host = utils .FQDN (host )
56- // Select random Protocol (DoH/DoT/etc) if not specified on the commandline.
57- if s .Proto == encdns .Random {
58- s .Proto = encdns .RandomProtocol ()
59- }
6076 // Select a random Provider to be used in this simulation.
61- p := randomProvider (ctx , s . Proto )
77+ p := s . randomProvider (ctx )
6278 if p == nil {
6379 return fmt .Errorf ("invalid DNS protocol: unable to select provider" )
6480 }
@@ -78,7 +94,6 @@ func (s *EncryptedDNS) Simulate(ctx context.Context, host string) error {
7894
7995 // Ignore timeout. In case of DoH, when err != nil, resp.Body has already been
8096 // closed.
81- // TODO: Need timeout/dial error check from issues/39
8297 if err != nil {
8398 if isSoftError (err ) {
8499 continue
@@ -111,7 +126,6 @@ func (s *EncryptedDNS) Simulate(ctx context.Context, host string) error {
111126 // TODO: If that's not the case, we can add more comprehensive response parsing.
112127 case encdns .DNSCrypt :
113128 dnsCryptResp , err := resp .DNSCryptResponse ()
114- fmt .Println (dnsCryptResp )
115129 if err != nil {
116130 return fmt .Errorf ("failed extracting DNSCrypt response: %v" , err )
117131 }
@@ -133,7 +147,9 @@ func (s *EncryptedDNS) Hosts(scope string, size int) ([]string, error) {
133147 }
134148 s .Proto = proto
135149 } else {
136- s .Proto = encdns .Random
150+ // Select random Protocol (DoH/DoT/etc) if not specified on the commandline.
151+ // NOTE: doing this from Hosts() to display in HostMsg().
152+ s .Proto = encdns .RandomProtocol ()
137153 }
138154 return []string {"sandbox.alphasoc.xyz" }, nil
139155}
0 commit comments