From 24c48500996db5e16351a7822de5044b4d690ffc Mon Sep 17 00:00:00 2001 From: Hugo Landau Date: Fri, 14 Nov 2014 08:51:57 +0000 Subject: [PATCH] Switch to using service management code. --- backend/backend.go | 2 +- main.go | 47 ++++++++++++++++++++++++++++++++++------------ server/server.go | 16 ++++++++++++---- 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/backend/backend.go b/backend/backend.go index 51a7216..7695e96 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -68,7 +68,7 @@ func New(cfg *Config) (backend *Backend, err error) { return } -// Keep domains in DNS format. +// Keep domains in parsed format. type domain struct { ncv *ncValue } diff --git a/main.go b/main.go index 65cf8a2..bf62953 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,10 @@ package main import "github.com/hlandau/degoutils/config" -import "github.com/hlandau/degoutils/log" -import "github.com/hlandau/degoutils/daemon" + +//import "github.com/hlandau/degoutils/log" +//import "github.com/hlandau/degoutils/daemon" +import "github.com/hlandau/degoutils/service" import "github.com/hlandau/ncdns/server" func main() { @@ -13,21 +15,42 @@ func main() { } config.ParseFatal(&cfg) - err := daemon.Init() - log.Fatale(err) + service.Main(&service.Info{ + Name: "ncdns", + Description: "Namecoin to DNS Daemon", + RunFunc: func(smgr service.Manager) error { + s, err := server.NewServer(&cfg) + if err != nil { + return err + } - if cfg.Daemonize { - err := daemon.Daemonize() + err = s.Start() + if err != nil { + return err + } + + smgr.SetStarted() + <-smgr.StopChan() + + return nil + }, + }) + + /* err := daemon.Init() log.Fatale(err) - } - err = daemon.DropPrivileges(cfg.UID, cfg.GID) - log.Fatale(err, "can't drop privileges") + if cfg.Daemonize { + err := daemon.Daemonize() + log.Fatale(err) + } - s, err := server.NewServer(&cfg) - log.Fatale(err) + err = daemon.DropPrivileges(cfg.UID, cfg.GID) + log.Fatale(err, "can't drop privileges") - s.Run() + s, err := server.NewServer(&cfg) + log.Fatale(err) + + s.Run()*/ } // © 2014 Hugo Landau GPLv3 or later diff --git a/server/server.go b/server/server.go index 920b309..5dfdc41 100644 --- a/server/server.go +++ b/server/server.go @@ -9,6 +9,8 @@ import "fmt" import "os/signal" import "syscall" +const version = "1.0" + type Server struct { cfg ServerConfig @@ -33,9 +35,9 @@ type ServerConfig struct { SelfName string `default:"" usage:"Canonical name for this nameserver (default: autogenerated psuedo-hostname resolving to SelfIP; SelfIP is not used if this is set)"` SelfIP string `default:"127.127.127.127" usage:"The canonical IP address for this service"` - Daemonize bool `default:"false" usage:"Daemonize (doesn't fork)"` - UID int `default:"0" usage:"UID to drop privileges to if run as root"` - GID int `default:"0" usage:"GID to drop privileges to if run as root"` + //Daemonize bool `default:"false" usage:"Daemonize (doesn't fork)"` + //UID int `default:"0" usage:"UID to drop privileges to if run as root"` + //GID int `default:"0" usage:"GID to drop privileges to if run as root"` } func NewServer(cfg *ServerConfig) (s *Server, err error) { @@ -58,6 +60,7 @@ func NewServer(cfg *ServerConfig) (s *Server, err error) { ecfg := &madns.EngineConfig{ Backend: b, + VersionString: "ncdns/" + version, } // key setup @@ -122,13 +125,18 @@ func (s *Server) loadKey(fn, privateFn string) (k *dns.DNSKEY, privatek dns.Priv return } -func (s *Server) Run() { +func (s *Server) Start() error { s.mux = dns.NewServeMux() s.mux.Handle(".", s.engine) s.udpListener = s.runListener("udp") s.tcpListener = s.runListener("tcp") + return nil +} + +func (s *Server) Run() { + s.Start() log.Info("Ready.") // wait