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
*.txt
gdns
gdns_arm

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

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

@ -2,16 +2,24 @@ package main
import (
"flag"
"github.com/fangdingjun/gpp/util"
"github.com/miekg/dns"
"log"
"os"
"time"
)
func initListeners(c *cfg) {
for _, a := range c.listen {
log.Printf("Listen on %s %s...\n", a.network, a.addr)
s := dns.Server{Addr: a.addr, Net: a.network}
go s.ListenAndServe()
s := &dns.Server{Addr: a.addr, Net: a.network}
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)
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 {}
}

Loading…
Cancel
Save