Absolucy / asar-rs

Asar archive parsing in Rust
Other
23 stars 5 forks source link

How to unpack asar archive? #1

Closed MidKnightXI closed 1 year ago

MidKnightXI commented 2 years ago

Context

I'm looking to unpack a .asar archive to rewrite some files in it.

I already did this in JS with the asar npm package from electron and I'm trying to rewrite this code in Rust due to "performance issues" and that I'm not satisfied with my own code.

JS Code with asar lib

Just to give you an exemple of what I'm trying to reproduce

import { extractAll, createPackageWithOptions } from "asar";
import { readdirSync, readFileSync, writeFileSync, existsSync } from "fs";
import { sync } from "rimraf";
import { dirname, normalize } from "path";
import { spawn } from "child_process";

function replaceAdFileContent(path) {
  let content = readFileSync(path).toString();

  content = content.replaceAll(
    "https://dtapp-player.op.gg/adsense.txt",
    "https://gist.githubusercontent.com/MidKnightXI/7ecf3cdd0a5804466cb790855e2524ae/raw/9b88cf64f3bb955edfff27bdfba72f5181d8748b/remover.txt"
  );
  content = content.replace(
    /exports\.countryHasAds=\w;/gm,
    "exports.countryHasAds=[];"
  );
  content = content.replace(
    /exports\.countryHasAdsAdsense=\w;/gm,
    "exports.countryHasAdsAdsense=[];"
  );
  content = content.replace(
    /exports\.adsenseAds=\w;/gm,
    "exports.adsenseAds=[];"
  );
  content = content.replace(
    /exports\.playwireAds=\w;/gm,
    "exports.playwireAds=[];"
  );
  content = content.replace(
    /exports\.nitropayAds=\w;/gm,
    "exports.nitropayAds=[];"
  );
  writeFileSync(path, content);
}

async function rebuildAddDir(asarFilePath) {
  console.log("Unpacking OPGG asar file");
  extractAll(asarFilePath, "op-gg-unpacked");

  const assetDir = normalize("op-gg-unpacked/assets/react");
  const assetFiles = readdirSync(assetDir);

  for (let fileName of assetFiles) {
    if (fileName.endsWith(".js")) {
      console.log(`Patching: ${fileName}`);
      replaceAdFileContent(normalize(`${assetDir}/${fileName}`));
    }
  }

  console.log(`Rebuilding ${asarFilePath} without ads urls`);
  await createPackageWithOptions("op-gg-unpacked", asarFilePath, {
    unpackDir: "{node_modules/node-ovhook,node_modules/rust-process}",
  });

  console.log(`Deleted temporary directory`);
  sync("op-gg-unpacked");
}

I tried to open a .asar with your crate (with the exemple provided and the path to my archive but it ends up not listing its content

MidKnightXI commented 2 years ago

Btw I used the function you provided like that

fn rebuild_ad_dir(path: &str) -> Result<()>
{
    let asar_file = std::fs::read(path)?;
    let asar = AsarReader::new(&asar_file)?;

    println!("There are {} files in archive.asar", asar.files().len());
    for content in asar.files().keys() {
        println!("{}", content.display());
    }
    Ok(())
}

pub fn remove_ads() -> bool
{
    let asar: String = format_asar_path();
    let asar_path = std::path::Path::new(&asar);

    if asar_path.exists()
    {
        kill_opgg();
        // unpack_asar(asar_path.to_str().unwrap());
        let test = rebuild_ad_dir(asar_path.to_str().unwrap());
        match test {
            Ok(v) => println!("working with version: {v:?}"),
            Err(e) => println!("error parsing header: {e:?}"),
        }
        return true;
    }
    return false;
}
Absolucy commented 2 years ago

Sorry, didn't notice my GitHub notifications until now!

Btw I used the function you provided like that

What does this program output when running rebuild_ad_dir?

MidKnightXI commented 2 years ago

Sorry I didn't work on my stuff lastly but I just checked and it appears that I'm getting this error:

error parsing header: Json(Error("data did not match any variant of untagged enum Header", line: 0, column: 0))
Absolucy commented 2 years ago

What does app.asar look like? Can you upload the asar somewhere (use catbox.moe if you don't already use another file uploading site) so I can take a look at it?

MidKnightXI commented 1 year ago

Here is a new link to the archive

Absolucy commented 1 year ago

Sorry for taking an eternity and a half, I've figured out the issue.

image

Some objects seem to have an unpacked value. I'm not entirely sure what this does?

Absolucy commented 1 year ago

Should be fixed with https://github.com/Absolucy/asar-rs/commit/97db6737d4d855a087c1882d234c6569014c70f5

MidKnightXI commented 1 year ago

Should be fixed with 97db673

Would it be possible for you to create a new release on crates.io so everyone can use it properly please?

Absolucy commented 1 year ago

Sure.

Absolucy commented 1 year ago

0.2.0 published on crates.io.