Skip to content

Commit

Permalink
Feature/83 (#84)
Browse files Browse the repository at this point in the history
* setup host resolution

* #83 removing unnecessary log

* #83 bump version
  • Loading branch information
mageddo authored Jul 22, 2018
1 parent ed68cfd commit f332c72
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.4
2.6.0
3 changes: 2 additions & 1 deletion dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func handleQuestion(respWriter dns.ResponseWriter, reqMsg *dns.Msg) {

// loading the solvers and try to solve the hostname in that order
solvers := []proxy.DnsSolver{
proxy.NewDockerSolver(docker.GetCache()), proxy.NewLocalDNSSolver(store.GetInstance()), proxy.NewRemoteDnsSolver(),
proxy.NewSystemSolver(), proxy.NewDockerSolver(docker.GetCache()),
proxy.NewLocalDNSSolver(store.GetInstance()), proxy.NewRemoteDnsSolver(),
}

for _, solver := range solvers {
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ services:
# Base Image to build project
compiler-dps:
image: golang:1.9
container_name: gocompiler-dps
working_dir: /app/src/github.com/mageddo/dns-proxy-server
volumes:
- $PWD:/app/src/github.com/mageddo/dns-proxy-server
Expand Down
4 changes: 2 additions & 2 deletions docs/developing/Developing-at-the-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

Setup the environment

$ docker-compose up -d compiler-dps && docker exec -it gocompiler-dps bash
$ docker-compose up -d compiler-dps && docker-compose exec compiler-dps bash

Running the application

go run src/dns.go
go run dns.go

Running unit tests

Expand Down
54 changes: 54 additions & 0 deletions proxy/system.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package proxy

import (
"errors"
"github.com/mageddo/go-logging"
"github.com/miekg/dns"
"golang.org/x/net/context"
"net"
"strconv"
"strings"
"github.com/mageddo/dns-proxy-server/utils"
"regexp"
)

type SystemDnsSolver struct {}

func (s SystemDnsSolver) Solve(ctx context.Context, question dns.Question) (*dns.Msg, error) {

questionName := question.Name[:len(question.Name)-1]
switch questionName {
case "host.docker":
ip, err, code := utils.Exec("sh", "-c", "ip r | awk '/default/{print $3}'")
if code == 0 {
clearedIP := regexp.MustCompile(`\s`).ReplaceAllLiteralString(string(ip), ``)
logging.Infof("status=solved, solver=system, question=%s, ip=%s", questionName, clearedIP)
return s.getMsg(questionName, clearedIP, question), nil
}
logging.Errorf("status=not-solved, solver=system, question=%s", questionName, err)
return nil, err
}
return nil, errors.New("host not found")
}


func NewSystemSolver() SystemDnsSolver {
return SystemDnsSolver{}
}

func (s SystemDnsSolver) getMsg(key, ip string, question dns.Question) *dns.Msg {
ipArr := strings.Split(ip, ".")
i1, _ := strconv.Atoi(ipArr[0])
i2, _ := strconv.Atoi(ipArr[1])
i3, _ := strconv.Atoi(ipArr[2])
i4, _ := strconv.Atoi(ipArr[3])

rr := &dns.A{
Hdr: dns.RR_Header{Name: question.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0},
A: net.IPv4(byte(i1), byte(i2), byte(i3), byte(i4)),
}

m := new(dns.Msg)
m.Answer = append(m.Answer, rr)
return m
}

0 comments on commit f332c72

Please sign in to comment.