diff --git a/VERSION b/VERSION index fe16b348d..e70b4523a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.5.4 +2.6.0 diff --git a/dns.go b/dns.go index a3beea139..686098c1d 100644 --- a/dns.go +++ b/dns.go @@ -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 { diff --git a/docker-compose.yml b/docker-compose.yml index d02490c8c..e49908eb7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/docs/developing/Developing-at-the-project.md b/docs/developing/Developing-at-the-project.md index 019dd2397..173a9b0a1 100644 --- a/docs/developing/Developing-at-the-project.md +++ b/docs/developing/Developing-at-the-project.md @@ -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 diff --git a/proxy/system.go b/proxy/system.go new file mode 100644 index 000000000..01d5879fc --- /dev/null +++ b/proxy/system.go @@ -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 +}