/
apk.go
316 lines (256 loc) · 7.35 KB
/
apk.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package main
import (
"archive/zip"
"bytes"
"dasa.cc/dae/context"
"dasa.cc/dae/datastore"
"dasa.cc/dae/handler"
"dasa.cc/dae/render"
"dasa.cc/dae/user"
"fmt"
"github.com/gorilla/mux"
"io"
"labix.org/v2/mgo/bson"
"net/http"
"os"
"os/exec"
"strings"
"time"
)
type Apk struct {
Id bson.ObjectId `_id`
FileId bson.ObjectId
IconId bson.ObjectId
ProguardMapId *bson.ObjectId
Time time.Time
ReportIds []bson.ObjectId
Badging struct {
ApplicationLabel string `application-label`
Package struct {
Name string `name`
VersionCode string `versionCode`
VersionName string `versionName`
}
}
}
// upload is a web handler for receiving an apk.
func upload(w http.ResponseWriter, r *http.Request) *handler.Error {
// dont use memory for holding apk
if err := r.ParseMultipartForm(0); err != nil {
return handler.NewError(err, 500, "Unable to parse multipart form.")
}
f, _, err := r.FormFile("apk")
if err != nil {
return handler.NewError(err, 500, "Form file \"apk\" does not exist")
}
// dump badging and locate appropriate icon name within apk
m := dumpBadging(f.(*os.File).Name())
k := fmt.Sprintf("application-icon-%s", m["densities"].([]interface{})[0])
res := m[k].(string)
// locate tmp file and extract icon
fi, err := f.(*os.File).Stat()
if err != nil {
return handler.NewError(err, 500, "Can't stat file.")
}
zr, err := zip.NewReader(f, fi.Size())
if err != nil {
return handler.NewError(err, 500, "Not a valid zip archive")
}
icon := zipReadBytes(zr, res)
// Link current user to package name, save badging, apk file, and icon file.
db := datastore.New()
defer db.Close()
u := user.Current(context.New(r), db)
apkFile, err := db.FS().Create("")
if err != nil {
return handler.NewError(err, 500, "Can't save apk")
}
defer apkFile.Close()
io.Copy(apkFile, f)
iconFile, err := db.FS().Create("")
if err != nil {
return handler.NewError(err, 500, "Can't save icon")
}
defer iconFile.Close()
iconFile.Write(icon)
// using bson.M to store all badging values, including those not specified in struct
apkId := bson.NewObjectId()
apkName := m["package"].(map[string]interface{})["name"].(string)
apkM := bson.M{
"_id": apkId,
"fileid": apkFile.Id(),
"iconid": iconFile.Id(),
"time": time.Now(),
"badging": m,
}
if err = db.C("apks").Insert(apkM); err != nil {
return handler.NewError(err, 500, "Error inserting apk info.")
}
project := ProjectByName(u, apkName)
project.ApkIds = append([]bson.ObjectId{apkId}, project.ApkIds...)
if _, err = db.C("projects").UpsertId(project.Id, project); err != nil {
return handler.NewError(err, 500, "Error upserting project.")
}
http.Redirect(w, r, "/console/index", 302)
return nil
}
// zipReadBytes opens an apk, locates the specified resource,
// and returns []bytes for resource.
func zipReadBytes(zr *zip.Reader, res string) []byte {
buf := new(bytes.Buffer)
for _, zf := range zr.File {
if zf.Name == res {
rc, err := zf.Open()
if err != nil {
panic(err)
}
io.Copy(buf, rc)
rc.Close()
return buf.Bytes()
}
}
panic("File not found.")
}
// dumpBadging calls aapt from the OS and parses the output into a mapping.
// Later on, we map key/value pairs from the mapping to a struct so that apks
// are backwards compatible for making use of new information not currently in
// use by the site.
func dumpBadging(path string) map[string]interface{} {
out, err := exec.Command("aapt", "d", "badging", path).Output()
if err != nil {
panic(err)
}
badging := make(map[string]interface{})
var key string
var value interface{}
for _, line := range strings.Split(fmt.Sprintf("%s", out), "\n") {
if !strings.Contains(line, ":") {
continue
}
tmp := strings.SplitN(line, ":", 2)
key = tmp[0]
value = tmp[1]
switch key {
case "package", "application", "launchable-activity":
value = parseMap(value.(string))
case "supports-screens", "densities":
value = parseSlice(value.(string))
default:
value = strings.Replace(value.(string), "'", "", -1)
}
switch badging[key].(type) {
case interface{}:
badging[key] = []interface{}{badging[key], value}
case []interface{}:
badging[key] = append(badging[key].([]interface{}), value)
case nil:
badging[key] = value
}
}
return badging
}
// parseSlice returns an array of interface{} given string.
func parseSlice(s string) []interface{} {
s = strings.Replace(s, "'", "", -1)
a := []interface{}{}
for _, v := range strings.Split(s, " ") {
if v == "" {
continue
}
a = append(a, v)
}
return a
}
// parseMap returns a mapping of key/value pairs based on input string
func parseMap(s string) map[string]interface{} {
var key string
escape := false
start := 0
m := make(map[string]interface{})
for i, c := range s {
switch c {
case ' ':
if !escape {
start = i + 1
}
case '=':
if !escape {
key = s[start:i]
start = i + 1
}
case '\'':
if escape && start != i {
m[key] = s[start:i]
} else {
start++
}
escape = !escape
}
}
return m
}
// download provides the requested apk by the given bson.ObjectId
func download(w http.ResponseWriter, r *http.Request) *handler.Error {
var (
apk Apk
buf bytes.Buffer
)
db := datastore.New()
defer db.Close()
q := bson.M{"_id": bson.ObjectIdHex(r.FormValue("id"))}
err := db.C("apks").Find(q).One(&apk)
if err != nil {
return handler.NewError(err, 404, "No record of apk.")
}
file, err := db.FS().OpenId(apk.FileId)
if err != nil {
return handler.NewError(err, 404, "No such apk.")
}
io.Copy(&buf, file)
filename := fmt.Sprintf("%s-%s.apk", apk.Badging.ApplicationLabel, apk.Time)
w.Header().Set("Content-Type", "application/vnd.android.package-archive")
w.Header().Set("Content-Disposition", "attachment;filename="+filename)
w.Write(buf.Bytes())
return nil
}
// icon retrieves a previously saved apk icon.
func icon(w http.ResponseWriter, r *http.Request) *handler.Error {
db := datastore.New()
defer db.Close()
id := mux.Vars(r)["iconId"]
if id == "" {
w.Header().Set("Content-type", "image/png")
w.Write([]byte{137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0, 0, 0, 31, 21, 196, 137, 0, 0, 0, 13, 73, 68, 65, 84, 8, 215, 99, 96, 96, 96, 96, 0, 0, 0, 5, 0, 1, 94, 243, 42, 58, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130})
return nil
}
file, err := db.FS().OpenId(bson.ObjectIdHex(id))
if err != nil {
return handler.NewError(err, 404, "No such icon.")
}
var buf bytes.Buffer
io.Copy(&buf, file)
// TODO set content type correctly
w.Header().Set("Content-type", "image/png")
w.Write(buf.Bytes())
return nil
}
func apkById(w http.ResponseWriter, r *http.Request) *handler.Error {
db := datastore.New()
defer db.Close()
apkId := bson.ObjectIdHex(mux.Vars(r)["id"])
var apk *Apk
// TODO confirm user has access to apk
if err := db.C("apks").FindId(apkId).One(&apk); err != nil {
return handler.NewError(err, 500, "Failed to locate apk.")
}
render.Json(w, apk)
return nil
}
// register our web handlers.
func init() {
router.Handle("/apk/upload", handler.New(handler.Auth, upload))
router.Handle("/apk/{apkId}", handler.New(handler.Auth, apkById))
router.Handle("/apk/{apkId}/download", handler.New(handler.Auth, download))
router.Handle("/apk/icon/", handler.New(handler.Auth, icon))
router.Handle("/apk/icon/{iconId}", handler.New(handler.Auth, icon))
}