forked from emitter-io/emitter
/
channel.go
259 lines (224 loc) · 6.47 KB
/
channel.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**********************************************************************************
* Copyright (c) 2009-2017 Misakai Ltd.
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
* Foundation, either version 3 of the License, or(at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see<http://www.gnu.org/licenses/>.
************************************************************************************/
package security
import (
"strconv"
"unsafe"
"github.com/emitter-io/emitter/config"
"github.com/emitter-io/emitter/utils"
)
// Channel types
const (
ChannelInvalid = uint8(iota)
ChannelStatic
ChannelWildcard
)
// ChannelOption represents a key/value pair option.
type ChannelOption struct {
Key string
Value string
}
// Channel represents a parsed MQTT topic.
type Channel struct {
Key []byte // Gets or sets the API key of the channel.
Channel []byte // Gets or sets the channel string.
Query []uint32 // Gets or sets the full ssid.
Options []ChannelOption // Gets or sets the options.
ChannelType uint8
}
// Target returns the channel target (first element of the query, second element of an SSID)
func (c *Channel) Target() uint32 {
return c.Query[0]
}
// TTL returns a Time-To-Live option
func (c *Channel) TTL() (uint32, bool) {
return c.getOptUint("ttl")
}
// Last returns the 'last' option
func (c *Channel) Last() (uint32, bool) {
return c.getOptUint("last")
}
// getOptUint retrieves a Uint option
func (c *Channel) getOptUint(name string) (uint32, bool) {
for i := 0; i < len(c.Options); i++ {
if c.Options[i].Key == name {
if val, err := strconv.ParseUint(c.Options[i].Value, 10, 32); err == nil {
return uint32(val), true
}
return 0, false
}
}
return 0, false
}
// ParseChannel attempts to parse the channel from the underlying slice.
func ParseChannel(text []byte) (channel *Channel) {
channel = new(Channel)
channel.Query = make([]uint32, 0, 6)
offset := 0
// First we need to parse the key part
i, ok := channel.parseKey(text)
if !ok {
channel.ChannelType = ChannelInvalid
return channel
}
// Now parse the channel
offset += i
i = channel.parseChannel(text[offset:])
if channel.ChannelType == ChannelInvalid {
return channel
}
// Now parse the options
offset += i
if offset < len(text) {
_, ok = channel.parseOptions(text[offset:])
if !ok {
channel.ChannelType = ChannelInvalid
return channel
}
}
// We've processed everything now
return channel
}
// ParseKey reads the provided API key, this should be the 32-character long
// key or 'emitter' string for custom API requests.
func (c *Channel) parseKey(text []byte) (i int, ok bool) {
//keyChars := 0
for ; i < len(text); i++ {
if text[i] == config.ChannelSeparator {
if c.Key = text[:i]; len(c.Key) > 0 {
return i + 1, true
}
break
}
}
return i, false
}
// ParseKey reads the provided API key, this should be the 32-character long
// key or 'emitter' string for custom API requests.
func (c *Channel) parseChannel(text []byte) (i int) {
length, offset := len(text), 0
chanChars := 0
wildcards := 0
for ; i < length; i++ {
symbol := text[i] // The current byte
switch {
// If we're reading a separator compute the SSID.
case symbol == config.ChannelSeparator:
if chanChars == 0 && wildcards == 0 {
c.ChannelType = ChannelInvalid
return i
}
c.Query = append(c.Query, utils.GetHash(text[offset:i]))
if i+1 == length { // The end flag
c.Channel = text[:i+1]
if c.ChannelType != ChannelWildcard {
c.ChannelType = ChannelStatic
}
return i + 1
} else if text[i+1] == '?' {
c.Channel = text[:i+1]
if c.ChannelType != ChannelWildcard {
c.ChannelType = ChannelStatic
}
return i + 2
}
offset = i + 1
chanChars = 0
wildcards = 0
continue
// If this symbol is a wildcard symbol
case symbol == '+' || symbol == '*':
if chanChars > 0 || wildcards > 0 {
c.ChannelType = ChannelInvalid
return i
}
wildcards++
c.ChannelType = ChannelWildcard
continue
// Valid character, but nothing special
case (symbol >= 45 && symbol <= 58) || (symbol >= 65 && symbol <= 122):
if wildcards > 0 {
c.ChannelType = ChannelInvalid
return i
}
chanChars++
continue
// Weird character, fail.
default:
c.ChannelType = ChannelInvalid
return i
}
}
c.ChannelType = ChannelInvalid
return i
}
// ParseOptions parses the key/value pairs of options, encoded as URL Query string.
func (c *Channel) parseOptions(text []byte) (i int, ok bool) {
length := len(text)
j := i
// We need to create the options container, if we do have options
c.Options = make([]ChannelOption, 0, 2)
var key, val []byte
//chanChars := 0
// Start reading the options.
for i < length {
// Get the key
for j < length {
symbol := text[j] // The current byte
j++
if symbol == '=' {
key = text[i : j-1]
i = j
break
} else if !((symbol >= 48 && symbol <= 57) || (symbol >= 65 && symbol <= 90) || (symbol >= 97 && symbol <= 122)) {
return i, false
}
}
// Get the value
for j < length {
symbol := text[j]
j++
if symbol == '&' {
val = text[i : j-1]
i = j
break
} else if !((symbol >= 48 && symbol <= 57) || (symbol >= 65 && symbol <= 90) || (symbol >= 97 && symbol <= 122)) {
return i, false
} else if j == length {
val = text[i:j]
i = j
// break ? and what about goto for perfs ?
}
}
// By now we should have a key and a value, otherwise this is not a valid channel string.
if len(key) == 0 || len(val) == 0 {
return i, false
}
// Set the option
c.Options = append(c.Options, ChannelOption{
Key: unsafeToString(key),
Value: unsafeToString(val),
})
val = val[0:0]
key = key[0:0]
}
return i, true
}
// unsafeToString is used when you really want to convert a slice
// of bytes to a string without incurring overhead. It is only safe
// to use if you really know the byte slice is not going to change
// in the lifetime of the string
func unsafeToString(bs []byte) string {
return *(*string)(unsafe.Pointer(&bs))
}