Browse Source

Breaking out the service so we can test it soon

Signed-off-by: Erik Hollensbe <github@hollensbe.org>
master
Erik Hollensbe 10 months ago
parent
commit
cfa62422cd
4 changed files with 91 additions and 66 deletions
  1. +4
    -66
      cmd/ldnsd/main.go
  2. +20
    -0
      config/config.go
  3. +6
    -0
      example.conf
  4. +61
    -0
      service/service.go

+ 4
- 66
cmd/ldnsd/main.go View File

@ -2,21 +2,13 @@ package main
import (
"fmt"
"net"
"os"
"os/signal"
"syscall"
"code.hollensbe.org/erikh/ldnsd/config"
"code.hollensbe.org/erikh/ldnsd/dnsdb"
"code.hollensbe.org/erikh/ldnsd/proto"
"code.hollensbe.org/erikh/ldnsd/service"
"code.hollensbe.org/erikh/ldnsd/version"
"github.com/erikh/dnsserver"
"github.com/erikh/go-transport"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"google.golang.org/grpc"
)
const (
@ -30,25 +22,6 @@ func main() {
app.Usage = "Light DNSd -- a small DNS server with a remote control plane"
app.UsageText = app.Name + " [options] [config file]"
app.Author = Author
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "domain, d",
Usage: "Name of FQDN to put records underneath. Trailing dot will be added automatically.",
Value: "internal",
},
cli.StringFlag{
Name: "listen, l",
Usage: "Change the host:port to listen for GRPC connections",
Value: "localhost:7847",
},
cli.StringFlag{
Name: "dnslisten, dl",
Usage: "Change the host:port to listen for DNS queries",
Value: "localhost:53",
},
}
app.Action = runDNS
if err := app.Run(os.Args); err != nil {
@ -57,25 +30,6 @@ func main() {
}
}
func installSignalHandler(appName string, grpcS *grpc.Server, l net.Listener, handler *dnsserver.Server) {
sigChan := make(chan os.Signal, 1)
go func() {
for {
switch <-sigChan {
// FIXME add config reload as SIGUSR1 or SIGHUP
case syscall.SIGTERM, syscall.SIGINT:
logrus.Infof("Stopping %v...", appName)
grpcS.GracefulStop()
l.Close()
handler.Close()
logrus.Infof("Done.")
os.Exit(0)
}
}
}()
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
}
func runDNS(ctx *cli.Context) error {
if len(ctx.Args()) != 1 {
return errors.New("invalid arguments")
@ -86,25 +40,9 @@ func runDNS(ctx *cli.Context) error {
return errors.Wrap(err, "while parsing configuration")
}
db, err := dnsdb.New(c.DBFile)
if err != nil {
return errors.Wrap(err, "could not open database")
if err := service.Boot(ctx.App.Name, c); err != nil {
return errors.Wrap(err, "while running service")
}
cert, err := c.Certificate.NewCert()
if err != nil {
return errors.Wrap(err, "invalid certificate configuration")
}
srv := dnsserver.NewWithDB(ctx.GlobalString("domain"), db)
grpcS := proto.Boot(srv)
l, err := transport.Listen(cert, "tcp", ctx.GlobalString("listen"))
if err != nil {
return errors.Wrap(err, "while configuring grpc listener")
}
go grpcS.Serve(l)
installSignalHandler(ctx.App.Name, grpcS, l, srv)
return srv.Listen(ctx.GlobalString("dnslisten"))
return nil
}

+ 20
- 0
config/config.go View File

@ -13,10 +13,18 @@ const (
defaultCAFile = "/etc/ldnsd/rootCA.pem"
defaultCertFile = "/etc/ldnsd/server.pem"
defaultKeyFile = "/etc/ldnsd/server.key"
defaultGRPCListen = "localhost:7847"
defaultDNSListen = "localhost:53"
defaultDomain = "internal"
)
// Config is the configuration of the dhcpd service
type Config struct {
GRPCListen string `yaml:"grpc"`
DNSListen string `yaml:"listen"`
Domain string `yaml:"domain"`
DBFile string `yaml:"db_file"`
Certificate Certificate `yaml:"certificate"`
}
@ -42,6 +50,18 @@ func (c *Config) validateAndFix() error {
c.DBFile = defaultDBFile
}
if c.GRPCListen == "" {
c.GRPCListen = defaultGRPCListen
}
if c.DNSListen == "" {
c.DNSListen = defaultDNSListen
}
if c.Domain == "" {
c.Domain = defaultDomain
}
if c.Certificate.CertFile == "" {
c.Certificate.CertFile = defaultCertFile
}


+ 6
- 0
example.conf View File

@ -5,3 +5,9 @@
# ca: "/etc/ldnsd/rootCA.pem"
# cert: "/etc/ldnsd/server.pem"
# key: "/etc/ldnsd/server.key"
# # grpc listening port
# grpc: "localhost:7847"
# # dns listening port (udp only!)
# listen: "localhost:53"
# # TLD for domains.
# domain: "internal"

+ 61
- 0
service/service.go View File

@ -0,0 +1,61 @@
package service
import (
"net"
"os"
"os/signal"
"syscall"
"code.hollensbe.org/erikh/ldnsd/config"
"code.hollensbe.org/erikh/ldnsd/dnsdb"
"code.hollensbe.org/erikh/ldnsd/proto"
"github.com/erikh/dnsserver"
"github.com/erikh/go-transport"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
func installSignalHandler(appName string, grpcS *grpc.Server, l net.Listener, handler *dnsserver.Server) {
sigChan := make(chan os.Signal, 1)
go func() {
for {
switch <-sigChan {
// FIXME add config reload as SIGUSR1 or SIGHUP
case syscall.SIGTERM, syscall.SIGINT:
logrus.Infof("Stopping %v...", appName)
grpcS.GracefulStop()
l.Close()
handler.Close()
logrus.Infof("Done.")
os.Exit(0)
}
}
}()
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
}
// Boot the service
func Boot(name string, c config.Config) error {
db, err := dnsdb.New(c.DBFile)
if err != nil {
return errors.Wrap(err, "could not open database")
}
cert, err := c.Certificate.NewCert()
if err != nil {
return errors.Wrap(err, "invalid certificate configuration")
}
srv := dnsserver.NewWithDB(c.Domain, db)
grpcS := proto.Boot(srv)
l, err := transport.Listen(cert, "tcp", c.GRPCListen)
if err != nil {
return errors.Wrap(err, "while configuring grpc listener")
}
go grpcS.Serve(l)
installSignalHandler(name, grpcS, l, srv)
return srv.Listen(c.DNSListen)
}

Loading…
Cancel
Save