|
| 1 | +const request = require('request') |
| 2 | +const os = require('os') |
| 3 | +const path = require('path') |
| 4 | +const fs = require('fs') |
| 5 | +const { ensureDir, copy, readdir } = require('fs-extra') |
| 6 | +const { exec } = require('child_process') |
| 7 | +const unzipper = require('unzipper') |
| 8 | +const Socks5ClientHttpsAgent = require('socks5-https-client/lib/Agent') |
| 9 | +const tmpDir = path.join(os.tmpdir(), 'electron-ssr-deps-fetch') |
| 10 | +const copyDir = path.join(process.cwd(), 'src', 'lib') |
| 11 | +console.log(`tmpDir: ${tmpDir}`) |
| 12 | +console.log(`copyDir: ${copyDir}`) |
| 13 | +function fetchIndex (url) { |
| 14 | + return new Promise((resolve, reject) => { |
| 15 | + console.log(`Start Fetching: ${url}`) |
| 16 | + let option = { |
| 17 | + url: url, |
| 18 | + headers: { |
| 19 | + 'User-Agent': 'electron-ssr' |
| 20 | + } |
| 21 | + } |
| 22 | + withProxy(option) |
| 23 | + withGHToken(option) |
| 24 | + request(option, (err, resp, body) => { |
| 25 | + if (err) { |
| 26 | + reject(err) |
| 27 | + } else { |
| 28 | + console.log('Response Headers') |
| 29 | + console.log(resp.headers) |
| 30 | + try { |
| 31 | + resolve(JSON.parse(body)) |
| 32 | + } catch (error) { |
| 33 | + console.error(`Failed to parse: ${body} err: ${error}`) |
| 34 | + reject(error) |
| 35 | + } |
| 36 | + } |
| 37 | + }) |
| 38 | + }) |
| 39 | +} |
| 40 | +async function getSocks2http () { |
| 41 | + const releaseIndex = 'https://api.github.com/repos/xVanTuring/socks2http-rs/releases/latest' |
| 42 | + let index = await fetchIndex(releaseIndex) |
| 43 | + let assets = index['assets'] |
| 44 | + if (!(assets && assets.length && assets.length !== 0)) { |
| 45 | + console.error('assets is invalid') |
| 46 | + console.log(assets) |
| 47 | + console.log(index) |
| 48 | + index = await fetchIndex(releaseIndex) |
| 49 | + assets = index['assets'] |
| 50 | + } |
| 51 | + let withName = 'linux' |
| 52 | + switch (process.platform) { |
| 53 | + case 'win32': |
| 54 | + withName = 'windows' |
| 55 | + break |
| 56 | + case 'linux': |
| 57 | + break |
| 58 | + case 'darwin': |
| 59 | + withName = 'osx' |
| 60 | + break |
| 61 | + default: |
| 62 | + console.error('Unsupported system') |
| 63 | + return |
| 64 | + } |
| 65 | + let downloadPath = path.join(tmpDir, 'socks2http.zip') |
| 66 | + for (const asset of assets) { |
| 67 | + if (asset['name'].indexOf(withName) >= 0) { |
| 68 | + await downloadFile(asset['browser_download_url'], downloadPath) |
| 69 | + break |
| 70 | + } |
| 71 | + } |
| 72 | + await extractFile(downloadPath) |
| 73 | + let socks2httpName = 'socks2http' |
| 74 | + if (process.platform === 'win32') { |
| 75 | + socks2httpName += '.exe' |
| 76 | + } |
| 77 | + let copiedPath = path.join(copyDir, socks2httpName) |
| 78 | + await copy(path.join(tmpDir, socks2httpName), copiedPath) |
| 79 | + if (process.platform !== 'win32') { |
| 80 | + await new Promise((resolve, reject) => { |
| 81 | + console.log(`executing chmod +x ${copiedPath}`) |
| 82 | + exec(['chmod', '+x', copiedPath].join(' '), (err) => { |
| 83 | + if (err) { |
| 84 | + reject(err) |
| 85 | + } else { |
| 86 | + resolve() |
| 87 | + } |
| 88 | + }) |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | +async function getSysProxy () { |
| 93 | + const releaseIndex = 'https://api.github.com/repos/xVanTuring/sysproxy/releases/latest' |
| 94 | + let index = await fetchIndex(releaseIndex) |
| 95 | + const assets = index['assets'] |
| 96 | + let downloadPath = path.join(tmpDir, 'sysproxy.exe') |
| 97 | + for (const asset of assets) { |
| 98 | + if (asset['name'].indexOf('64') >= 0) { |
| 99 | + await downloadFile(asset['browser_download_url'], downloadPath) |
| 100 | + break |
| 101 | + } |
| 102 | + } |
| 103 | + let copiedPath = path.join(copyDir, 'sysproxy.exe') |
| 104 | + await copy(downloadPath, copiedPath) |
| 105 | +} |
| 106 | +async function getWindowsKill () { |
| 107 | + const releaseIndex = 'https://api.github.com/repos/alirdn/windows-kill/releases/latest' |
| 108 | + let index = await fetchIndex(releaseIndex) |
| 109 | + const assets = index['assets'] |
| 110 | + let downloadPath = path.join(tmpDir, 'windows-kill.zip') |
| 111 | + let folderName = '' |
| 112 | + for (const asset of assets) { |
| 113 | + if (asset['name'].startsWith('windows-kill_x64')) { |
| 114 | + folderName = asset['name'].replace('.zip', '') |
| 115 | + await downloadFile(asset['browser_download_url'], downloadPath) |
| 116 | + break |
| 117 | + } |
| 118 | + } |
| 119 | + await extractFile(downloadPath) |
| 120 | + await copy(path.join(tmpDir, folderName, 'windows-kill.exe'), path.join(copyDir, 'windows-kill.exe')) |
| 121 | +} |
| 122 | +function downloadFile (url, path) { |
| 123 | + return new Promise((resolve, reject) => { |
| 124 | + console.log(`Start Downloading: ${url}`) |
| 125 | + const file = fs.createWriteStream(path) |
| 126 | + let option = { |
| 127 | + url: url, |
| 128 | + header: { |
| 129 | + 'User-Agent': 'request' |
| 130 | + } |
| 131 | + } |
| 132 | + withProxy(option) |
| 133 | + withGHToken(option) |
| 134 | + request(option).on('error', reject).pipe(file) |
| 135 | + file.on('close', () => { |
| 136 | + console.log('WRITE DONE!') |
| 137 | + resolve() |
| 138 | + }) |
| 139 | + }) |
| 140 | +} |
| 141 | +async function getLibsodium () { |
| 142 | + const releaseIndex = 'https://api.github.com/repos/xVanTuring/libsodium-msvc-release/releases/latest' |
| 143 | + let index = await fetchIndex(releaseIndex) |
| 144 | + const assets = index['assets'] |
| 145 | + const libsodiumName = 'libsodium.dll' |
| 146 | + let downloadPath = path.join(tmpDir, libsodiumName) |
| 147 | + for (const asset of assets) { |
| 148 | + if (asset['name'] === 'libsodium.dll') { |
| 149 | + await downloadFile(asset['browser_download_url'], downloadPath) |
| 150 | + break |
| 151 | + } |
| 152 | + } |
| 153 | + let copiedPath = path.join(copyDir, libsodiumName) |
| 154 | + console.log(`Copy File to ${copiedPath}`) |
| 155 | + await copy(downloadPath, copiedPath) |
| 156 | +} |
| 157 | +function extractFile (zipPath) { |
| 158 | + return new Promise((resolve, reject) => { |
| 159 | + console.log(`Start Unzipping ${zipPath}`) |
| 160 | + fs.createReadStream(zipPath).pipe(unzipper.Extract({ path: tmpDir })) |
| 161 | + .on('finish', resolve) |
| 162 | + .on('error', reject) |
| 163 | + }) |
| 164 | +} |
| 165 | +let agent = null |
| 166 | +function withProxy (option) { |
| 167 | + let proxy = process.env.http_proxy || process.env.all_proxy || process.env.fetch_proxy |
| 168 | + if (proxy) { |
| 169 | + if (proxy.startsWith('socks5://')) { |
| 170 | + console.log(`Using Socks5 Proxy ${proxy}`) |
| 171 | + if (agent == null) { |
| 172 | + let url = new (require('url').URL)(proxy) |
| 173 | + agent = new Socks5ClientHttpsAgent({ |
| 174 | + socksHost: url.hostname, |
| 175 | + socksPort: parseInt(url.port, 10) |
| 176 | + }) |
| 177 | + } |
| 178 | + option['agent'] = agent |
| 179 | + return |
| 180 | + } |
| 181 | + console.log(`Using proxy: ${proxy}`) |
| 182 | + option['proxy'] = proxy |
| 183 | + } |
| 184 | +} |
| 185 | +function withGHToken (option) { |
| 186 | + if (process.env.GH_TOKEN) { |
| 187 | + console.log(`Find GH_TOKE ${process.env.GH_TOKEN.substr(0, 10)}...`) |
| 188 | + if (!option['headers']) { |
| 189 | + option['headers'] = {} |
| 190 | + } |
| 191 | + option['headers']['Authorization'] = `token ${process.env.GH_TOKEN}` |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +async function main () { |
| 196 | + await ensureDir(tmpDir) |
| 197 | + await getSocks2http() |
| 198 | + if (process.platform === 'win32') { |
| 199 | + await getSysProxy() |
| 200 | + await getLibsodium() |
| 201 | + await getWindowsKill() |
| 202 | + } |
| 203 | + await printLib() |
| 204 | +} |
| 205 | +async function printLib () { |
| 206 | + console.log('---LISTING LIB DIR---') |
| 207 | + let result = await readdir(copyDir) |
| 208 | + for (const fileName of result) { |
| 209 | + console.log(path.join(copyDir, fileName)) |
| 210 | + } |
| 211 | +} |
| 212 | +try { |
| 213 | + main() |
| 214 | +} catch (error) { |
| 215 | + console.error(error) |
| 216 | +} |
0 commit comments