Merge pull request #11 from GreenPenguino/status-command

Add status command
This commit is contained in:
Erik van Bennekum 2023-04-26 13:29:14 +02:00 committed by GitHub
commit ddc838f2f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 31 deletions

View File

@ -7,4 +7,4 @@ curl --header "Content-Type: application/json" \
"id": "67e55044-10b1-426f-9247-bb680e5fe0c8" "id": "67e55044-10b1-426f-9247-bb680e5fe0c8"
} }
}' \ }' \
http://localhost:3000/command http://localhost:14000/command

View File

@ -6,4 +6,4 @@ curl --header "Content-Type: application/json" \
"id": "67e55044-10b1-426f-9247-bb680e5fe0c8" "id": "67e55044-10b1-426f-9247-bb680e5fe0c8"
} }
}' \ }' \
http://localhost:3000/command http://localhost:14000/command

View File

@ -6,4 +6,4 @@ curl --header "Content-Type: application/json" \
"id": "67e55044-10b1-426f-9247-bb680e5fe0c8" "id": "67e55044-10b1-426f-9247-bb680e5fe0c8"
} }
}' \ }' \
http://localhost:3000/command http://localhost:14000/command

View File

@ -49,11 +49,15 @@ enum Command {
Delete { Delete {
id: Uuid, id: Uuid,
}, },
Status,
} }
#[derive(Serialize)] #[derive(Serialize)]
pub struct ProxyResponse { pub enum ProxyResponse {
message: String, Message(String),
Status {
tunnels: HashMap<Uuid, (u16, SocketAddr)>,
},
} }
#[derive(Debug)] #[derive(Debug)]
@ -95,9 +99,7 @@ pub async fn process_command(
if !payload.verify_signature(&state.verifying_key) { if !payload.verify_signature(&state.verifying_key) {
return ( return (
StatusCode::UNAUTHORIZED, StatusCode::UNAUTHORIZED,
Json(ProxyResponse { Json(ProxyResponse::Message("Invalid signature".to_string())),
message: "Invalid signature".to_string(),
}),
); );
} }
match payload.command { match payload.command {
@ -111,17 +113,17 @@ pub async fn process_command(
if state.proxies.lock().unwrap().get(&id).is_some() { if state.proxies.lock().unwrap().get(&id).is_some() {
return ( return (
StatusCode::CONFLICT, StatusCode::CONFLICT,
Json(ProxyResponse { Json(ProxyResponse::Message(
message: "Id already exists. Use the modify command instead.".to_string(), "Id already exists. Use the modify command instead.".to_string(),
}), )),
); );
} }
if !state.ports.write().unwrap().insert(incoming_port) { if !state.ports.write().unwrap().insert(incoming_port) {
return ( return (
StatusCode::CONFLICT, StatusCode::CONFLICT,
Json(ProxyResponse { Json(ProxyResponse::Message(format!(
message: format!("The `incoming_port` already in use: {incoming_port}"), "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?? add_proxy(incoming_port, rx).await.unwrap(); // TODO: error propagation??
( (
StatusCode::ACCEPTED, StatusCode::ACCEPTED,
Json(ProxyResponse { Json(ProxyResponse ::
message: format!( Message( format!(
"Created tunnel {id} on port {incoming_port} to use {destination_ip}:{destination_port}" "Created tunnel {id} on port {incoming_port} to use {destination_ip}:{destination_port}"
), ),
}), )),
) )
} }
Command::Modify { Command::Modify {
@ -161,18 +163,14 @@ pub async fn process_command(
.unwrap(); .unwrap();
( (
StatusCode::ACCEPTED, StatusCode::ACCEPTED,
Json(ProxyResponse { Json(ProxyResponse::Message(format!(
message: format!( "Changed tunnel {id} to use {destination_ip}:{destination_port}"
"Changed tunnel {id} to use {destination_ip}:{destination_port}" ))),
),
}),
) )
} else { } else {
( (
StatusCode::NOT_FOUND, StatusCode::NOT_FOUND,
Json(ProxyResponse { Json(ProxyResponse::Message(format!("Id not found: {id}"))),
message: format!("Id not found: {id}"),
}),
) )
} }
} }
@ -182,19 +180,27 @@ pub async fn process_command(
state.ports.write().unwrap().remove(&proxy.incoming_port); state.ports.write().unwrap().remove(&proxy.incoming_port);
( (
StatusCode::ACCEPTED, StatusCode::ACCEPTED,
Json(ProxyResponse { Json(ProxyResponse::Message(format!("Deleted tunnel: {id}"))),
message: format!("Deleted tunnel: {id}"),
}),
) )
} else { } else {
( (
StatusCode::NOT_FOUND, StatusCode::NOT_FOUND,
Json(ProxyResponse { Json(ProxyResponse::Message(format!("Id not found: {id}"))),
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(),
}),
),
} }
} }

5
status.sh Executable file
View File

@ -0,0 +1,5 @@
curl --header "Content-Type: application/json" \
--data '{
"status": null
}' \
http://localhost:14000/command