GithubHelp home page GithubHelp logo

asar-rs's Introduction

asar

This crate allows for the parsing, reading, and writing of asar archives, often seen in Electron-based applications.

Examples

Listing the contents of an asar archive

use asar::{AsarReader, Header, Result};
use std::fs;

fn main() -> Result<()> {
	let asar_file = fs::read("archive.asar")?;
	let asar = AsarReader::new(&asar_file)?;

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

Reading a file from an asar archive

use asar::{AsarReader, Header, Result};
use std::{fs, path::PathBuf};

fn main() -> Result<()> {
	let asar_file = fs::read("archive.asar")?;
	let asar = AsarReader::new(&asar_file)?;

	let path = PathBuf::from("hello.txt");
	let file = asar.files().get(&path).unwrap();
	let contents = std::str::from_utf8(file.data()).unwrap();
	assert_eq!(contents, "Hello, World!");
	Ok(())
}

Writing a file to an asar archive

use asar::{AsarWriter, Result};
use std::fs::File;

fn main() -> Result<()> {
	let mut asar = AsarWriter::new();
	asar.write_file("hello.txt", b"Hello, World!", false)?;
	asar.finalize(File::create("archive.asar")?)?;
	Ok(())
}

Features

  • integrity: Enable integrity checks/calculation.
  • check-integrity-on-read: Enable integrity checks when reading an archive, failing if any integrity check fails.
  • write - Enable writing an asar archive. Enabled by default, also enables integrity.

License

asar is licensed under either the MIT license or the Apache License 2.0, at the choice of the user.

License: Apache-2.0 OR MIT

Amendment

I, @Absolucy, fully give permission for any of my code (including the entirety of this project, asar-rs), anywhere, no matter the license, to be used to train machine learning models intended to be used for general-purpose programming or code analysis.

asar-rs's People

Contributors

absolucy avatar valentinegb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

asar-rs's Issues

tag releases on git

hey, first of all, thanks a lot for this project. releases are uploaded to crates.io, but none of them is tagged in git - could you tag them here as well?

data did not match any variant of untagged enum Header

Got a pretty simple project, but when I try to read app.asar I get this error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Json(Error("data did not match any variant of untagged enum Header", line: 0, column: 0))', src/main.rs:17:54
stack backtrace:
   0:        0x100f38448 - std::backtrace_rs::backtrace::libunwind::trace::hbe97f4303076f99a
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:        0x100f38448 - std::backtrace_rs::backtrace::trace_unsynchronized::hb462a91ac605b16b
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:        0x100f38448 - std::sys_common::backtrace::_print_fmt::h555c176cf2344151
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/sys_common/backtrace.rs:65:5
   3:        0x100f38448 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::he84d5e26d26c914d
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/sys_common/backtrace.rs:44:22
   4:        0x100f4df5c - core::fmt::write::h07c7e4739f78f3b0
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/fmt/mod.rs:1209:17
   5:        0x100f35974 - std::io::Write::write_fmt::h4a3c5f094ffc65e8
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/io/mod.rs:1682:15
   6:        0x100f3825c - std::sys_common::backtrace::_print::h03a1c9ac6001091a
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/sys_common/backtrace.rs:47:5
   7:        0x100f3825c - std::sys_common::backtrace::print::h6fc2cc8be2392a8e
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/sys_common/backtrace.rs:34:9
   8:        0x100f398b0 - std::panicking::default_hook::{{closure}}::h3bb44ba6ff437e23
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/panicking.rs:267:22
   9:        0x100f39608 - std::panicking::default_hook::h92c6668a1a98ae8c
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/panicking.rs:286:9
  10:        0x100f39eec - std::panicking::rust_panic_with_hook::h9b8acc949e1fb807
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/panicking.rs:688:13
  11:        0x100f39cfc - std::panicking::begin_panic_handler::{{closure}}::hfc38b2cc56e3623c
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/panicking.rs:579:13
  12:        0x100f388b0 - std::sys_common::backtrace::__rust_end_short_backtrace::hbafd151207fc448f
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/sys_common/backtrace.rs:137:18
  13:        0x100f39a50 - rust_begin_unwind
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/panicking.rs:575:5
  14:        0x100f57898 - core::panicking::panic_fmt::h8c7f3d540a75578f
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/panicking.rs:65:14
  15:        0x100f57a38 - core::result::unwrap_failed::h666d2c7e0d130ea0
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/result.rs:1791:5
  16:        0x100ed87b8 - core::result::Result<T,E>::unwrap::h5b394c118d573d65
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/result.rs:1113:23
  17:        0x100ed42f4 - discord_patcher::main::h9b58132cccd0add9
                               at /Users/valentinebriese/projects/discord_patcher/src/main.rs:17:20
  18:        0x100eeb71c - core::ops::function::FnOnce::call_once::h5c1650a19e0385d6
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/ops/function.rs:251:5
  19:        0x100eee128 - std::sys_common::backtrace::__rust_begin_short_backtrace::h18d1bc8008e59eed
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/sys_common/backtrace.rs:121:18
  20:        0x100ee42e8 - std::rt::lang_start::{{closure}}::h2401fc11b2027750
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/rt.rs:166:18
  21:        0x100f32a04 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::ha4b10a239e2af884
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/ops/function.rs:286:13
  22:        0x100f32a04 - std::panicking::try::do_call::h6b4bcb7d3635e86a
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/panicking.rs:483:40
  23:        0x100f32a04 - std::panicking::try::h579c8cca81ff0f69
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/panicking.rs:447:19
  24:        0x100f32a04 - std::panic::catch_unwind::h4a997c12755a6e33
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/panic.rs:137:14
  25:        0x100f32a04 - std::rt::lang_start_internal::{{closure}}::hf20057b44f57f87c
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/rt.rs:148:48
  26:        0x100f32a04 - std::panicking::try::do_call::hdfca34da16d8863f
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/panicking.rs:483:40
  27:        0x100f32a04 - std::panicking::try::h4f4022e500de0807
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/panicking.rs:447:19
  28:        0x100f32a04 - std::panic::catch_unwind::h62721286166676e8
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/panic.rs:137:14
  29:        0x100f32a04 - std::rt::lang_start_internal::h659a783147314d97
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/rt.rs:148:20
  30:        0x100ee42b0 - std::rt::lang_start::hc9c7e6884dc58986
                               at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/std/src/rt.rs:165:17
  31:        0x100ed450c - _main

Here's a snippet of my program:

let asar_path = format!("{discord_path}/Contents/Resources/app.asar");
let asar_file = fs::read(asar_path).unwrap();
let asar = AsarReader::new(&asar_file, None).unwrap();

writing files as unpacked

there currently is support for unpacked files in the reader, but not in the writer.

not sure what should it look like in terms of the api. in electron_tasje, i'm currently writing "unpacked" files both in the asar and separately, which works enough but makes them take double the space

How to unpack asar archive?

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.