-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathinstallCloudflared.js
More file actions
191 lines (159 loc) · 5.31 KB
/
Copy pathinstallCloudflared.js
File metadata and controls
191 lines (159 loc) · 5.31 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
/**
* Script to install cloudflared binary on the current platform.
* Reference: https://github.com/JacobLinCool/node-cloudflared/blob/main/src/install.ts
*/
const { execSync } = require('child_process');
const fs = require('fs');
const os = require('os');
const https = require('https');
const path = require('path');
const RELEASE_BASE = 'https://github.com/cloudflare/cloudflared/releases/';
const CLOUDFLARED_VERSION = '2026.3.0';
const LINUX_URL = {
arm64: 'cloudflared-linux-arm64',
arm: 'cloudflared-linux-arm',
x64: 'cloudflared-linux-amd64',
ia32: 'cloudflared-linux-386',
};
const MACOS_URL = {
arm64: 'cloudflared-darwin-amd64.tgz',
x64: 'cloudflared-darwin-amd64.tgz',
};
const WINDOWS_URL = {
x64: 'cloudflared-windows-amd64.exe',
ia32: 'cloudflared-windows-386.exe',
};
function resolveBase(version) {
if (version === 'latest') {
return `${RELEASE_BASE}latest/download/`;
}
return `${RELEASE_BASE}download/${version}/`;
}
function getGitBookDataDirectory() {
if (process.platform === 'darwin') {
return path.join(os.homedir(), 'Library', 'Application Support', 'gitbook');
}
if (process.platform === 'win32') {
return path.join(
process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local'),
'gitbook',
);
}
return path.join(
process.env.XDG_DATA_HOME || path.join(os.homedir(), '.local', 'share'),
'gitbook',
);
}
function getCloudflaredBinaryPath() {
return path.join(
getGitBookDataDirectory(),
'bin',
process.platform === 'win32' ? 'cloudflared.exe' : 'cloudflared',
);
}
async function hasCloudflared(binaryPath) {
const mode = process.platform === 'win32' ? fs.constants.F_OK : fs.constants.X_OK;
try {
await fs.promises.access(binaryPath, mode);
return true;
} catch {
return false;
}
}
async function ensureCloudflaredInstalled(onInstall = () => {}, onComplete = () => {}) {
const binaryPath = getCloudflaredBinaryPath();
if (await hasCloudflared(binaryPath)) {
return binaryPath;
}
onInstall();
await installCloudflared(binaryPath, CLOUDFLARED_VERSION);
onComplete();
return binaryPath;
}
/**
* Install cloudflared to the given path.
* @param {string} to The path to the binary to install.
* @param {string} version The version of cloudflared to install.
* @returns {Promise<string>} The path to the binary that was installed.
*/
async function installCloudflared(to, version = 'latest') {
if (process.platform === 'linux') {
return installLinux(to, version);
} else if (process.platform === 'darwin') {
return installMacOS(to, version);
} else if (process.platform === 'win32') {
return installWindows(to, version);
} else {
console.error(`Unsupported platform: ${process.platform}`);
process.exit(1);
}
}
async function installLinux(to, version = 'latest') {
const file = LINUX_URL[process.arch];
if (file === undefined) {
console.error(`Unsupported architecture: ${process.arch}`);
process.exit(1);
}
await download(resolveBase(version) + file, to);
fs.chmodSync(to, '755');
return to;
}
async function installMacOS(to, version = 'latest') {
const file = MACOS_URL[process.arch];
if (file === undefined) {
console.error(`Unsupported architecture: ${process.arch}`);
process.exit(1);
}
await download(resolveBase(version) + file, `${to}.tgz`);
process.env.VERBOSE && console.log(`Extracting to ${to}`);
execSync(`tar -xzf ${path.basename(`${to}.tgz`)}`, { cwd: path.dirname(to) });
fs.unlinkSync(`${to}.tgz`);
fs.renameSync(`${path.dirname(to)}/cloudflared`, to);
return to;
}
async function installWindows(to, version = 'latest') {
const file = WINDOWS_URL[process.arch];
if (file === undefined) {
console.error(`Unsupported architecture: ${process.arch}`);
process.exit(1);
}
await download(resolveBase(version) + file, to);
return to;
}
function download(url, to, redirect = 0) {
if (redirect === 0) {
process.env.VERBOSE && console.log(`Downloading ${url} to ${to}`);
} else {
process.env.VERBOSE && console.log(`Redirecting to ${url}`);
}
return new Promise((resolve, reject) => {
if (!fs.existsSync(path.dirname(to))) {
fs.mkdirSync(path.dirname(to), { recursive: true });
}
let done = true;
const file = fs.createWriteStream(to);
const request = https.get(url, (res) => {
if (res.statusCode === 302 && res.headers.location !== undefined) {
const redirection = res.headers.location;
done = false;
request.destroy();
file.close(() => resolve(download(redirection, to, redirect + 1)));
return;
}
res.pipe(file);
});
file.on('finish', () => {
if (done) {
file.close(() => resolve(to));
}
});
request.on('error', (err) => {
fs.unlink(to, () => reject(err));
});
file.on('error', (err) => {
fs.unlink(to, () => reject(err));
});
request.end();
});
}
module.exports = ensureCloudflaredInstalled;