Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
215 lines (192 loc) · 7.58 KB
/
Copy pathindex.js
File metadata and controls
215 lines (192 loc) · 7.58 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
212
213
214
215
// @ts-check
import { spawn } from 'child_process'
import asar from '@electron/asar'
import prompts from 'prompts'
import yargs from 'yargs'
import chalk from 'chalk'
import path from 'path'
import fs from 'fs'
import os from 'os'
const argv = await yargs(process.argv.slice(2))
.usage(`Usage: ${process.argv0} . <command> [options]`)
.command('patch', 'Patch HTTP Toolkit using the specified script')
.command('restore', 'Restore HTTP Toolkit files to their original state')
.command('start', 'Start HTTP Toolkit')
.demandCommand(1, 'You need at least one command before moving on')
.alias('h', 'help')
.parse()
const isWin = process.platform === 'win32'
const isMac = process.platform === 'darwin'
const appPath =
isWin ? path.join(process.env.LOCALAPPDATA ?? '', 'Programs', 'httptoolkit', 'resources')
: isMac ? '/Applications/HTTP Toolkit.app/Contents/Resources'
: fs.existsSync('/opt/HTTP Toolkit/resources') ? '/opt/HTTP Toolkit/resources'
: '/opt/httptoolkit/resources'
const isSudo = !isWin && (process.getuid || (() => process.env.SUDO_UID ? 0 : null))() === 0
if (+(process.versions.node.split('.')[0]) < 15) {
console.error(chalk.redBright`[!] Node.js version 15 or higher is recommended, you are using version {bold ${process.versions.node}}`)
}
if (!fs.existsSync(path.join(appPath, 'app.asar'))) {
console.error(chalk.redBright`[-] HTTP Toolkit not found`)
process.exit(1)
}
console.log(chalk.blueBright`[+] HTTP Toolkit found at {bold ${path.dirname(appPath)}}`)
const rm = dirPath => {
if (!fs.existsSync(dirPath)) return
if (!fs.lstatSync(dirPath).isDirectory()) return fs.rmSync(dirPath, { force: true })
for (const entry of fs.readdirSync(dirPath)) {
const entryPath = path.join(dirPath, entry)
if (fs.lstatSync(entryPath).isDirectory()) rm(entryPath)
else fs.rmSync(entryPath, { force: true })
}
}
/** @type {Array<import('child_process').ChildProcess>} */
const activeProcesses = []
let isCancelled = false
const cleanUp = async () => {
isCancelled = true
console.log(chalk.redBright`[-] Operation cancelled, cleaning up...`)
if (activeProcesses.length) {
console.log(chalk.yellowBright`[+] Killing active processes...`)
for (const proc of activeProcesses) {
proc.kill('SIGINT')
console.log(chalk.yellowBright`[+] Process {bold ${proc.pid ? process.pid + ' ' : ''}}killed`)
}
await new Promise(resolve => setTimeout(resolve, 1000))
}
const paths = [
path.resolve(os.tmpdir(), 'httptoolkit-patch'),
path.resolve(appPath, 'app')
]
try {
for (const p of paths) {
if (fs.existsSync(p)) {
console.log(chalk.yellowBright`[+] Removing {bold ${p}}`)
rm(p)
}
}
} catch (e) {
console.error(chalk.redBright`[-] An error occurred while cleaning up`, e)
}
process.exit(1)
}
const patchApp = async () => {
const filePath = path.join(appPath, 'app.asar')
const tempPath = path.join(appPath, 'app')
if (fs.readFileSync(filePath).includes('Injected by HTTP Toolkit Patcher')) {
console.log(chalk.greenBright`[+] App already patched`)
return
}
const globalProxy = process.env.PROXY
console.log(chalk.blueBright`[+] Started patching app...`)
if (globalProxy) {
if (!globalProxy.match(/^https?:/)) {
console.error(chalk.redBright`[-] Global proxy must start with http:// or https://`)
process.exit(1)
}
console.log(chalk.yellowBright`[+] Adding a custom proxy: {bold ${globalProxy}}`)
}
console.log(chalk.yellowBright`[+] Extracting app...`)
;['SIGINT', 'SIGTERM'].forEach(signal => process.on(signal, cleanUp))
try {
rm(tempPath)
asar.extractAll(filePath, tempPath)
} catch (e) {
if (!isSudo && e.errno === -13) { //? Permission denied
console.error(chalk.redBright`[-] Permission denied${!isWin ? ', try running with sudo' : ', try running node as administrator'}`)
process.exit(1)
}
console.error(chalk.redBright`[-] An error occurred while extracting app`, e)
process.exit(1)
}
const indexPath = path.join(tempPath, 'build', 'index.js')
if (!fs.existsSync(indexPath)) {
console.error(chalk.redBright`[-] Index file not found`)
cleanUp()
}
const data = fs.readFileSync(indexPath, 'utf-8')
;['SIGINT', 'SIGTERM'].forEach(signal => process.off(signal, cleanUp))
const { email } = await prompts({
type: 'text',
name: 'email',
message: 'Enter a email for the pro plan',
validate: value => value.includes('@') || 'Invalid email'
})
if (!email || typeof email !== 'string') {
console.error(chalk.redBright`[-] Email not provided`)
cleanUp()
}
;['SIGINT', 'SIGTERM'].forEach(signal => process.on(signal, cleanUp))
const patch = fs.readFileSync('patch.js', 'utf-8')
const patchedData = data
.replace('const APP_URL =', `// ------- Injected by HTTP Toolkit Patcher -------\nconst email = \`${email.replace(/`/g, '\\`')}\`\nconst globalProxy = process.env.PROXY ?? \`${globalProxy ? globalProxy.replace(/`/g, '\\`') : ''}\`\n${patch}\n// ------- End patched content -------\nconst APP_URL =`)
if (data === patchedData || !patchedData) {
console.error(chalk.redBright`[-] Patch failed`)
cleanUp()
}
fs.writeFileSync(indexPath, patchedData, 'utf-8')
console.log(chalk.greenBright`[+] Patched index.js`)
console.log(chalk.yellowBright`[+] Installing dependencies...`)
try {
const proc = spawn('npm install express', { cwd: tempPath, stdio: 'inherit', shell: true })
activeProcesses.push(proc)
await new Promise(resolve =>
proc.on('close', resolve)
)
activeProcesses.splice(activeProcesses.indexOf(proc), 1)
if (isCancelled) return
} catch (e) {
console.error(chalk.redBright`[-] An error occurred while installing dependencies`, e)
cleanUp()
}
rm(path.join(tempPath, 'package-lock.json'))
fs.copyFileSync(filePath, `${filePath}.bak`)
console.log(chalk.greenBright`[+] Backup created at {bold ${filePath}.bak}`)
console.log(chalk.yellowBright`[+] Building app...`)
await asar.createPackage(tempPath, filePath)
rm(tempPath)
console.log(chalk.greenBright`[+] App patched`)
}
switch (argv._[0]) {
case 'patch':
await patchApp()
break
case 'restore':
try {
console.log(chalk.blueBright`[+] Restoring app...`)
if (!fs.existsSync(path.join(appPath, 'app.asar.bak')))
console.error(chalk.redBright`[-] App not patched or restore file not found`)
else {
fs.copyFileSync(path.join(appPath, 'app.asar.bak'), path.join(appPath, 'app.asar'))
console.log(chalk.greenBright`[+] App restored`)
}
rm(path.join(os.tmpdir(), 'httptoolkit-patch'))
} catch (e) {
if (!isSudo && e.errno === -13) { //? Permission denied
console.error(chalk.redBright`[-] Permission denied${!isWin ? ', try running with sudo' : ', try running node as administrator'}`)
process.exit(1)
}
console.error(chalk.redBright`[-] An error occurred`, e)
process.exit(1)
}
break
case 'start':
console.log(chalk.blueBright`[+] Starting HTTP Toolkit...`)
try {
const command =
isWin ? `"${path.resolve(appPath, '..', 'HTTP Toolkit.exe')}"`
: isMac ? 'open -a "HTTP Toolkit"'
: 'httptoolkit'
const proc = spawn(command, { stdio: 'inherit', shell: true })
proc.on('close', code => process.exit(code))
} catch (e) {
console.error(chalk.redBright`[-] An error occurred`, e)
if (isSudo) console.error(chalk.redBright`[-] Try running without sudo`)
process.exit(1)
}
break
default:
console.error(chalk.redBright`[-] Unknown command`)
process.exit(1)
}
if (!isCancelled) console.log(chalk.greenBright`[+] Done`)