-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
133 lines (120 loc) · 2.88 KB
/
main.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
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
package main
import (
"os"
"os/signal"
"strings"
"syscall"
"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
const version = "0.4.4"
var (
compress bool
dom string
regContainerName bool
regHostName bool
ttl uint32
)
func main() {
app := cli.NewApp()
app.Name = "dkdns"
app.Usage = "Docker Dynamic DNS"
app.Version = version
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "debug, d",
Usage: "Enable debugging.",
},
&cli.IntFlag{
Name: "resync, r",
Value: 0,
Usage: "Periodically resync all containers from docker hosts.",
},
&cli.StringFlag{
Name: "domain, dom",
Value: "dkdns.",
Usage: "Top level domain to serve.",
},
&cli.BoolFlag{
Name: "no-hostname",
Usage: "Don't register container hostnames.",
},
&cli.BoolFlag{
Name: "no-containername",
Usage: "Don't register container names.",
},
&cli.StringFlag{
Name: "listen",
Usage: "Address and port to listen on",
Value: ":53",
},
&cli.BoolFlag{
Name: "compress",
Usage: "Compress dns replies.",
},
&cli.UintFlag{
Name: "ttl",
Usage: "TTL for dns replies.",
Value: 5,
},
&cli.StringSliceFlag{
Name: "docker-endpoint",
Usage: "URL to docker endpoint(s)",
},
&cli.StringFlag{
Name: "ca",
Usage: "Path to docker CA if using TLS",
},
&cli.StringFlag{
Name: "cert",
Usage: "Path to docker cert if using TLS",
},
&cli.StringFlag{
Name: "key",
Usage: "Path to docker key if using TLS",
},
&cli.BoolFlag{
Name: "no-validate",
Usage: "Don't validate ssl connections.",
},
}
app.Action = Run
err := app.Run(os.Args)
if err != nil {
panic(err)
}
}
func Run(ctx *cli.Context) error {
log.SetFormatter(&log.TextFormatter{
//ForceColors: false,
//DisableColors: true,
DisableTimestamp: false,
FullTimestamp: true,
})
if ctx.Bool("debug") {
log.SetLevel(log.DebugLevel)
log.Info("Debug logging enabled")
}
log.WithField("Level", log.GetLevel()).Info("Log level set")
compress = ctx.Bool("compress")
dom = strings.TrimSuffix(normalizeName(ctx.String("domain")), ".") + "."
regContainerName = !ctx.Bool("no-containername")
regHostName = !ctx.Bool("no-hostname")
ttl = uint32(ctx.Uint("ttl"))
log.WithField("Domain", dom).Debug("Domain set")
log.WithField("Compress", compress).Debug("Compression set")
dns.HandleFunc(".", handle)
go serve("tcp", ctx.String("listen"))
go serve("udp", ctx.String("listen"))
endpoints := ctx.StringSlice("docker-endpoint")
if len(endpoints) == 0 {
endpoints = []string{"unix:///var/run/docker.sock"}
}
go monDocker(endpoints, ctx.String("ca"), ctx.String("cert"), ctx.String("key"), !ctx.Bool("no-validate"), ctx.Int("resync"))
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
s := <-sig
log.Infof("Signal (%s) received, stopping\n", s)
return nil
}