add privilege drop function

add a option to drop the privilege
check error on ListenAndServe
dns
Dingjun 8 years ago
parent 8bc60e2527
commit 6515c9e44a

1
.gitignore vendored

@ -3,3 +3,4 @@
*.json *.json
*.txt *.txt
gdns gdns
gdns_arm

@ -44,6 +44,8 @@ type Rule struct {
type cfg struct { type cfg struct {
Listen []string `json:"listen"` Listen []string `json:"listen"`
User string `json:"user"`
Group string `json:"group"`
listen []addr listen []addr
ServersString []string `json:"default_servers"` ServersString []string `json:"default_servers"`
servers []addr servers []addr

@ -1,6 +1,8 @@
{ {
"listen":["tcp:0.0.0.0:8053","udp::8053"], "listen":["tcp:0.0.0.0:8053","udp::8053"],
"default_servers":["tcp:208.67.222.222:53","tcp:8.8.8.8:53"], "default_servers":["tcp:208.67.222.222:53","tcp:8.8.8.8:53"],
"user":"nobody",
"group":"nogroup",
"ttl":3600, "ttl":3600,
"timeout":1, "timeout":1,
"blacklist_ips":["ip.txt"], "blacklist_ips":["ip.txt"],

@ -2,16 +2,24 @@ package main
import ( import (
"flag" "flag"
"github.com/fangdingjun/gpp/util"
"github.com/miekg/dns" "github.com/miekg/dns"
"log" "log"
"os" "os"
"time"
) )
func initListeners(c *cfg) { func initListeners(c *cfg) {
for _, a := range c.listen { for _, a := range c.listen {
log.Printf("Listen on %s %s...\n", a.network, a.addr) log.Printf("Listen on %s %s...\n", a.network, a.addr)
s := dns.Server{Addr: a.addr, Net: a.network} s := &dns.Server{Addr: a.addr, Net: a.network}
go s.ListenAndServe() go func(s *dns.Server) {
err := s.ListenAndServe()
if err != nil {
log.Println(err)
os.Exit(-1)
}
}(s)
} }
} }
@ -30,5 +38,13 @@ func main() {
initRouters(config) initRouters(config)
initListeners(config) initListeners(config)
// make a delay to make sure net bind completed before drop privilege
time.Sleep(time.Second)
err = util.DropPrivilege(config.User, config.Group)
if err != nil {
log.Println(err)
}
select {} select {}
} }

Loading…
Cancel
Save