UniAlternative / activation-script

A script designed to generate configuration files and activate software for the Surge
GNU Affero General Public License v3.0
173 stars 28 forks source link

[Application] Typora #10

Closed wibus-wee closed 9 months ago

wibus-wee commented 9 months ago
// core file
// =====
// 验证许可证合法性
quickValidate = (e) => {
    const n = "L23456789ABCDEFGHJKMNPQRSTUVWXYZ";
    if (!/^([A-Z0-9]{6}-){3}[A-Z0-9]{6}$/.exec(e)) return !1; // 格式验证(6-6-6-6)
    var t = e.replace(/-/g, ""),
      i = t.substr(22);
    return (
      !t.replace(/[L23456789ABCDEFGHJKMNPQRSTUVWXYZ]/g, "") &&
      i ==
        ((e) => {
          for (var t = "", i = 0; i < 2; i++) {
            for (var a = 0, s = 0; s < 16; s += 2) a += n.indexOf(e[i + s]);
            t += n[(a %= n.length)];
          }
          return t;
        })(t)
    );
  },

getComputerName = async function () {
    var e = process.env.USER;
    switch ((e || (e = require("os").userInfo().username), process.platform)) {
      case "win32":
        return process.env.COMPUTERNAME + " | " + e + " | Windows";
      case "darwin":
        return new Promise((n) => {
          require("child_process").exec(
            "scutil --get ComputerName",
            { timeout: 5e3 },
            (t, i) => {
              n(
                !t && i
                  ? i.toString().trim() + " | " + e + " | darwin"
                  : require("os").hostname() + " | " + e + " | darwin"
              );
            }
          );
        });
      default:
        return require("os").hostname() + " | " + e + " | Linux";
    }
  },

doActivation = async function (e, n, t) {
    // 执行邮箱地址的格式验证 Start
    if (
      ((e = (e || "")
        .replace(/^[\s\u200b ]/g, "")
        .replace(/[\s\u200b ]$/g, "")),
      (n = (n || "").replace(/^[\s\u200b ]/g, "").replace(/[\s\u200b ]$/g, "")),
      !(function (e) {
        return /^[^\s@'"/\\=?]+@[^\s@'"/\\]+\.[^\s@'"/\\]+$/.test(e);
      })(e))
    )
      return [!1, "Please input a valid email address"];
    // 执行邮箱地址的格式验证 End

    // 执行许可证的合法性验证 Start
    if (!quickValidate(n)) return [!1, "Please input a valid license code"];
    // 执行许可证的合法性验证 End

    // 构造参数 Start
    const i = {
      v: getOS() + "|" + app.getVersion(),
      license: n,
      email: e,
      l: await getComputerName(), // scutil --get ComputerName
      f: await getFingerPrint(),
      u: app.setting.generateUUID(),
      type: global.devVersion ? "dev" : "",
      force: t,
    };
    JSON.stringify(i);
    // 构造参数 End
    try {
      const e = {
        data: {
          code: 0,
          msg: Buffer.from(
            JSON.stringify({
              deviceId: i.u, // generateUUID
              fingerprint: i.f, // getFingerPrint
              email: i.email,
              license: i.license, // quickValidate
              version: i.v, // getOS + app.getVersion
              date: new Date().toLocaleDateString("en-US"),
            }),
            "utf-8"
          ).toString("base64"),
        },
      };
// others
const ActiveResponseCode = {
    SUCCESS: 0,
    OUT_OF_LIMIT: 1,
    INVALIDATE: -1,
    WRONG_USER: -2,
  },
// utils
const getFingerPrint = async () => {
  if (!fingerPrint) {
    if (isWin) {
      const e = mustRequire("native-reg"),
        n = e.openKey(
          e.HKEY.LOCAL_MACHINE,
          "SOFTWARE\\Microsoft\\Cryptography",
          e.Access.WOW64_64KEY | e.Access.READ
        );
      (fingerPrint = e.getValue(n, null, "MachineGuid")), e.closeKey(n);
    } else
      fingerPrint = await require("node-machine-id").machineId({
        original: !0,
      });
    fingerPrint || Raven.captureMessage("[License] Failed to get fingerPrint"),
      (fingerPrint = makeHash(fingerPrint, "typora")
        .substr(0, 10)
        .replace(/[/=+-]/g, "a")),
      isMac && (fingerPrint += "darwin");
  }
  return fingerPrint;
};

// makeHash
makeHash = function () {
    var e = Array.from(arguments);
    const n = require("crypto").createHash("sha256");
    return (
      e.forEach((e) => {
        n.update(e);
      }),
      n.digest("base64")
    );
  },
wibus-wee commented 9 months ago

failed.

Public key verification, private key cannot be obtained.