diff --git a/create.sh b/create.sh index db34ab8..a4831f0 100755 --- a/create.sh +++ b/create.sh @@ -7,4 +7,4 @@ curl --header "Content-Type: application/json" \ "id": "67e55044-10b1-426f-9247-bb680e5fe0c8" } }' \ - http://localhost:3000/command + http://localhost:14000/command diff --git a/modify.sh b/modify.sh index a5aa3e7..ec69d35 100755 --- a/modify.sh +++ b/modify.sh @@ -6,4 +6,4 @@ curl --header "Content-Type: application/json" \ "id": "67e55044-10b1-426f-9247-bb680e5fe0c8" } }' \ - http://localhost:3000/command + http://localhost:14000/command diff --git a/modifyback.sh b/modifyback.sh index b1fffda..fe0c8e7 100755 --- a/modifyback.sh +++ b/modifyback.sh @@ -6,4 +6,4 @@ curl --header "Content-Type: application/json" \ "id": "67e55044-10b1-426f-9247-bb680e5fe0c8" } }' \ - http://localhost:3000/command + http://localhost:14000/command diff --git a/src/lib.rs b/src/lib.rs index 1f34250..365a508 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,11 +49,15 @@ enum Command { Delete { id: Uuid, }, + Status, } #[derive(Serialize)] -pub struct ProxyResponse { - message: String, +pub enum ProxyResponse { + Message(String), + Status { + tunnels: HashMap, + }, } #[derive(Debug)] @@ -95,9 +99,7 @@ pub async fn process_command( if !payload.verify_signature(&state.verifying_key) { return ( StatusCode::UNAUTHORIZED, - Json(ProxyResponse { - message: "Invalid signature".to_string(), - }), + Json(ProxyResponse::Message("Invalid signature".to_string())), ); } match payload.command { @@ -111,17 +113,17 @@ pub async fn process_command( if state.proxies.lock().unwrap().get(&id).is_some() { return ( StatusCode::CONFLICT, - Json(ProxyResponse { - message: "Id already exists. Use the modify command instead.".to_string(), - }), + Json(ProxyResponse::Message( + "Id already exists. Use the modify command instead.".to_string(), + )), ); } if !state.ports.write().unwrap().insert(incoming_port) { return ( StatusCode::CONFLICT, - Json(ProxyResponse { - message: format!("The `incoming_port` already in use: {incoming_port}"), - }), + Json(ProxyResponse::Message(format!( + "The `incoming_port` already in use: {incoming_port}" + ))), ); } @@ -138,11 +140,11 @@ pub async fn process_command( add_proxy(incoming_port, rx).await.unwrap(); // TODO: error propagation?? ( StatusCode::ACCEPTED, - Json(ProxyResponse { - message: format!( + Json(ProxyResponse :: + Message( format!( "Created tunnel {id} on port {incoming_port} to use {destination_ip}:{destination_port}" ), - }), + )), ) } Command::Modify { @@ -161,18 +163,14 @@ pub async fn process_command( .unwrap(); ( StatusCode::ACCEPTED, - Json(ProxyResponse { - message: format!( - "Changed tunnel {id} to use {destination_ip}:{destination_port}" - ), - }), + Json(ProxyResponse::Message(format!( + "Changed tunnel {id} to use {destination_ip}:{destination_port}" + ))), ) } else { ( StatusCode::NOT_FOUND, - Json(ProxyResponse { - message: format!("Id not found: {id}"), - }), + Json(ProxyResponse::Message(format!("Id not found: {id}"))), ) } } @@ -182,19 +180,27 @@ pub async fn process_command( state.ports.write().unwrap().remove(&proxy.incoming_port); ( StatusCode::ACCEPTED, - Json(ProxyResponse { - message: format!("Deleted tunnel: {id}"), - }), + Json(ProxyResponse::Message(format!("Deleted tunnel: {id}"))), ) } else { ( StatusCode::NOT_FOUND, - Json(ProxyResponse { - message: format!("Id not found: {id}"), - }), + Json(ProxyResponse::Message(format!("Id not found: {id}"))), ) } } + Command::Status => ( + StatusCode::OK, + Json(ProxyResponse::Status { + tunnels: state + .proxies + .lock() + .unwrap() + .iter() + .map(|(key, value)| (*key, (value.incoming_port, value.destination))) + .collect(), + }), + ), } } diff --git a/status.sh b/status.sh new file mode 100755 index 0000000..792ba4f --- /dev/null +++ b/status.sh @@ -0,0 +1,5 @@ +curl --header "Content-Type: application/json" \ + --data '{ + "status": null + }' \ + http://localhost:14000/command