-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathinstall.js
200 lines (166 loc) · 5.66 KB
/
install.js
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
const { execSync } = require('child_process');
const https = require('https');
const fs = require('fs');
const os = require('os');
const { createHash } = require('crypto');
const { gunzipSync } = require('zlib');
const packageJson = require('./package.json');
const outDir = `${__dirname}/dist`;
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir);
}
const { version } = packageJson;
const releasesAssetsUrl = `https://github.com/ulixee/secret-agent/releases/download/v${version}`;
// tslint:disable:no-console
const forceBuild = Boolean(JSON.parse(process.env.SA_REBUILD_MITM_SOCKET || 'false'));
(async function install() {
let programName = 'connect';
const filename = buildFilename();
if (os.platform() === 'win32') {
programName += '.exe';
}
const installed = getInstalledVersion();
if (!forceBuild && installed && installed.startsWith(version) && isBinaryInstalled(programName)) {
console.log('Latest SecretAgent connect library already installed');
process.exit(0);
}
const checksum = await getSourceChecksum(filename);
const filepath = `${releasesAssetsUrl}/${filename}`;
if (!checksum) {
if (tryBuild(programName)) {
saveVersion();
console.log('Successfully compiled Secret Agent connect library');
process.exit(0);
}
const goVersionNeeded = getGoVersionNeeded();
console.log(
`The architecture file you need for the Secret Agent connect library is not available (${filepath}).\n\n
You can install golang ${goVersionNeeded} (https://golang.org/) and run "go build" from the mitm-socket/go directory\n\n`,
);
process.exit(1);
}
console.log('Downloading Secret Agent connect library from %s (checksum=%s)', filepath, checksum);
const zippedFile = await download(filepath);
const downloadedChecksum = getFileChecksum(zippedFile);
if (downloadedChecksum !== checksum) {
console.log('WARN!! Checksum mismatch for the Secret Agent connect library', {
checksum,
downloadedChecksum,
});
process.exit(1);
}
const file = gunzipSync(zippedFile);
fs.writeFileSync(`${outDir}/${programName}`, file);
fs.chmodSync(`${outDir}/${programName}`, 0o755);
saveVersion();
console.log('Successfully downloaded');
process.exit(0);
})();
function tryBuild(programName) {
const goVersionNeeded = getGoVersionNeeded();
const isGoInstalled = isGoVersionInstalled(goVersionNeeded);
console.log('Is go installed? %s, %s', goVersionNeeded, isGoInstalled);
if (isGoInstalled) {
if (compile()) {
fs.renameSync(`${__dirname}/go/${programName}`, `${outDir}/${programName}`);
return true;
}
}
return false;
}
function getInstalledVersion() {
if (fs.existsSync(`${outDir}/version`)) {
return fs.readFileSync(`${outDir}/version`, 'utf8');
}
return null;
}
function isBinaryInstalled(programName) {
return fs.existsSync(`${outDir}/${programName}`);
}
function saveVersion() {
fs.writeFileSync(`${outDir}/version`, version);
}
function buildFilename() {
let platform = String(os.platform());
let arch = os.arch();
if (arch === 'x64') arch = 'x86_64';
if (arch === 'ia32') arch = 'i386';
if (platform === 'win32') {
platform = 'win';
}
if (platform === 'darwin') {
platform = 'mac';
}
return `connect_${version}_${platform}_${arch}.gz`;
}
function download(filepath) {
return new Promise((resolve, reject) => {
const req = https.get(filepath, async res => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
return download(res.headers.location).then(resolve).catch(reject);
}
try {
const buffer = [];
for await (const chunk of res) {
buffer.push(chunk);
}
const output = Buffer.concat(buffer);
resolve(output);
} catch (err) {
reject(err);
}
});
req.on('error', err => {
console.log('ERROR downloading needed Secret Agent library %s', filepath, err);
reject(err);
});
});
}
function getFileChecksum(file) {
return createHash('sha256').update(file).digest().toString('hex');
}
async function getSourceChecksum(filename) {
if (forceBuild) return null;
const buffer = await download(`${releasesAssetsUrl}/connect.checksum`);
const checksum = buffer.toString('utf8');
const match = checksum.split(/\r?\n/).find(x => x.endsWith(filename));
const expectedChecksum = match ? match.split(/\s+/).shift() : undefined;
if (!expectedChecksum) {
throw new Error('Invalid checksum found for Secret Agent MitmSocket library');
}
return expectedChecksum;
}
/////// /// GO BUILD ////////////////////////////////////////////////////////////////////////////////
function compile() {
try {
execSync('go build', { cwd: `${__dirname}/go` });
return true;
} catch (err) {
console.log(
'Error compiling Secret Agent MitmSocket library.\n\nWill download instead.',
err.message,
);
return false;
}
}
function getGoVersionNeeded() {
const goMod = fs.readFileSync(`${__dirname}/go/go.mod`, 'utf8');
const goMatch = goMod.match(/go ([\d.]+)/);
return goMatch[1];
}
function isGoVersionInstalled(wantedVersion) {
const goVersionNeeded = wantedVersion.split('.');
try {
const goVersionResult = execSync('go version', { encoding: 'utf8' });
const goVersion = goVersionResult.match(/go version go([\d.]+)\s\w+\/\w+/);
if (!goVersion || !goVersion.length) return false;
if (goVersion && goVersion.length) {
const versionParts = goVersion[1].split('.');
if (versionParts[0] !== goVersionNeeded[0]) return false;
if (parseInt(versionParts[1], 10) < parseInt(goVersionNeeded[1], 10)) return false;
return true;
}
} catch (err) {
return false;
}
}