Skip to content

Commit

Permalink
DNS over HTTPs (#414)
Browse files Browse the repository at this point in the history
* DNS over HTTPs

* fix proto
  • Loading branch information
asim committed Apr 8, 2022
1 parent 1b01637 commit b40fc2c
Show file tree
Hide file tree
Showing 15 changed files with 781 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dns/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

dns
3 changes: 3 additions & 0 deletions dns/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM alpine
ADD dns /dns
ENTRYPOINT [ "/dns" ]
27 changes: 27 additions & 0 deletions dns/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

GOPATH:=$(shell go env GOPATH)
.PHONY: init
init:
go install github.com/golang/protobuf/protoc-gen-go@latest
go install github.com/micro/micro/v3/cmd/protoc-gen-micro@latest
go install github.com/micro/micro/v3/cmd/protoc-gen-openapi@latest

.PHONY: api
api:
protoc --openapi_out=. --proto_path=. proto/dns.proto

.PHONY: proto
proto:
protoc --proto_path=. --micro_out=. --go_out=:. proto/dns.proto

.PHONY: build
build:
go build -o dns *.go

.PHONY: test
test:
go test -v ./... -cover

.PHONY: docker
docker:
docker build . -t dns:latest
5 changes: 5 additions & 0 deletions dns/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DNS over HTTPS (DoH)

# DNS Service

Query DNS over HTTPS with a single API call
34 changes: 34 additions & 0 deletions dns/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"query": [
{
"title": "Make a dns query",
"description": "Make a dns query over https",
"request": {
"name": "google.com"
},
"response": {
"status": 0,
"TC": false,
"RD": true,
"RA": true,
"AD": false,
"CD": false,
"question": [
{
"name": "google.com.",
"type": 1
}
],
"answer": [
{
"name": "google.com.",
"type": 1,
"TTL": 117,
"data": "142.250.178.14"
}
],
"provider": "google"
}
}
]
}
3 changes: 3 additions & 0 deletions dns/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

//go:generate make proto
64 changes: 64 additions & 0 deletions dns/handler/dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package handler

import (
"context"

"github.com/likexian/doh-go"
"github.com/likexian/doh-go/dns"

"github.com/micro/micro/v3/service/errors"
pb "github.com/micro/services/dns/proto"
)

type Dns struct{}

// Return a new handler
func New() *Dns {
return &Dns{}
}

func (d *Dns) Query(ctx context.Context, req *pb.QueryRequest, rsp *pb.QueryResponse) error {
if len(req.Name) == 0 {
return errors.BadRequest("dns.resolve", "invalid name")
}
if len(req.Type) == 0 {
req.Type = "A"
}

c := doh.Use(doh.CloudflareProvider, doh.GoogleProvider)

// do doh query
resp, err := c.Query(ctx, dns.Domain(req.Name), dns.Type(req.Type))
if err != nil {
return errors.InternalServerError("dns.resolve", err.Error())
}
// close the client
c.Close()

rsp.Status = int32(resp.Status)
rsp.TC = resp.TC
rsp.RD = resp.RD
rsp.RA = resp.RA
rsp.AD = resp.AD
rsp.CD = resp.CD

for _, q := range resp.Question {
rsp.Question = append(rsp.Question, &pb.Question{
Name: q.Name,
Type: int32(q.Type),
})
}

for _, a := range resp.Answer {
rsp.Answer = append(rsp.Answer, &pb.Answer{
Name: a.Name,
Type: int32(a.Type),
TTL: int32(a.TTL),
Data: a.Data,
})
}

rsp.Provider = resp.Provider

return nil
}
23 changes: 23 additions & 0 deletions dns/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/logger"
"github.com/micro/services/dns/handler"
pb "github.com/micro/services/dns/proto"
)

func main() {
// Create service
srv := service.New(
service.Name("dns"),
)

// Register handler
pb.RegisterDnsHandler(srv.Server(), handler.New())

// Run service
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}
1 change: 1 addition & 0 deletions dns/micro.mu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service dns

0 comments on commit b40fc2c

Please sign in to comment.