Merge pull request #9 from RaspberryProgramming/V0.1.3

V0.1.3
This commit is contained in:
Camerin Figueroa 2024-07-19 11:21:48 -04:00 committed by GitHub
commit 99eca430df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 246 additions and 87 deletions

2
Cargo.lock generated
View File

@ -1277,7 +1277,7 @@ checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
[[package]] [[package]]
name = "request-mirror" name = "request-mirror"
version = "0.1.2" version = "0.1.3"
dependencies = [ dependencies = [
"chrono", "chrono",
"diesel", "diesel",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "request-mirror" name = "request-mirror"
version = "0.1.2" version = "0.1.3"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -6,9 +6,8 @@ This application provides a web ui for sending get/post requests and provides a
![Build and Test Rust](https://github.com/RaspberryProgramming/request-mirror/actions/workflows/rust.yml/badge.svg) ![Build and Test Rust](https://github.com/RaspberryProgramming/request-mirror/actions/workflows/rust.yml/badge.svg)
## TODO: ## TODO:
- Update Readme
- Document N/a
- Develop Pipelines
## Docker ## Docker
@ -51,6 +50,71 @@ Then push the image
docker push raspberrypi99/request-mirror:latest docker push raspberrypi99/request-mirror:latest
``` ```
## Deploying to azure ## Development Environment
TODO During development, you'll want to use a few tools to help work on this project.
First, you'll want to install docker.
For environments with a GUI: https://docs.docker.com/desktop/
For environments without a GUI: https://docs.docker.com/engine/install/
You can use docker for many things. This project has a set of files for creating and deploying as a docker container. In order to start development, you'll need a postgres container. By default in the .env file, the rust code will attempt to connect a localhost instance of postgres.
Use the following command to start a postgres container.
```bash
docker run --name postgres -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD=password -e POSTGRES_USER=postgres -e POSTGRES_DB=request_mirror_db -d postgres
```
This will start up an instance of postgres to only localhost. Remote computers can't connect to the database. This setup does not use any volumes, meaning that when the container is removed, the data will be gone. If you'd like to add volumes, you can run the following command instead which maps a new postgres-data volume to /var/lib/postgresql/data.
```bash
docker run --name postgres -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD=password -e POSTGRES_USER=postgres -e POSTGRES_DB=request_mirror_db -v postgres-data:/var/lib/postgresql/data -d postgres:latest
```
Now that postgres is running, you can now install rust if you haven't already. Follow the instructions on the following site [rustup.rs](https://rustup.rs/)
Follow the instructions for installing the stable toolchain for rust. You may need to log in and log out after installation.
**Notice**: Older versions of request mirror used rocket v0.4.x which required the nightly toolchain, please update your repo/fork.
Next, we can install [diesel](https://diesel.rs/), an ORM and query builder for rust. This is how we deploy tables to our database. Install diesel cli:
```bash
cargo install diesel_cli --no-default-features --features postgres
```
You can deploy the database with the following command:
**Notice**: If you are not adding a volume to your postgres database, you may need to re-run this step each time you create the postgres docker container.
```bash
diesel migration run
```
Now that the database is ready and rust is installed, we can move onto running the project.
```bash
cargo run
```
You can also run the following to run a release binary
```bash
cargo run --release
```
### Build Docker Image and Run Locally
You can build the docker image by running the following comand
```bash
docker build . -t raspberrypi99/request-mirror
```
Next, you can run the project using the following command. This can be run even with the development postgres container running. This will open port 80 for the user to connect to.
```bash
docker compose up -d
```

View File

@ -3,6 +3,7 @@ pub mod schema;
use chrono::offset::Utc; use chrono::offset::Utc;
use diesel::prelude::*; use diesel::prelude::*;
use rocket::http::CookieJar;
use std::env; use std::env;
use dotenvy::dotenv; use dotenvy::dotenv;
use models::{ use models::{
@ -129,3 +130,12 @@ pub fn ownership_exists(client_id: &str, owner_id: &str, connection: &mut PgConn
ownerships.len() > 0 ownerships.len() > 0
} }
/// Looks for a cookie in the cookiejar. If it is a pending cookie, the pending will be returned as a String.
/// This accounts for if this client hasn't connected before and is making its first request
pub fn get_cookie(key: &str, cookies: &CookieJar<'_>) -> String {
match cookies.get_pending(key) {
Some(v) => v.value().to_string(),
None => cookies.get(key).unwrap().value().to_string()
}
}

View File

@ -152,30 +152,27 @@ fn index(_info: RequestInfo) -> Template {
#[get("/test")] #[get("/test")]
fn test_get(request: RequestInfo, cookies: &CookieJar<'_>) -> Template { fn test_get(request: RequestInfo, cookies: &CookieJar<'_>) -> Template {
let client_id = cookies.get("mirror-id"); let client_id = get_cookie("mirror-id", cookies);
// If the cookie exists, create new database records for this request // create new database records for this request
if client_id.is_some() { let connection = &mut establish_connection();
let connection = &mut establish_connection();
// Create new history record and retrieve new history_id // Create new history record and retrieve new history_id
let history_id = create_history_record(connection, client_id.unwrap().value(), "Get"); let history_id = create_history_record(connection, &client_id, "Get");
// Create header pair records // Create header pair records
for row in &request.header { for row in &request.header {
create_pair_record(connection, history_id, PairType::Header, &row.key, &row.value); create_pair_record(connection, history_id, PairType::Header, &row.key, &row.value);
} }
// Create cookie pair records
for row in &request.cookies {
create_pair_record(connection, history_id, PairType::Cookie, &row.key, &row.value);
}
// Create query pair records
for row in &request.query {
create_pair_record(connection, history_id, PairType::Query, &row.key, &row.value);
}
// Create cookie pair records
for row in &request.cookies {
create_pair_record(connection, history_id, PairType::Cookie, &row.key, &row.value);
}
// Create query pair records
for row in &request.query {
create_pair_record(connection, history_id, PairType::Query, &row.key, &row.value);
} }
// Define context for the template // Define context for the template
@ -204,36 +201,33 @@ fn test_get(request: RequestInfo, cookies: &CookieJar<'_>) -> Template {
#[post("/test", data = "<body>")] #[post("/test", data = "<body>")]
fn test_post(body: RequestBody, request: RequestInfo, cookies: &CookieJar<'_>) -> Template { fn test_post(body: RequestBody, request: RequestInfo, cookies: &CookieJar<'_>) -> Template {
let client_id = cookies.get("mirror-id"); let client_id = get_cookie("mirror-id", cookies);
// If the cookie exists, create new database records for this request // Create new database records for this request
if client_id.is_some() { let connection = &mut establish_connection();
let connection = &mut establish_connection();
// Create new history record and retrieve new history_id
// Create new history record and retrieve new history_id let history_id = create_history_record(connection, &client_id, "Post");
let history_id = create_history_record(connection, client_id.unwrap().value(), "Post");
// Create header pair records
// Create header pair records for row in &request.header {
for row in &request.header { create_pair_record(connection, history_id, PairType::Header, &row.key, &row.value);
create_pair_record(connection, history_id, PairType::Header, &row.key, &row.value);
}
// Create cookie pair records
for row in &request.cookies {
create_pair_record(connection, history_id, PairType::Cookie, &row.key, &row.value);
}
// Create query pair records
for row in &request.query {
create_pair_record(connection, history_id, PairType::Query, &row.key, &row.value);
}
// Create a pair record for body of the request
println!("Creating body Records for {history_id}");
create_pair_record(connection, history_id, PairType::Body, "body", &body.0.clone());
} }
// Create cookie pair records
for row in &request.cookies {
create_pair_record(connection, history_id, PairType::Cookie, &row.key, &row.value);
}
// Create query pair records
for row in &request.query {
create_pair_record(connection, history_id, PairType::Query, &row.key, &row.value);
}
// Create a pair record for body of the request
println!("Creating body Records for {history_id}");
create_pair_record(connection, history_id, PairType::Body, "body", &body.0.clone());
// Define context for the template // Define context for the template
#[derive(Serialize)] #[derive(Serialize)]
struct Context<'a> { struct Context<'a> {
@ -260,10 +254,10 @@ fn test_post(body: RequestBody, request: RequestInfo, cookies: &CookieJar<'_>) -
/// Request function that returns a history of requests that the current client has made /// Request function that returns a history of requests that the current client has made
/// The user can click a history_id and view the request itself /// The user can click a history_id and view the request itself
#[get("/history")] #[get("/history")]
fn history_req(cookies: &CookieJar<'_>) -> Template { fn history_req(_info: RequestInfo, cookies: &CookieJar<'_>) -> Template {
// Get the client_id from the cookies // Get the client_id from the cookies
let client_id = cookies.get("mirror-id").unwrap().value(); let client_id = get_cookie("mirror-id", cookies);
let connection = &mut establish_connection(); let connection = &mut establish_connection();
@ -275,7 +269,7 @@ fn history_req(cookies: &CookieJar<'_>) -> Template {
.expect("Error loading clients"); .expect("Error loading clients");
// Get ownership relationships // Get ownership relationships
let ownerships: Vec<Ownership> = get_ownerships(client_id, connection); let ownerships: Vec<Ownership> = get_ownerships(&client_id, connection);
// Add any records that owned clients have // Add any records that owned clients have
for ownership in ownerships { for ownership in ownerships {
@ -325,14 +319,14 @@ fn history_req(cookies: &CookieJar<'_>) -> Template {
/// request that was recorded to the database. /// request that was recorded to the database.
/// This includes the body of a post request, headers, cookies and query parameters. /// This includes the body of a post request, headers, cookies and query parameters.
#[get("/history/<history_id>")] #[get("/history/<history_id>")]
fn history_details(history_id: i64, cookies: &CookieJar<'_>) -> Template { fn history_details(history_id: i64, _info: RequestInfo, cookies: &CookieJar<'_>) -> Template {
let client_id = cookies.get("mirror-id").unwrap().value(); let client_id = get_cookie("mirror-id", cookies);
let connection: &mut PgConnection = &mut establish_connection(); let connection: &mut PgConnection = &mut establish_connection();
// Get owned client ids // Get owned client ids
let owned_clients: Vec<String> = get_ownerships(client_id, connection) let owned_clients: Vec<String> = get_ownerships(&client_id, connection)
.iter() .iter()
.map(|x|x.client_id.to_string()) .map(|x|x.client_id.to_string())
.collect::<Vec<String>>(); .collect::<Vec<String>>();
@ -415,26 +409,33 @@ fn history_details(history_id: i64, cookies: &CookieJar<'_>) -> Template {
#[get("/ownership_registration")] #[get("/ownership_registration")]
fn ownership_registration(info: RequestInfo, cookies: &CookieJar<'_>) -> Template { fn ownership_registration(info: RequestInfo, cookies: &CookieJar<'_>) -> Template {
let client_id = cookies.get("mirror-id").unwrap().value().to_string(); let host_pair = info.header.iter().find(|&h| h.key == "host").unwrap();
let client_id = get_cookie("mirror-id", cookies);
let owner_id_pair = info.find_query_key("owner_id"); let owner_id_pair = info.find_query_key("owner_id");
let mut disp_owner_reg = false; let mut disp_owner_reg = false;
let mut failed_owner_reg = false;
let re = Regex::new(r"^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$").unwrap(); let re = Regex::new(r"^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$").unwrap();
let connection: &mut PgConnection = &mut establish_connection(); let connection: &mut PgConnection = &mut establish_connection();
let ownerships: Vec<Ownership> = get_ownerships(&client_id, connection); let ownerships: Vec<Ownership> = get_ownerships(&client_id, connection);
let owner_id = match owner_id_pair { let owner_id = match owner_id_pair {
Some(v) => v.value.to_string(), Some(v) => v.value.trim().to_string(),
None => "".to_string() None => "".to_string()
}; };
if if
owner_id_pair.is_some() && owner_id_pair.is_some()
{
if
re.is_match(&owner_id) && re.is_match(&owner_id) &&
owner_id != client_id && owner_id != client_id &&
!ownership_exists(&client_id, &owner_id, connection) !ownership_exists(&client_id, &owner_id, connection)
{ {
create_owner_record(connection, owner_id.clone(), client_id.clone()); create_owner_record(connection, owner_id.clone(), client_id.clone());
disp_owner_reg = true; disp_owner_reg = true;
} else {
failed_owner_reg = true;
}
} }
#[derive(Serialize)] #[derive(Serialize)]
@ -442,14 +443,18 @@ fn ownership_registration(info: RequestInfo, cookies: &CookieJar<'_>) -> Templat
client_id: String, client_id: String,
owner_id: String, owner_id: String,
disp_owner_reg: bool, disp_owner_reg: bool,
ownerships: Vec<Ownership> failed_owner_reg: bool,
ownerships: Vec<Ownership>,
host: String
} }
Template::render("ownership_registration", Context { Template::render("ownership_registration", Context {
client_id, client_id,
owner_id, owner_id,
disp_owner_reg, disp_owner_reg,
ownerships failed_owner_reg,
ownerships,
host: host_pair.value.to_string()
}) })
} }

View File

@ -19,7 +19,7 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent"> <div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0"> <ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item"> <li class="nav-item">
<a class="nav-link active" aria-current="page" href="/">Home</a> <a class="nav-link" aria-current="page" href="/">Home</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/test">Test Page</a> <a class="nav-link" href="/test">Test Page</a>

View File

@ -19,13 +19,13 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent"> <div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0"> <ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item"> <li class="nav-item">
<a class="nav-link active" aria-current="page" href="/">Home</a> <a class="nav-link" aria-current="page" href="/">Home</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/test">Test Page</a> <a class="nav-link" href="/test">Test Page</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/history">History</a> <a class="nav-link active" href="/history">History</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/ownership_registration">Ownership Registration</a> <a class="nav-link" href="/ownership_registration">Ownership Registration</a>

View File

@ -19,7 +19,7 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent"> <div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0"> <ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item"> <li class="nav-item">
<a class="nav-link active" aria-current="page" href="/">Home</a> <a class="nav-link" aria-current="page" href="/">Home</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/test">Test Page</a> <a class="nav-link" href="/test">Test Page</a>
@ -28,7 +28,7 @@
<a class="nav-link" href="/history">History</a> <a class="nav-link" href="/history">History</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/ownership_registration">Ownership Registration</a> <a class="nav-link active" href="/ownership_registration">Ownership Registration</a>
</li> </li>
</ul> </ul>
</div> </div>
@ -36,19 +36,27 @@
</nav> </nav>
<div id="body" class="p-5"> <div id="body" class="p-5">
<h3>Your Client ID: {{client_id}}</h3> <p>
<h5>
Your Client ID:
</h5>
<button id="client_id" class="border-0 bg-transparent" onclick="copy_content(event)" data-bs-toggle="tooltip"
data-bs-title="Click To Copy">
{{client_id}}
</button>
</p>
<p>
<h5>
Registration Link:
</h5>
<button id="reg_link" class="border-0 bg-transparent" onclick="copy_content(event)" data-bs-toggle="tooltip"
data-bs-title="Click To Copy">
{{host}}/ownership_registration?owner_id=937b0f08-a3af-457b-b65e-4ed858517588
</button>
</p>
<form target="/owner_registration"> <h5>Owned Clients</h5>
<h5>Ownership Registration</h5> <table class="table table-striped m-1">
<input type="text" name="owner_id">
</form>
{{#if disp_owner_reg}}
<p>Registered new owner {{owner_id}} for {{client_id}}</p>
{{/if}}
<h3>Owned Clients</h3>
<table>
<tr> <tr>
<th>Client ID</th> <th>Client ID</th>
</tr> </tr>
@ -60,10 +68,82 @@
</tr> </tr>
{{/each}} {{/each}}
</table> </table>
<form action="/ownership_registration" method="get">
<h5>Ownership Registration</h5>
<div class="p-1">
<h6>Instructions:</h6>
<p>
Copy the Client ID from the browser you'd like to see your requests. Paste it into the Owner ID Form and
submit.
</p>
<p>
This will allow the other client to access any requests you've made in the history page.
</p>
<p>
If you'd like to register a new owner on a client that doesn't have access to this html form, you can make the
request directly
by making an http/https request to the registration link above. Click it to copy to your clipboard.
</p>
</div>
<div class="mb-3 ms-1 me-1">
<label for="owner_id" class="form-label">Owner ID</label>
<input class="form-control" type="text" name="owner_id">
</div>
<div class="">
<button type="submit" class="btn btn-primary mb-3">Submit</button>
</div>
</form>
{{#if disp_owner_reg}}
<p>Registered new owner {{owner_id}} for {{client_id}}</p>
{{/if}}
{{#if failed_owner_reg}}
<p>Failed to register {{owner_id}} for {{client_id}}.</p>
<p>This may have ocurred because the owner_id does not exist, is already registered as an owner, or that it is the same as the client_id.</p>
{{/if}}
</div> </div>
<div class="toast-container position-fixed bottom-0 end-0 p-3">
<div id="clipboard_toast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<svg class="bd-placeholder-img rounded me-2" width="20" height="20" xmlns="http://www.w3.org/2000/svg"
aria-hidden="true" preserveAspectRatio="xMidYMid slice" focusable="false">
<rect width="100%" height="100%" fill="#007aff"></rect>
</svg>
<strong class="me-auto">Clipboard</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Copied to Clipboard.
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script> crossorigin="anonymous"></script>
<script>
function copy_content(event) {
// Get the text field
var copyText = document.getElementById(event.target.id);
// Copy the text inside the text field
navigator.clipboard.writeText(copyText.innerText);
const toastLiveExample = document.getElementById('clipboard_toast')
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastLiveExample)
toastBootstrap.show()
}
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl));
</script>
</body> </body>
</html> </html>

View File

@ -19,10 +19,10 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent"> <div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0"> <ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item"> <li class="nav-item">
<a class="nav-link active" aria-current="page" href="/">Home</a> <a class="nav-link" aria-current="page" href="/">Home</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/test">Test Page</a> <a class="nav-link active" href="/test">Test Page</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/history">History</a> <a class="nav-link" href="/history">History</a>