-
Notifications
You must be signed in to change notification settings - Fork 3
/
hg8347r.go
76 lines (70 loc) · 2.13 KB
/
hg8347r.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
package hg8347r
import (
"encoding/base64"
"encoding/json"
"net/http"
"net/url"
"regexp"
)
// Router instance
type Router struct {
URL string
Req *req
}
// Device info
type Device struct {
Domain string
HostName string
IPAddr string `json:"IpAddr"`
MacAddr string
PortType string
PortID string
TrafficSend string
TrafficRecv string
DevStatus string // Online | Offline
IPType string `json:"IpType"` // DHCP or STATIC
Time string // online time, format is mm:ss
}
// New create a new *Router
func New(URL, username, password string) *Router {
client := newReq()
token := client.Post(URL + "/asp/GetRandCount.asp").String()
// remove first character \ufeff
token = token[len(token)-32:]
pass := base64.StdEncoding.EncodeToString([]byte(password))
header := http.Header{"Cookie": {"Cookie=body:Language:chinese:id=-1; MenuJumpIndex=0"}}
body := url.Values{"UserName": {username}, "PassWord": {pass}, "x.X_HW_Token": {token}}
client.Post(URL+"/login.cgi", header, body)
return &Router{URL: URL, Req: client}
}
// Logout sign out
func (r *Router) Logout() {
r.Req.Post(r.URL + "/logout.cgi?RequestFile=html/logout.html")
}
// ListDevices list devices connected to router
func (r *Router) ListDevices() []Device {
page := r.Req.Get(r.URL + "/html/bbsp/userdevinfo/userdevinfolan.asp").String()
regex := regexp.MustCompile(`<input type="hidden" name="onttoken" id="hwonttoken" value="(\w{32})">`)
token := regex.FindStringSubmatch(page)[1]
body := url.Values{
"HostName": {""},
"IpAddr": {""},
"MacAddr": {""},
"PortType": {""},
"PortID": {""},
"TrafficSend": {""},
"TrafficRecv": {""},
"DevStatus": {""},
"time": {""},
"IpType": {""},
"x.X_HW_Token": {token},
}
urlSuffix := "/getajax.cgi?x=InternetGatewayDevice.LANDevice.1.X_HW_UserDev.{i}&RequestFile=html/bbsp/userdevinfo/userdevinfolan.asp"
s := r.Req.Post(r.URL+urlSuffix, body).EscapeString()
// fmt.Println(s)
devices := []Device{}
json.Unmarshal([]byte(s), &devices)
// last one is {"result": 0}, which should be removed
devices = devices[:len(devices)-1]
return devices
}