-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
218 lines (191 loc) · 4.97 KB
/
client.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
package edgemax
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
)
// A Client is a client for a Ubiquiti EdgeMAX device.
//
// Client.Login must be called and return a nil error before any additional
// actions can be performed with a Client.
type Client struct {
client *http.Client
url *url.URL
}
const (
// userAgent is the default user agent this package will report to
// the EdgeMAX device.
userAgent = "github.com/vaga/edgemax_exporter"
// sessionCookie is the name of the session cookie used to authenticate
// against EdgeMAX devices.
sessionCookie = "PHPSESSID"
)
// NewClient creates a new Client, using the input EdgeMAX device address
// and an optional HTTP client. If no HTTP client is specified, a default
// one will be used.
//
// Client.Login must be called and return a nil error before any additional
// actions can be performed with a Client.
func NewClient(addr string, client *http.Client) (*Client, error) {
u, err := url.Parse(strings.TrimRight(addr, "/"))
if err != nil {
return nil, err
}
if client == nil {
client = &http.Client{
Timeout: 10 * time.Second,
}
}
if client.Jar == nil {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
client.Jar = jar
}
return &Client{
client: client,
url: u,
}, nil
}
// Login authenticates against the EdgeMAX device using the specified username
// and password. Login must be called and return a nil error before any
// additional actions can be performed.
func (c *Client) Login(username, password string) error {
v := make(url.Values, 2)
v.Set("username", username)
v.Set("password", password)
_, err := c.client.PostForm(c.url.String(), v)
return err
}
// Stats opens a websocket connection to an EdgeMAX device to retrieve
// statistics which are sent using the socket.
func (c *Client) Stats(
systemCh chan<- SystemStat,
dpiCh chan<- DPIStat,
ifacesCh chan<- InterfacesStat,
) (func(), error) {
conn, err := c.dial()
if err != nil {
return nil, err
}
var sessionID string
for _, c := range c.client.Jar.Cookies(c.url) {
if c.Name == sessionCookie {
sessionID = c.Value
break
}
}
if err := conn.WriteMessage(websocket.TextMessage, marshalWS(
connectRequest{
Subscribe: []stat{
stat{Name: "system-stats"},
stat{Name: "export"},
stat{Name: "interfaces"},
},
SessionID: sessionID,
},
)); err != nil {
return nil, err
}
doneCh := make(chan struct{})
wg := new(sync.WaitGroup)
go c.keepAlive(wg, doneCh)
go c.read(conn, wg, doneCh, systemCh, dpiCh, ifacesCh)
return func() { close(doneCh); wg.Wait() }, nil
}
// dial initializes the websocket used for Client.Stats
func (c *Client) dial() (*websocket.Conn, error) {
// Websocket URL is adapted from HTTP URL
wsURL := *c.url
wsURL.Scheme = "wss"
wsURL.Path = "/ws/stats"
d := &websocket.Dialer{EnableCompression: true}
// Copy TLS config from client if using standard *http.Transport, so that
// using InsecureHTTPClient can also apply to websocket connections
if tr, ok := c.client.Transport.(*http.Transport); ok {
d.TLSClientConfig = tr.TLSClientConfig
}
conn, _, err := d.Dial(wsURL.String(), http.Header{
"Origin": []string{c.url.Scheme + "://" + c.url.Host},
})
if err != nil {
return nil, err
}
return conn, nil
}
// read receives raw stats from a websocket and decodes them into
// Stat structs of various types.
func (c *Client) read(
conn *websocket.Conn,
wg *sync.WaitGroup,
doneCh chan struct{},
systemCh chan<- SystemStat, dpiCh chan<- DPIStat, ifacesCh chan<- InterfacesStat,
) {
defer conn.Close()
wg.Add(1)
defer wg.Done()
for {
select {
case <-doneCh:
return
default:
}
_, m, err := conn.ReadMessage()
if err != nil {
log.Println("read:", err)
close(doneCh)
return
}
rm := make(map[string]json.RawMessage)
if err := unmarshalWS(m, &rm); err != nil {
log.Println("unmarshal:", err)
}
for sn, sk := range rm {
switch sn {
case "system-stats":
var s SystemStat
if err := json.Unmarshal(sk, &s); err != nil {
log.Println("unmarshal system-stats:", err)
}
systemCh <- s
case "export":
var s DPIStat
if err := json.Unmarshal(sk, &s); err != nil {
log.Println("unmarshal export:", err)
}
dpiCh <- s
case "interfaces":
var s InterfacesStat
if err := json.Unmarshal(sk, &s); err != nil {
log.Println("unmarshal interfaces:", err)
}
ifacesCh <- s
}
}
}
}
// keepalive sends heartbeat requests at regular intervals to the EdgeMAX
// device to keep a session active while Client.Stats is running.
func (c *Client) keepAlive(wg *sync.WaitGroup, doneCh chan struct{}) {
wg.Add(1)
defer wg.Done()
for {
_, err := c.client.Get(fmt.Sprintf("%s/api/edge/heartbeat.json?_=%d", c.url.String(), time.Now().UnixNano()))
if err != nil {
log.Printf("could not request edgemax API: %v", err)
}
select {
case <-time.After(10 * time.Second):
case <-doneCh:
return
}
}
}