-
Notifications
You must be signed in to change notification settings - Fork 0
/
unifi.go
210 lines (186 loc) · 5.2 KB
/
unifi.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
/* cSpell:disable */
import (
"crypto/tls"
"fmt"
"os"
"time"
"github.com/urfave/cli/v2" // imports as package "cli"
// imports as package "cli"
"github.com/go-resty/resty/v2"
"github.com/spf13/viper"
"github.com/knadh/koanf"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
var (
logger *logrus.Entry
client *resty.Client
authURL, username, password, csrfHeader string
k = koanf.New(".")
ruleStatus *ruleChecker
)
func init() {
viper.SetConfigName("youtube-controller") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("/config/") // path to look for the config file in
viper.AddConfigPath("/etc/youtube/") // path to look for the config file in
viper.AddConfigPath("$HOME/.youtube") // call multiple times to add many search paths
viper.AddConfigPath(".") // optionally look for config in the working directory
err := viper.ReadInConfig() // Find and read the config file
ruleStatus = &ruleChecker{
resty: resty.New(),
config: viper.GetViper(),
}
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
}
log.SetFormatter(&log.TextFormatter{})
log.SetOutput(os.Stdout)
log.SetReportCaller(true)
// Set log level
level, err := logrus.ParseLevel(ruleStatus.config.GetString("logLevel"))
if err != nil {
log.Fatal(err)
}
logrus.SetLevel(level)
// log.SetLevel(log.InfoLevel)
logger = logrus.WithFields(log.Fields{
"component": "cli-client",
})
ruleStatus.resty.SetRedirectPolicy(resty.FlexibleRedirectPolicy(10))
ruleStatus.resty.SetHeader("Content-Type", "application/json")
ruleStatus.resty.SetHeader("Accept", "application/json")
ruleStatus.resty.SetHeader("User-Agent", "youtube-controller")
ruleStatus.resty.SetTimeout(time.Duration(ruleStatus.config.GetInt64("unifi.httpTimeout")) * time.Millisecond)
ruleStatus.resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: ruleStatus.config.GetBool("unifi.insecureSkipVerify")})
ruleStatus.setup()
}
func main() {
app := &cli.App{
Version: "v0.0.1",
Name: "Youtube",
Description: "Grossly over engineered CLI to manage Unifi filter rules on UDM Pro",
Compiled: time.Now(),
Commands: []*cli.Command{
{
Name: "status",
Aliases: []string{"s"},
Usage: "Get current status",
Action: func(c *cli.Context) error {
_, status := ruleStatus.getCurrentStateJSON()
fmt.Println(string(status))
return nil
},
},
{
Name: "allow",
Usage: "Enable allow rule",
Aliases: []string{"A"},
Subcommands: []*cli.Command{
{
Name: "on",
Usage: "enable allow rule",
Category: "action",
Action: func(c *cli.Context) error {
ruleStatus.switchAllowRule(true)
return nil
},
},
{
Name: "off",
Category: "action",
Usage: "disable allow rule",
Action: func(c *cli.Context) error {
ruleStatus.switchAllowRule(false)
return nil
},
},
{
Name: "toggle",
Aliases: []string{"t"},
Category: "action",
Usage: "togle allow rule",
Action: func(c *cli.Context) error {
ruleStatus.toggleAllowRule()
return nil
},
},
{
Name: "status",
Usage: "get status of allow rule",
Aliases: []string{"s"},
Category: "action",
Action: func(c *cli.Context) error {
cs, _ := ruleStatus.getCurrentStateJSON()
if cs.AllowRuleEnabled {
fmt.Println("allow rule enabled")
os.Exit(0)
} else {
fmt.Println("allow rule disabled")
os.Exit(1)
}
return nil
},
},
},
},
{
Name: "block",
Aliases: []string{"B"},
Usage: "Enable block rule",
Subcommands: []*cli.Command{
{
Name: "on",
Usage: "enable block rule",
Category: "action",
Action: func(c *cli.Context) error {
ruleStatus.switchBlockRule(true)
return nil
},
},
{
Name: "off",
Usage: "disable block rule",
Category: "action",
Action: func(c *cli.Context) error {
ruleStatus.switchBlockRule(false)
return nil
},
},
{
Name: "status",
Usage: "get status of block rule",
Category: "action",
Action: func(c *cli.Context) error {
cs, _ := ruleStatus.getCurrentStateJSON()
if cs.BlockRuleEnabled {
fmt.Println("block rule enabled")
os.Exit(0)
} else {
fmt.Println("block rule disabled")
os.Exit(1)
}
return nil
},
},
{
Name: "toggle",
Aliases: []string{"t"},
Category: "action",
Usage: "togle block rule",
Action: func(c *cli.Context) error {
ruleStatus.toggleDenyRule()
return nil
},
},
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}