-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
260 lines (203 loc) · 6.11 KB
/
Copy pathmain.go
File metadata and controls
260 lines (203 loc) · 6.11 KB
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
package fastnetmon
import (
"fmt"
"net/url"
"strings"
"errors"
"github.com/levigross/grequests"
)
type ResponseArrayJson struct {
Success bool `json:"success"`
ErrorText string `json:"error_text"`
Values []string `json:"values"`
}
type ErrorJson struct {
Success bool `json:"success"`
ErrorText string `json:"error_text"`
}
type ResponseJson struct {
Success bool `json:"success"`
ErrorText string `json:"error_text"`
Value string `json:"value"`
}
type FastNetMonClient struct {
User string `json:"api_user"`
Password string `json:"api_password"`
Host string `json:"api_host"`
Port uint32 `json:"api_port"`
Ro *grequests.RequestOptions
Prefix string
}
// Creates new client, just checks input, does not execute connection attemps
//
// Depricated: new one NewClientV2 with support V2 API
func NewClient(host string, port uint32, user, password string) (*FastNetMonClient, error) {
client := FastNetMonClient{}
client.User = user
client.Password = password
client.Host = host
client.Port = port
if user != "" && password != "" && host != "" && port != 0 {
client.Ro = &grequests.RequestOptions{Auth: []string{client.User, client.Password}}
client.Prefix = fmt.Sprintf("http://%s:%d", client.Host, client.Port)
return &client, nil
}
return nil, errors.New("Please provide all fields")
}
// Creates new client, just checks input, does not execute connection attemps
func NewClientV2(host string, port uint32, user, password string, EnabledV2 bool, TLS bool, InsecureSkipVerify bool) (*FastNetMonClient, error) {
client := FastNetMonClient{}
client.User = user
client.Password = password
client.Host = host
client.Port = port
if user == "" && password == "" && host == "" && port == 0 {
return nil, errors.New("Please provide all fields")
}
client.Ro = &grequests.RequestOptions{Auth: []string{client.User, client.Password}}
proto := "http"
if TLS {
proto = "https"
if InsecureSkipVerify {
client.Ro.InsecureSkipVerify = true
}
}
client.Prefix = fmt.Sprintf("%s://%s:%d", proto, client.Host, client.Port)
// if IPv6 - need []
if strings.Contains(host, ":") {
client.Prefix = fmt.Sprintf("%s://[%s]:%d", proto, client.Host, client.Port)
}
if EnabledV2 {
client.Prefix, _ = url.JoinPath(client.Prefix, "/api/v2")
}
return &client, nil
}
func apiStatusError(resp *grequests.Response) error {
if resp.StatusCode == 401 {
return errors.New("Auth denied")
}
responseBody := strings.TrimSpace(resp.String())
if responseBody != "" {
return fmt.Errorf("Did not return OK: %d, response: %s", resp.StatusCode, responseBody)
}
return fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
func decodeResponseJSON(response *grequests.Response, userStruct interface{}) (err error) {
responseBody := strings.TrimSpace(response.String())
err = response.JSON(&userStruct)
if err != nil {
responseBody = strings.ReplaceAll(responseBody, "\n", " ")
if len(responseBody) > 31 {
responseBody = responseBody[:30] + "..."
}
err = fmt.Errorf("Cannot parse output '%s' to json: %v", responseBody, err)
return
}
return
}
// Returns all networks known by FastNetMon
func (client *FastNetMonClient) GetNetworks() ([]string, error) {
resp, err := grequests.Get(client.Prefix+"/main/networks_list", grequests.FromRequestOptions(client.Ro))
if err != nil {
return nil, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
return nil, apiStatusError(resp)
}
networks_response := ResponseArrayJson{}
err = decodeResponseJSON(resp, &networks_response)
if err != nil {
return nil, err
}
return networks_response.Values, nil
}
func (client *FastNetMonClient) AddNetwork(cidr string) error {
encoded := strings.Replace(cidr, "/", "%2f", 1)
resp, err := grequests.Put(client.Prefix+"/main/networks_list/"+encoded, grequests.FromRequestOptions(client.Ro))
if err != nil {
return fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
return apiStatusError(resp)
}
response := ErrorJson{}
err = decodeResponseJSON(resp, &response)
if err != nil {
return err
}
if !response.Success {
return fmt.Errorf("API error: %s", response.ErrorText)
}
return nil
}
func (client *FastNetMonClient) RemoveNetwork(cidr string) error {
encoded := strings.Replace(cidr, "/", "%2f", 1)
resp, err := grequests.Delete(client.Prefix+"/main/networks_list/"+encoded, grequests.FromRequestOptions(client.Ro))
if err != nil {
return fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
return apiStatusError(resp)
}
response := ErrorJson{}
err = decodeResponseJSON(resp, &response)
if err != nil {
return err
}
if !response.Success {
return fmt.Errorf("API error: %s", response.ErrorText)
}
return nil
}
func (client *FastNetMonClient) Commit() error {
resp, err := grequests.Put(client.Prefix+"/commit", grequests.FromRequestOptions(client.Ro))
if err != nil {
return fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
return apiStatusError(resp)
}
response := ErrorJson{}
err = decodeResponseJSON(resp, &response)
if err != nil {
return err
}
if !response.Success {
return fmt.Errorf("API error: %s", response.ErrorText)
}
return nil
}
func (client *FastNetMonClient) GetLicense() (info LicenseInfo, err error) {
resp, err := grequests.Get(client.Prefix+"/license", grequests.FromRequestOptions(client.Ro))
if err != nil {
return info, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
return info, apiStatusError(resp)
}
response := ResponseLicense{}
err = decodeResponseJSON(resp, &response)
info = response.Object
if err != nil {
return info, err
}
return info, nil
}
func (client *FastNetMonClient) GetVersion() (string, error) {
resp, err := grequests.Get(client.Prefix+"/fastnetmon_version", grequests.FromRequestOptions(client.Ro))
if err != nil {
return "", fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
return "", apiStatusError(resp)
}
response := ResponseJson{}
err = decodeResponseJSON(resp, &response)
if err != nil {
return "", err
}
if !response.Success {
return "", fmt.Errorf("API error: %s", response.ErrorText)
}
return response.Value, nil
}