Skip to content

Commit

Permalink
Merge pull request #21 from mageddo/feature/MG-337
Browse files Browse the repository at this point in the history
Feature/mg 337
  • Loading branch information
mageddo authored Jul 13, 2017
2 parents f27c4da + 6587f67 commit dbfa2fa
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 55 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ You can also configure the options at the configuration file

```javascript
{
"remoteDnsServers": [], // not used at the current version
"remoteDnsServers": [ [8,8,8,8], [4,4,4,4] ], // Remote DNS servers to be asked when can not solve from docker or local storage
// If no one server was specified then the 8.8.8.8 will be used
"envs": [ // all existent environments
{
"name": "", // empty string is the default
Expand Down
2 changes: 1 addition & 1 deletion src/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func handleQuestion(respWriter dns.ResponseWriter, reqMsg *dns.Msg) {
questionsQtd, firstQuestion.Name, utils.DnsQTypeCodeToName(firstQuestion.Qtype))

// loading the solvers and try to solve the hostname in that order
solvers := []proxy.DnsSolver{proxy.LocalDnsSolver{}, proxy.DockerDnsSolver{}, proxy.RemoteDnsSolver{}}
solvers := []proxy.DnsSolver{proxy.DockerDnsSolver{}, proxy.LocalDnsSolver{}, proxy.RemoteDnsSolver{}}
for _, solver := range solvers {

solverID := reflect.TypeOf(solver).Name()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ func GetConfiguration(ctx context.Context) *LocalConfiguration {


type LocalConfiguration struct {
/**
* The remote servers to ask when, DPS can not solve from docker or local file,
* it will try one by one in order, if no one is specified then 8.8.8.8 is used by default
* DO NOT call this variable directly, use GetRemoteDnsServers instead
*/
RemoteDnsServers [][4]byte `json:"remoteDnsServers"`
Envs []EnvVo `json:"envs"`
ActiveEnv string `json:"activeEnv"`
Expand Down Expand Up @@ -345,4 +350,13 @@ func (lc *LocalConfiguration) SetActiveEnv(ctx context.Context, env EnvVo) error

func NewEmptyEnv() []EnvVo {
return []EnvVo{{Hostnames:[]HostnameVo{}, Name:""}}
}

func (lc *LocalConfiguration) GetRemoteServers(ctx context.Context) [][4]byte {
if len(lc.RemoteDnsServers) == 0 {
lc.RemoteDnsServers = append(lc.RemoteDnsServers, [4]byte{8, 8, 8, 8})
logger := log.GetLogger(ctx)
logger.Infof("status=put-default-server")
}
return lc.RemoteDnsServers
}
48 changes: 32 additions & 16 deletions src/github.com/mageddo/dns-proxy-server/proxy/RemoteDnsSolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"fmt"
"golang.org/x/net/context"
"github.com/mageddo/dns-proxy-server/events/local"
"github.com/mageddo/log"
)

type RemoteDnsSolver struct {
Expand All @@ -15,26 +17,40 @@ type RemoteDnsSolver struct {
// reference https://miek.nl/2014/August/16/go-dns-package/
func (RemoteDnsSolver) Solve(ctx context.Context, question dns.Question) (*dns.Msg, error) {

//config, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
c := new(dns.Client)
logger := log.GetLogger(ctx)
c := new(dns.Client)
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(question.Name), question.Qtype) // CAN BE A, AAA, MX, etc.
m.RecursionDesired = true

m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(question.Name), question.Qtype) // CAN BE A, AAA, MX, etc.
m.RecursionDesired = true
var err error
config := local.GetConfiguration(ctx)

//r, _, err := c.Exchange(m, net.JoinHostPort(config.Servers[0], config.Port)) // server and port to ask
r, _, err := c.Exchange(m, net.JoinHostPort("8.8.8.8", "53")) // server and port to ask
for _, server := range config.GetRemoteServers(ctx) {

// if the answer not be returned
if r == nil {
return nil, errors.New(fmt.Sprintf("status=answer-can-not-be-bull, err=%v", err))
if len(server) != 4 {
logger.Warning("status=wrong-server, server=%+v", server)
continue
}

// what the code of the return message ?
if r.Rcode != dns.RcodeSuccess {
return nil, errors.New(fmt.Sprintf("status=invalid-answer-name, name=%s, rcode=%d", question.Name, r.Rcode))
// server and port to ask
formatServer := fmt.Sprintf("%d.%d.%d.%d", server[0], server[1], server[2], server[3])
logger.Debugf("status=format-server, server=%s", formatServer)

var r *dns.Msg
r, _, err = c.Exchange(m, net.JoinHostPort(formatServer, "53"))

// if the answer not be returned
if r == nil {
err = errors.New(fmt.Sprintf("status=answer-can-not-be-null, err=%v", err))
logger.Infof("status=no-answer, err=%s", err)
continue
} else if r.Rcode != dns.RcodeSuccess { // what the code of the return message ?
err = errors.New(fmt.Sprintf("status=invalid-answer-name, name=%s, rcode=%d", question.Name, r.Rcode))
logger.Infof("status=bad-code, name=%s, rcode=%d, err=%s", question.Name, r.Rcode, err)
continue
}
return r, nil
}

return r, nil

return nil, err
}
37 changes: 0 additions & 37 deletions src/github.com/mageddo/dns-proxy-server/utils/dnsclient.go

This file was deleted.

0 comments on commit dbfa2fa

Please sign in to comment.