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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #307 from mesosphere/rfc5625
Transparent proxying of external queries
- Loading branch information
Showing
7 changed files
with
255 additions
and
295 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
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) { | ||
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.