Compare commits

..

8 Commits

Author SHA1 Message Date
969cc68a71 fail better 2023-09-11 19:53:42 -07:00
add817dddf Commands::Interactive 2023-09-11 19:39:26 -07:00
7e7d3c507a upload comments 2023-09-11 19:19:07 -07:00
6d6c6a7f94 Scan::Flagged 2023-09-11 19:19:07 -07:00
ffb4b09e77 scan enum, Source property missing if fatal 2023-09-11 19:18:55 -07:00
06384f1086 use wget to download maps 2023-09-11 18:13:28 -07:00
7e36d3d2a6 optimize release binary size 2023-09-11 18:13:28 -07:00
a152ec5883 v2 with clap 2023-09-11 18:13:28 -07:00

View File

@ -15,16 +15,10 @@ enum Commands {
Download(MapList), Download(MapList),
Upload, Upload,
Scan, Scan,
Extract(Map),
Replace, Replace,
Interactive, Interactive,
} }
#[derive(Args)]
struct Map {
id:u64,
}
#[derive(Args)] #[derive(Args)]
struct MapList { struct MapList {
maps: Vec<u64>, maps: Vec<u64>,
@ -42,35 +36,15 @@ fn class_is_a(class: &str, superclass: &str) -> bool {
} }
return false return false
} }
fn recursive_collect_scripts(scripts: &mut std::vec::Vec<rbx_dom_weak::types::Ref>,dom: &rbx_dom_weak::WeakDom, instance: &rbx_dom_weak::Instance){
for &referent in instance.children() {
if let Some(c) = dom.get_by_ref(referent) {
if class_is_a(c.class.as_str(), "LuaSourceContainer") {
scripts.push(c.referent());//copy ref
}
recursive_collect_scripts(scripts,dom,c);
}
}
}
fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance) -> String{
let mut full_name=instance.name.clone();
let mut pref=instance.parent();
while let Some(parent)=dom.get_by_ref(pref){
full_name.insert(0, '.');
full_name.insert_str(0, &parent.name);
pref=parent.parent();
}
full_name
}
//download //download
//download list of maps to maps/unprocessed //download list of maps to maps/unprocessed
//scan (scripts) //scan (scripts)
//iter maps/unprocessed //iter maps/unprocessed
//passing moves to maps/verified //passing moves to maps/verified
//failing moves to maps/blocked //failing moves to maps/purgatory
//replace (edits & deletions) //replace (edits & deletions)
//iter maps/blocked //iter maps/purgatory
//replace scripts and put in maps/unprocessed //replace scripts and put in maps/unprocessed
//upload //upload
//iter maps/verified //iter maps/verified
@ -81,60 +55,18 @@ fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance) ->
//I can edit the file and it will edit it in place //I can edit the file and it will edit it in place
//I pass/fail(with comment)/allow each script //I pass/fail(with comment)/allow each script
fn get_script_refs(dom:&rbx_dom_weak::WeakDom) -> Vec<rbx_dom_weak::types::Ref>{ fn get_scripts(dom:rbx_dom_weak::WeakDom) -> Vec<rbx_dom_weak::Instance>{
let mut scripts = std::vec::Vec::new(); let mut scripts = std::vec::Vec::<rbx_dom_weak::Instance>::new();
recursive_collect_scripts(&mut scripts, dom, dom.root());
let (_,mut instances) = dom.into_raw();
for (_,instance) in instances.drain() {
if class_is_a(instance.class.as_str(), "LuaSourceContainer") {
scripts.push(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.path().file_stem().unwrap().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.path().file_stem().unwrap().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=&[
@ -159,10 +91,23 @@ enum Scan{
Flagged, Flagged,
} }
fn scan() -> Result<(), Box<dyn std::error::Error>>{ fn scan() -> Result<(), Box<dyn std::error::Error>>{
let mut id = get_id()?; let mut id = 0u32;
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 allowed_set = get_allowed_set()?; let mut allowed_set = std::collections::HashSet::<String>::new();
let mut blocked = get_blocked()?; 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::<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?;
@ -170,16 +115,15 @@ fn scan() -> Result<(), Box<dyn std::error::Error>>{
let dom = rbx_binary::from_reader(input)?; let dom = rbx_binary::from_reader(input)?;
let script_refs = get_script_refs(&dom); let scripts = get_scripts(dom);
//check scribb //check scribb
let mut fail_count=0; let mut fail_count=0;
let mut fail_type=Scan::Passed; let mut fail_type=Scan::Passed;
for &script_ref in script_refs.iter() { for script in scripts.iter() {
if let Some(script)=dom.get_by_ref(script_ref){
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 check_source_illegal_keywords(s){ if s.find("getfenv").is_some()||s.find("require").is_some(){
println!("{:?} - flagged.",file_thing.file_name()); println!("{:?} - flagged.",file_thing.file_name());
fail_type=Scan::Flagged; fail_type=Scan::Flagged;
break; break;
@ -198,15 +142,12 @@ fn scan() -> Result<(), Box<dyn std::error::Error>>{
}else{ }else{
panic!("FATAL: failed to get source for {:?}",file_thing.file_name()); panic!("FATAL: failed to get source for {:?}",file_thing.file_name());
} }
}else{
panic!("FATAL: failed to get_by_ref {:?}",script_ref);
}
} }
let mut dest=match fail_type { let mut dest=match fail_type {
Scan::Passed => std::path::PathBuf::from("maps/processed"), Scan::Passed => std::path::PathBuf::from("maps/processed"),
Scan::Blocked => { Scan::Blocked => {
println!("{:?} - {} {} not allowed.",file_thing.file_name(),fail_count,if fail_count==1 {"script"}else{"scripts"}); println!("{:?} - {} {} not allowed.",file_thing.file_name(),fail_count,if fail_count==1 {"script"}else{"scripts"});
std::path::PathBuf::from("maps/blocked") std::path::PathBuf::from("maps/purgatory")
} }
Scan::Flagged => std::path::PathBuf::from("maps/flagged") Scan::Flagged => std::path::PathBuf::from("maps/flagged")
}; };
@ -216,64 +157,35 @@ fn scan() -> Result<(), Box<dyn std::error::Error>>{
std::fs::write("id",id.to_string())?; std::fs::write("id",id.to_string())?;
Ok(()) Ok(())
} }
fn extract(file_id:u64) -> Result<(), Box<dyn std::error::Error>>{ fn replace() -> Result<(), Box<dyn std::error::Error>>{
let mut id = 0;
//Construct allowed scripts //Construct allowed scripts
let mut script_set = std::collections::HashSet::<String>::new(); let mut allowed_map = std::collections::HashMap::<u32,String>::new();
for entry in std::fs::read_dir("scripts/allowed")? {
let file_id_string=file_id.to_string(); let entry=entry?;
allowed_map.insert(entry.file_name().to_str().unwrap().parse::<u32>()?,std::fs::read_to_string(entry.path())?);
for entry in std::fs::read_dir("maps/unprocessed")? {
let file_thing=entry?;
if file_thing.file_name().to_str().unwrap().find(&file_id_string).is_none(){
continue;
} }
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")? {
let file_thing=entry?;
let input = std::io::BufReader::new(std::fs::File::open(file_thing.path())?); let input = std::io::BufReader::new(std::fs::File::open(file_thing.path())?);
let dom = rbx_binary::from_reader(input)?; let dom = rbx_binary::from_reader(input)?;
let script_refs = get_script_refs(&dom); let mut write_dom = rbx_dom_weak::WeakDom::new(rbx_dom_weak::InstanceBuilder::empty());
dom.clone_into_external(dom.root_ref(), &mut write_dom);
//extract scribb let scripts = get_scripts(dom);
for &script_ref in script_refs.iter() {
if let Some(script)=dom.get_by_ref(script_ref){
if let Some(rbx_dom_weak::types::Variant::String(s)) = script.properties.get("Source") {
if script_set.contains(s) {
continue;
}else{
script_set.insert(s.clone());
std::fs::write(format!("scripts/extracted/{:?}_{}_{}.lua",file_thing.file_name(),id,script.name),s)?;
id+=1;
}
}else{
panic!("FATAL: failed to get source for {:?}",file_thing.file_name());
}
}else{
panic!("FATAL: failed to get_by_ref {:?}",script_ref);
}
}
}
println!("extracted {} {}",id,if id==1 {"script"}else{"scripts"});
Ok(())
}
fn replace() -> Result<(), Box<dyn std::error::Error>>{
let allowed_map=get_allowed_map()?;
let replace_map=get_replace_map()?;
for entry in std::fs::read_dir("maps/blocked")? {
let file_thing=entry?;
let input = std::io::BufReader::new(std::fs::File::open(file_thing.path())?);
let mut dom = rbx_binary::from_reader(input)?;
let script_refs = get_script_refs(&dom);
//check scribb //check scribb
let mut any_failed=false; let mut any_failed=false;
for &script_ref in script_refs.iter() { for script in scripts.iter() {
if let Some(script)=dom.get_by_ref(script_ref){
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_map.get(source),dom.get_by_ref_mut(script.referent())) { if let (Some(replace_id),Some(replace_script))=(replace.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){
@ -287,10 +199,8 @@ fn replace() -> Result<(), Box<dyn std::error::Error>>{
any_failed=true; any_failed=true;
} }
}else{ }else{
panic!("FATAL: failed to get source for {:?}",file_thing.file_name()); println!("failed to failed to get source");
} any_failed=true;
}else{
panic!("FATAL: failed to get_by_ref {:?}",script_ref);
} }
} }
if any_failed { if any_failed {
@ -298,8 +208,9 @@ fn replace() -> Result<(), Box<dyn std::error::Error>>{
}else{ }else{
let mut dest=std::path::PathBuf::from("maps/unprocessed"); let mut dest=std::path::PathBuf::from("maps/unprocessed");
dest.set_file_name(file_thing.file_name()); dest.set_file_name(file_thing.file_name());
dest.set_extension("rbxl");//extension is always rbxl even if source file is extensionless
let output = std::io::BufWriter::new(std::fs::File::open(dest)?); let output = std::io::BufWriter::new(std::fs::File::open(dest)?);
rbx_binary::to_writer(output, &dom, &[dom.root_ref()])?; rbx_binary::to_writer(output, &write_dom, &[write_dom.root_ref()])?;
} }
} }
Ok(()) Ok(())
@ -318,204 +229,8 @@ fn upload() -> Result<(), Box<dyn std::error::Error>>{
unimplemented!() unimplemented!()
} }
enum Interactive{
Passed,
Blocked,
Flagged,
}
enum ScriptAction {
Pass,
Replace(u32),
Flag,
Block,
Delete,
}
enum ScriptActionParseResult {
Pass,
Block,
Exit,
Delete,
}
struct ParseScriptActionErr;
impl std::str::FromStr for ScriptActionParseResult {
type Err=ParseScriptActionErr;
fn from_str(s: &str) -> Result<Self, Self::Err>{
if s=="pass\n"||s=="1\n"{
Ok(Self::Pass)
}else if s=="block\n"{
Ok(Self::Block)
}else if s=="exit\n"{
Ok(Self::Exit)
}else if s=="delete\n"{
Ok(Self::Delete)
}else{
Err(ParseScriptActionErr)
}
}
}
fn interactive() -> Result<(), Box<dyn std::error::Error>>{ fn interactive() -> Result<(), Box<dyn std::error::Error>>{
let mut id=get_id()?; unimplemented!()
//Construct allowed scripts
let mut allowed_set=get_allowed_set()?;
let mut allowed_map=get_allowed_map()?;
let mut replace_map=get_replace_map()?;
let mut blocked = get_blocked()?;
'map_loop: for entry in std::fs::read_dir("maps/unprocessed")? {
let file_thing=entry?;
println!("processing map={:?}",file_thing.file_name());
let input = std::io::BufReader::new(std::fs::File::open(file_thing.path())?);
let mut dom = rbx_binary::from_reader(input)?;
let script_refs = get_script_refs(&dom);
//check scribb
let mut script_count=0;
let mut replace_count=0;
let mut block_count=0;
let mut fail_type=Interactive::Passed;
for &script_ref in script_refs.iter() {
if let Some(script)=dom.get_by_ref(script_ref){
if let Some(rbx_dom_weak::types::Variant::String(source)) = script.properties.get("Source") {
script_count+=1;
let source_action=if check_source_illegal_keywords(source) {
ScriptAction::Flag//script triggers flagging -> Flag
} else if blocked.contains(source) {
ScriptAction::Block//script is blocked -> Block
} else if allowed_set.contains(source) {
ScriptAction::Pass//script is allowed -> Pass
}else if let Some(replace_id)=replace_map.get(source) {
ScriptAction::Replace(*replace_id)
}else{
//interactive logic goes here
print!("unresolved source location={}\naction: ",get_full_name(&dom, script));
std::io::Write::flush(&mut std::io::stdout())?;
//load source into current.lua
std::fs::write("current.lua",source)?;
//prompt action in terminal
//wait for input
let script_action;
loop{
let mut action_string = String::new();
std::io::stdin().read_line(&mut action_string)?;
if let Ok(parsed_script_action)=action_string.parse::<ScriptActionParseResult>(){
script_action=parsed_script_action;
break;
}else{
println!("illegal action string.");
}
}
//update allowed/replace/blocked
match script_action{
ScriptActionParseResult::Pass => {
//if current.lua was updated, create an allowed and replace file and set script_action to replace(new_id)
let modified_source=std::fs::read_to_string("current.lua")?;
if &modified_source==source{
//it's always new.
//insert allowed_set
allowed_set.insert(modified_source.clone());
//insert allowed_map
allowed_map.insert(id,modified_source.clone());
//write allowed/id.lua
std::fs::write(format!("scripts/allowed/{}.lua",id),modified_source)?;
id+=1;
ScriptAction::Pass
}else{
//insert allowed_set
allowed_set.insert(modified_source.clone());
//insert allowed_map
allowed_map.insert(id,modified_source.clone());
//insert replace_map
replace_map.insert(source.clone(),id);//this cannot be reached if it already exists
//write allowed/id.lua
std::fs::write(format!("scripts/allowed/{}.lua",id),modified_source)?;
//write replace/id.lua
std::fs::write(format!("scripts/replace/{}.lua",id),source)?;
let ret=ScriptAction::Replace(id);
id+=1;
ret
}
},
ScriptActionParseResult::Block => {
blocked.insert(source.clone());
std::fs::write(format!("scripts/blocked/{}.lua",id),source)?;
id+=1;
ScriptAction::Block
},
ScriptActionParseResult::Exit => break 'map_loop,
ScriptActionParseResult::Delete => ScriptAction::Delete,
}
};
let location=get_full_name(&dom, script);
match source_action{
ScriptAction::Pass => println!("passed source location={}",location),
ScriptAction::Replace(replace_id)=>{
//replace the source
if let (Some(replace_source),Some(replace_script))=(allowed_map.get(&replace_id),dom.get_by_ref_mut(script.referent())){
replace_count+=1;
println!("replaced source id={} location={}",replace_id,location);
replace_script.properties.insert("Source".to_string(), rbx_dom_weak::types::Variant::String(replace_source.clone()));
}else{
panic!("failed to get replacement source id={} location={}",replace_id,location);
}
},
ScriptAction::Delete => {
println!("deleted source location={}",location);
replace_count+=1;
dom.destroy(script.referent());
},
ScriptAction::Flag => {
println!("flagged source location={}",location);
fail_type=Interactive::Flagged;
},
ScriptAction::Block => {
block_count+=1;
println!("blocked source location={}",location);
match fail_type{
Interactive::Passed => fail_type=Interactive::Blocked,
_=>(),
}
},
}
}else{
panic!("FATAL: failed to get source for {:?}",file_thing.file_name());
}
}else{
panic!("FATAL: failed to get_by_ref {:?}",script_ref);
}
}
let mut dest=match fail_type{
Interactive::Passed => {
println!("map={:?} passed with {} {}",file_thing.file_name(),script_count,if script_count==1 {"script"}else{"scripts"});
if replace_count==0{
std::path::PathBuf::from("maps/passed")
}else{
//create new file
println!("{} {} replaced - generating new file...",replace_count,if replace_count==1 {"script was"}else{"scripts were"});
let mut dest=std::path::PathBuf::from("maps/passed");
dest.push(file_thing.file_name());
let output = std::io::BufWriter::new(std::fs::File::create(dest)?);
rbx_binary::to_writer(output, &dom, &[dom.root_ref()])?;
//move original to processed folder
std::path::PathBuf::from("maps/unaltered")
}
},//write map into maps/processed
Interactive::Blocked => {
println!("map={:?} blocked with {}/{} {} blocked",file_thing.file_name(),block_count,script_count,if script_count==1 {"script"}else{"scripts"});
std::path::PathBuf::from("maps/blocked")
},//write map into maps/blocked
Interactive::Flagged => {
println!("map={:?} flagged",file_thing.file_name());
std::path::PathBuf::from("maps/flagged")
},//write map into maps/flagged
};
dest.push(file_thing.file_name());
std::fs::rename(file_thing.path(), dest)?;
}
std::fs::write("id",id.to_string())?;
Ok(())
} }
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
@ -526,6 +241,5 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Commands::Scan=>scan(), Commands::Scan=>scan(),
Commands::Replace=>replace(), Commands::Replace=>replace(),
Commands::Interactive=>interactive(), Commands::Interactive=>interactive(),
Commands::Extract(map)=>extract(map.id),
} }
} }