forked from kevin.dorner/iserv-ipad-helper
		
	Pretty big refactor - Only the last step is broken
This commit is contained in:
		
							
								
								
									
										73
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										73
									
								
								src/main.rs
									
									
									
									
									
								
							| @@ -18,7 +18,7 @@ struct Args { | |||||||
|     target_room: u16, |     target_room: u16, | ||||||
|  |  | ||||||
|     #[clap(long)] |     #[clap(long)] | ||||||
|     target_compilation: u16, |     target_compilation: Option<u16>, | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -141,9 +141,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||||||
|         println!("Received response status: {:?}", status); |         println!("Received response status: {:?}", status); | ||||||
|         println!("-----------------------------------"); |         println!("-----------------------------------"); | ||||||
|  |  | ||||||
|  |         if args.target_compilation.is_none() { | ||||||
|  |             continue; | ||||||
|  |         } | ||||||
|          |          | ||||||
|         let resp = client |         let resp = client | ||||||
|         .get(format!("https://{}/iserv/admin/mdm/ios/compilation/edit/1", args.hostname)) |         .get(format!("https://{}/iserv/admin/mdm/ios/compilation/edit/{}", args.hostname, args.target_compilation.unwrap())) | ||||||
|         .header(header.0, header.to_owned().1) |         .header(header.0, header.to_owned().1) | ||||||
|         .send()?; |         .send()?; | ||||||
|         //TODO Check for non 200 response |         //TODO Check for non 200 response | ||||||
| @@ -156,6 +159,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||||||
|         println!("Received response status: {:?}", status); |         println!("Received response status: {:?}", status); | ||||||
|         println!("-----------------------------------"); |         println!("-----------------------------------"); | ||||||
|          |          | ||||||
|  |         let csrf_token = squeeze_html_form(plain, "name=\"ioscompilation[_token]\"", "value=\"", "\""); | ||||||
|  |  | ||||||
|         let mut form_data = Vec::from([ |         let mut form_data = Vec::from([ | ||||||
|             ( |             ( | ||||||
| @@ -180,37 +184,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||||||
|         let plain_inner_devices = squeeze_html_form(plain, "name=\"ioscompilation[devices][]\"", ">", "</select>"); |         let plain_inner_devices = squeeze_html_form(plain, "name=\"ioscompilation[devices][]\"", ">", "</select>"); | ||||||
|         form_data.append(&mut squeeze_loop(plain_inner_devices, "<option value=\"", "\"", " selected=\"selected\"", 3, "ioscompilation[devices][]")); |         form_data.append(&mut squeeze_loop(plain_inner_devices, "<option value=\"", "\"", " selected=\"selected\"", 3, "ioscompilation[devices][]")); | ||||||
|  |  | ||||||
|         /* |  | ||||||
|         let re = |  | ||||||
|             regex::Regex::new(r#"<option value="(\d+)" selected="selected">(iPad-\w+)</option>"#) |  | ||||||
|                 .unwrap(); |  | ||||||
|         form_data.extend( |  | ||||||
|             re.captures_iter(plain) |  | ||||||
|                 .map(|cap| ("ioscompilation[devices][]".into(), cap[1].to_owned().into())), |  | ||||||
|         ); */ |  | ||||||
|  |  | ||||||
|         // push new device |         // push new device | ||||||
|  |         let mdm_new_ipad_id = squeeze_right(plain_inner_devices, "<option value=\"", &format!("\">{}", form_name)); | ||||||
|         if plain_inner_devices.contains(form_name) { |         if plain_inner_devices.contains(form_name) { | ||||||
|             form_data.push(("ioscompilation[devices][]".into(), squeeze_right(plain_inner_devices, "<option value=\"", &format!("\">{}", form_name)).into())); |             form_data.push(("ioscompilation[devices][]".into(), mdm_new_ipad_id.into())); | ||||||
|         } else { |         } else { | ||||||
|             eprintln!("Device not found in compilation edit.") |             eprintln!("Device not found in compilation edit.") | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         /* |  | ||||||
|         let re = regex::Regex::new(&format!( |  | ||||||
|             "<option value=\"(\\d+)\">({})</option>", |  | ||||||
|             form_name |  | ||||||
|         )) |  | ||||||
|         .unwrap(); |  | ||||||
|         let mut iter = re |  | ||||||
|             .captures_iter(plain) |  | ||||||
|             .map(|cap| ("ioscompilation[devices][]".into(), cap[1].to_owned().into())); |  | ||||||
|         form_data.push(iter.next().expect("Device not found in compilation edit")); |  | ||||||
|         if iter.next().is_none() { |  | ||||||
|             eprintln!("Expected single device match, found multiple"); |  | ||||||
|             eprintln!("Perhaps the item already was added to the compilation"); |  | ||||||
|         };*/ |  | ||||||
|  |  | ||||||
|         // push submit action |         // push submit action | ||||||
|         form_data.push(("ioscompilation[actions][submit]".into(), "".into())); |         form_data.push(("ioscompilation[actions][submit]".into(), "".into())); | ||||||
|  |  | ||||||
| @@ -218,20 +199,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||||||
|         let form_token = squeeze_html_form(plain, "name=\"ioscompilation[_token]\"", "value=\"", "\""); |         let form_token = squeeze_html_form(plain, "name=\"ioscompilation[_token]\"", "value=\"", "\""); | ||||||
|         form_data.push(("ioscompilation[_token]".into(), form_token.into())); |         form_data.push(("ioscompilation[_token]".into(), form_token.into())); | ||||||
|  |  | ||||||
|         println!("{:#?}", form_data); |  | ||||||
|  |  | ||||||
|         //remove me |  | ||||||
|         //std::fs::write("plain.out", &plain).unwrap(); |  | ||||||
|         continue; |  | ||||||
|  |  | ||||||
|         let resp = client |         let resp = client | ||||||
|             .post(format!("https://{}/iserv/admin/mdm/ios/compilation/edit/1", args.hostname)) |             .post(format!("https://{}/iserv/admin/mdm/ios/compilation/edit/{}", args.hostname, args.target_compilation.unwrap())) | ||||||
|             .header(header.0, header.to_owned().1) |             .header(header.0, header.to_owned().1) | ||||||
|             .form(&form_data) |             .form(&form_data) | ||||||
|             .send()?; |             .send()?; | ||||||
|  |  | ||||||
|         let status = &resp.status(); |         let status = &resp.status(); | ||||||
|         //let plain = &resp.text()?; |         let plain = &resp.text()?; | ||||||
|  |         std::fs::write("device-add.out.html", plain).unwrap(); | ||||||
|  |  | ||||||
|         println!("-----------------------------------"); |         println!("-----------------------------------"); | ||||||
|         println!("6/7 Added device to MDM Compilation"); |         println!("6/7 Added device to MDM Compilation"); | ||||||
|         println!("Received response status: {:?}", status); |         println!("Received response status: {:?}", status); | ||||||
| @@ -239,36 +216,30 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||||||
|  |  | ||||||
|  |  | ||||||
|         let form_data = [ |         let form_data = [ | ||||||
|             ("iserv_crud_multi_select[actions][apply-all-changes]", ""), |             ("iserv_crud_multi_select[confirm]", "apply-changes"), | ||||||
|             ("iserv_crud_multi_select[grouped_actions]", ""), |             ("iserv_crud_multi_select[multi][]", mdm_new_ipad_id), | ||||||
|             // seems to work with and without token :o (but without it throws a csrf error) // example token: rqB8um_Y6sJ1hCFMeVlg9r3L0v__tH3GS5KcV4Lj_ug |             ("iserv_crud_multi_select[topReplacementActions][apply-changes]", ""), | ||||||
|             ("iserv_crud_multi_select[_token]", ""), |             //("iserv_crud_multi_select[_token]", csrf_token), | ||||||
|  |             // seems to work with and without token :o (but without it iserv throws a csrf error) // example token: rqB8um_Y6sJ1hCFMeVlg9r3L0v__tH3GS5KcV4Lj_ug | ||||||
|         ]; |         ]; | ||||||
|  |  | ||||||
|         let resp = client |         let resp = client | ||||||
|             .post(format!("https://{}/iserv/admin/mdm/ios/device/batch/confirm", args.hostname)) |             .post(format!("https://{}/iserv/admin/mdm/ios/device/batch", args.hostname)) | ||||||
|             .header(header.0, header.to_owned().1) |             .header(header.0, header.to_owned().1) | ||||||
|             .form(&form_data) |             .form(&form_data) | ||||||
|             .send()?; |             .send()?; | ||||||
|  |  | ||||||
|             let status = &resp.status(); |             let status = &resp.status(); | ||||||
|             //let plain = &resp.text()?; |             let plain = &resp.text()?; | ||||||
|  |             std::fs::write("confirm.out.html", plain).unwrap(); | ||||||
|  |  | ||||||
|             //println!("{}", plain); |  | ||||||
|  |  | ||||||
|              |  | ||||||
|             /*{ |  | ||||||
|                 "iserv_crud_multi_select[actions][apply-all-changes]": "", |  | ||||||
|                 "iserv_crud_multi_select[grouped_actions]": "", |  | ||||||
|                 "iserv_crud_multi_select[_token]": "rqB8um_Y6sJ1hCFMeVlg9r3L0v__tH3GS5KcV4Lj_ug" |  | ||||||
|             }*/ |  | ||||||
|  |  | ||||||
|         println!("-----------------------------------"); |         println!("-----------------------------------"); | ||||||
|         println!("7/7 Confirmed actions"); |         println!("7/7 Confirmed device actions"); | ||||||
|         println!("Received response status: {:?}", status); |         println!("Received response status: {:?}", status); | ||||||
|         println!("-----------------------------------"); |         println!("-----------------------------------"); | ||||||
|     } |     } | ||||||
|     Ok(()) |     //Ok(()) | ||||||
| } | } | ||||||
|  |  | ||||||
| // helper functions | // helper functions | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user