2013-07-23 11:10:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2013-07-26 10:54:19 +00:00
|
|
|
"strconv"
|
2015-02-10 09:00:59 +00:00
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2018-07-03 05:35:20 +00:00
|
|
|
"strings"
|
2013-07-23 11:10:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
settings Settings
|
|
|
|
)
|
|
|
|
|
2015-10-13 11:33:51 +00:00
|
|
|
var LogLevelMap = map[string]int{
|
|
|
|
"DEBUG": LevelDebug,
|
|
|
|
"INFO": LevelInfo,
|
|
|
|
"NOTICE": LevelNotice,
|
|
|
|
"WARN": LevelWarn,
|
|
|
|
"ERROR": LevelError,
|
|
|
|
}
|
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
type Settings struct {
|
|
|
|
Version string
|
|
|
|
Debug bool
|
|
|
|
Server DNSServerSettings `toml:"server"`
|
|
|
|
ResolvConfig ResolvSettings `toml:"resolv"`
|
|
|
|
Redis RedisSettings `toml:"redis"`
|
2016-02-12 13:29:25 +00:00
|
|
|
Memcache MemcacheSettings `toml:"memcache"`
|
2013-07-23 11:10:38 +00:00
|
|
|
Log LogSettings `toml:"log"`
|
|
|
|
Cache CacheSettings `toml:"cache"`
|
2013-07-26 04:06:16 +00:00
|
|
|
Hosts HostsSettings `toml:"hosts"`
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResolvSettings struct {
|
2018-02-01 07:39:47 +00:00
|
|
|
Timeout int
|
|
|
|
Interval int
|
|
|
|
SetEDNS0 bool
|
2015-02-04 08:21:08 +00:00
|
|
|
ServerListFile string `toml:"server-list-file"`
|
|
|
|
ResolvFile string `toml:"resolv-file"`
|
2018-07-01 03:08:29 +00:00
|
|
|
DOHServer string `toml:"dns-over-https"`
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DNSServerSettings struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
|
|
|
type RedisSettings struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
DB int
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
2016-02-12 13:29:25 +00:00
|
|
|
type MemcacheSettings struct {
|
|
|
|
Servers []string
|
|
|
|
}
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
func (s RedisSettings) Addr() string {
|
2018-07-03 05:35:20 +00:00
|
|
|
if addr := os.Getenv("REDIS_ADDR"); addr != "" {
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
return s.Host + ":" + strconv.Itoa(s.Port)
|
|
|
|
}
|
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
type LogSettings struct {
|
2015-10-13 11:33:51 +00:00
|
|
|
Stdout bool
|
|
|
|
File string
|
|
|
|
Level string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls LogSettings) LogLevel() int {
|
|
|
|
l, ok := LogLevelMap[ls.Level]
|
|
|
|
if !ok {
|
|
|
|
panic("Config error: invalid log level: " + ls.Level)
|
|
|
|
}
|
|
|
|
return l
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CacheSettings struct {
|
|
|
|
Backend string
|
2013-07-24 16:09:07 +00:00
|
|
|
Expire int
|
2013-07-24 10:29:38 +00:00
|
|
|
Maxcount int
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 04:06:16 +00:00
|
|
|
type HostsSettings struct {
|
2016-09-18 04:04:21 +00:00
|
|
|
Enable bool
|
|
|
|
HostsFile string `toml:"host-file"`
|
|
|
|
RedisEnable bool `toml:"redis-enable"`
|
|
|
|
RedisKey string `toml:"redis-key"`
|
|
|
|
TTL uint32 `toml:"ttl"`
|
|
|
|
RefreshInterval uint32 `toml:"refresh-interval"`
|
2013-07-26 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
func init() {
|
|
|
|
var configFile string
|
|
|
|
|
2018-07-03 05:35:20 +00:00
|
|
|
flag.StringVar(&configFile, "c", "/etc/godns.conf", "Look for godns toml-formatting config file in this directory")
|
2013-07-23 11:10:38 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if _, err := toml.DecodeFile(configFile, &settings); err != nil {
|
|
|
|
fmt.Printf("%s is not a valid toml config file\n", configFile)
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-07-03 05:35:20 +00:00
|
|
|
// Env overrides
|
|
|
|
if cacheBackend := os.Getenv("CACHE_BACKEND"); cacheBackend != "" {
|
|
|
|
settings.Cache.Backend = cacheBackend
|
|
|
|
}
|
|
|
|
|
|
|
|
if memcacheServers := os.Getenv("MEMCACHE_SERVERS"); memcacheServers != "" {
|
|
|
|
settings.Memcache.Servers = strings.Split(memcacheServers, ",")
|
|
|
|
}
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|