This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
/
status.go
341 lines (315 loc) · 7.59 KB
/
status.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Package openvpn provides various methods for parsing and interacting with
// the OpenVPN server.
package openvpn
import (
"bufio"
"errors"
"io"
"net"
"os"
"reflect"
"strconv"
"strings"
"time"
"fmt"
)
// Represents an INET address with an IP and port
type Addr struct {
IP net.IP
Port int
}
// Represents an route address with an IP and mask
type RouteAddr struct {
net.IPNet
// Remote means it was learned via the remote host
// The server is forwarding this route to that host
Remote bool
}
// Represents a OpenVPN client
type Client struct {
CommonName string
RealAddress Addr
BytesReceived uint64
BytesSent uint64
Since time.Time
}
// Represents a OpenVPN route
type Route struct {
VirtualAddress RouteAddr
CommonName string
RealAddress Addr
LastRef time.Time
}
// readState represents the current seciton/state of the Status as we
// are parsing it
type readState int
const (
stateUnknown = iota
stateClients
stateRoutes
stateStats
stateEnd
)
// Represents the OpenVPN status at a point in time
type Status struct {
Updated time.Time
Clients []Client
Routes []Route
MaxQueue uint64
state readState
}
// parseFn is the type returned from parseLine and is used to further
// process the line
type parseFn func(*Status, string) error
// EOF is returned when the end of the file is reached in parseLine
var EOF = errors.New("EOF reached")
// parseTime parses a string date time following the format:
// Mon Jan 2 15:04:05 2006
// it returns a Time struct
func parseTime(text string) (time.Time, error) {
return time.Parse("Mon Jan 2 15:04:05 2006", text)
}
// parseAddr parses a string address and returns the resulting Addr
func parseAddr(text string) (Addr, error) {
a := Addr{}
h, p, err := net.SplitHostPort(text)
if err != nil {
return a, err
}
a.IP = net.ParseIP(h)
if a.IP == nil {
return a, errors.New("Invalid IP encountered")
}
a.Port, err = strconv.Atoi(p)
if err != nil {
return a, err
}
return a, nil
}
// parseRouteAddr parses a string route address and returns the resulting
// RouteAddr struct
func parseRouteAddr(text string) (RouteAddr, error) {
r := RouteAddr{}
// detect if this is a remote addr
if strings.HasSuffix(text, "C") {
text = strings.TrimSuffix(text, "C")
r.Remote = true
}
// for some reason ipm.IP is 16 bytes for an IPv4, but ip is 4 bytes
// so we use ip instead
ip, ipm, err := net.ParseCIDR(text)
if err == nil {
r.IP = ip
r.Mask = ipm.Mask
return r, nil
}
//it might not be a CIDR and instead just an IP
r.IP = net.ParseIP(text)
if r.IP == nil {
return r, err
}
r.Mask = net.CIDRMask(32, 8 * net.IPv4len)
if r.IP.To4() == nil {
r.Mask = net.CIDRMask(128, 8 * net.IPv6len)
}
return r, nil
}
var timeType = reflect.TypeOf(time.Time{})
var addrType = reflect.TypeOf(Addr{})
var netIPType = reflect.TypeOf(net.IP{})
var routeAddrType = reflect.TypeOf(RouteAddr{})
// parseStructParts takes a struct and fills in the fields based on reflection
// and the order of the []string slice passed in
func parseStructParts(v reflect.Value, parts []string) error {
var f reflect.Value
for i := 0; i < v.NumField() && i < len(parts); i += 1 {
f = v.Field(i)
if !f.CanSet() {
continue
}
switch f.Kind() {
case reflect.Uint64:
p, err := strconv.ParseUint(parts[i], 10, 64)
if err != nil {
return err
}
f.SetUint(p)
case reflect.Int64:
p, err := strconv.ParseInt(parts[i], 10, 64)
if err != nil {
return err
}
f.SetInt(p)
case reflect.String:
f.SetString(parts[i])
default:
t := f.Type()
switch {
case t.AssignableTo(timeType):
p, err := parseTime(parts[i])
if err != nil {
return err
}
f.Set(reflect.ValueOf(p))
case t.AssignableTo(addrType):
p, err := parseAddr(parts[i])
if err != nil {
return err
}
f.Set(reflect.ValueOf(p))
case t.AssignableTo(netIPType):
p := net.ParseIP(parts[i])
if p == nil {
return errors.New("Invalid IP encountered")
}
f.Set(reflect.ValueOf(p))
case t.AssignableTo(routeAddrType):
p, err := parseRouteAddr(parts[i])
if err != nil {
return err
}
f.Set(reflect.ValueOf(p))
default:
return errors.New("Unknown type encountered in struct")
}
}
}
return nil
}
// parseNothing is a placeholder function that does nothing
func parseNothing(s *Status, text string) error {
return nil
}
// parseUpdated parses the "Updated," line
func parseUpdated(s *Status, text string) error {
if text[0:7] == "Updated" {
t, err := parseTime(text[8:])
if err == nil {
s.Updated = t
}
return err
}
return nil
}
// parseClient parses lines in the CLIENT LIST section
func parseClient(s *Status, text string) error {
parts := strings.Split(text, ",")
c := Client{}
v := reflect.ValueOf(&c).Elem()
err := parseStructParts(v, parts)
if err != nil {
//detect column header line
//todo: better detect this
if parts[0] == "Common Name" {
return nil
}
return err
}
s.Clients = append(s.Clients, c)
return nil
}
// parseRoute parses lines in the ROUTING TABLE section
func parseRoute(s *Status, text string) error {
parts := strings.Split(text, ",")
c := Route{}
v := reflect.ValueOf(&c).Elem()
err := parseStructParts(v, parts)
if err != nil {
//detect column header line
//todo: better detect this
if parts[0] == "Virtual Address" {
return nil
}
return err
}
s.Routes = append(s.Routes, c)
return nil
}
// parseStat parses lines in the GLOBAL STATS section
func parseStat(s *Status, text string) error {
parts := strings.Split(text, ",")
if strings.Contains(parts[0], "queue length") {
p, err := strconv.ParseUint(parts[1], 10, 64)
if err != nil {
return err
}
s.MaxQueue = p
}
return nil
}
// parseLine accepts a string and returns the appropriate parse* function
// for detailed, specific parsing of the line
func (s *Status) parseLine(text string) (parseFn, error) {
var err error
// for all these section headers we want to return parseNothing since we
// just want to set the state from the header not actually process anything
fn := parseNothing
if text == "END" || text == "" {
s.state = stateEnd
err = EOF
} else if strings.Contains(text, "CLIENT LIST") {
s.state = stateClients
} else if strings.Contains(text, "ROUTING TABLE") {
s.state = stateRoutes
} else if strings.Contains(text, "GLOBAL STATS") {
s.state = stateStats
} else if strings.HasPrefix(text, "Updated,") {
// since updated is in the middle of a section do not change the state
fn = parseUpdated
} else {
// return the appropriate fn for the state we were in as determined by
// the last header
switch (s.state) {
case stateClients:
fn = parseClient
case stateRoutes:
fn = parseRoute
case stateStats:
fn = parseStat
case stateEnd:
fn = parseNothing
case stateUnknown:
fn = parseNothing
err = errors.New("Unexpected text encountered")
}
}
return fn, err
}
// Parses an io.Reader into a Status
func Parse(r io.Reader) (*Status, error) {
s := &Status{}
scanner := bufio.NewScanner(r)
var t string
var err error
var fn parseFn
line := 0
for scanner.Scan() {
line++
t = scanner.Text()
fn, err = s.parseLine(t)
if err == nil {
// parse the line using the returned function from parseLine
err = fn(s, t)
}
if err != nil {
if err == EOF {
break
}
return nil, errors.New(fmt.Sprintf("Error on line %d: %s", line, err))
}
}
err = scanner.Err()
if err != nil {
return nil, err
}
return s, nil
}
// Helper that reads a file into Parse
func ParseFile(path string) (*Status, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return Parse(file)
}