2013-07-23 11:10:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-01-25 17:43:02 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2020-01-25 18:48:26 +00:00
|
|
|
"github.com/go-redis/redis/v7"
|
2020-01-25 17:43:02 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-01-25 18:48:26 +00:00
|
|
|
"meow.tf/joker/godns/cache"
|
|
|
|
"meow.tf/joker/godns/hosts"
|
2020-01-25 17:43:02 +00:00
|
|
|
"meow.tf/joker/godns/log"
|
2020-01-25 18:48:26 +00:00
|
|
|
"meow.tf/joker/godns/resolver"
|
2020-01-25 17:43:02 +00:00
|
|
|
"meow.tf/joker/godns/settings"
|
2013-07-23 11:10:38 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2013-07-25 04:32:57 +00:00
|
|
|
"runtime"
|
2015-10-14 07:21:21 +00:00
|
|
|
"runtime/pprof"
|
2013-07-23 11:10:38 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
const (
|
|
|
|
Version = "0.3.0"
|
|
|
|
)
|
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
var (
|
2020-01-25 17:43:02 +00:00
|
|
|
cfgFile string
|
2013-07-23 11:10:38 +00:00
|
|
|
)
|
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
func init() {
|
|
|
|
flag.StringVar(&cfgFile, "config", "/etc/godns/godns.conf", "")
|
|
|
|
}
|
2013-07-23 11:10:38 +00:00
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
func main() {
|
2015-10-13 11:33:51 +00:00
|
|
|
initLogger()
|
2013-07-23 11:10:38 +00:00
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
|
|
|
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
|
|
|
}
|
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
server := &Server{
|
2020-01-25 18:48:26 +00:00
|
|
|
host: viper.GetString("server.host"),
|
|
|
|
networks: viper.GetStringSlice("server.nets"),
|
2013-07-23 11:10:38 +00:00
|
|
|
rTimeout: 5 * time.Second,
|
|
|
|
wTimeout: 5 * time.Second,
|
|
|
|
}
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
var resolverCache, negCache cache.Cache
|
2020-02-08 03:38:22 +00:00
|
|
|
r := resolver.NewResolver(resolver.Settings{
|
2020-01-25 18:48:26 +00:00
|
|
|
Timeout: viper.GetInt("resolv.timeout"),
|
|
|
|
Interval: viper.GetInt("resolv.interval"),
|
|
|
|
SetEDNS0: viper.GetBool("resolv.edns0"),
|
|
|
|
ServerListFile: viper.GetStringSlice("resolv.server-list"),
|
|
|
|
ResolvFile: viper.GetString("resolv.file"),
|
|
|
|
})
|
|
|
|
|
|
|
|
cacheDuration := viper.GetDuration("cache.expire")
|
|
|
|
|
|
|
|
redisConfig := settings.RedisSettings{
|
|
|
|
Host: viper.GetString("cache.redis.host"),
|
|
|
|
Port: viper.GetInt("cache.redis.port"),
|
|
|
|
DB: viper.GetInt("cache.redis.db"),
|
|
|
|
Password: viper.GetString("cache.redis.password"),
|
|
|
|
}
|
|
|
|
|
|
|
|
backend := viper.GetString("cache.backend")
|
|
|
|
|
|
|
|
switch backend {
|
|
|
|
case "memory":
|
|
|
|
cacheMaxCount := viper.GetInt("cache.memory.maxCount")
|
|
|
|
|
|
|
|
negCache = cache.NewMemoryCache(cacheDuration/2, cacheMaxCount)
|
|
|
|
resolverCache = cache.NewMemoryCache(cacheDuration, cacheMaxCount)
|
|
|
|
case "memcached":
|
|
|
|
servers := viper.GetStringSlice("cache.memcached.servers")
|
|
|
|
|
|
|
|
resolverCache = cache.NewMemcachedCache(servers, int32(cacheDuration.Seconds()))
|
|
|
|
negCache = cache.NewMemcachedCache(servers, int32(cacheDuration.Seconds()/2))
|
|
|
|
case "redis":
|
|
|
|
resolverCache = cache.NewRedisCache(redisConfig, cacheDuration)
|
|
|
|
negCache = cache.NewRedisCache(redisConfig, cacheDuration/2)
|
|
|
|
default:
|
|
|
|
log.Error("Invalid cache backend %s", backend)
|
|
|
|
panic("Invalid cache backend")
|
|
|
|
}
|
|
|
|
|
|
|
|
providers := make([]hosts.Provider, 0)
|
|
|
|
|
|
|
|
if viper.GetBool("hosts.file.enable") {
|
|
|
|
providers = append(providers, hosts.NewFileProvider(viper.GetString("hosts.file.file"), viper.GetDuration("hosts.file.ttl")))
|
|
|
|
}
|
|
|
|
|
|
|
|
if viper.GetBool("hosts.redis.enable") {
|
|
|
|
rc := redis.NewClient(&redis.Options{Addr: redisConfig.Addr(), DB: redisConfig.DB, Password: redisConfig.Password})
|
2013-07-23 11:10:38 +00:00
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
providers = append(providers, hosts.NewRedisProvider(rc, viper.GetString("hosts.redis.key"), viper.GetDuration("hosts.redis.ttl")))
|
|
|
|
}
|
|
|
|
|
|
|
|
h := hosts.NewHosts(providers)
|
|
|
|
|
|
|
|
server.Run(NewHandler(r, resolverCache, negCache, h))
|
2013-07-25 04:32:57 +00:00
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
log.Info("godns %s (%s)", Version, runtime.Version())
|
|
|
|
|
|
|
|
if viper.GetBool("debug") {
|
2015-10-14 07:21:21 +00:00
|
|
|
go profileCPU()
|
|
|
|
go profileMEM()
|
|
|
|
}
|
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
sig := make(chan os.Signal)
|
|
|
|
signal.Notify(sig, os.Interrupt)
|
2020-01-25 17:43:02 +00:00
|
|
|
<- sig
|
2020-01-25 18:48:26 +00:00
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
log.Info("signal received, stopping")
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 07:21:21 +00:00
|
|
|
func profileCPU() {
|
|
|
|
f, err := os.Create("godns.cprof")
|
|
|
|
if err != nil {
|
2020-01-25 17:43:02 +00:00
|
|
|
log.Error("%s", err)
|
2015-10-14 07:21:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
time.AfterFunc(6*time.Minute, func() {
|
|
|
|
pprof.StopCPUProfile()
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func profileMEM() {
|
|
|
|
f, err := os.Create("godns.mprof")
|
2020-01-25 17:43:02 +00:00
|
|
|
|
2015-10-14 07:21:21 +00:00
|
|
|
if err != nil {
|
2020-01-25 17:43:02 +00:00
|
|
|
log.Error("%s", err)
|
2015-10-14 07:21:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
time.AfterFunc(5*time.Minute, func() {
|
|
|
|
pprof.WriteHeapProfile(f)
|
|
|
|
f.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-13 11:33:51 +00:00
|
|
|
func initLogger() {
|
2020-01-25 17:43:02 +00:00
|
|
|
if viper.GetBool("log.stdout") {
|
|
|
|
log.SetLogger("console", nil)
|
2015-10-13 11:33:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
if file := viper.GetString("log.file"); file != "" {
|
|
|
|
log.SetLogger("file", map[string]interface{}{"file": file})
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
log.SetLevel(settings.LogLevelFor(viper.GetString("log.level")))
|
2013-07-25 04:32:57 +00:00
|
|
|
}
|