-
Notifications
You must be signed in to change notification settings - Fork 12
/
controller.go
150 lines (134 loc) · 2.98 KB
/
controller.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
package redfi
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/tidwall/redcon"
)
var configAddr = ":6380"
const (
RULEADD = "ruleadd"
RULEDEL = "ruledel"
RULELIST = "rulelist"
RULEGET = "ruleget"
RULECOUNTERS = "rulecounters"
)
const (
COMMAND = "command"
DELAY = "delay"
DROP = "drop"
RETEMPTY = "return_empty"
RETERR = "return_err"
CLIENTADDR = "client_addr"
PERCENTAGE = "percentage"
)
type Controller struct {
plan *Plan
}
func newController(p *Plan) (*Controller, error) {
return &Controller{
plan: p,
}, nil
}
func (c *Controller) parseRule(rule *Rule, buf string) error {
kv := strings.Split(buf, "=")
if len(kv) < 2 {
return fmt.Errorf("rule arguments are too low")
}
key := strings.ToLower(kv[0])
switch key {
case DELAY:
delay, err := strconv.Atoi(kv[1])
if err != nil {
return fmt.Errorf("parse delay error: %s", err.Error())
}
rule.Delay = delay
case DROP:
rule.Drop = false
if strings.ToLower(kv[1]) == "true" || kv[1] == "1" {
rule.Drop = true
}
case RETEMPTY:
rule.ReturnEmpty = false
if strings.ToLower(kv[1]) == "true" || kv[1] == "1" {
rule.ReturnEmpty = true
}
case CLIENTADDR:
if len(kv[1]) <= 0 {
return fmt.Errorf("client addr mustn't be empty")
}
rule.ClientAddr = kv[1]
case RETERR:
if len(kv[1]) <= 0 {
return fmt.Errorf("error message mustn't be empty")
}
rule.ReturnErr = kv[1]
case PERCENTAGE:
perc, err := strconv.Atoi(kv[1])
if err != nil {
return fmt.Errorf("parse delay error: %s", err.Error())
}
rule.Percentage = perc
}
return nil
}
func (c *Controller) Start() error {
err := redcon.ListenAndServe(configAddr,
func(conn redcon.Conn, cmd redcon.Command) {
switch strings.ToLower(string(cmd.Args[0])) {
default:
conn.WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'")
case RULEADD:
rulename := string(cmd.Args[1])
if len(rulename) <= 0 {
conn.WriteError("name mustn't be empty")
return
}
rule := Rule{
Name: rulename,
}
for _, frag := range cmd.Args[2:] {
err := c.parseRule(&rule, string(frag))
if err != nil {
conn.WriteError(err.Error())
return
}
}
err := c.plan.AddRule(rule)
if err != nil {
conn.WriteError(err.Error())
return
}
conn.WriteString("OK")
case RULEDEL:
rulename := string(cmd.Args[1])
if len(rulename) <= 0 {
conn.WriteError("name mustn't be empty")
return
}
err := c.plan.DeleteRule(rulename)
if err != nil {
conn.WriteError(err.Error())
return
}
conn.WriteString("OK")
case RULELIST:
rules := c.plan.ListRules()
conn.WriteArray(len(rules))
for _, rule := range rules {
conn.WriteString(rule.String())
}
}
},
func(conn redcon.Conn) bool {
fmt.Println(conn.RemoteAddr())
return true
},
func(conn redcon.Conn, err error) {
log.Printf("closed: %s, err: %v", conn.RemoteAddr(), err)
},
)
fmt.Println("Closed", err)
return err
}