GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — clibrowser/src/session.rs at main · allthingssecurity/clibrowser Expand file tree/ Copy pathsession.rsMore file actions 138 lines (118 loc) · 3.84 KB / Copy pathsession.rsFile metadata and controls138 lines (118 loc) · 3.84 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 use anyhow::{Context, Result}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::path::PathBuf; #[derive(Serialize, Deserialize, Default)] pub struct SessionState { pub current_url: Option<String>, pub status_code: Option<u16>, pub response_headers: Option<HashMap<String, String>>, pub content_type: Option<String>, pub last_fetched: Option<String>, } #[derive(Serialize, Deserialize, Default)] pub struct FillData { pub form_selector: Option<String>, pub form_index: Option<usize>, pub fields: HashMap<String, String>, } pub struct Session { pub name: String, pub dir: PathBuf, pub state: SessionState, pub fills: FillData, cookies_raw: Option<String>, } impl Session { pub fn load(name: &str) -> Result<Self> { let base = session_base_dir()?; let dir = base.join(name); std::fs::create_dir_all(&dir) .with_context(|| format!("creating session dir: {}", dir.display()))?; let state: SessionState = match std::fs::read_to_string(dir.join("state.json")) { Ok(s) => serde_json::from_str(&s).unwrap_or_default(), Err(_) => SessionState::default(), }; let fills: FillData = match std::fs::read_to_string(dir.join("fills.json")) { Ok(s) => serde_json::from_str(&s).unwrap_or_default(), Err(_) => FillData::default(), }; let cookies_raw = std::fs::read_to_string(dir.join("cookies.json")).ok(); Ok(Session { name: name.to_string(), dir, state, fills, cookies_raw, }) } pub fn save(&self) -> Result<()> { let state_json = serde_json::to_string_pretty(&self.state)?; std::fs::write(self.dir.join("state.json"), state_json)?; if let Some(ref raw) = self.cookies_raw { std::fs::write(self.dir.join("cookies.json"), raw)?; } let fills_json = serde_json::to_string_pretty(&self.fills)?; std::fs::write(self.dir.join("fills.json"), fills_json)?; Ok(()) } pub fn page_html(&self) -> Option<String> { std::fs::read_to_string(self.dir.join("page.html")).ok() } pub fn save_page(&self, html: &str) -> Result<()> { std::fs::write(self.dir.join("page.html"), html)?; Ok(()) } pub fn cookies_json(&self) -> Option<&str> { self.cookies_raw.as_deref() } pub fn set_cookies_json(&mut self, json: String) { self.cookies_raw = Some(json); } pub fn clear(&mut self) -> Result<()> { self.state = SessionState::default(); self.fills = FillData::default(); self.cookies_raw = None; // Remove all files in the session dir for entry in std::fs::read_dir(&self.dir)? { let entry = entry?; std::fs::remove_file(entry.path()).ok(); } Ok(()) } } pub fn session_base_dir() -> Result<PathBuf> { let base = if let Ok(home) = std::env::var("CLIBROWSER_HOME") { PathBuf::from(home) } else { dirs::home_dir() .context("cannot determine home directory")? .join(".clibrowser") }; Ok(base.join("sessions")) } pub fn list_sessions() -> Result<Vec<String>> { let base = session_base_dir()?; if !base.exists() { return Ok(vec![]); } let mut sessions = vec![]; for entry in std::fs::read_dir(&base)? { let entry = entry?; if entry.file_type()?.is_dir() { if let Some(name) = entry.file_name().to_str() { sessions.push(name.to_string()); } } } sessions.sort(); Ok(sessions) } pub fn delete_session(name: &str) -> Result<()> { let base = session_base_dir()?; let dir = base.join(name); if dir.exists() { std::fs::remove_dir_all(&dir)?; } Ok(()) } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |