|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import axios, { AxiosResponse } from "axios"; |
| 4 | +import cliProgress from "cli-progress"; |
| 5 | +import crypto from "crypto"; |
| 6 | +import fs from "fs"; |
| 7 | +import { parse as xmlParse } from "fast-xml-parser"; |
| 8 | +import path from "path"; |
| 9 | +import unzip from "unzip-stream"; |
| 10 | +import yargs from "yargs"; |
| 11 | + |
| 12 | +import { handleAuthRotation } from "./utils/authUtils"; |
| 13 | +import { |
| 14 | + getBinaryInformMsg, |
| 15 | + getBinaryInitMsg, |
| 16 | + getDecryptionKey, |
| 17 | +} from "./utils/msgUtils"; |
| 18 | +import { version as packageVersion } from "./package.json"; |
| 19 | + |
| 20 | +const getLatestVersion = async ( |
| 21 | + region: string, |
| 22 | + model: string |
| 23 | +): Promise<{ pda: string; csc: string; modem: string }> => { |
| 24 | + return axios |
| 25 | + .get( |
| 26 | + `https://fota-cloud-dn.ospserver.net/firmware/${region}/${model}/version.xml` |
| 27 | + ) |
| 28 | + .then((res: AxiosResponse) => { |
| 29 | + const [pda, csc, modem] = xmlParse( |
| 30 | + res.data |
| 31 | + ).versioninfo.firmware.version.latest.split("/"); |
| 32 | + |
| 33 | + return { pda, csc, modem }; |
| 34 | + }); |
| 35 | +}; |
| 36 | + |
| 37 | +const main = async (region: string, model: string): Promise<void> => { |
| 38 | + console.log(` |
| 39 | + Model: ${model} |
| 40 | + Region: ${region}`); |
| 41 | + |
| 42 | + const { pda, csc, modem } = await getLatestVersion(region, model); |
| 43 | + |
| 44 | + console.log(` |
| 45 | + Latest version: |
| 46 | + PDA: ${pda} |
| 47 | + CSC: ${csc} |
| 48 | + MODEM: ${modem}`); |
| 49 | + |
| 50 | + const nonce = { |
| 51 | + encrypted: "", |
| 52 | + decrypted: "", |
| 53 | + }; |
| 54 | + |
| 55 | + const headers: Record<string, string> = { |
| 56 | + "User-Agent": "Kies2.0_FUS", |
| 57 | + }; |
| 58 | + |
| 59 | + const handleHeaders = (responseHeaders: any) => { |
| 60 | + if (responseHeaders.nonce != null) { |
| 61 | + const { Authorization, nonce: newNonce } = handleAuthRotation( |
| 62 | + responseHeaders |
| 63 | + ); |
| 64 | + |
| 65 | + Object.assign(nonce, newNonce); |
| 66 | + headers.Authorization = Authorization; |
| 67 | + } |
| 68 | + |
| 69 | + const sessionID = responseHeaders["set-cookie"] |
| 70 | + ?.find((cookie: string) => cookie.startsWith("JSESSIONID")) |
| 71 | + ?.split(";")[0]; |
| 72 | + |
| 73 | + if (sessionID != null) { |
| 74 | + headers.Cookie = sessionID; |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + await axios |
| 79 | + .post("https://neofussvr.sslcs.cdngc.net/NF_DownloadGenerateNonce.do", "", { |
| 80 | + headers: { |
| 81 | + Authorization: |
| 82 | + 'FUS nonce="", signature="", nc="", type="", realm="", newauth="1"', |
| 83 | + "User-Agent": "Kies2.0_FUS", |
| 84 | + Accept: "application/xml", |
| 85 | + }, |
| 86 | + }) |
| 87 | + .then((res) => { |
| 88 | + handleHeaders(res.headers); |
| 89 | + return res; |
| 90 | + }); |
| 91 | + |
| 92 | + const { |
| 93 | + binaryByteSize, |
| 94 | + binaryDescription, |
| 95 | + binaryFilename, |
| 96 | + binaryLogicValue, |
| 97 | + binaryModelPath, |
| 98 | + binaryOSVersion, |
| 99 | + binaryVersion, |
| 100 | + } = await axios |
| 101 | + .post( |
| 102 | + "https://neofussvr.sslcs.cdngc.net/NF_DownloadBinaryInform.do", |
| 103 | + getBinaryInformMsg( |
| 104 | + `${pda}/${csc}/${modem}/${pda}`, |
| 105 | + region, |
| 106 | + model, |
| 107 | + nonce.decrypted |
| 108 | + ), |
| 109 | + { |
| 110 | + headers: { |
| 111 | + ...headers, |
| 112 | + Accept: "application/xml", |
| 113 | + "Content-Type": "application/xml", |
| 114 | + }, |
| 115 | + } |
| 116 | + ) |
| 117 | + .then((res) => { |
| 118 | + handleHeaders(res.headers); |
| 119 | + return res; |
| 120 | + }) |
| 121 | + .then((res: AxiosResponse) => { |
| 122 | + const parsedInfo = xmlParse(res.data); |
| 123 | + |
| 124 | + return { |
| 125 | + binaryByteSize: parsedInfo.FUSMsg.FUSBody.Put.BINARY_BYTE_SIZE.Data, |
| 126 | + binaryDescription: parsedInfo.FUSMsg.FUSBody.Put.DESCRIPTION.Data, |
| 127 | + binaryFilename: parsedInfo.FUSMsg.FUSBody.Put.BINARY_NAME.Data, |
| 128 | + binaryLogicValue: |
| 129 | + parsedInfo.FUSMsg.FUSBody.Put.LOGIC_VALUE_FACTORY.Data, |
| 130 | + binaryModelPath: parsedInfo.FUSMsg.FUSBody.Put.MODEL_PATH.Data, |
| 131 | + binaryOSVersion: parsedInfo.FUSMsg.FUSBody.Put.CURRENT_OS_VERSION.Data, |
| 132 | + binaryVersion: parsedInfo.FUSMsg.FUSBody.Results.LATEST_FW_VERSION.Data, |
| 133 | + }; |
| 134 | + }); |
| 135 | + |
| 136 | + console.log(` |
| 137 | + OS: ${binaryOSVersion} |
| 138 | + Filename: ${binaryFilename} |
| 139 | + Size: ${binaryByteSize} bytes |
| 140 | + Logic Value: ${binaryLogicValue} |
| 141 | + Description: |
| 142 | + ${binaryDescription.split("\n").join("\n ")}`); |
| 143 | + |
| 144 | + const decryptionKey = getDecryptionKey(binaryVersion, binaryLogicValue); |
| 145 | + |
| 146 | + await axios |
| 147 | + .post( |
| 148 | + "https://neofussvr.sslcs.cdngc.net/NF_DownloadBinaryInitForMass.do", |
| 149 | + getBinaryInitMsg(binaryFilename, nonce.decrypted), |
| 150 | + { |
| 151 | + headers: { |
| 152 | + ...headers, |
| 153 | + Accept: "application/xml", |
| 154 | + "Content-Type": "application/xml", |
| 155 | + }, |
| 156 | + } |
| 157 | + ) |
| 158 | + .then((res) => { |
| 159 | + handleHeaders(res.headers); |
| 160 | + return res; |
| 161 | + }); |
| 162 | + |
| 163 | + const binaryDecipher = crypto.createDecipheriv( |
| 164 | + "aes-128-ecb", |
| 165 | + decryptionKey, |
| 166 | + null |
| 167 | + ); |
| 168 | + |
| 169 | + await axios |
| 170 | + .get("http://cloud-neofussvr.sslcs.cdngc.net/NF_DownloadBinaryForMass.do", { |
| 171 | + headers, |
| 172 | + params: { |
| 173 | + file: `${binaryModelPath}${binaryFilename}`, |
| 174 | + }, |
| 175 | + responseType: "stream", |
| 176 | + }) |
| 177 | + .then((res: AxiosResponse) => { |
| 178 | + const outputFolder = `${process.cwd()}/${model}_${region}/`; |
| 179 | + console.log(); |
| 180 | + console.log(outputFolder); |
| 181 | + fs.mkdirSync(outputFolder, { recursive: true }); |
| 182 | + |
| 183 | + let downloadedSize = 0; |
| 184 | + let currentFile = ""; |
| 185 | + const progressBar = new cliProgress.SingleBar({ |
| 186 | + format: "{bar} {percentage}% | {value}/{total} | {file}", |
| 187 | + barCompleteChar: "\u2588", |
| 188 | + barIncompleteChar: "\u2591", |
| 189 | + }); |
| 190 | + progressBar.start(binaryByteSize, downloadedSize); |
| 191 | + |
| 192 | + return res.data |
| 193 | + .on("data", (buffer: Buffer) => { |
| 194 | + downloadedSize += buffer.length; |
| 195 | + progressBar.update(downloadedSize, { file: currentFile }); |
| 196 | + }) |
| 197 | + .pipe(binaryDecipher) |
| 198 | + .pipe(unzip.Parse()) |
| 199 | + .on("entry", (entry) => { |
| 200 | + currentFile = `${entry.path.slice(0, 18)}...`; |
| 201 | + progressBar.update(downloadedSize, { file: currentFile }); |
| 202 | + entry |
| 203 | + .pipe(fs.createWriteStream(path.join(outputFolder, entry.path))) |
| 204 | + .on("finish", () => { |
| 205 | + if (downloadedSize === binaryByteSize) { |
| 206 | + process.exit(); |
| 207 | + } |
| 208 | + }); |
| 209 | + }); |
| 210 | + }); |
| 211 | +}; |
| 212 | + |
| 213 | +const { argv } = yargs |
| 214 | + .option("model", { |
| 215 | + alias: "m", |
| 216 | + describe: "Model", |
| 217 | + type: "string", |
| 218 | + demandOption: true, |
| 219 | + }) |
| 220 | + .option("region", { |
| 221 | + alias: "r", |
| 222 | + describe: "Region", |
| 223 | + type: "string", |
| 224 | + demandOption: true, |
| 225 | + }) |
| 226 | + .version(packageVersion) |
| 227 | + .alias("v", "version") |
| 228 | + .help(); |
| 229 | + |
| 230 | +main(argv.region, argv.model); |
| 231 | + |
| 232 | +export {}; |
0 commit comments