-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
186 lines (172 loc) · 6.95 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
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
package client
import (
"fmt"
"github.com/ahimgit/navidrome-alexa/pkg/alexa/client/httpclient"
"github.com/ahimgit/navidrome-alexa/pkg/alexa/client/model"
"github.com/pkg/errors"
"net/url"
"strings"
)
const (
headerUserAgent = "Mozilla/5.0 (Linux; Android 13; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/112.0.5615.136 Mobile Safari/537.36"
headerUserAgentApp = "PitanguiBridge/2.2.527420.0-[PLATFORM=Android][MANUFACTURER=samsung][RELEASE=13][BRAND=samsung][SDK=33][MODEL=S2]"
headerAcceptLanguage = "en-US"
)
type IAlexaClient interface {
LogIn() (err error)
PostSequenceCmd(command model.AlexaCmd) (err error)
GetDevices() (devices model.DevicesResponse, err error)
GetVolume() (devices model.VolumeResponse, err error)
}
type AlexaClient struct {
client httpclient.IHttpClient
cookieHelper httpclient.ICookieHelper
baseDomain string
user string
password string
csrf string
}
func NewAlexaClient(baseDomain string, user string, password string, cookieFile string) IAlexaClient {
return &AlexaClient{
client: httpclient.NewHttpClient(),
cookieHelper: httpclient.NewCookieHelper(cookieFile),
baseDomain: baseDomain,
user: user,
password: password,
}
}
func NewAlexaClientWithHttpClient(baseDomain string, user string, password string,
cookieHelper httpclient.ICookieHelper, client httpclient.IHttpClient) IAlexaClient {
return &AlexaClient{
client: client,
cookieHelper: cookieHelper,
baseDomain: baseDomain,
user: user,
password: password,
}
}
func (c *AlexaClient) LogIn() (err error) {
if !c.cookieHelper.CookiesSaved() {
if c.user == "" || c.password == "" {
return errors.New("Alexa.LogIn no saved cookies, user and password are required but empty")
}
// get login form
formBody, referer, err := getLoginForm(c.baseDomain, c.client)
if err != nil {
return errors.Wrap(err, "Alexa.LogIn getting form failed")
}
// submit login form
formData := c.cookieHelper.ExtractLoginFormInputsCSRF(formBody)
formData.Add("email", c.user)
formData.Add("password", c.password)
if err = submitLoginForm(c.baseDomain, referer, formData, c.client); err != nil {
return errors.Wrap(err, "Alexa.LogIn submit form failed")
}
// get devices (sets csrf cookie) and save cookies
_, err = c.GetDevices()
if err != nil {
return errors.Wrap(err, "Alexa.LogIn getting devices failed")
}
if err := c.cookieHelper.SaveCookies(c.client.GetCookieJar(), c.baseDomain); err != nil {
return errors.Wrap(err, "Alexa.LogIn saving cookies failed")
}
} else {
if err := c.cookieHelper.LoadCookies(c.client.GetCookieJar(), c.baseDomain); err != nil {
return errors.Wrap(err, "Alexa.LogIn loading cookies failed")
}
}
csrf := c.cookieHelper.ExtractCSRF(c.client.GetCookieJar(), c.baseDomain)
if csrf == "" {
return errors.New("Alexa.LogIn empty csrf cookie")
}
c.csrf = csrf // sets csrf param
return nil
}
func (c *AlexaClient) PostSequenceCmd(command model.AlexaCmd) (err error) {
apiUrl := fmt.Sprintf("https://alexa.%s/api/behaviors/preview", c.baseDomain)
if err = c.client.RestPOST(apiUrl, buildAppHeaders(c.csrf), command, nil); err != nil {
return errors.Wrap(err, "Alexa.PostSequenceCmd failed")
}
return nil
}
func (c *AlexaClient) GetDevices() (devices model.DevicesResponse, err error) {
apiUrl := fmt.Sprintf("https://alexa.%s/api/devices-v2/device?cached=false", c.baseDomain)
if err = c.client.RestGET(apiUrl, buildAppHeaders(c.csrf), &devices); err != nil {
return devices, errors.Wrap(err, "Alexa.GetDevices failed")
}
return devices, nil
}
func (c *AlexaClient) GetVolume() (volume model.VolumeResponse, err error) {
apiUrl := fmt.Sprintf("https://alexa.%s/api/devices/deviceType/dsn/audio/v1/allDeviceVolumes", c.baseDomain)
if err = c.client.RestGET(apiUrl, buildAppHeaders(c.csrf), &volume); err != nil {
return volume, errors.Wrap(err, "Alexa.GetVolume failed")
}
return volume, nil
}
func getLoginForm(baseDomain string, client httpclient.IHttpClient) (formBody string, referer string, err error) {
formUrl := "https://www." + baseDomain + "/ap/signin" +
"?openid.pape.max_auth_age=0" +
"&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select" +
"&accountStatusPolicy=P1" +
"&language=en_US" +
"&openid.return_to=https%3A%2F%2Fwww." + baseDomain + "%2Fap%2Fmaplanding" +
"&openid.assoc_handle=amzn_dp_project_dee_android" +
"&openid.oa2.response_type=code" +
"&openid.mode=checkid_setup" +
"&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0" +
"&openid.oa2.code_challenge_method=S256" +
"&openid.ns.oa2=http%3A%2F%2Fwww.amazon.com%2Fap%2Fext%2Foauth%2F2" +
"&openid.oa2.code_challenge=" +
"&openid.oa2.scope=device_auth_access" +
"&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select" +
"&openid.oa2.client_id=" +
"&disableLoginPrepopulate=0" +
"&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0" // params order matters ;(
response, err := client.SimpleGET(formUrl, buildWebViewHeaders(referer))
if err != nil {
return "", "", errors.Wrap(err, "getting login form failed")
}
if response.Status != 200 {
return "", "", errors.Errorf("getting login form returned wrong status: %d", response.Status)
}
return response.Body, formUrl, nil
}
func submitLoginForm(baseDomain string, referer string, formData *url.Values, client httpclient.IHttpClient) (err error) {
formUrl := fmt.Sprintf("https://www.%s/ap/signin", baseDomain)
response, err := client.SimplePOST(formUrl, buildWebViewHeaders(referer), formData)
if err != nil {
return errors.Wrap(err, "submit failed")
}
if response.Status != 302 || response.Redirect == "" {
return errors.Errorf("submit failed, wrong status: %d, successful login submit should be a redirect", response.Status)
}
if !strings.Contains(response.Redirect, "maplanding") {
return errors.Errorf("submit failed, try logining in from an app on the same network: %s", response.Redirect)
}
return nil
}
func buildAppHeaders(csrf string) (headers *httpclient.Headers) {
return &httpclient.Headers{
{Key: "Accept", Value: "application/json; charset=utf-8"},
{Key: "csrf", Value: csrf},
{Key: "User-Agent", Value: headerUserAgentApp},
{Key: "Connection", Value: "keep-alive"},
{Key: "Upgrade-Insecure-Requests", Value: "1"},
{Key: "Accept-Language", Value: headerAcceptLanguage},
}
}
func buildWebViewHeaders(referer string) (headers *httpclient.Headers) {
headersCollection := httpclient.Headers{
{Key: "Connection", Value: "keep-alive"},
{Key: "Cache-Control", Value: "max-age=0"},
{Key: "Upgrade-Insecure-Requests", Value: "1"},
{Key: "Content-Type", Value: "application/x-www-form-urlencoded"},
{Key: "User-Agent", Value: headerUserAgent},
{Key: "X-Requested-With", Value: "com.amazon.dee.app"},
{Key: "Accept-Language", Value: headerAcceptLanguage},
}
if referer != "" {
headersCollection = append(headersCollection, httpclient.Header{Key: "Referer", Value: referer})
}
return &headersCollection
}