-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
134 lines (118 loc) · 3.83 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
134
// Shaman is a small, clusterable, lightweight, api-driven dns server.
//
// Usage
//
// To start shaman as a server, simply run (with administrator privileges):
//
// shaman -s
//
// For more specific usage information, refer to the help doc `shaman -h`:
// Usage:
// shaman [flags]
// shaman [command]
//
// Available Commands:
// add Add a domain to shaman
// delete Remove a domain from shaman
// list List all domains in shaman
// get Get records for a domain
// update Update records for a domain
// reset Reset all domains in shaman
//
// Flags:
// -C, --api-crt string Path to SSL crt for API access
// -k, --api-key string Path to SSL key for API access
// -p, --api-key-password string Password for SSL key
// -H, --api-listen string Listen address for the API (ip:port) (default "127.0.0.1:1632")
// -c, --config-file string Configuration file to load
// -O, --dns-listen string Listen address for DNS requests (ip:port) (default "127.0.0.1:53")
// -d, --domain string Parent domain for requests (default ".")
// -i, --insecure Disable tls key checking (client) and listen on http (api). Also disables auth-token
// -2, --l2-connect string Connection string for the l2 cache (default "scribble:///var/db/shaman")
// -l, --log-level string Log level to output [fatal|error|info|debug|trace] (default "INFO")
// -s, --server Run in server mode
// -t, --token string Token for API Access (default "secret")
// -T, --ttl int Default TTL for DNS records (default 60)
// -v, --version Print version info and exit
//
package main
import (
"fmt"
"github.com/jcelliott/lumber"
"github.com/spf13/cobra"
"github.com/nanopack/shaman/api"
"github.com/nanopack/shaman/cache"
"github.com/nanopack/shaman/commands"
"github.com/nanopack/shaman/config"
"github.com/nanopack/shaman/server"
)
var (
// shaman provides the shaman cli/server functionality
shamanTool = &cobra.Command{
Use: "shaman",
Short: "shaman - api driven dns server",
Long: ``,
PersistentPreRunE: readConfig,
PreRunE: preFlight,
RunE: startShaman,
SilenceErrors: true,
SilenceUsage: true,
}
// shaman version information (populated by go linker)
// -ldflags="-X main.version=${tag} -X main.commit=${commit}"
version string
commit string
)
// add supported cli commands/flags
func init() {
shamanTool.AddCommand(commands.AddDomain)
shamanTool.AddCommand(commands.DelDomain)
shamanTool.AddCommand(commands.ListDomains)
shamanTool.AddCommand(commands.GetDomain)
shamanTool.AddCommand(commands.UpdateDomain)
shamanTool.AddCommand(commands.ResetDomains)
config.AddFlags(shamanTool)
}
func main() {
shamanTool.Execute()
}
func readConfig(ccmd *cobra.Command, args []string) error {
if err := config.LoadConfigFile(); err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
return nil
}
func preFlight(ccmd *cobra.Command, args []string) error {
if config.Version {
fmt.Printf("shaman %s (%s)\n", version, commit)
return fmt.Errorf("")
}
if !config.Server {
ccmd.HelpFunc()(ccmd, args)
return fmt.Errorf("")
}
return nil
}
func startShaman(ccmd *cobra.Command, args []string) error {
config.Log = lumber.NewConsoleLogger(lumber.LvlInt(config.LogLevel))
// initialize cache
err := cache.Initialize()
if err != nil {
config.Log.Fatal(err.Error())
return err
}
// make channel for errors
errors := make(chan error)
go func() {
errors <- api.Start()
}()
go func() {
errors <- server.Start()
}()
// break if any of them return an error (blocks exit)
if err := <-errors; err != nil {
config.Log.Fatal(err.Error())
}
return err
}