diff --git a/src/asn1.rs b/src/asn1.rs index f4dec698..0db67ab6 100644 --- a/src/asn1.rs +++ b/src/asn1.rs @@ -8,15 +8,15 @@ mod scheme; use std::rc::Rc; -use asn1_parser::{Asn1, Asn1Decoder}; +use asn1_parser::{Asn1, Asn1Decoder, Asn1Encoder}; use web_sys::KeyboardEvent; use yew::{classes, function_component, html, use_effect_with_deps, use_reducer, use_state, Callback, Html, Reducible}; -use yew_hooks::{use_clipboard, use_location}; +use yew_hooks::{use_clipboard, use_local_storage, use_location}; use yew_notifications::{use_notification, Notification, NotificationType}; use crate::asn1::asn1_viewer::Asn1Viewer; use crate::asn1::hex_view::HexViewer; -use crate::common::ByteInput; +use crate::common::{encode_bytes, ByteInput, BytesFormat}; use crate::url_query_params; use crate::url_query_params::generate_asn1_link; @@ -26,6 +26,7 @@ pub const TEST_ASN1: &[u8] = &[ 48, 30, 160, 2, 5, 0, 161, 24, 30, 22, 0, 67, 0, 101, 0, 114, 0, 116, 0, 105, 0, 102, 0, 105, 0, 99, 0, 97, 0, 116, 0, 101, ]; +const ASN1_LOCAL_STORAGE_KEY: &str = "ASN1_DATA"; pub fn compare_ids(asn1_node_id: u64, cur_node: &Option) -> bool { matches!(cur_node, Some(node_id) if *node_id == asn1_node_id) @@ -109,11 +110,25 @@ pub fn asn1_parser_page() -> Html { let notifications = notification_manager.clone(); let raw_asn1_setter = raw_asn1.setter(); let asn1_setter = parsed_asn1.setter(); + let local_storage = use_local_storage::(ASN1_LOCAL_STORAGE_KEY.to_owned()); use_effect_with_deps( move |_: &[(); 0]| { let query = &location.search; if query.len() < 2 { + // URL query params is empty. We try to load ASN1 from local storage. + if let Some(raw_asn1) = (*local_storage).as_ref() { + if let Ok(bytes) = hex::decode(raw_asn1) { + match Asn1::decode_buff(&bytes) { + Ok(asn1) => { + asn1_setter.set(asn1.to_owned_with_asn1(asn1.inner_asn1().to_owned())); + } + Err(err) => { + error!("Can not decode asn1: {:?}", err); + } + } + } + } return; } @@ -122,7 +137,6 @@ pub fn asn1_parser_page() -> Html { let url_query_params::Asn1 { asn1: asn1_data } = asn1; match Asn1::decode_buff(&asn1_data) { Ok(asn1) => { - debug!("parsed!"); asn1_setter.set(asn1.to_owned_with_asn1(asn1.inner_asn1().to_owned())); } Err(error) => notifications.spawn(Notification::new( @@ -145,6 +159,16 @@ pub fn asn1_parser_page() -> Html { [], ); + let local_storage = use_local_storage::(ASN1_LOCAL_STORAGE_KEY.to_owned()); + use_effect_with_deps( + move |asn1| { + let mut encoded = vec![0; asn1.needed_buf_size()]; + asn1.encode_buff(&mut encoded).expect("ASN1 encoding should not fail"); + local_storage.set(encode_bytes(encoded, BytesFormat::Hex)); + }, + parsed_asn1.clone(), + ); + let clipboard = use_clipboard(); let raw_asn1_data = (*raw_asn1).clone(); let share_by_link = Callback::from(move |_| { diff --git a/src/common/mod.rs b/src/common/mod.rs index 891251b8..bec9513d 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -63,7 +63,7 @@ pub const BYTES_FORMATS: [BytesFormat; 5] = [ BytesFormat::Binary, ]; -fn encode_bytes(bytes: impl AsRef<[u8]>, format: BytesFormat) -> String { +pub fn encode_bytes(bytes: impl AsRef<[u8]>, format: BytesFormat) -> String { match format { BytesFormat::Hex => hex::encode(bytes), BytesFormat::Base64 => base64::encode(bytes), diff --git a/src/jwt.rs b/src/jwt.rs index 34993b28..7ba0bece 100644 --- a/src/jwt.rs +++ b/src/jwt.rs @@ -93,7 +93,7 @@ pub fn jwt() -> Html { let query = &location.search; if query.len() < 2 { - // URL query params is empty. We try to local JWT from local storage. + // URL query params is empty. We try to load JWT from local storage. if let Some(raw_jwt) = (*local_storage).as_ref() { match serde_json::from_str(raw_jwt.as_str()) { Ok(jwt) => {