-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
182 lines (157 loc) · 4.9 KB
/
main.go
File metadata and controls
182 lines (157 loc) · 4.9 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2016 The HADES Authors. All rights reserved.
package main
import (
"flag"
"fmt"
"math/rand"
"net"
"strconv"
"strings"
"time"
"github.com/coreos/go-etcd/etcd"
backendetcd "github.com/ipdcode/hades/backends/etcd"
"github.com/ipdcode/hades/msg"
"github.com/ipdcode/hades/server"
"github.com/golang/glog"
)
const (
glogFlushPeriod = 5 * time.Second
syncIpStatusPeriod = 60 * time.Second
HadesVersion = "1.0.1"
)
var (
tlskey = ""
tlspem = ""
cacert = ""
config = &server.Config{ReadTimeout: 0, Domain: "", DnsAddr: ""}
nameserver = ""
machine = ""
version = false
)
func init() {
flag.StringVar(&config.Domain, "domain", "hades.local.", "domain to anchor requests")
flag.StringVar(&config.DnsAddr, "addr", "127.0.0.1:53", "ip:port mode , the addr to be bind)")
flag.StringVar(&config.MetricsPort, "metrics-port", "", "the metrics port")
flag.StringVar(&config.IpMonitorPath, "ip-monitor-path", "/hades/monitor/status/", "the ips to check available")
flag.StringVar(&nameserver, "nameservers", "", "nameserver address(es) to forward (non-local) queries to e.g. 8.8.8.8:53,8.8.4.4:53")
flag.StringVar(&machine, "machines", "", "machine address(es) running etcd")
flag.StringVar(&tlskey, "tls-key", "", "TLS Private Key path")
flag.StringVar(&tlspem, "tls-pem", "", "X509 Certificate")
flag.BoolVar(&version, "version",false, "Print version information and quit")
flag.StringVar(&cacert, "ca-cert", "", "CA Certificate")
flag.DurationVar(&config.ReadTimeout, "rtimeout", 2*time.Second, "read timeout")
flag.IntVar(&config.RCache, "rcache", server.RCacheCapacity, "capacity of the response cache")
flag.IntVar(&config.RCacheTtl, "rcache-ttl", server.RCacheTtl, "TTL of the response cache")
}
func glogFlush(period time.Duration) {
for range time.Tick(period) {
glog.Flush()
}
}
func main() {
flag.Parse()
if version{
fmt.Printf("%s\n",HadesVersion)
return
}
go glogFlush(glogFlushPeriod)
defer glog.Flush()
machines := strings.Split(machine, ",")
client := newEtcdClient(machines, tlspem, tlskey, cacert)
if nameserver != "" {
for _, hostPort := range strings.Split(nameserver, ",") {
if err := validateHostPort(hostPort); err != nil {
glog.Fatalf("hades: nameserver is invalid: %s", err)
}
config.Nameservers = append(config.Nameservers, hostPort)
}
}
if err := validateHostPort(config.DnsAddr); err != nil {
glog.Fatalf("hades: addr is invalid: %s", err)
}
if err := server.SetDefaults(config); err != nil {
glog.Fatalf("hades: defaults could not be set from /etc/resolv.conf: %v", err)
}
backend := backendetcd.NewBackend(client, &backendetcd.Config{
Ttl: config.Ttl,
Priority: config.Priority,
})
s := server.New(backend, config)
go func() {
recv := make(chan *etcd.Response)
go client.Watch(msg.Path(config.Domain), 0, true, recv, nil)
duration := 1 * time.Second
for {
select {
case n := <-recv:
if n != nil {
s.UpdateRcache(n)
duration = 1 * time.Second // reset
} else {
glog.Infof("hades: etcd machine cluster update failed, sleeping %s + ~3s", duration)
time.Sleep(duration + (time.Duration(rand.Float32() * 3e9))) // Add some random.
duration *= 2
if duration > 32*time.Second {
duration = 32 * time.Second
}
}
}
}
}()
server.Metrics(config.MetricsPort)
// watch ip status
go func() {
recv := make(chan *etcd.Response)
go client.Watch(config.IpMonitorPath, 0, true, recv, nil)
duration := 1 * time.Second
for {
select {
case n := <-recv:
if n != nil {
s.UpdateHostStatus(n)
duration = 1 * time.Second // reset
} else {
glog.Infof("hades: etcd machine cluster update failed, sleeping %s + ~3s", duration)
time.Sleep(duration + (time.Duration(rand.Float32() * 3e9))) // Add some random.
duration *= 2
if duration > 32*time.Second {
duration = 32 * time.Second
}
}
}
}
}()
// before server run we get the active ips
s.SyncHadesHostStatus()
go s.HostStatusSync(syncIpStatusPeriod)
if err := s.Run(); err != nil {
glog.Fatalf("hades: %s", err)
}
}
func validateHostPort(hostPort string) error {
host, port, err := net.SplitHostPort(hostPort)
if err != nil {
return err
}
if ip := net.ParseIP(host); ip == nil {
return fmt.Errorf("bad IP address: %s", host)
}
if p, _ := strconv.Atoi(port); p < 1 || p > 65535 {
return fmt.Errorf("bad port number %s", port)
}
return nil
}
func newEtcdClient(machines []string, tlsCert, tlsKey, tlsCACert string) (client *etcd.Client) {
// set default if not specified in env
if len(machines) == 1 && machines[0] == "" {
machines[0] = "http://127.0.0.1:4001"
}
if strings.HasPrefix(machines[0], "https://") {
var err error
if client, err = etcd.NewTLSClient(machines, tlsCert, tlsKey, tlsCACert); err != nil {
glog.Fatalf("hades: failure to connect: %s", err)
}
return client
}
return etcd.NewClient(machines)
}