-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcontroller.go
More file actions
213 lines (179 loc) · 4.54 KB
/
Copy pathcontroller.go
File metadata and controls
213 lines (179 loc) · 4.54 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
package omada
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"strconv"
"time"
)
type Controller struct {
httpClient *http.Client
baseURL string
controllerId string
controllerVer string
token string
siteId string
Sites map[string]string
user string
pass string
}
type ControllerInfo struct {
ErrorCode int `json:"errorCode"`
Msg string `json:"msg"`
Result struct {
ControllerVer string `json:"controllerVer"`
APIVer string `json:"apiVer"`
Configured bool `json:"configured"`
Type int `json:"type"`
SupportApp bool `json:"supportApp"`
OmadacID string `json:"omadacId"`
} `json:"result"`
}
type LoginBody struct {
Username string `json:"username"`
Password string `json:"password"`
}
type LoginResponse struct {
ErrorCode int `json:"errorCode"`
Msg string `json:"msg"`
Result struct {
RoleType int `json:"roleType"`
Token string `json:"token"`
} `json:"result"`
}
type currentUserResponse struct {
ErrorCode int `json:"errorCode"`
Msg string `json:"msg"`
Result struct {
ID string `json:"id"`
Type int `json:"type"`
RoleType int `json:"roleType"`
Name string `json:"name"`
OmadacID string `json:"omadacId"`
Adopt bool `json:"adopt"`
Manage bool `json:"manage"`
License bool `json:"license"`
SiteManage bool `json:"siteManage"`
Privilege struct {
Sites []Sites
LastVisited string `json:"lastVisited"`
All bool `json:"all"`
} `json:"privilege"`
Disaster int `json:"disaster"`
NeedFeedback bool `json:"needFeedback"`
DefaultSite bool `json:"defaultSite"`
ForceModify bool `json:"forceModify"`
Dbnormal bool `json:"dbnormal"`
} `json:"result"`
}
type Sites struct {
Name string `json:"name"`
Key string `json:"key"`
}
func New(baseURL string) Controller {
jar, _ := cookiejar.New(nil)
v, _ := os.LookupEnv("OMADA_DISABLE_HTTPS_VERIFICATION")
disableHttpsVerification, _ := strconv.ParseBool(v)
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: disableHttpsVerification},
}
httpClient := &http.Client{
Jar: jar,
Timeout: (30 * time.Second),
Transport: transport,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
return Controller{
httpClient: httpClient,
baseURL: baseURL,
}
}
func (c *Controller) GetControllerInfo() error {
address, err := url.JoinPath(c.baseURL, "/api/info")
if err != nil {
return err
}
req, err := http.NewRequest("GET", address, nil)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
res, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("status code: %d", res.StatusCode)
}
var infoResponse ControllerInfo
if err := json.NewDecoder(res.Body).Decode(&infoResponse); err != nil {
return err
}
if infoResponse.ErrorCode != 0 {
err = fmt.Errorf("failed to get controller info: code='%d', message='%s'", infoResponse.ErrorCode, infoResponse.Msg)
return err
}
c.controllerId = infoResponse.Result.OmadacID
c.controllerVer = infoResponse.Result.ControllerVer
return nil
}
func (c *Controller) Login(user string, pass string) error {
c.user = user
c.pass = pass
endpoint, err := url.JoinPath(c.baseURL, c.controllerId, "/api/v2/login")
if err != nil {
return err
}
loginBody := LoginBody{
Username: user,
Password: pass,
}
loginJSON, err := json.Marshal(loginBody)
if err != nil {
return err
}
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(loginJSON))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
res, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
err = fmt.Errorf("status code: %d", res.StatusCode)
return err
}
var login LoginResponse
if err := json.NewDecoder(res.Body).Decode(&login); err != nil {
return err
}
if login.ErrorCode != 0 {
return fmt.Errorf("omada controller login error, code: %d, message: %s", login.ErrorCode, login.Msg)
}
c.token = login.Result.Token
err = c.getSites()
if err != nil {
return err
}
return nil
}
func (c *Controller) refreshLogin() error {
jar, _ := cookiejar.New(nil)
c.httpClient.Jar = jar
err := c.Login(c.user, c.pass)
if err != nil {
return err
}
return nil
}