diff --git a/src/main.rs b/src/main.rs index a8aa286..aa86a6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,6 +78,54 @@ fn get_scripts(dom:rbx_dom_weak::WeakDom) -> Vec{ scripts } +fn get_id() -> Result>{ + match std::fs::read_to_string("id"){ + Ok(id_file)=>Ok(id_file.parse::()?), + 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, Box>{ + let mut set=std::collections::HashSet::::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, Box>{ + get_set_from_file("scripts/allowed") +} + +fn get_blocked() -> Result, Box>{ + get_set_from_file("scripts/blocked") +} + +fn get_allowed_map() -> Result, Box>{ + let mut allowed_map = std::collections::HashMap::::new(); + for entry in std::fs::read_dir("scripts/allowed")? { + let entry=entry?; + allowed_map.insert(entry.file_name().to_str().unwrap().parse::()?,std::fs::read_to_string(entry.path())?); + } + Ok(allowed_map) +} + +fn get_replace_map() -> Result, Box>{ + let mut replace = std::collections::HashMap::::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::()?); + } + 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) -> Result<(), Box>{ let header=format!("Cookie: .ROBLOSECURITY={}",std::env::var("RBXCOOKIE")?); let shared_args=&[ @@ -102,23 +150,10 @@ enum Scan{ Flagged, } fn scan() -> Result<(), Box>{ - let mut id = 0u32; - match std::fs::read_to_string("id"){ - Ok(id_file)=>id=id_file.parse::()?, - 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)?, - } - } + let mut id = get_id()?; //Construct allowed scripts - let mut allowed_set = std::collections::HashSet::::new(); - for entry in std::fs::read_dir("scripts/allowed")? { - allowed_set.insert(std::fs::read_to_string(entry?.path())?); - } - let mut blocked = std::collections::HashSet::::new(); - for entry in std::fs::read_dir("scripts/blocked")? { - blocked.insert(std::fs::read_to_string(entry?.path())?); - } + let allowed_set = get_allowed_set()?; + let mut blocked = get_blocked()?; for entry in std::fs::read_dir("maps/unprocessed")? { let file_thing=entry?; @@ -134,7 +169,7 @@ fn scan() -> Result<(), Box>{ for script in scripts.iter() { if let Some(rbx_dom_weak::types::Variant::String(s)) = script.properties.get("Source") { //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()); fail_type=Scan::Flagged; break; @@ -169,17 +204,8 @@ fn scan() -> Result<(), Box>{ Ok(()) } fn replace() -> Result<(), Box>{ - //Construct allowed scripts - let mut allowed_map = std::collections::HashMap::::new(); - for entry in std::fs::read_dir("scripts/allowed")? { - let entry=entry?; - allowed_map.insert(entry.file_name().to_str().unwrap().parse::()?,std::fs::read_to_string(entry.path())?); - } - let mut replace = std::collections::HashMap::::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::()?); - } + let allowed_map=get_allowed_map()?; + let replace_map=get_replace_map()?; for entry in std::fs::read_dir("maps/purgatory")? { let file_thing=entry?; @@ -196,7 +222,7 @@ fn replace() -> Result<(), Box>{ let mut any_failed=false; for script in scripts.iter() { 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); //replace the source if let Some(replace_source)=allowed_map.get(replace_id){