This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
Transparent proxying of external queries #307
Merged
Changes from all commits
Commits
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
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,49 @@ | ||
package exchanger | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/miekg/dns" | ||
) | ||
|
||
// A Forwarder is a DNS message forwarder that transparently proxies messages | ||
// to DNS servers. | ||
type Forwarder func(*dns.Msg, string) (*dns.Msg, error) | ||
|
||
// Forward is an utility method that calls f itself. | ||
func (f Forwarder) Forward(m *dns.Msg, proto string) (*dns.Msg, error) { | ||
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. don't understand the need for this utility func at all since it adds no value. 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. There is no need, that's why it's an utility function. Code reads much better with it. If we define an interface, it would also implement it automatically. |
||
return f(m, proto) | ||
} | ||
|
||
// NewForwarder returns a new Forwarder for the given addrs with the given | ||
// Exchangers map which maps network protocols to Exchangers. | ||
// | ||
// Every message will be exchanged with each address until no error is returned. | ||
// If no addresses or no matching protocol exchanger exist, a *ForwardError will | ||
// be returned. | ||
func NewForwarder(addrs []string, exs map[string]Exchanger) Forwarder { | ||
return func(m *dns.Msg, proto string) (r *dns.Msg, err error) { | ||
ex, ok := exs[proto] | ||
if !ok || len(addrs) == 0 { | ||
return nil, &ForwardError{Addrs: addrs, Proto: proto} | ||
} | ||
for _, a := range addrs { | ||
if r, _, err = ex.Exchange(m, net.JoinHostPort(a, "53")); err == nil { | ||
break | ||
} | ||
} | ||
return | ||
} | ||
} | ||
|
||
// A ForwardError is returned by Forwarders when they can't forward. | ||
type ForwardError struct { | ||
Addrs []string | ||
Proto string | ||
} | ||
|
||
// Error implements the error interface. | ||
func (e ForwardError) Error() string { | ||
return fmt.Sprintf("can't forward to %v over %q", e.Addrs, e.Proto) | ||
} |
Oops, something went wrong.
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.
this looks strange because the increment of success depends on a negative outcome of total.Inc(). unless I'm missing something, total.Inc() should really be on its own line.
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.
total.Inc()
has no impact on the conditional. It runs unconditionally.