user: rewrite this fully cjs: const axios = require("axios");

async function nextlinkvertise(url) {
  try {
    const response = await axios.get(
      `https://ethos.kys.gay/api/free/bypass?url=${url}`
    );
    const json = response.data;
    return json.status === "success" ? json.result : "failed";
  } catch (err) {
    console.error("err fetching next link:", err);
    return "failed";
  }
}

async function fetchkey(hwid, serviceid, tk = null) {
  try {
    let url = `https://gw.aisboost.com/a/${serviceid}?hwid=${hwid}`;
    if (tk) url += `&tk=${tk}`;

    const response = await axios.get(url);
    const keyptrn = /<span class="rounded bg-muted px-2 py-1">([\w-]+)<\/span>/;
    const match = response.data.match(keyptrn);

    if (match && match[1]) {
      return match[1];
    } else if (response.data.includes("Choose preferred method")) {
      return "Not Completed";
    } else {
      return "Failed";
    }
  } catch (err) {
    console.error("error fetching key:", err);
    return "Failed";
  }
}

async function postnextlink(hwid, serviceid, link) {
  try {
    const json = {
      t: {
        t: 9,
        i: 0,
        l: 3,
        a: [
          { t: 0, s: parseInt(serviceid) },
          { t: 1, s: hwid },
          { t: 0, s: 60 },
        ],
        o: 0,
      },
      f: 31,
      m: [],
    };
    const jsonstring = JSON.stringify(json);
    const jsonlength = Buffer.byteLength(jsonstring);
    const headers = {
      "Content-Type": "application/json",
      "Content-Length": jsonlength,
      "Sec-CH-UA":
        '"Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"',
      "X-Server-Instance": "server-fn:2",
      "Sec-CH-UA-Mobile": "?0",
      "User-Agent":
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
      "X-Server-ID": "c_jlshx9#$$function3",
      "X-Single-Flight": "true",
      "Sec-CH-UA-Platform": '"Linux"',
      Accept: "*/*",
      Origin: "https://gw.aisboost.com",
      "Sec-Fetch-Site": "same-origin",
      "Sec-Fetch-Mode": "cors",
      "Sec-Fetch-Dest": "empty",
      Referer: link,
      "Accept-Encoding": "gzip, deflate, br, zstd",
      "Accept-Language": "en-US,en;q=0.9",
      Priority: "u=1, i",
    };

    const response = await axios.post("https://gw.aisboost.com/_server", json, {
      headers,
    });
    return response.headers.location || null;
  } catch (err) {
    console.error("error posting to api:", err);
    return null;
  }
}

async function aisboost(link) {
  try {
    const url = new URL(link);
    const serviceid = url.pathname.split("/").pop();
    const hwid = url.searchParams.get("hwid");
    const tk = url.searchParams.get("tk");

    if (!hwid) {
      console.log("hwid not found.");
      return;
    }

    console.log(
      `hwid: ${hwid}, service id: ${serviceid}, token: ${tk || "None"}`
    );

    let attempt = 0;
    const maxattemps = 5;

    while (attempt < maxattemps) {
      attempt++;
      let response = await fetchkey(hwid, serviceid);
      if (response === "Not Completed") {
        const locationurl = await postnextlink(hwid, serviceid, link);
        if (locationurl && locationurl.startsWith("https://linkvertise.com")) {
          const nextlink = await nextlinkvertise(locationurl);
          if (nextlink === "failed") {
            console.log("failed to get next link from linkvertise link");
            return;
          }
          const nextlinparam = new URL(nextlink);
          const newtk = nextlinparam.searchParams.get("tk");

          if (newtk) {
            console.log(`token found: ${newtk}`);
            response = await fetchkey(hwid, serviceid, newtk);
          }
        }
      }

      if (response === "Failed") {
        console.log("failed to fetch key");
        return;
      } else if (response !== "Not Completed") {
        console.log(`Key: ${response}`);
        return;
      }

      console.log(`retrying... attempt: ${attempt}/${maxattemps}`);
    }

    if (attempt === maxattemps) {
      console.log("max retries.");
    }
  } catch (err) {
    console.error("caught an error:", err);
  }
}

aisboost("https://gw.aisboost.com/a/18?hwid=132213123213");
PROMPT: user: rewrite this fully cjs: const axios = require("axios");

