<commit_before>import * as fs from "fs";
import * as npath from "path";
import PropTypes from "prop-types";
import React from "react";
import { connect } from "react-redux";
import { loadLicense } from "../../AC";
import { LICENSE_PATH, PLATFORM } from "../../constants";
import * as jwt from "../../trusted/jwt";
import { toBase64 } from "../../utils";
import HeaderWorkspaceBlock from "../HeaderWorkspaceBlock";

const dialog = window.electron.remote.dialog;

interface ILicenseSetupModalProps {
  status: number;
  loading: boolean;
  closeWindow: () => void;
  loadLicense: () => void;
}

interface ILicenseSetupModalState {
  license_file: string;
  license_key: string;
}

class LicenseSetupModal extends React.Component<ILicenseSetupModalProps, ILicenseSetupModalState> {
  static contextTypes = {
    locale: PropTypes.string,
    localize: PropTypes.func,
  };

  constructor(props: ILicenseSetupModalProps) {
    super(props);

    this.state = ({
      license_file: "",
      license_key: "",
    });
  }

  componentDidMount() {
    $("input#input_file, textarea#input_key").characterCounter();
  }

  paste() {
    $("#input_key").focus();
    document.execCommand("paste");
    $("#input_key").trigger("autoresize");
  }

  setupLicense() {
    const { localize, locale } = this.context;
    const { license_key, license_file } = this.state;
    // tslint:disable-next-line:no-shadowed-variable
    const { loadLicense } = this.props;

    const path = npath.dirname(LICENSE_PATH);
    const options = {
      name: "CryptoARM GOST",
    };

    let command = "";
    let status: any;

    if (PLATFORM !== "win32") {
      command = "sh -c " + "\"";
      command = command + "mkdir -p " + "'" + path + "'" + " && ";
    } else {
      if (!fs.existsSync(path)) {
        command = command + " mkdir " + '"' + path + '"' + " && ";
      }
    }

    let data = null;
    let key;
    let lic_format;

    if (license_key) {
      key = license_key;
    } else if (fs.existsSync(license_file)) {
      const licFileContent: string = fs.readFileSync(license_file, "utf8");

      if (licFileContent) {
        key = licFileContent.trim();
      } else {
        $(".toast-read_file_error").remove();
        Materialize.toast(localize("Common.read_file_error", locale), 2000, "toast-read_file_error");
      }
    } else {
      $(".toast-lic_file_not_found").remove();
      Materialize.toast(localize("License.lic_file_not_found", locale), 2000, "toast-lic_file_not_found");
    }

    if (!key) {
      $(".toast-lic_key_uncorrect").remove();
      Materialize.toast(localize("License.lic_key_uncorrect", locale), 2000, "toast-lic_key_uncorrect");
    } else {
      let parsedLicense;
      let buffer;
      const result = key.match(/[<KEY>A-<KEY>}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}/ig);

      if (result != null) {
        lic_format = "MTX";
        let productMarker = key.match(/^CAG/ig);
        if (productMarker == null) {
          status = 907;
          data = "-";
        } else {
          let expirationTime = trusted.utils.Jwt.getExpirationTime(key);
          let dateExp = new Date(expirationTime * 1000).getTime();
          let dateNow = new Date().getTime();
          let dateDif = dateExp - dateNow;
          if ((dateDif < 0) && (dateExp != 0)) { status = 908 } else status = 1;
          //validate
          data = {
            aud: "-",
            sub: "CryptoARM GOST",
            core: 65535,
            iss: 'ООО "Цифровые технологии"',
            exp: expirationTime,
            iat: "",
            jti: "",
            desc: "CryptoARM GOST",
          };
        }
      } else {
        const splitLicense = key.split(".");
        if (splitLicense[1]) {
          try {
            buffer = new Buffer(toBase64(splitLicense[1]), "base64").toString("utf8");
            parsedLicense = JSON.parse(buffer);

            if (parsedLicense.exp && parsedLicense.aud && parsedLicense.iat && parsedLicense.iss
              && parsedLicense.jti && parsedLicense.sub) {
              data = parsedLicense;
              lic_format = "JWT";
              if (data.sub !== "CryptoARM GOST") {
                status = 907;
                data = data.sub = "-";
              }
            }
          } catch (e) {
            data = null;
            lic_format = "NONE";
          }
        }
      }
    }

