helper funcs

This commit is contained in:
Quaternions 2023-09-12 16:28:10 -07:00
parent 6ef8fd2f69
commit b434dce0f6

View File

@ -78,6 +78,54 @@ fn get_scripts(dom:rbx_dom_weak::WeakDom) -> Vec<rbx_dom_weak::Instance>{
scripts scripts
} }
fn get_id() -> Result<u32, Box<dyn std::error::Error>>{
match std::fs::read_to_string("id"){
Ok(id_file)=>Ok(id_file.parse::<u32>()?),
Err(e) => match e.kind() {
std::io::ErrorKind::NotFound => Ok(0),//implicitly take on id=0
_ => Err(e)?,
}
}
}
fn get_set_from_file(file:&str) -> Result<std::collections::HashSet<String>, Box<dyn std::error::Error>>{
let mut set=std::collections::HashSet::<String>::new();
for entry in std::fs::read_dir(file)? {
set.insert(std::fs::read_to_string(entry?.path())?);
}
Ok(set)
}
fn get_allowed_set() -> Result<std::collections::HashSet<String>, Box<dyn std::error::Error>>{
get_set_from_file("scripts/allowed")
}
fn get_blocked() -> Result<std::collections::HashSet<String>, Box<dyn std::error::Error>>{
get_set_from_file("scripts/blocked")
}
fn get_allowed_map() -> Result<std::collections::HashMap::<u32,String>, Box<dyn std::error::Error>>{
let mut allowed_map = std::collections::HashMap::<u32,String>::new();
for entry in std::fs::read_dir("scripts/allowed")? {
let entry=entry?;
allowed_map.insert(entry.file_name().to_str().unwrap().parse::<u32>()?,std::fs::read_to_string(entry.path())?);
}
Ok(allowed_map)
}
fn get_replace_map() -> Result<std::collections::HashMap::<String,u32>, Box<dyn std::error::Error>>{
let mut replace = std::collections::HashMap::<String,u32>::new();
for entry in std::fs::read_dir("scripts/replace")? {
let entry=entry?;
replace.insert(std::fs::read_to_string(entry.path())?,entry.file_name().to_str().unwrap().parse::<u32>()?);
}
Ok(replace)
}
fn check_source_illegal_keywords(source:&String)->bool{
source.find("getfenv").is_some()||source.find("require").is_some()
}
fn download(map_list: Vec<u64>) -> Result<(), Box<dyn std::error::Error>>{ fn download(map_list: Vec<u64>) -> Result<(), Box<dyn std::error::Error>>{
let header=format!("Cookie: .ROBLOSECURITY={}",std::env::var("RBXCOOKIE")?); let header=format!("Cookie: .ROBLOSECURITY={}",std::env::var("RBXCOOKIE")?);
let shared_args=&[ let shared_args=&[
@ -102,23 +150,10 @@ enum Scan{
Flagged, Flagged,
} }
fn scan() -> Result<(), Box<dyn std::error::Error>>{ fn scan() -> Result<(), Box<dyn std::error::Error>>{
let mut id = 0u32; let mut id = get_id()?;
match std::fs::read_to_string("id"){
Ok(id_file)=>id=id_file.parse::<u32>()?,
Err(e) => match e.kind() {
std::io::ErrorKind::NotFound => println!("id file does not exist: starting from 0"),//continue on, implicitly take on id=0, write the id file at the end
_ => return Err(e)?,
}
}
//Construct allowed scripts //Construct allowed scripts
let mut allowed_set = std::collections::HashSet::<String>::new(); let allowed_set = get_allowed_set()?;
for entry in std::fs::read_dir("scripts/allowed")? { let mut blocked = get_blocked()?;
allowed_set.insert(std::fs::read_to_string(entry?.path())?);
}
let mut blocked = std::collections::HashSet::<String>::new();
for entry in std::fs::read_dir("scripts/blocked")? {
blocked.insert(std::fs::read_to_string(entry?.path())?);
}
for entry in std::fs::read_dir("maps/unprocessed")? { for entry in std::fs::read_dir("maps/unprocessed")? {
let file_thing=entry?; let file_thing=entry?;
@ -134,7 +169,7 @@ fn scan() -> Result<(), Box<dyn std::error::Error>>{
for script in scripts.iter() { for script in scripts.iter() {
if let Some(rbx_dom_weak::types::Variant::String(s)) = script.properties.get("Source") { if let Some(rbx_dom_weak::types::Variant::String(s)) = script.properties.get("Source") {
//flag keywords and instantly fail //flag keywords and instantly fail
if s.find("getfenv").is_some()||s.find("require").is_some(){ if check_source_illegal_keywords(s){
println!("{:?} - flagged.",file_thing.file_name()); println!("{:?} - flagged.",file_thing.file_name());
fail_type=Scan::Flagged; fail_type=Scan::Flagged;
break; break;
@ -169,17 +204,8 @@ fn scan() -> Result<(), Box<dyn std::error::Error>>{
Ok(()) Ok(())
} }
fn replace() -> Result<(), Box<dyn std::error::Error>>{ fn replace() -> Result<(), Box<dyn std::error::Error>>{
//Construct allowed scripts let allowed_map=get_allowed_map()?;
let mut allowed_map = std::collections::HashMap::<u32,String>::new(); let replace_map=get_replace_map()?;
for entry in std::fs::read_dir("scripts/allowed")? {
let entry=entry?;
allowed_map.insert(entry.file_name().to_str().unwrap().parse::<u32>()?,std::fs::read_to_string(entry.path())?);
}
let mut replace = std::collections::HashMap::<String,u32>::new();
for entry in std::fs::read_dir("scripts/replace")? {
let entry=entry?;
replace.insert(std::fs::read_to_string(entry.path())?,entry.file_name().to_str().unwrap().parse::<u32>()?);
}
for entry in std::fs::read_dir("maps/purgatory")? { for entry in std::fs::read_dir("maps/purgatory")? {
let file_thing=entry?; let file_thing=entry?;
@ -196,7 +222,7 @@ fn replace() -> Result<(), Box<dyn std::error::Error>>{
let mut any_failed=false; let mut any_failed=false;
for script in scripts.iter() { for script in scripts.iter() {
if let Some(rbx_dom_weak::types::Variant::String(source)) = script.properties.get("Source") { if let Some(rbx_dom_weak::types::Variant::String(source)) = script.properties.get("Source") {
if let (Some(replace_id),Some(replace_script))=(replace.get(source),write_dom.get_by_ref_mut(script.referent())) { if let (Some(replace_id),Some(replace_script))=(replace_map.get(source),write_dom.get_by_ref_mut(script.referent())) {
println!("replace {}",replace_id); println!("replace {}",replace_id);
//replace the source //replace the source
if let Some(replace_source)=allowed_map.get(replace_id){ if let Some(replace_source)=allowed_map.get(replace_id){