async function nextlinkvertise(url) {
  try {
    const response = await axios.get(
      `https://ethos.kys.gay/api/free/bypass?url=${url}`
    );
    const json = response.data;
    return json.status === "success" ? json.result : "failed";
  } catch (err) {
    console.error("err fetching next link:", err);
    return "failed";
  }
}

async function fetchkey(hwid, serviceid, tk = null) {
  try {
    let url = `https://gw.aisboost.com/a/${serviceid}?hwid=${hwid}`;
    if (tk) url += `&tk=${tk}`;

    const response = await axios.get(url);
    const keyptrn = /<span class="rounded bg-muted px-2 py-1">([\w-]+)<\/span>/;
    const match = response.data.match(keyptrn);

    if (match && match[1]) {
      return match[1];
    } else if (response.data.includes("Choose preferred method")) {
      return "Not Completed";
    } else {
      return "Failed";
    }
  } catch (err) {
    console.error("error fetching key:", err);
    return "Failed";
  }
}

async function postnextlink(hwid, serviceid, link) {
  try {
    const json = {
      t: {
        t: 9,
        i: 0,
        l: 3,
        a: [
          { t: 0, s: parseInt(serviceid) },
          { t: 1, s: hwid },
          { t: 0, s: 60 },
        ],
        o: 0,
      },
      f: 31,
      m: [],
    };
    const jsonstring = JSON.stringify(json);
    const jsonlength = Buffer.byteLength(jsonstring);
    const headers = {
      "Content-Type": "application/json",
      "Content-Length": jsonlength,
      "Sec-CH-UA":
        '"Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"',
      "X-Server-Instance": "server-fn:2",
      "Sec-CH-UA-Mobile": "?0",
      "User-Agent":
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
      "X-Server-ID": "c_jlshx9#$$function3",
      "X-Single-Flight": "true",
      "Sec-CH-UA-Platform": '"Linux"',
      Accept: "*/*",
      Origin: "https://gw.aisboost.com",
      "Sec-Fetch-Site": "same-origin",
      "Sec-Fetch-Mode": "cors",
      "Sec-Fetch-Dest": "empty",
      Referer: link,
      "Accept-Encoding": "gzip, deflate, br, zstd",
      "Accept-Language": "en-US,en;q=0.9",
      Priority: "u=1, i",
    };

    const response = await axios.post("https://gw.aisboost.com/_server", json, {
      headers,
    });
    return response.headers.location || null;
  } catch (err) {
    console.error("error posting to api:", err);
    return null;
  }
}

async function aisboost(link) {
  try {
    const url = new URL(link);
    const serviceid = url.pathname.split("/").pop();
    const hwid = url.searchParams.get("hwid");
    const tk = url.searchParams.get("tk");

    if (!hwid) {
      console.log("hwid not found.");
      return;
    }

    console.log(
      `hwid: ${hwid}, service id: ${serviceid}, token: ${tk || "None"}`
    );

    let attempt = 0;
    const maxattemps = 5;

    while (attempt < maxattemps) {
      attempt++;
      let response = await fetchkey(hwid, serviceid);
      if (response === "Not Completed") {
        const locationurl = await postnextlink(hwid, serviceid, link);
        if (locationurl && locationurl.startsWith("https://linkvertise.com")) {
          const nextlink = await nextlinkvertise(locationurl);
          if (nextlink === "failed") {
            console.log("failed to get next link from linkvertise link");
            return;
          }
          const nextlinparam = new URL(nextlink);
          const newtk = nextlinparam.searchParams.get("tk");

          if (newtk) {
            console.log(`token found: ${newtk}`);
            response = await fetchkey(hwid, serviceid, newtk);
          }
        }
      }

      if (response === "Failed") {
        console.log("failed to fetch key");
        return;
      } else if (response !== "Not Completed") {
        console.log(`Key: ${response}`);
        return;
      }

      console.log(`retrying... attempt: ${attempt}/${maxattemps}`);
    }

    if (attempt === maxattemps) {
      console.log("max retries.");
    }
  } catch (err) {
    console.error("caught an error:", err);
  }
}

aisboost("https://gw.aisboost.com/a/18?hwid=132213123213");