manifest.js
6.2 KB
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
'use strict'
const BinaryXmlParser = require('./binaryxml')
// const NS_ANDROID = 'http://schemas.android.com/apk/res/android'
const INTENT_MAIN = 'android.intent.action.MAIN'
const CATEGORY_LAUNCHER = 'android.intent.category.LAUNCHER'
class ManifestParser {
constructor(buffer, options = {}) {
this.buffer = buffer
this.xmlParser = new BinaryXmlParser(this.buffer, options)
}
collapseAttributes(element) {
const collapsed = Object.create(null)
for (let attr of Array.from(element.attributes)) {
collapsed[attr.name] = attr.typedValue.value
}
return collapsed
}
parseIntents(element, target) {
target.intentFilters = []
target.metaData = []
return element.childNodes.forEach(element => {
switch (element.nodeName) {
case 'intent-filter': {
const intentFilter = this.collapseAttributes(element)
intentFilter.actions = []
intentFilter.categories = []
intentFilter.data = []
element.childNodes.forEach(element => {
switch (element.nodeName) {
case 'action':
intentFilter.actions.push(this.collapseAttributes(element))
break
case 'category':
intentFilter.categories.push(this.collapseAttributes(element))
break
case 'data':
intentFilter.data.push(this.collapseAttributes(element))
break
}
})
target.intentFilters.push(intentFilter)
break
}
case 'meta-data':
target.metaData.push(this.collapseAttributes(element))
break
}
})
}
parseApplication(element) {
const app = this.collapseAttributes(element)
app.activities = []
app.activityAliases = []
app.launcherActivities = []
app.services = []
app.receivers = []
app.providers = []
app.usesLibraries = []
element.childNodes.forEach(element => {
switch (element.nodeName) {
case 'activity': {
const activity = this.collapseAttributes(element)
this.parseIntents(element, activity)
app.activities.push(activity)
if (this.isLauncherActivity(activity)) {
app.launcherActivities.push(activity)
}
break
}
case 'activity-alias': {
const activityAlias = this.collapseAttributes(element)
this.parseIntents(element, activityAlias)
app.activityAliases.push(activityAlias)
if (this.isLauncherActivity(activityAlias)) {
app.launcherActivities.push(activityAlias)
}
break
}
case 'service': {
const service = this.collapseAttributes(element)
this.parseIntents(element, service)
app.services.push(service)
break
}
case 'receiver': {
const receiver = this.collapseAttributes(element)
this.parseIntents(element, receiver)
app.receivers.push(receiver)
break
}
case 'provider': {
const provider = this.collapseAttributes(element)
provider.grantUriPermissions = []
provider.metaData = []
provider.pathPermissions = []
element.childNodes.forEach(element => {
switch (element.nodeName) {
case 'grant-uri-permission':
provider.grantUriPermissions.push(this.collapseAttributes(element))
break
case 'meta-data':
provider.metaData.push(this.collapseAttributes(element))
break
case 'path-permission':
provider.pathPermissions.push(this.collapseAttributes(element))
break
}
})
app.providers.push(provider)
break
}
case 'uses-library':
app.usesLibraries.push(this.collapseAttributes(element))
break
}
})
return app
}
isLauncherActivity(activity) {
return activity.intentFilters.some(function(filter) {
const hasMain = filter.actions.some(action => action.name === INTENT_MAIN)
if (!hasMain) {
return false
}
return filter.categories.some(category => category.name === CATEGORY_LAUNCHER)
})
}
parse() {
const document = this.xmlParser.parse()
const manifest = this.collapseAttributes(document)
manifest.usesPermissions = []
manifest.permissions = []
manifest.permissionTrees = []
manifest.permissionGroups = []
manifest.instrumentation = null
manifest.usesSdk = null
manifest.usesConfiguration = null
manifest.usesFeatures = []
manifest.supportsScreens = null
manifest.compatibleScreens = []
manifest.supportsGlTextures = []
manifest.application = Object.create(null)
document.childNodes.forEach(element => {
switch (element.nodeName) {
case 'uses-permission':
manifest.usesPermissions.push(this.collapseAttributes(element))
break
case 'permission':
manifest.permissions.push(this.collapseAttributes(element))
break
case 'permission-tree':
manifest.permissionTrees.push(this.collapseAttributes(element))
break
case 'permission-group':
manifest.permissionGroups.push(this.collapseAttributes(element))
break
case 'instrumentation':
manifest.instrumentation = this.collapseAttributes(element)
break
case 'uses-sdk':
manifest.usesSdk = this.collapseAttributes(element)
break
case 'uses-configuration':
manifest.usesConfiguration = this.collapseAttributes(element)
break
case 'uses-feature':
manifest.usesFeatures.push(this.collapseAttributes(element))
break
case 'supports-screens':
manifest.supportsScreens = this.collapseAttributes(element)
break
case 'compatible-screens':
element.childNodes.forEach(screen => {
return manifest.compatibleScreens.push(this.collapseAttributes(screen))
})
break
case 'supports-gl-texture':
manifest.supportsGlTextures.push(this.collapseAttributes(element))
break
case 'application':
case 'com.stub.StubApp': // 360 encryption services (adbkit-apkreader#13)
manifest.application = this.parseApplication(element)
break
}
})
return manifest
}
}
module.exports = ManifestParser