-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_helper.go
187 lines (156 loc) · 6.51 KB
/
http_helper.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
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
func GetDeviceList() DeviceGroupList {
verbosePrint("[+] Getting device list for " + GlobalConfig.Username)
respBody := performHTTPRequest("GET", "/device/group", nil, GlobalConfig.RetryAttempts)
var deviceStatus DeviceGroupList
err := json.Unmarshal(respBody, &deviceStatus)
if err != nil {
verbosePrint("[!] Device list could not be read" + string(err.Error()))
}
verbosePrint("[+] Device list found for " + GlobalConfig.Username)
return deviceStatus
}
func SetDeviceStatus(parameters string) {
getDeviceId()
verbosePrint("[+] Setting device status for " + GlobalConfig.DeviceGuid)
controlJson := `{"deviceGuid":"` + GlobalConfig.DeviceGuid + `","parameters":{`
controlJson += parameters
controlJson += `}}`
respBody := performHTTPRequest("POST", "/deviceStatus/control", []byte(controlJson), GlobalConfig.RetryAttempts)
var result map[string]interface{}
json.Unmarshal([]byte(respBody), &result)
if result["result"] != nil && result["result"].(float64) == 0 {
verbosePrint("[+] Device status set for " + GlobalConfig.DeviceGuid)
} else if result["message"] != nil {
verbosePrint("[!] Error getting setting device status: " + result["message"].(string))
} else {
verbosePrint("[!] Setting device status update may have failed or was only partial " + GlobalConfig.DeviceGuid)
}
}
func GetDeviceStatus() DeviceStatusFull {
getDeviceId()
verbosePrint("[+] Getting device status for " + GlobalConfig.DeviceGuid)
deviceId := strings.Replace(GlobalConfig.DeviceGuid, "+", "%2B", -1)
respBody := performHTTPRequest("GET", "/deviceStatus/now/"+deviceId, nil, GlobalConfig.RetryAttempts)
var deviceStatus DeviceStatusFull
err := json.Unmarshal(respBody, &deviceStatus)
if err != nil {
verbosePrint("[!] Device status could not be read" + string(err.Error()))
}
verbosePrint("[+] Device status found for " + GlobalConfig.DeviceGuid)
//visualize device status
return deviceStatus
}
func getDeviceId() {
if !(GlobalConfig.DeviceGuid == "") {
return
}
verbosePrint("[+] DeviceID not found querying first device in list")
respBody := performHTTPRequest("GET", "/device/group", nil, GlobalConfig.RetryAttempts)
var result map[string]interface{}
json.Unmarshal([]byte(respBody), &result)
// if we got back a json with at least 1 registered device (this cannot be the right way to assert this)
if result["groupList"] != nil &&
result["groupList"].([]interface{})[0] != nil &&
result["groupList"].([]interface{})[0].(map[string]interface{})["deviceList"] != nil &&
result["groupList"].([]interface{})[0].(map[string]interface{})["deviceList"].([]interface{})[0] != nil &&
result["groupList"].([]interface{})[0].(map[string]interface{})["deviceList"].([]interface{})[0].(map[string]interface{})["deviceGuid"] != nil {
deviceGuid := result["groupList"].([]interface{})[0].(map[string]interface{})["deviceList"].([]interface{})[0].(map[string]interface{})["deviceGuid"].(string)
verbosePrint("[+] Device ID " + deviceGuid + " chosen")
GlobalConfig.DeviceGuid = deviceGuid
overwriteConfigFile(GlobalConfig)
} else if result["message"] != nil {
verbosePrint("[!] Error getting DeviceID: " + result["message"].(string))
} else {
verbosePrint("[!] Getting DeviceID failed, reason unknown")
}
}
func refreshLoginAuthToken() {
verbosePrint("[+] Token Expired or invalid, trying Login as " + GlobalConfig.Username)
postBody := []byte(`{"language":"0","loginId":"` + GlobalConfig.Username + `","password":"` + GlobalConfig.Password + `"}`)
//login attempts will be repeated in the main request, should not be retried here, it would cause infinite recursion
respBody := performHTTPRequest("POST", "/auth/login", postBody, -1)
var result map[string]interface{}
json.Unmarshal([]byte(respBody), &result)
if result["uToken"] != nil {
verbosePrint("[+] Login successful")
GlobalConfig.Bearer = result["uToken"].(string)
overwriteConfigFile(GlobalConfig)
return
} else if result["message"] != nil {
verbosePrint("[!] Login failed")
verbosePrint("[!] Error: " + result["message"].(string))
}
verbosePrint("[!] Login failed, reason unknown")
}
func performHTTPRequest(method string, reqURL string, body []byte, retryCount int) []byte {
reqURL = "https://accsmart.panasonic.com" + reqURL
// +1 is here in case login expired, and we need to get a new token
for i := 0; i <= retryCount+1; i++ {
req, _ := http.NewRequest(method, reqURL, bytes.NewBuffer(body))
req.Host = "accsmart.panasonic.com"
if GlobalConfig.Bearer != "" {
req.Header.Set("X-User-Authorization", GlobalConfig.Bearer)
}
req.Header.Set("X-APP-TYPE", "1") //these two get lowercased when its actually sent
req.Header.Set("X-APP-VERSION", "1.14.0")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Accept", "application/json; charset=utf-8")
req.Header.Set("User-Agent", "G-RAC")
tr := &http.Transport{}
if GlobalConfig.HttpProxy != "" {
/*Debug feature to Turn off cert validation */
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
// this is for debug proxying
proxy, _ := url.Parse(GlobalConfig.HttpProxy)
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: http.ProxyURL(proxy),
}
}
//for avoiding infinite redirect loops
client := &http.Client{
Transport: tr,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Do(req)
if err != nil {
verbosePrint("[!] HTTP request failed to " + method + " " + reqURL + " Attempt: " + strconv.Itoa(i))
verbosePrint(err.Error())
continue
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if GlobalConfig.HttpDebug {
fmt.Printf("%s %s Attempt: %s\nRequest: %s \nResponse: %s \n", method, reqURL, strconv.Itoa(i), string(body), string(respBody))
}
if resp.StatusCode != 200 {
//returned with <status code>
verbosePrint("[!] HTTP request returned with " + strconv.Itoa(resp.StatusCode) + " : " + method + " " + reqURL)
// if token was expired, get a new token and retry request (if not already a login request)
if resp.StatusCode == 401 && retryCount != -1 {
refreshLoginAuthToken()
}
// 500 error code would mean the request was wrong, but did not time out
if resp.StatusCode != 500 {
verbosePrint("[!] Retrying previous HTTP request")
continue
}
}
return []byte(respBody)
}
return nil
}