Skip to content

Commit 849a2be

Browse files
committed
use uuid instead of counter
1 parent 4f67f34 commit 849a2be

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bitbox-bridge/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ windows-service = "0.7.0"
2020
clap = { version = "4.5", features = ["cargo"] }
2121
warp = "0.3.7"
2222
tera = "1.20"
23+
uuid = { version = "1.10.0", features = ["v4"] }
2324

2425
[dependencies.u2fframing]
2526
version = "0.1"

bitbox-bridge/resources/confirmation_dialog.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ <h1>BitBoxBridge</h1>
111111
</div>
112112
<script>
113113
function sendResponse(userChoice) {
114-
fetch(`/confirm/response/{{ counter }}/${userChoice}`, { method: 'POST' })
114+
fetch(`/confirm/response/{{ confirm_id }}/${userChoice}`, { method: 'POST' })
115115
.then(() => window.close())
116116
.catch(err => console.error('Error sending response:', err));
117117
}

bitbox-bridge/src/web.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use futures_util::sink::SinkExt;
1818
use percent_encoding::percent_decode_str;
1919
use std::collections::{HashMap, HashSet};
2020
use std::net::SocketAddr;
21-
use std::sync::atomic::AtomicU32;
2221
use std::sync::{Arc, Mutex};
2322
use warp::{self, Filter, Rejection, Reply};
2423

@@ -159,14 +158,12 @@ async fn ws_upgrade(
159158

160159
// Global state to store the current oneshot sender
161160
struct ConfirmState {
162-
counter: AtomicU32,
163-
sender: Mutex<HashMap<u32, (oneshot::Sender<bool>, String)>>,
161+
sender: Mutex<HashMap<String, (oneshot::Sender<bool>, String)>>,
164162
}
165163

166164
impl ConfirmState {
167165
fn new() -> Arc<Self> {
168166
Arc::new(ConfirmState {
169-
counter: AtomicU32::new(0),
170167
sender: Mutex::new(HashMap::new()),
171168
})
172169
}
@@ -193,16 +190,14 @@ async fn user_confirm(
193190
base_url: &str,
194191
) -> Result<bool, ()> {
195192
let (tx, rx) = oneshot::channel();
196-
let counter = confirm_state
197-
.counter
198-
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
193+
let confirm_id = uuid::Uuid::new_v4();
199194
{
200195
let mut sender = confirm_state.sender.lock().unwrap();
201-
sender.insert(counter, (tx, message));
196+
sender.insert(confirm_id.to_string(), (tx, message));
202197
}
203198

204199
// Launch the web browser to show the dialog
205-
let dialog_url = format!("{}/confirm/{}", base_url, counter);
200+
let dialog_url = format!("{}/confirm/{}", base_url, confirm_id);
206201
if webbrowser::open(&dialog_url).is_err() {
207202
return Err(());
208203
}
@@ -242,16 +237,16 @@ async fn user_confirm_origin(
242237
fn setup_confirm_routes(
243238
confirm_state: Arc<ConfirmState>,
244239
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
245-
let confirm_dialog = warp::path!("confirm" / u32)
240+
let confirm_dialog = warp::path!("confirm" / String)
246241
.and(warp::get())
247242
.and(with_state(confirm_state.clone()))
248-
.map(|counter: u32, confirm_state: Arc<ConfirmState>| {
243+
.map(|confirm_id: String, confirm_state: Arc<ConfirmState>| {
249244
let sender_locked = confirm_state.sender.lock().unwrap();
250-
if let Some((_, message)) = sender_locked.get(&counter) {
245+
if let Some((_, message)) = sender_locked.get(&confirm_id) {
251246
let html = include_str!("../resources/confirmation_dialog.html");
252247
let ctx = {
253248
let mut ctx = tera::Context::new();
254-
ctx.insert("counter", &counter);
249+
ctx.insert("confirm_id", &confirm_id);
255250
ctx.insert("message", &message);
256251
ctx
257252
};
@@ -267,11 +262,11 @@ fn setup_confirm_routes(
267262
});
268263

269264
async fn handle_user_response(
270-
counter: u32,
265+
confirm_id: String,
271266
choice: bool,
272267
confirm_state: Arc<ConfirmState>,
273268
) -> Result<impl warp::Reply, warp::Rejection> {
274-
if let Some((sender, _)) = confirm_state.sender.lock().unwrap().remove(&counter) {
269+
if let Some((sender, _)) = confirm_state.sender.lock().unwrap().remove(&confirm_id) {
275270
let _ = sender.send(choice);
276271

277272
Ok(warp::reply::with_status("", warp::http::StatusCode::OK))
@@ -283,7 +278,7 @@ fn setup_confirm_routes(
283278
}
284279
}
285280

286-
let handle_response = warp::path!("confirm" / "response" / u32 / bool)
281+
let handle_response = warp::path!("confirm" / "response" / String / bool)
287282
.and(warp::post())
288283
.and(with_state(confirm_state.clone()))
289284
.and_then(handle_user_response);

0 commit comments

Comments
 (0)