2013-07-23 11:10:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-01-25 17:43:02 +00:00
|
|
|
"flag"
|
2020-01-25 18:48:26 +00:00
|
|
|
"github.com/go-redis/redis/v7"
|
2021-04-15 03:42:24 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-01-25 17:43:02 +00:00
|
|
|
"github.com/spf13/viper"
|
2021-04-15 03:42:24 +00:00
|
|
|
"io"
|
|
|
|
"meow.tf/joker/godns/api"
|
2020-01-25 18:48:26 +00:00
|
|
|
"meow.tf/joker/godns/cache"
|
|
|
|
"meow.tf/joker/godns/hosts"
|
|
|
|
"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 {
|
2021-04-15 03:42:24 +00:00
|
|
|
log.WithField("file", viper.ConfigFileUsed()).Info("Using configuration from file")
|
2020-01-25 17:43:02 +00:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
var resolverSettings resolver.Settings
|
|
|
|
|
|
|
|
/*
|
|
|
|
resolver.Settings{
|
|
|
|
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"),
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
viper.UnmarshalKey("resolv", &resolverSettings)
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
var resolverCache, negCache cache.Cache
|
2021-04-15 03:42:24 +00:00
|
|
|
r := resolver.NewResolver(resolverSettings)
|
2020-01-25 18:48:26 +00:00
|
|
|
|
|
|
|
cacheDuration := viper.GetDuration("cache.expire")
|
|
|
|
|
|
|
|
backend := viper.GetString("cache.backend")
|
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
var redisConfig settings.RedisSettings
|
|
|
|
|
|
|
|
viper.UnmarshalKey("cache.redis", &redisConfig)
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
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":
|
2021-04-15 03:42:24 +00:00
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
resolverCache = cache.NewRedisCache(redisConfig, cacheDuration)
|
|
|
|
negCache = cache.NewRedisCache(redisConfig, cacheDuration/2)
|
|
|
|
default:
|
2021-04-15 03:42:24 +00:00
|
|
|
log.WithField("backend", backend).Fatalln("Invalid cache backend")
|
2020-01-25 18:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")))
|
|
|
|
}
|
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
if viper.GetBool("hosts.bolt.enable") {
|
|
|
|
providers = append(providers, hosts.NewBoltProvider(viper.GetString("hosts.bolt.file")))
|
|
|
|
}
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
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
|
|
|
|
2021-04-15 04:00:36 +00:00
|
|
|
providers = append(providers, hosts.NewRedisProvider(rc, viper.GetString("hosts.redis.key")))
|
2020-01-25 18:48:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
if viper.GetBool("api.enabled") {
|
|
|
|
go func() {
|
|
|
|
err := api.Start()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatalln("Unable to bind API")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
h := hosts.NewHosts(providers)
|
|
|
|
|
|
|
|
server.Run(NewHandler(r, resolverCache, negCache, h))
|
2013-07-25 04:32:57 +00:00
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
log.Infof("joker dns %s (%s)", Version, runtime.Version())
|
2020-01-25 18:48:26 +00:00
|
|
|
|
|
|
|
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)
|
2021-04-15 03:42:24 +00:00
|
|
|
|
2015-10-14 07:21:21 +00:00
|
|
|
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") {
|
2021-04-15 03:42:24 +00:00
|
|
|
// 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 != "" {
|
2021-04-15 03:42:24 +00:00
|
|
|
f, err := os.Create(file)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetOutput(io.MultiWriter(f, log.StandardLogger().Out))
|
|
|
|
}
|
|
|
|
|
|
|
|
level, err := log.ParseLevel(viper.GetString("log.level"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
log.SetLevel(level)
|
2013-07-25 04:32:57 +00:00
|
|
|
}
|