forked from maxtors/surisoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
138 lines (128 loc) · 4.21 KB
/
message.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
package surisoc
import (
"fmt"
"net/url"
"strconv"
)
// SocketMessage is a json message that will be sent to the suricata socket
type SocketMessage struct {
Command string `json:"command"`
Arguments *map[string]interface{} `json:"arguments"`
}
// NewSocketMessage will take a command and some arguments and create a
// a new socket message struct
func NewSocketMessage(command string) *SocketMessage {
return &SocketMessage{Command: command}
}
// ParseArgumentsList will check if any arguments are to be parsed and add to the message
func (sm *SocketMessage) ParseArgumentsList(arguments ...string) error {
if len(arguments) != 0 {
err := sm.parseArgumentsList(arguments...)
if err != nil {
return &Error{Message: fmt.Sprintf("Could not parse arguments: %s, %+v (%s)", sm.Command, arguments, err.Error())}
}
}
return nil
}
// ParseArgumentsURLMap will parse a map of arguments and add to the message
func (sm *SocketMessage) ParseArgumentsURLMap(arguments url.Values) error {
var err error
argumentsMap := make(map[string]interface{})
switch sm.Command {
case "iface-stat":
if value, ok := arguments["iface"]; ok {
argumentsMap["iface"] = value[0]
break
} else {
return &Error{Message: fmt.Sprintf("iface-stat should have an iface parameter: %+v", arguments)}
}
case "pcap-file":
if value, ok := arguments["filename"]; ok {
argumentsMap["filename"] = value[0]
} else {
return &Error{Message: fmt.Sprintf("pcap-file should have a filename parameter: %+v", arguments)}
}
if value, ok := arguments["output-dir"]; ok {
argumentsMap["output-dir"] = value[0]
} else {
return &Error{Message: fmt.Sprintf("pcap-file should have a output-dir parameter: %+v", arguments)}
}
if value, ok := arguments["tenant"]; ok {
argumentsMap["tenant"], err = strconv.Atoi(value[0])
if err != nil {
return &Error{Message: fmt.Sprintf("Tenant ID is not an intager: %+v", value)}
}
}
case "conf-get":
if value, ok := arguments["variable"]; ok {
argumentsMap["variable"] = value[0]
} else {
return &Error{Message: fmt.Sprintf("conf-get should have a variable parameter: %+v", arguments)}
}
case "unregister-tenant-handler":
return nil
case "register-tenant-handler":
return nil
case "unregister-tenant":
return nil
case "register-tenant":
return nil
default:
return &Error{Message: fmt.Sprintf("The command \"%s\" with %d arguments is unkown", sm.Command, len(arguments))}
}
sm.Arguments = &argumentsMap
return nil
}
func (sm *SocketMessage) parseArgumentsList(arguments ...string) error {
var err error
argumentsMap := make(map[string]interface{})
switch sm.Command {
case "iface-stat":
if len(arguments) == 1 {
argumentsMap["iface"] = arguments[0]
break
}
return &Error{Message: fmt.Sprintf("iface-stat should have one argument, found %d", len(arguments))}
case "pcap-file":
if len(arguments) > 1 {
argumentsMap["filename"] = arguments[0]
argumentsMap["output-dir"] = arguments[1]
if len(arguments) >= 3 {
argumentsMap["tenant"], err = strconv.Atoi(arguments[2])
if err != nil {
return &Error{Message: fmt.Sprintf("Tenant ID is not an integer: %s", arguments[2])}
}
}
if len(arguments) == 5 {
argumentsMap["continuous"], err = strconv.ParseBool(arguments[3])
if err != nil {
return &Error{Message: fmt.Sprintf("continuous is not a bool: %s", arguments[2])}
}
argumentsMap["delete-when-done"], err = strconv.ParseBool(arguments[4])
if err != nil {
return &Error{Message: fmt.Sprintf("delete-when-done is not a bool: %s", arguments[2])}
}
}
break
}
return &Error{Message: fmt.Sprintf("pcap-file should have atleast 2 arguments, found %d", len(arguments))}
case "conf-get":
if len(arguments) == 1 {
argumentsMap["variable"] = arguments[0]
break
}
return &Error{Message: fmt.Sprintf("conf-get should have one argument, found %d", len(arguments))}
case "unregister-tenant-handler":
return nil
case "register-tenant-handler":
return nil
case "unregister-tenant":
return nil
case "register-tenant":
return nil
default:
return &Error{Message: fmt.Sprintf("The command \"%s\" with %d arguments is unkown", sm.Command, len(arguments))}
}
sm.Arguments = &argumentsMap
return nil
}