Skip to content

Commit

Permalink
Fixing DNS resolution and ping slowness (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
mageddo authored Apr 11, 2019
1 parent 03809d7 commit 5e1f1cd
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 26 deletions.
4 changes: 4 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 2.13.2
* Fixing broken answer when hostname is not found
* Fixing ping slowness

### 2.13.1
* Make sure value column will not break the table (#116)

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.13.1
2.13.2
18 changes: 6 additions & 12 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func handleQuestion(respWriter dns.ResponseWriter, reqMsg *dns.Msg) {
defer func() {
err := recover()
if err != nil {
logging.Errorf("status=error, error=%v, stack=%s", ctx, err, string(debug.Stack()))
logging.Errorf("status=fatal-error-handling-question, req=%+v, err=%s, stack=%+v", ctx, reqMsg.Question, err, string(debug.Stack()))
}
}()

Expand All @@ -38,7 +38,7 @@ func handleQuestion(respWriter dns.ResponseWriter, reqMsg *dns.Msg) {
if questionsQtd != 0 {
firstQuestion = reqMsg.Question[0]
} else {
logging.Error(ctx, "status=question-is-nil")
logging.Error(ctx, "status=no-questions-to-answer, reqId=%d", reqMsg.Id)
return
}

Expand All @@ -49,16 +49,10 @@ func handleQuestion(respWriter dns.ResponseWriter, reqMsg *dns.Msg) {

solverFactory := proxy.NewCnameDnsSolverFactory(&proxy.DefaultDnsSolverFactory{})
msg, err := solverFactory.Solve(ctx, firstQuestion, getSolvers())
if err != nil {
logging.Errorf("status=not-resolved, question=%+v", ctx, firstQuestion, err)
respWriter.WriteMsg(msg)
} else {
logging.Debugf("status=resolved, question=%+v, answers=%+v", ctx, firstQuestion, msg.Answer)
msg.SetReply(reqMsg)
msg.Compress = conf.Compress()
respWriter.WriteMsg(msg)
}

msg.SetReply(reqMsg)
msg.Compress = conf.Compress()
respWriter.WriteMsg(msg)
logging.Debugf("status=complete, question=%+v, answers=%+v, err=%+v", ctx, firstQuestion, msg.Answer, err)
}

var solversCreated int32 = 0
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ services:
- TERM=xterm
- GOPATH=/app
- MG_WORK_DIR=/app/src/github.com/mageddo/dns-proxy-server
network_mode: bridge
command: tail -f /dev/null

app-dps:
Expand All @@ -22,7 +23,7 @@ services:
- ./app:/app
working_dir: /app
ports:
- 5380:3000
- 5381:3000
command: tail -f /dev/null

# Run from docker image
Expand Down
12 changes: 4 additions & 8 deletions events/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"time"
)

var confPath string = GetConfPath()
var confPath = GetConfPath()

func GetConfPath() string {
return utils.GetPath(*flags.ConfPath)
Expand All @@ -29,11 +29,8 @@ func LoadConfiguration() (*LocalConfiguration, error){
RemoteDnsServers: make([][4]byte, 0),
}

logging.Infof("status=begin, confPath=%s", confPath)

if _, err := os.Stat(confPath); err == nil {

logging.Info("status=openingFile")
f, _ := os.Open(confPath)

defer f.Close()
Expand All @@ -51,16 +48,15 @@ func LoadConfiguration() (*LocalConfiguration, error){
}
}
}
logging.Info("status=success")
logging.Debugf("status=success-loaded-file, path=%s", confPath)
} else {
logging.Info("status=create-new-conf")
err := os.MkdirAll(confPath[:strings.LastIndex(confPath, "/")], 0755)
if err != nil {
logging.Errorf("status=error-to-create-conf-folder, err=%v", err)
logging.Errorf("status=error-to-create-conf-path, path=%s", confPath)
return nil, err
}
SaveConfiguration(&configuration)
logging.Info("status=success")
logging.Info("status=success-creating-conf-file, path=%s", confPath)
}
return &configuration, nil
}
Expand Down
12 changes: 9 additions & 3 deletions proxy/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"github.com/mageddo/go-logging"
"github.com/miekg/dns"
"reflect"
)

type DnsSolverFactory interface {
Expand All @@ -14,14 +15,18 @@ type DnsSolverFactory interface {
type DefaultDnsSolverFactory struct {}

func (*DefaultDnsSolverFactory) Solve(ctx context.Context, question dns.Question, solvers []DnsSolver) (*dns.Msg, error) {
for _, solver := range solvers {
var solver DnsSolver
for _, solver = range solvers {
msg, err := solver.Solve(ctx, question)
if msg != nil {
logging.Debugf("solver-factory=default, status=found, question=%+v, answers=%d", question, len(msg.Answer))
logging.Debugf(
"solver=%s, status=found, question=%+v, answers=%d",
ctx, reflect.TypeOf(solver).String(), question, len(msg.Answer),
)
return msg, err
}
}
logging.Debugf("solver-factory=default, status=not-found, question=%+v", question)
logging.Debugf("status=not-found, lastSolver=%s, question=%+v", ctx, reflect.TypeOf(solver).String(), question)
return nil, errors.New("Not solver for the question " + question.Name)
}

Expand All @@ -43,6 +48,7 @@ func (s *CnameDnsSolverFactory) Solve(ctx context.Context, question dns.Question
firstAnswer := firstMsg.Answer[0]
if firstAnswer.Header().Rrtype == dns.TypeCNAME && firstAnswer.Header().Class == 256 {
question.Name = firstAnswer.(*dns.CNAME).Target
logging.Debugf("status=solving-cname, questionName=%s", ctx, question.Name)
if secondMsg, err := s.proxy.Solve(ctx, question, solvers); secondMsg != nil {
if err != nil {
return nil, err
Expand Down
5 changes: 4 additions & 1 deletion proxy/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/miekg/dns"
"golang.org/x/net/context"
"net"
"reflect"
)

type localDnsSolver struct {
Expand All @@ -16,9 +17,11 @@ func (s localDnsSolver) Solve(ctx context.Context, question dns.Question) (*dns.
questionName := question.Name[:len(question.Name)-1]
for _, host := range getAllHosts("." + questionName) {
if msg, err := s.solveHostname(ctx, question, host); err == nil {
logging.Debugf("status=found, solver=%s, question=%+v", ctx, reflect.TypeOf(s).String(), question)
return msg, nil
}
}
logging.Debugf("status=not-found, solver=%s, hostname=%+v", ctx, reflect.TypeOf(s).String(), question)
return nil, errors.New("hostname not found " + questionName)
}

Expand Down Expand Up @@ -55,7 +58,7 @@ func (s localDnsSolver) solveHostname(ctx context.Context, question dns.Question
}
activeEnv, _ := conf.GetActiveEnv()
if activeEnv == nil {
return nil, errors.New("Not active env found")
return nil, errors.New("Not active env found.")
}

if hostname, _ := activeEnv.GetHostname(key); hostname != nil {
Expand Down

0 comments on commit 5e1f1cd

Please sign in to comment.