Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Enhancement/plugin/v1 #9

Merged
merged 11 commits into from
Oct 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

cmd/apexd/apexd
63 changes: 63 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
name = "github.com/BurntSushi/toml"
version = "0.3.0"

[[constraint]]
name = "github.com/Sirupsen/logrus"
version = "1.0.3"

[[constraint]]
branch = "master"
name = "github.com/miekg/dns"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.1.4"
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test :
./hack/test.sh

build :
- cd cmd/apexd
- go build

clean :
- rm *.coverprofile

.PHONY: clean
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
ΛPΞX is a DNS server written in Go, help you connect to the real Internet.

# Feature
* Cache
* edns-client-subnet
* Multiple DNS upstream
* DNS Over TLS
* UDP/TCP DNS
* Hosts
* DNSSEC
* Cache

# TODO
- [x] Google DNS over TLS plugin
- [ ] Plugin System
- [ ] Cache
- [ ] Context
- [ ] Documents
# Package Management
Apex uses the Go community [dep](https://github.com/golang/dep) project for package management, but it's young so maybe will cause some unexcepted issue occurred during building. For more information about dep project status, check [dep - Current status](https://github.com/golang/dep#current-status)

# License
MIT
Binary file removed cmd/apexd/apexd
Binary file not shown.
6 changes: 4 additions & 2 deletions cmd/apexd/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package main

import (
"github.com/Sirupsen/logrus"
inbound "github.com/oif/apex/pkg/inbound/v1"
"github.com/oif/apex/plugin"
"github.com/oif/apex/plugin/gdns"
)

func main() {
logrus.SetLevel(logrus.DebugLevel)
s := new(inbound.Server)
s.ListenAddress = ":53"
s.ListenProtocol = []string{"udp"}
s.RegisterPlugins(func() *plugin.GoogleDNS { return new(plugin.GoogleDNS) }())
s.RegisterPlugins(func() *gdns.Plugin { return new(gdns.Plugin) }())
s.Run()
}
71 changes: 47 additions & 24 deletions pkg/inbound/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ package v1

import (
"sync"
"time"

log "github.com/Sirupsen/logrus"
"github.com/miekg/dns"
plugin "github.com/oif/apex/pkg/plugin/v1"
"github.com/oif/apex/pkg/types"
"github.com/sony/sonyflake"
)

// Server implements DNS server with dns.Handler
type Server struct {
ListenAddress string
ListenProtocol []string

pluginObjs []plugin.Object
mux *dns.ServeMux
srvs []*dns.Server
lock sync.RWMutex
wg *sync.WaitGroup
plugins plugin.PluginChain
mux *dns.ServeMux
srvs []*dns.Server
lock sync.RWMutex
wg *sync.WaitGroup
uuid *sonyflake.Sonyflake
}

// Run server
Expand All @@ -30,6 +32,15 @@ func (s *Server) Run() {
s.mux.Handle(".", s)

s.wg = new(sync.WaitGroup)
s.uuid = sonyflake.NewSonyflake(sonyflake.Settings{
StartTime: time.Now(),
MachineID: func() (uint16, error) {
return 1, nil
},
CheckMachineID: func(id uint16) bool {
return true
},
})
// Add wait group
s.wg.Add(len(s.ListenProtocol))

Expand Down Expand Up @@ -58,35 +69,47 @@ func (s *Server) Run() {
// ServeDNS implements dns.Handler interface
func (s *Server) ServeDNS(w dns.ResponseWriter, m *dns.Msg) {
var (
abort bool
err error
// abort bool
err error
reqID uint64
context *plugin.Context
)
log.Debugf("Receive request\n%v", m)
pack := new(types.DNSPack)
pack.Msg = m

for _, p := range s.pluginObjs {
pack, abort, err = p.Patch(pack)
if err != nil {
// error occ
log.Errorf("Error in %s: %v", p.Name(), err)
break
}
if abort {
log.Infof("Abort after %s", p.Name())
break
reqID, err = s.uuid.NextID()
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Get request ID failed")
goto RESPONSE
}

log.WithFields(log.Fields{
"req_id": reqID,
}).Debug("Receive request")

context = plugin.NewContext(w, m, reqID)
context.MustRegisterPluginsOnce(s.plugins)
context.Next()

if context.HasError() {
for _, err = range context.Errors {
log.Errorf("Context error: %v", err)
}
}

log.WithFields(log.Fields{
"req_id": reqID,
}).Debug("Resolve done ready to response")

RESPONSE:
// write resposne message
if err = w.WriteMsg(pack.Msg); err != nil {
if err = w.WriteMsg(m); err != nil {
log.Errorf("Error when write response message: %v", err)
}
}

// RegisterPlugins for server
func (s *Server) RegisterPlugins(p plugin.Object) error {
s.pluginObjs = append(s.pluginObjs, p)
s.plugins = append(s.plugins, p)
// @TODO do some initialization works here
return p.Initialize()
}
Expand Down
26 changes: 12 additions & 14 deletions pkg/inbound/v1/server_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package v1

import (
"os"
"testing"
)

func TestServer(t *testing.T) {
s := &Server{
ListenAddress: ":53",
ListenProtocol: []string{"udp", "tcp"},
}
if os.Getenv("TRAVIS") != "true" {
s.Run()
}
}
// temporarily commented due to some issue of dns graceful shutdown https://github.com/miekg/dns/issues/457
// func TestServer(t *testing.T) {
// s := &Server{
// ListenAddress: ":53",
// ListenProtocol: []string{"udp", "tcp"},
// }
// go func() {
// time.Sleep(10 * time.Second)
// s.Stop()
// }()
// s.Run()
// }
Loading