-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdns.go
102 lines (77 loc) · 2.15 KB
/
sdns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
//go:generate go run gen.go
import (
"flag"
"fmt"
"math/rand"
"os"
"os/signal"
"runtime"
"time"
"github.com/faceair/go-dnsmasq/config"
"github.com/faceair/go-dnsmasq/middleware"
"github.com/faceair/go-dnsmasq/server"
"github.com/semihalev/log"
)
var (
// Config is the global configuration
Config *config.Config
// Version returns the build version of sdns, this should be incremented every new release
Version = "0.3.0-rc1"
// ConfigVersion returns the version of sdns, this should be incremented every time the config changes so sdns presents a warning
ConfigVersion = "0.3.0"
// ConfigPath returns the configuration path
ConfigPath = flag.String("config", "sdns.toml", "location of the config file, if not found it will be generated")
// Usage return print usage information
Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS]\n\n", os.Args[0])
fmt.Fprintln(os.Stderr, "OPTIONS:")
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, "USAGE:")
fmt.Fprintln(os.Stderr, "./sdns -config=sdns.toml")
fmt.Fprintln(os.Stderr, "")
}
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Usage = Usage
}
func setup() {
var err error
if Config, err = config.Load(*ConfigPath, ConfigVersion); err != nil {
log.Crit("Config loading failed", "error", err.Error())
}
lvl, err := log.LvlFromString(Config.LogLevel)
if err != nil {
log.Crit("Log verbosity level unknown")
}
log.Root().SetLevel(lvl)
log.Root().SetHandler(log.LvlFilterHandler(lvl, log.StdoutHandler))
if Config.Timeout.Duration < 250*time.Millisecond {
Config.Timeout.Duration = 250 * time.Millisecond
}
if Config.ConnectTimeout.Duration < 250*time.Millisecond {
Config.ConnectTimeout.Duration = 250 * time.Millisecond
}
if Config.CacheSize < 1024 {
Config.CacheSize = 1024
}
if Config.CookieSecret == "" {
Config.CookieSecret = fmt.Sprintf("%16x", rand.Int63())
}
}
func run() {
middleware.Setup(Config)
server := server.New(Config)
server.Run()
}
func main() {
flag.Parse()
log.Info("Starting sdns...", "version", Version)
setup()
run()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
log.Info("Stopping sdns...")
}