-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
115 lines (99 loc) · 2.45 KB
/
cli.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
package cli
import (
"bytes"
"fmt"
"github.com/razielsd/antibruteforce/app/config"
)
// Cli - cli api.
type Cli struct {
client *clientAPI
}
// NewCli - create new instance of Cli.
func NewCli(cfg config.AppConfig) *Cli {
return &Cli{
client: newClientAPI(cfg.Addr),
}
}
// ShowWhitelist show whitelist.
func (c *Cli) ShowWhitelist() (string, error) {
l, err := c.client.getWhitelist()
if err != nil {
return "", fmt.Errorf("%w: unable get whitelist", err)
}
return c.drawList("Whitelist", l), nil
}
// ShowBlacklist show blacklist.
func (c *Cli) ShowBlacklist() (string, error) {
l, err := c.client.getBlacklist()
if err != nil {
return "", fmt.Errorf("%w: unable get blacklist", err)
}
return c.drawList("Blacklist", l), nil
}
func (c *Cli) drawList(title string, l []string) string {
buf := bytes.NewBuffer([]byte{})
fmt.Fprintf(buf, "--=== %s ===--\n", title)
for _, ip := range l {
_, _ = fmt.Fprintf(buf, "%s\n", ip)
}
if len(l) == 0 {
_, _ = fmt.Fprintln(buf, "Empty")
}
return buf.String()
}
// AppendBlacklist add ip/subnet to blacklist.
func (c *Cli) AppendBlacklist(clientIP string) (string, error) {
err := c.client.appendBlacklist(clientIP)
if err != nil {
return "", err
}
return "OK", nil
}
// AppendWhitelist add ip/subnet to whitelist.
func (c *Cli) AppendWhitelist(clientIP string) (string, error) {
err := c.client.appendWhitelist(clientIP)
if err != nil {
return "", err
}
return "OK", nil
}
// RemoveBlacklist remove ip/subnet from blacklist.
func (c *Cli) RemoveBlacklist(clientIP string) (string, error) {
err := c.client.removeBlacklist(clientIP)
if err != nil {
return "", err
}
return "OK", nil
}
// RemoveWhitelist - remove ip/subnet from whitelist.
func (c *Cli) RemoveWhitelist(clientIP string) (string, error) {
err := c.client.removeWhitelist(clientIP)
if err != nil {
return "", err
}
return "OK", nil
}
// DropBucketByLogin remove bucket by login.
func (c *Cli) DropBucketByLogin(key string) (string, error) {
err := c.client.dropBucketByLogin(key)
if err != nil {
return "", err
}
return "OK", nil
}
// DropBucketByPwd remove bucket by password.
func (c *Cli) DropBucketByPwd(key string) (string, error) {
err := c.client.dropBucketByPasswd(key)
if err != nil {
return "", err
}
return "OK", nil
}
// DropBucketByIP remove bucket by IP.
func (c *Cli) DropBucketByIP(key string) (string, error) {
err := c.client.dropBucketByIP(key)
if err != nil {
return "", err
}
return "OK", nil
}