-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
120 lines (102 loc) · 2.44 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/mdlayher/unifi"
)
type configuration struct {
api string
user string
pass string
site string
bind string
base string
wlist string
freq time.Duration
}
var (
identifier = "mielke/1.0"
config = configuration{}
)
func main() {
var c *unifi.Client
var w *whitelist
var err error
fmt.Println(identifier + " initialising...")
if c, err = initClient(); err == nil {
if _, err = initSite(c); err == nil {
if w, err = NewWhitelist(c, config.wlist).Load(); err == nil {
go w.UpdateLoop(config.freq)
fmt.Println(identifier + " now serving...")
http.ListenAndServe(config.bind, w.Router())
os.Exit(0)
}
}
}
fmt.Println("error: " + err.Error())
os.Exit(1)
}
// initClient connects and logs into the unifi endpoint
func initClient() (c *unifi.Client, err error) {
c, err = unifi.NewClient(
config.api,
unifi.InsecureHTTPClient(4*time.Second),
)
if err == nil {
c.UserAgent = identifier
err = c.Login(config.user, config.pass)
}
return
}
// initSite checks, whether the configured site name exists
func initSite(c *unifi.Client) (site *unifi.Site, err error) {
var sites []*unifi.Site
var siteNames []string
if sites, err = c.Sites(); err == nil {
for _, s := range sites {
siteNames = append(siteNames, s.Name)
if s.Name == config.site {
site = s
config.site = site.Name
break
}
}
if site == nil {
err = fmt.Errorf(
"could not find site '%s' in ['%s'] on '%s'",
config.site,
strings.Join(siteNames, "','"),
config.api,
)
}
}
return
}
func init() {
mkFlag(&config.api, "api", "", "UniFi API")
mkFlag(&config.user, "user", "", "UniFi API Username")
mkFlag(&config.pass, "pass", "", "UniFi API Password")
mkFlag(&config.site, "site", "default", "UniFi Site")
mkFlag(&config.bind, "bind", ":5520", "HTTP Host:Port to bind to")
mkFlag(&config.base, "base", "/", "Reverse Proxy Path Prefix")
mkFlag(&config.wlist, "whitelist", "whitelist.json", "List File")
mkFlag(&config.freq, "freq", "30s", "Update Frequency")
flag.Parse()
}
func mkFlag(v interface{}, name string, def string, desc string) {
val := os.Getenv("MIELKE_" + strings.ToUpper(name))
if val == "" {
val = def
}
switch v.(type) {
case *string:
flag.StringVar(v.(*string), name, val, desc)
case *time.Duration:
dur, _ := time.ParseDuration(val)
flag.DurationVar(v.(*time.Duration), name, dur, desc)
}
}