-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* DNS over HTTPs * fix proto
- Loading branch information
Showing
15 changed files
with
781 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
dns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
FROM alpine | ||
ADD dns /dns | ||
ENTRYPOINT [ "/dns" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package main | ||
|
||
//go:generate make proto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
service dns |
Oops, something went wrong.