    if (data && data.sub !== "-") {
      if (status != 908) status = jwt.checkLicense(key);
      if (status === 0) {
        if (PLATFORM === "win32") {
          command = command + "echo " + key.trim() + " > " + '"' + LICENSE_PATH + '"';
        } else {
          command = command + " printf " + key.trim() + " > " + "'" + LICENSE_PATH + "'" + " && ";
          command = command + " chmod 777 " + "'" + LICENSE_PATH + "'" + "\"";
        }
        window.sudo.exec(command, options, function (error: any) {
          if (!error) {
            trusted.common.OpenSSL.stop();
            trusted.common.OpenSSL.run();

            loadLicense();

            $(".toast-lic_key_setup").remove();
            Materialize.toast(localize("License.lic_key_setup", locale), 2000, "toast-lic_key_setup");
          } else {
            $(".toast-write_file_error").remove();
            Materialize.toast(localize("Common.write_file_error", locale), 2000, "toast-write_file_error");
          }
        });
      } else {
        $(".toast-status.message").remove();
        Materialize.toast(localize(jwt.getErrorMessage(status), locale), 2000, "toast-status.message");
      }
    } else {
      $(".toast-lic_key_uncorrect").remove();
      Materialize.toast(localize("License.lic_key_uncorrect", locale), 2000, "toast-lic_key_uncorrect");<commit_msg>reload openssl config after install license<commit_after>import * as crypto_module from "crypto";
import * as fs from "fs";
import * as npath from "path";
import PropTypes from "prop-types";
import React from "react";
import { connect } from "react-redux";
import { loadLicense } from "../../AC";
import { DEFAULT_PATH, LICENSE_PATH, PLATFORM } from "../../constants";
import * as jwt from "../../trusted/jwt";
import { toBase64 } from "../../utils";
import HeaderWorkspaceBlock from "../HeaderWorkspaceBlock";
const dialog = window.electron.remote.dialog;

interface ILicenseSetupModalProps {
  status: number;
  loading: boolean;
  closeWindow: () => void;
  loadLicense: () => void;
}

interface ILicenseSetupModalState {
  license_file: string;
  license_key: string;
}

class LicenseSetupModal extends React.Component<ILicenseSetupModalProps, ILicenseSetupModalState> {
  static contextTypes = {
    locale: PropTypes.string,
    localize: PropTypes.func,
  };

  constructor(props: ILicenseSetupModalProps) {
    super(props);

    this.state = ({
      license_file: "",
      license_key: "",
    });
  }

  componentDidMount() {
    $("input#input_file, textarea#input_key").characterCounter();
  }

  paste() {
    $("#input_key").focus();
    document.execCommand("paste");
    $("#input_key").trigger("autoresize");
  }

  setupLicense() {
    const { localize, locale } = this.context;
    const { license_key, license_file } = this.state;
    // tslint:disable-next-line:no-shadowed-variable
    const { loadLicense } = this.props;

    const path = npath.dirname(LICENSE_PATH);
    const options = {
      name: "<NAME>",
    };

    let command = "";
    let status: any;

    if (PLATFORM !== "win32") {
      command = "sh -c " + "\"";
      command = command + "mkdir -p " + "'" + path + "'" + " && ";
    } else {
      if (!fs.existsSync(path)) {
        command = command + " mkdir " + '"' + path + '"' + " && ";
      }
    }

    let data = null;
    let key;
    let lic_format;

    if (license_key) {
      key = license_key;
    } else if (fs.existsSync(license_file)) {
      const licFileContent: string = fs.readFileSync(license_file, "utf8");

      if (licFileContent) {
        key = licFileContent.trim();
      } else {
        $(".toast-read_file_error").remove();
        Materialize.toast(localize("Common.read_file_error", locale), 2000, "toast-read_file_error");
      }
    } else {
      $(".toast-lic_file_not_found").remove();
      Materialize.toast(localize("License.lic_file_not_found", locale), 2000, "toast-lic_file_not_found");
    }

    if (!key) {
      $(".toast-lic_key_uncorrect").remove();
      Materialize.toast(localize("License.lic_key_uncorrect", locale), 2000, "toast-lic_key_uncorrect");
    } else {
      let parsedLicense;
      let buffer;
      const result = key.match(/[<KEY>A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}/ig);

      if (result != null) {
        lic_format = "MTX";
        let productMarker = key.match(/^CAG/ig);
        if (productMarker == null) {
          status = 907;
          data = "-";
        } else {
          let expirationTime = trusted.utils.Jwt.getExpirationTime(key);
          let dateExp = new Date(expirationTime * 1000).getTime();
          let dateNow = new Date().getTime();
          let dateDif = dateExp - dateNow;
          if ((dateDif < 0) && (dateExp != 0)) { status = 908 } else status = 1;
          //validate
          data = {
            aud: "-",
            sub: "CryptoARM GOST",
            core: 65535,
            iss: 'ООО "Цифровые технологии"',
            exp: expirationTime,
            iat: "",
            jti: "",
            desc: "CryptoARM GOST",
          };
        }
      } else {
        const splitLicense = key.split(".");
        if (splitLicense[1]) {
          try {
            buffer = new Buffer(toBase64(splitLicense[1]), "base64").toString("utf8");
            parsedLicense = JSON.parse(buffer);

            if (parsedLicense.exp && parsedLicense.aud && parsedLicense.iat && parsedLicense.iss
              && parsedLicense.jti && parsedLicense.sub) {
              data = parsedLicense;
              lic_format = "JWT";
              if (data.sub !== "CryptoARM GOST") {
                status = 907;
                data = data.sub = "-";
              }
            }
          } catch (e) {
            data = null;
            lic_format = "NONE";
          }
        }
      }
    }

    if (data && data.sub !== "-") {
      if (status != 908) status = jwt.checkLicense(key);
      if (status === 0) {
        if (PLATFORM === "win32") {
          command = command + "echo " + key.trim() + " > " + '"' + LICENSE_PATH + '"';
        } else {
          command = command + " printf " + key.trim() + " > " + "'" + LICENSE_PATH + "'" + " && ";
          command = command + " chmod 777 " + "'" + LICENSE_PATH + "'" + "\"";
        }
        window.sudo.exec(command, options, function (error: any) {
          if (!error) {
            trusted.common.OpenSSL.stop();
            trusted.common.OpenSSL.run();

            if (process.platform === "win32") {
              try {
                crypto_module.loadConfig(npath.join(DEFAULT_PATH, "./config/openssl.cfg"));
              } catch (e) {
                // error
              }
            }

            loadLicense();

            $(".toast-lic_key_setup").remove();
            Materialize.toast(localize("License.lic_key_setup", locale), 2000, "toast-lic_key_setup");
          } else {
            $(".toast-write_file_error").remove();
            Materialize.toast(localize("Common.write_file_error", locale), 2000, "toast-write_file_error");
          }
        });
      } else {
        $(".toast-status.message").remove();
        Materialize.toast(localize(jwt.getErrorMessage(status), locale), 2000, "toast-status.message");
      }
    } else {
      $(".toast-lic_key_uncorrect").remove();
      Materialize.toast(localize("License.lic_key_uncorrect", locale), 2000, "toast-lic_key_uncorrect");
{'fraction_non_alphanumeric': 0.1615143558905734, 'fraction_numerical': 0.014313542813585161, 'mean_word_length': 2.186184565569347, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2640458', 'n_tokens_mistral': 3814, 'n_tokens_neox': 3679, 'n_words': 702}