-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
100 lines (83 loc) · 2.46 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
package netgear_nighthawk_m5_api
import (
"bytes"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"time"
)
type NetgearNighthawkM5Api struct {
httpClient http.Client
routerBaseURL string
}
func NewNetgearNighthawkM5Api(routerBaseURL string) *NetgearNighthawkM5Api {
cookieJar, err := cookiejar.New(nil)
if err != nil {
panic(err)
}
return &NetgearNighthawkM5Api{
httpClient: http.Client{
Jar: cookieJar,
Timeout: 5 * time.Second,
CheckRedirect: checkRedirect,
},
routerBaseURL: routerBaseURL,
}
}
// checkRedirect stops redirects to /index.html to allow getting the redirect on the request.
func checkRedirect(req *http.Request, via []*http.Request) error {
//if req.URL.Path == "/success.json" || req.URL.Path == "/error.json" {
// return http.ErrUseLastResponse
//}
return nil
}
func (n *NetgearNighthawkM5Api) FormRequest(formName string, v url.Values) error {
modelInfo, err := n.ModelInfo()
if err != nil {
return fmt.Errorf("failed to get model info: %w", err)
}
const failedLocation = "/error.json"
const successLocation = "/success.json"
values := url.Values{
"token": []string{modelInfo.Session.SecToken},
"err_redirect": []string{failedLocation},
"ok_redirect": []string{successLocation},
}
for k, v := range v {
values[k] = v
}
req, err := http.NewRequest(http.MethodPost, n.routerBaseURL+"/Forms/"+formName, bytes.NewBufferString(values.Encode()))
if err != nil {
return fmt.Errorf("failed to create config request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := n.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to send config request: %w", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("config failed: %d %s, expected (200 Ok)", resp.StatusCode, resp.Status)
}
if resp.Request.URL.Path != successLocation {
return fmt.Errorf("config failed: %s, expected %s", resp.Request.URL.Path, successLocation)
}
return nil
}
// Login will log the session into the router.
func (n *NetgearNighthawkM5Api) Login(password string) error {
if err := n.FormRequest("config", url.Values{
"session.password": []string{password},
}); err != nil {
return fmt.Errorf("failed to login: %w", err)
}
return nil
}
func (n *NetgearNighthawkM5Api) Logout() error {
if err := n.FormRequest("config", url.Values{
"session.password": []string{""},
}); err != nil {
return fmt.Errorf("failed to logout: %w", err)
}
return nil
}