forked from 17-12-20-ll/googleplay
-
Notifications
You must be signed in to change notification settings - Fork 2
/
play.go
181 lines (162 loc) · 4.11 KB
/
play.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
package googleplay
import (
"bufio"
"fmt"
"github.com/TravelSir/format"
"github.com/TravelSir/format/crypto"
"io"
"net/http"
"net/url"
"strings"
"time"
)
const Sleep = 4 * time.Second
var LogLevel format.LogLevel
func parseQuery(query io.Reader) url.Values {
vals := make(url.Values)
buf := bufio.NewScanner(query)
for buf.Scan() {
key, val, ok := strings.Cut(buf.Text(), "=")
if ok {
vals.Add(key, val)
}
}
return vals
}
type Delivery struct {
DownloadURL String
SplitDeliveryData []SplitDeliveryData
}
func (d Delivery) Data() []SplitDeliveryData {
datas := d.SplitDeliveryData
data := SplitDeliveryData{DownloadURL: d.DownloadURL}
return append(datas, data)
}
type Details struct {
Title String
Creator String
UploadDate String
VersionString String
VersionCode Varint
NumDownloads Varint
Size Varint
Files int
Micros Varint
CurrencyCode String
}
func (d Details) Format(f fmt.State, verb rune) {
fmt.Fprintln(f, "Title:", d.Title)
fmt.Fprintln(f, "Creator:", d.Creator)
fmt.Fprintln(f, "UploadDate:", d.UploadDate)
fmt.Fprintln(f, "VersionString:", d.VersionString)
fmt.Fprintln(f, "VersionCode:", d.VersionCode)
fmt.Fprintln(f, "NumDownloads:", format.LabelNumber(d.NumDownloads))
fmt.Fprintln(f, "Size:", format.LabelSize(d.Size))
fmt.Fprintln(f, "Files:", d.Files)
fmt.Fprint(f, "Offer: ", d.Micros, " ", d.CurrencyCode)
}
type SplitDeliveryData struct {
ID String
DownloadURL String
}
func (s SplitDeliveryData) Name(app string, ver uint64) string {
if s.ID != "" {
return fmt.Sprint(app, "-", s.ID, "-", ver, ".apk")
}
return fmt.Sprint(app, "-", ver, ".apk")
}
type Token struct {
Services string
Token string
}
// You can also use host "android.clients.google.com", but it also uses
// TLS fingerprinting.
func NewToken(email, password string, s5addr, s5user, s5pwd string) (*Token, error) {
body := url.Values{
"Email": {email},
"Passwd": {password},
"client_sig": {""},
"droidguard_results": {""},
}.Encode()
req, err := http.NewRequest(
"POST", "https://android.googleapis.com/auth", strings.NewReader(body),
)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
hello, err := crypto.ParseJA3(crypto.AndroidAPI26)
if err != nil {
return nil, err
}
LogLevel.Dump(req)
res, err := crypto.Transport(hello, s5addr, s5user, s5pwd).RoundTrip(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errorString(res.Status)
}
val := parseQuery(res.Body)
var tok Token
tok.Services = val.Get("services")
tok.Token = val.Get("Token")
return &tok, nil
}
func OpenToken(elem ...string) (*Token, error) {
return format.Open[Token](elem...)
}
func (t Token) Create(elem ...string) error {
return format.Create(t, elem...)
}
func (t Token) Header(dev *Device) (*Header, error) {
return t.headerVersion(dev, 9999_9999)
}
func (t Token) SingleAPK(dev *Device) (*Header, error) {
return t.headerVersion(dev, 8091_9999)
}
func (t Token) headerVersion(dev *Device, version int64) (*Header, error) {
val := url.Values{
"Token": {t.Token},
"service": {"oauth2:https://www.googleapis.com/auth/googleplay"},
}.Encode()
req, err := http.NewRequest(
"POST", "https://android.googleapis.com/auth", strings.NewReader(val),
)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
LogLevel.Dump(req)
res, err := new(http.Transport).RoundTrip(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errorString(res.Status)
}
var head Header
head.Header = make(http.Header)
// Authorization
head.Set(
"Authorization",
"Bearer "+parseQuery(res.Body).Get("Auth"),
)
// User-Agent
head.Set(
"User-Agent",
fmt.Sprintf("Android-Finsky (sdk=9,versionCode=%v", version),
)
// X-DFE-Device-ID
head.Set(
"X-DFE-Device-ID",
fmt.Sprintf("%x", dev.AndroidID),
)
return &head, nil
}
type errorString string
func (e errorString) Error() string {
return string(e)
}