Skip to content

Commit

Permalink
add no_emded feature
Browse files Browse the repository at this point in the history
  • Loading branch information
DiamondHunters committed Nov 14, 2022
1 parent b814e8a commit a50a5fb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ edition = "2021"

[dependencies]
rasar = {git = 'https://github.com/Zerthox/rasar.git'}
random-string = "1.0.0"
random-string = "1.0.0"

[features]
no_embed = []
default = []
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
An inject tools for injecting js code into electron application

Please follow DMCA when using this code
### How it works:
### How it works

1. unpack `node_modules.asar` package (in `./resources`)
2. write `hook.js` into `raven` package directory (raven will be required at the early stage of startup in some application)
3. modify `index.js` of `raven`,injecting require of `hook.js`

> Currently using embedded javascript file (`hooklog.js`)
usage:
### Usage

1. modify `hook.js` if you need
2. use `cargo build` to make executable
1. modify `hook.js` if you need or enable `no_embed` feature to use specified js (`NO_EMBED_HOOK_JS_PATH`) at runtime
2. use `cargo build` to make executable with embedded js or `cargo build --features no_embed` without embedded js
3. Move the program to the electron application directory
4. run

Expand All @@ -27,4 +27,4 @@ Since macos may adopt different packaging methods and webkit as the execution en

### Examples

https://github.com/DiamondHunters/NodeInject_Hook_example
https://github.com/DiamondHunters/NodeInject_Hook_example :Use NodeInject to realize specific functions in Typora
45 changes: 22 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
use std::convert::AsRef;
use std::fs::{File, OpenOptions};
use std::io::Write;
use random_string::generate;


const BYTES: &[u8] = include_bytes!("hooklog.js");
const LICENSE_CHARS: &str = "L23456789ABCDEFGHJKMNPQRSTUVWXYZ";
const HOOK_JS_WRITE_PATH: &str = "./node/raven/hook.js"; //path to hook.js in unpacked module, if you change it, change it in append_require_to_file too
#[cfg(feature = "no_embed")]
const NO_EMBED_HOOK_JS_PATH: &str = "./hook.js"; // if no_emded feature is enabled, this file will be used in runtime
#[cfg(not(feature = "no_embed"))]
const EMBED_HOOK_JS_BYTES: &[u8] = include_bytes!("hooklog.js"); // embedded hooking code file,will be embedded in binary file
const INJECT_JS_PATH: &str = "./node/raven/index.js"; //path for unpacked module to inject code into,if you want inject code into another module, change it here and in HOOK_JS_WRITE_PATH


fn main() {
generate_license();
if file_exist("./node"){
println!("You may have already installed the hook.");
println!("You may have already installed the hook.Please check manually.");
return;
}
if !file_exist("./resources/node_modules.asar") {
println!("no node_modules.asar found");
println!("move me to the root of your typora installation(the same directory as executable of electron)");
return;
}
Expand All @@ -30,29 +35,23 @@ fn file_exist(archive: &str) -> bool {
return std::path::Path::new(archive).exists()
}


#[cfg(not(feature = "no_embed"))]
fn write_js_to_file() {
let mut file = File::create(HOOK_JS_WRITE_PATH).unwrap();
file.write_all(EMBED_HOOK_JS_BYTES).unwrap();
}
#[cfg(feature = "no_embed")]
fn write_js_to_file() {
let mut file = File::create("./node/raven/hook.js").unwrap();
file.write_all(BYTES).unwrap();
let mut file = File::create(HOOK_JS_WRITE_PATH).unwrap();
let mut hook_js = File::open(NO_EMBED_HOOK_JS_PATH).unwrap();
std::io::copy(&mut hook_js, &mut file).unwrap();
}

fn append_require_to_file() {
let mut file = OpenOptions::new()
.append(true)
.open("./node/raven/index.js")
.open(INJECT_JS_PATH)
.unwrap();
file.write_all("\nrequire('./hook')".as_ref()).unwrap();
}
fn generate_license(){
let mut license = generate(22, LICENSE_CHARS);
for n in 0..2 {
let mut o = 0;
for i in (0..16).step_by(2) {
o += LICENSE_CHARS.find(&license[n+i..=n+i]).unwrap()
}
o %= LICENSE_CHARS.len();
license += &LICENSE_CHARS[o..=o];
}
license.insert(6, '-');
license.insert(13, '-');
license.insert(20, '-');
println!("License for you: {}", license);
}

0 comments on commit a50a5fb

Please sign in to comment.