-
Notifications
You must be signed in to change notification settings - Fork 355
/
cli.ts
195 lines (167 loc) · 5.21 KB
/
cli.ts
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
import * as path from 'path'
import parseArgs = require('yargs-parser')
import chalk = require('chalk')
import Listr = require('listr')
import tempy = require('tempy')
import patchApk, { showAppBundleWarning } from './patch-apk'
import { patchXapkBundle, patchApksBundle } from './patch-app-bundle'
import Apktool from './tools/apktool'
import UberApkSigner from './tools/uber-apk-signer'
import Tool from './tools/tool'
export type TaskOptions = {
inputPath: string
outputPath: string
skipPatches: boolean
apktool: Apktool
uberApkSigner: UberApkSigner
tmpDir: string
wait: boolean
debuggable: boolean
}
interface PatchingError extends Error {
/**
* Interleaved stdout and stderr output on execa errors
* @see https://github.com/sindresorhus/execa#all-1
*/
all?: string
}
const { version } = require('../package.json')
async function main() {
const args = parseArgs(process.argv.slice(2), {
string: ['apktool'],
boolean: ['help', 'wait', 'skip-patches', 'debuggable'],
})
if (args.help) {
showHelp()
process.exit()
}
const [input] = args._
if (!input) {
showHelp()
process.exit(1)
}
const inputPath = path.resolve(process.cwd(), input)
const fileExtension = path.extname(input)
const baseName = path.basename(input, fileExtension)
const outputName = `${baseName}-patched${fileExtension}`
const outputPath = path.resolve(path.dirname(inputPath), outputName)
let taskFunction: (options: TaskOptions) => Listr
switch (fileExtension) {
case '.apk':
taskFunction = patchApk
break
case '.xapk':
taskFunction = patchXapkBundle
break
case '.apks':
case '.zip':
taskFunction = patchApksBundle
break
default:
showSupportedExtensions()
}
const tmpDir = tempy.directory({ prefix: 'apk-mitm-' })
process.chdir(tmpDir)
const apktool = new Apktool({
frameworkPath: path.join(tmpDir, 'framework'),
customPath: args.apktool,
})
const uberApkSigner = new UberApkSigner()
showVersions({ apktool, uberApkSigner })
console.log(chalk.dim(` Using temporary directory:\n ${tmpDir}\n`))
taskFunction({
inputPath,
outputPath,
tmpDir,
apktool,
uberApkSigner,
wait: args.wait,
skipPatches: args.skipPatches,
debuggable: args.debuggable,
})
.run()
.then(context => {
if (taskFunction === patchApk && context.usesAppBundle) {
showAppBundleWarning()
}
console.log(
chalk`\n {green.inverse Done! } Patched file: {bold ./${outputName}}\n`,
)
})
.catch((error: PatchingError) => {
const message = getErrorMessage(error, { tmpDir })
console.error(
[
'',
chalk` {red.inverse.bold Failed! } An error occurred:`,
'',
message,
'',
` The full logs of all commands are available here:`,
` ${path.join(tmpDir, 'logs')}`,
'',
].join('\n'),
)
if (process.arch.startsWith('arm')) showArmWarning()
process.exit(1)
})
}
function getErrorMessage(error: PatchingError, { tmpDir }: { tmpDir: string }) {
if (error.all) return formatCommandError(error.all, { tmpDir })
return error.stack
}
function formatCommandError(error: string, { tmpDir }: { tmpDir: string }) {
return (
error
// Replace mentions of the (sometimes very long) temporary directory path
.replace(new RegExp(tmpDir, 'g'), chalk`{bold <tmp_dir>}`)
// Highlight (usually relevant) warning lines in Apktool output
.replace(/^W: .+$/gm, line => chalk`{yellow ${line}}`)
// De-emphasize Apktool info lines
.replace(/^I: .+$/gm, line => chalk`{dim ${line}}`)
// De-emphasize (not very helpful) Apktool "could not exec" error message
.replace(
/^.+brut\.common\.BrutException: could not exec.+$/gm,
line => chalk`{dim ${line}}`,
)
)
}
function showHelp() {
console.log(chalk`
$ {bold apk-mitm} <path-to-apk/xapk/apks>
{dim {bold --wait} Wait for manual changes before re-encoding {gray.italic (optional)}}
{dim {bold --debuggable} Make the patched app debuggable {gray.italic (optional)}}
{dim {bold --apktool} Path to custom Apktool.jar {gray.italic (optional)}}
{dim {bold --skip-patches} Don't apply any patches {gray.italic (optional)}}
`)
}
function showSupportedExtensions(): never {
console.log(chalk`{yellow
It looks like you tried running {bold apk-mitm} with an unsupported file type!
Only the following file extensions are supported: {bold .apk}, {bold .xapk}, and {bold .apks} (or {bold .zip})
}`)
process.exit(1)
}
function showVersions({
apktool,
uberApkSigner,
}: {
apktool: Tool
uberApkSigner: Tool
}) {
console.log(chalk`
{dim ╭} {blue {bold apk-mitm} v${version}}
{dim ├ {bold apktool} ${apktool.version.name}
╰ {bold uber-apk-signer} ${uberApkSigner.version.name}}
`)
}
export function showArmWarning() {
console.log(chalk`{yellow
{inverse.bold NOTE }
{bold apk-mitm} doesn't officially support ARM-based devices (like Raspberry Pi's)
at the moment, so the error above might be a result of that. Please try
patching this APK on a device with a more common CPU architecture like x64
before reporting an issue.
}`)
}
main()