|
|
@@ -1,4 +1,3 @@
|
|
|
-const request = require("request");
|
|
|
const CryptoJS = require("crypto-js");
|
|
|
const { deflate, unzip } = require("zlib");
|
|
|
const md5 = require("./md5.min.js");
|
|
|
@@ -6,6 +5,13 @@ const axios = require("axios");
|
|
|
const HtmlParser = require("node-html-parser");
|
|
|
const https = require("https");
|
|
|
const qs = require("qs");
|
|
|
+const fs = require("fs");
|
|
|
+const path = require("path");
|
|
|
+const { NodeSSH } = require("node-ssh");
|
|
|
+const { exec } = require("child_process");
|
|
|
+const { spawn } = require("child_process");
|
|
|
+const config = require("./config.json");
|
|
|
+const servers = require("./servers.json");
|
|
|
|
|
|
function encryptPasswd(sessionId, password) {
|
|
|
var key = CryptoJS.enc.Hex.parse(md5(sessionId));
|
|
|
@@ -42,9 +48,6 @@ function aesEncrypt(word, key, iv) {
|
|
|
}
|
|
|
|
|
|
let sessionId;
|
|
|
-let username = "swjmrhb_ggfw";
|
|
|
-let password = "EqRa6KSd2kF%C&mj";
|
|
|
-const loginUrl = "https://10.5.17.151/shterm/login";
|
|
|
const instance = axios.create({
|
|
|
httpsAgent: new https.Agent({
|
|
|
rejectUnauthorized: false,
|
|
|
@@ -55,7 +58,7 @@ const instance = axios.create({
|
|
|
instance.interceptors.request.use(
|
|
|
function (config) {
|
|
|
config.headers = config.headers || {};
|
|
|
- config.headers.accept = "text/html";
|
|
|
+ config.headers.accept = config.headers.accept || "text/html";
|
|
|
if (sessionId) {
|
|
|
config.headers["Cookie"] = "SESSION=" + sessionId;
|
|
|
}
|
|
|
@@ -65,46 +68,202 @@ instance.interceptors.request.use(
|
|
|
return Promise.reject(error);
|
|
|
}
|
|
|
);
|
|
|
-instance
|
|
|
- .get(loginUrl)
|
|
|
- .then((res) => {
|
|
|
- console.log(res.headers);
|
|
|
- sessionId = res.headers["set-cookie"][0].match(/SESSION=(.*?);/)[1];
|
|
|
- console.log("sessionId=" + sessionId);
|
|
|
- const root = HtmlParser.parse(res.data);
|
|
|
- const csrf = root
|
|
|
- .querySelector("#loginForm [name=_csrf]")
|
|
|
- .getAttribute("value");
|
|
|
- console.log("csrf=" + csrf);
|
|
|
- return instance.post(
|
|
|
- loginUrl,
|
|
|
- qs.stringify({
|
|
|
- language: "zh_CN",
|
|
|
- _csrf: csrf,
|
|
|
- username: username,
|
|
|
- password: encryptPasswd(sessionId, password),
|
|
|
- captcha: "",
|
|
|
- captchaPage: "",
|
|
|
- })
|
|
|
- );
|
|
|
+
|
|
|
+const getSessionId = () => {
|
|
|
+ return Promise.resolve()
|
|
|
+ .then(() => {
|
|
|
+ return instance.get(config.loginUrl);
|
|
|
+ })
|
|
|
+ .then((res) => {
|
|
|
+ sessionId = res.headers["set-cookie"][0].match(/SESSION=(.*?);/)[1];
|
|
|
+ const root = HtmlParser.parse(res.data);
|
|
|
+ const csrf = root
|
|
|
+ .querySelector("#loginForm [name=_csrf]")
|
|
|
+ .getAttribute("value");
|
|
|
+ return instance.post(
|
|
|
+ config.loginUrl,
|
|
|
+ qs.stringify({
|
|
|
+ language: "zh_CN",
|
|
|
+ _csrf: csrf,
|
|
|
+ username: config.username,
|
|
|
+ password: encryptPasswd(sessionId, config.password),
|
|
|
+ captcha: "",
|
|
|
+ captchaPage: "",
|
|
|
+ })
|
|
|
+ );
|
|
|
+ })
|
|
|
+ .then((res) => {
|
|
|
+ console.log(res.headers);
|
|
|
+ console.log(res.data);
|
|
|
+ sessionId = res.headers["set-cookie"][0].match(/SESSION=(.*?);/)[1];
|
|
|
+ })
|
|
|
+ .catch((e) => {
|
|
|
+ sessionId =
|
|
|
+ e.response.headers["set-cookie"][0].match(/SESSION=(.*?);/)[1];
|
|
|
+ fs.writeFileSync("sessionId", sessionId);
|
|
|
+ console.log("login sessionId=" + sessionId);
|
|
|
+ return Promise.resolve(sessionId);
|
|
|
+ });
|
|
|
+};
|
|
|
+if (fs.existsSync("sessionId")) {
|
|
|
+ sessionId = fs.readFileSync("sessionId").toString().trim();
|
|
|
+ console.log("using exist sessionId: " + sessionId);
|
|
|
+}
|
|
|
+const checkSessionId = () => {
|
|
|
+ if (!sessionId) {
|
|
|
+ return Promise.reject();
|
|
|
+ } else {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ instance
|
|
|
+ .get(config.checkAccessUrl, {
|
|
|
+ headers: {
|
|
|
+ accept: "application/json",
|
|
|
+ },
|
|
|
+ })
|
|
|
+ .then((res) => {
|
|
|
+ resolve();
|
|
|
+ })
|
|
|
+ .catch((e) => {
|
|
|
+ console.error("sessionId expired");
|
|
|
+ reject();
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const getAccess = (dev) => {
|
|
|
+ return instance
|
|
|
+ .post(
|
|
|
+ config.accessUrl,
|
|
|
+ {
|
|
|
+ misc: {
|
|
|
+ resolution: "1159x897:maximize",
|
|
|
+ isDualAuth: false,
|
|
|
+ anyAccount: "admin",
|
|
|
+ anyPassword: "yWfuA@4NTX!b#2kG",
|
|
|
+ recheckCode: "",
|
|
|
+ },
|
|
|
+ sessRemark: "",
|
|
|
+ account: "any",
|
|
|
+ proto: "sftp",
|
|
|
+ dev: dev,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ headers: {
|
|
|
+ accept: "application/json",
|
|
|
+ },
|
|
|
+ }
|
|
|
+ )
|
|
|
+ .then((res) => {
|
|
|
+ return Promise.resolve(res.data);
|
|
|
+ })
|
|
|
+ .catch((e) => {
|
|
|
+ console.log(e);
|
|
|
+ if (e.response.status === 401) {
|
|
|
+ sessionId = null;
|
|
|
+ }
|
|
|
+ return Promise.reject(e);
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+// const ls = spawn("sh", ["build.sh"], { cwd: config.projectDir });
|
|
|
+
|
|
|
+// ls.stdout.on("data", (data) => {
|
|
|
+// process.stdout.write(data);
|
|
|
+// });
|
|
|
+
|
|
|
+// ls.stderr.on("data", (data) => {
|
|
|
+// process.stderr.write(data);
|
|
|
+// });
|
|
|
+
|
|
|
+// ls.on("error", (error) => {
|
|
|
+// console.log(`error: ${error.message}`);
|
|
|
+// });
|
|
|
+
|
|
|
+// ls.on("close", (code) => {
|
|
|
+// console.log(`build finished with code ${code}`);
|
|
|
+
|
|
|
+// });
|
|
|
+new Promise((resolve, reject) => {
|
|
|
+ checkSessionId()
|
|
|
+ .then(() => {
|
|
|
+ resolve();
|
|
|
+ })
|
|
|
+ .catch(() => {
|
|
|
+ sessionId = null;
|
|
|
+ getSessionId().then(() => {
|
|
|
+ resolve();
|
|
|
+ });
|
|
|
+ });
|
|
|
+})
|
|
|
+ .then(() => {
|
|
|
+ return getAccess(6908);
|
|
|
})
|
|
|
.then((res) => {
|
|
|
- console.log(res.headers);
|
|
|
- console.log(res.data);
|
|
|
- sessionId = res.headers["set-cookie"][0].match(/SESSION=(.*?);/)[1];
|
|
|
- })
|
|
|
- .catch((e) => {
|
|
|
- console.log(e);
|
|
|
- });
|
|
|
+ console.log(res);
|
|
|
|
|
|
-// const buffer = Buffer.from(
|
|
|
-// "eJxNjcmKwkAQht+lz1qdjolKTooOeBEjjApeQgwVO0NnoavdRnx3K1HB27/VV3cRp06LSIieSJuGRV4Y/C+MSTkhx0Fa3SbbZUKXv9LqQ195EIBSISiPF2e0PAlgCIrdhjr7nibHY37htKTsM/MgBJ+juLYt2m/1ou7eqLZTI1BhSyLt0JbJ66672M1ZrX7jaFzg/kevz5Verrg4WcOFdq6hSMovShQEA/niSItUn2yGJLO6yuU0Y0kzU2DlAK8oHk8lo02q",
|
|
|
-// "base64"
|
|
|
-// );
|
|
|
-// unzip(buffer, (err, buffer) => {
|
|
|
-// if (err) {
|
|
|
-// console.error("An error occurred:", err);
|
|
|
-// process.exitCode = 1;
|
|
|
-// }
|
|
|
-// console.log(buffer.toString());
|
|
|
-// });
|
|
|
+ const buffer = Buffer.from(
|
|
|
+ res.url.match(/accessclient:\/\/(.*)/)[1],
|
|
|
+ "base64"
|
|
|
+ );
|
|
|
+ unzip(buffer, (err, buffer) => {
|
|
|
+ if (err) {
|
|
|
+ console.error("An error occurred:", err);
|
|
|
+ process.exitCode = 1;
|
|
|
+ }
|
|
|
+ let accessJson = JSON.parse(buffer.toString());
|
|
|
+ console.log(accessJson);
|
|
|
+
|
|
|
+ const ssh = new NodeSSH();
|
|
|
+ ssh.connect({
|
|
|
+ host: accessJson.Host,
|
|
|
+ username: accessJson.User,
|
|
|
+ password: accessJson.PWD,
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ ssh.connection.sftp((err, sftp) => {
|
|
|
+ if (err) throw err;
|
|
|
+ sftp.fastPut(
|
|
|
+ path.resolve(config.projectDir, "build.tar.gz"),
|
|
|
+ "/home/admin/build.tar.gz",
|
|
|
+ {
|
|
|
+ step: (total_transferred, chunk, total) => {
|
|
|
+ console.log(
|
|
|
+ "Uploaded",
|
|
|
+ total_transferred,
|
|
|
+ "of",
|
|
|
+ total
|
|
|
+ );
|
|
|
+ },
|
|
|
+ }
|
|
|
+ );
|
|
|
+ });
|
|
|
+ // ssh.putFile(
|
|
|
+ // path.resolve(config.projectDir, "build.tar.gz"),
|
|
|
+ // "/home/admin/build.tar.gz",
|
|
|
+ // undefined,
|
|
|
+ // {
|
|
|
+ // step: (total_transferred, chunk, total) => {
|
|
|
+ // console.log(
|
|
|
+ // "Uploaded",
|
|
|
+ // total_transferred,
|
|
|
+ // "of",
|
|
|
+ // total
|
|
|
+ // );
|
|
|
+ // },
|
|
|
+ // }
|
|
|
+ // ).then(
|
|
|
+ // function () {
|
|
|
+ // console.log("The File thing is done");
|
|
|
+ // },
|
|
|
+ // function (error) {
|
|
|
+ // console.log("Something's wrong");
|
|
|
+ // console.log(error);
|
|
|
+ // }
|
|
|
+ // );
|
|
|
+ })
|
|
|
+ .catch((e) => {
|
|
|
+ console.log(e);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|