Compare commits
6 Commits
2987221c93
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 55a868eef3 | |||
|
|
20d9ce0f45 | ||
|
|
0fb49cbfd0 | ||
|
|
b5e30e467b | ||
|
|
55ef1a788f | ||
|
|
f60f5350ef |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -1182,12 +1182,12 @@ dependencies = [
|
|||||||
name = "middleman"
|
name = "middleman"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"base64",
|
||||||
"minio",
|
"minio",
|
||||||
"rocket",
|
"rocket",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
"url",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -7,6 +7,6 @@ edition = "2021"
|
|||||||
rocket = {path = "../Rocket/core/lib"}
|
rocket = {path = "../Rocket/core/lib"}
|
||||||
tokio = { version = "1.40.0", features = ["full"] }
|
tokio = { version = "1.40.0", features = ["full"] }
|
||||||
tracing = "0.1.40"
|
tracing = "0.1.40"
|
||||||
tracing-subscriber = "0.3.18"
|
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
|
||||||
url = "2.5.2"
|
|
||||||
minio = {git="https://github.com/minio/minio-rs.git", rev = "c28f576"}
|
minio = {git="https://github.com/minio/minio-rs.git", rev = "c28f576"}
|
||||||
|
base64 = "0.22.1"
|
||||||
|
|||||||
44
browser/background.js
Normal file
44
browser/background.js
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
function handle_header(req) {
|
||||||
|
// Headers
|
||||||
|
// let x = req.requestHeaders;
|
||||||
|
// if (!x) {
|
||||||
|
x = new Headers()
|
||||||
|
// }
|
||||||
|
x.append("og", old)
|
||||||
|
|
||||||
|
console.log(x)
|
||||||
|
|
||||||
|
// appending header of the original url so that relative urls can be built into
|
||||||
|
// absolute urls
|
||||||
|
// TODO This doesn't work yet
|
||||||
|
return {
|
||||||
|
requestHeaders: x,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let old = ""
|
||||||
|
|
||||||
|
function url_redirect (req) {
|
||||||
|
|
||||||
|
let url = new URL(req.url);
|
||||||
|
let path = url.pathname;
|
||||||
|
let domain = url.hostname;
|
||||||
|
|
||||||
|
let b64 = btoa(path)
|
||||||
|
if (!req.url.startsWith("http://localhost")) {
|
||||||
|
old = req.url
|
||||||
|
// console.debug(`Loading: ${req.url} || ${b64}`);
|
||||||
|
return {
|
||||||
|
redirectUrl: `http://localhost:4433/s3/${domain}/${b64}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
browser.webRequest.onBeforeSendHeaders.addListener(
|
||||||
|
handle_header, { urls: ["<all_urls>"] }, ["blocking", "requestHeaders"]
|
||||||
|
);
|
||||||
|
|
||||||
|
browser.webRequest.onBeforeRequest.addListener(
|
||||||
|
url_redirect, { urls: ["<all_urls>"] }, ["blocking"]
|
||||||
|
)
|
||||||
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
||||||
<title></title>
|
|
||||||
<meta name="description" content="">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Now click "load" in the extension window</h1>
|
|
||||||
<div id="anchor" hidden></div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -4,8 +4,14 @@
|
|||||||
"version": "0.1",
|
"version": "0.1",
|
||||||
"description": "Adds a red border to all webpages matching mozilla.org.",
|
"description": "Adds a red border to all webpages matching mozilla.org.",
|
||||||
"permissions": [
|
"permissions": [
|
||||||
"activeTab"
|
"activeTab",
|
||||||
|
"webRequest",
|
||||||
|
"webRequestBlocking",
|
||||||
|
"<all_urls>"
|
||||||
],
|
],
|
||||||
|
"background": {
|
||||||
|
"scripts": ["background.js"]
|
||||||
|
},
|
||||||
"browser_action": {
|
"browser_action": {
|
||||||
"default_icon": "icon.png",
|
"default_icon": "icon.png",
|
||||||
"default_title": "Click me!",
|
"default_title": "Click me!",
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
|
|
||||||
let anchor = document.getElementById('anchor');
|
|
||||||
if (anchor) {
|
|
||||||
window.fetch(anchor.innerText)
|
|
||||||
.then(res => res.text())
|
|
||||||
.then((body) => {
|
|
||||||
document.body.textContent = "";
|
|
||||||
document.body.innerHTML = body;
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -5,10 +5,9 @@
|
|||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<button id="newtab">Open canvas</button>
|
<button id="">On</button>
|
||||||
<button id="replacer">Load page</button>
|
<button id="">Off</button>
|
||||||
<p id="output"></p>
|
<p id="output"></p>
|
||||||
<script src="popup.js"></script>
|
|
||||||
</body>
|
</body>
|
||||||
<style>
|
<style>
|
||||||
html {
|
html {
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
const output = document.getElementById('output');
|
|
||||||
let page_url = "http://127.0.0.1:4433/s3/style.css";
|
|
||||||
|
|
||||||
document.getElementById("newtab").addEventListener('click', async function(e) {
|
|
||||||
browser.tabs.create({ url: "/main/main.html" }).then((t) => {
|
|
||||||
if (t.id) {
|
|
||||||
output.innerText += t.id
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
document.getElementById("replacer").addEventListener('click', async function(e) {
|
|
||||||
browser.tabs.executeScript({ code: injection })
|
|
||||||
browser.tabs.executeScript({ file: "inject.js" });
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
const injection = `
|
|
||||||
let head = document.getElementById('anchor');
|
|
||||||
head.innerText = "${page_url}";
|
|
||||||
`
|
|
||||||
101
src/main.rs
101
src/main.rs
@@ -1,7 +1,7 @@
|
|||||||
use rocket::{fairing::{Fairing, Info, Kind}, fs::FileServer, get, http::Header, routes, Request, Response, State};
|
use rocket::{fairing::{Fairing, Info, Kind}, fs::FileServer, futures::io::Cursor, get, http::{ContentType, CookieJar, Header, HeaderMap, Status}, outcome::Outcome, request::{self, FromRequest}, response::content, routes, Error, Request, Response, State};
|
||||||
use s3::S3;
|
use s3::S3;
|
||||||
use tracing::{info, Level};
|
use tracing::{debug, info, trace, Level};
|
||||||
use url::Url;
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
mod s3;
|
mod s3;
|
||||||
|
|
||||||
@@ -17,57 +17,92 @@ async fn main() {
|
|||||||
tracing_subscriber::fmt()
|
tracing_subscriber::fmt()
|
||||||
.with_line_number(false)
|
.with_line_number(false)
|
||||||
.without_time()
|
.without_time()
|
||||||
.with_max_level(Level::INFO)
|
.with_env_filter(EnvFilter::from_default_env())
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
let config = Config {
|
let config = Config {
|
||||||
s3_bucket: "v1.10",
|
s3_bucket: "b64v2",
|
||||||
s3_url: "http://localhost:9000",
|
s3_url: "http://localhost:9000",
|
||||||
s3_access_key: "8UO76z8wCs9DnpxSbQUY",
|
s3_access_key: "Eyp21VVclXP8xx49bJ6D",
|
||||||
s3_secret_key: "xwKVMpf2jzgprsdo85Dvo74UmO84y0aRrAUorYY5",
|
s3_secret_key: "VxqU9hQ2gl7TvHIFzCM74Og6rQUZcpgYpHNUBnVm",
|
||||||
};
|
};
|
||||||
|
|
||||||
let s3 = S3::connect(&config).await.expect("Failed to connect to minio, aborting.");
|
let s3 = S3::connect(&config).await.expect("Failed to connect to minio, aborting.");
|
||||||
let cors = CORS;
|
// let cors = CORS;
|
||||||
|
|
||||||
let _ = rocket::build()
|
let _ = rocket::build()
|
||||||
.mount("/", FileServer::new("www/"))
|
.mount("/", FileServer::new("www/"))
|
||||||
.mount("/", routes![get_s3_content])
|
.mount("/", routes![get_s3_content])
|
||||||
.manage(s3)
|
.manage(s3)
|
||||||
.attach(cors)
|
// .attach(cors)
|
||||||
.launch()
|
.launch()
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
info!("Bye");
|
info!("Bye");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/s3/<path>")]
|
struct Og {
|
||||||
async fn get_s3_content(path: &str, db: &State<S3>) -> String {
|
og: Option<String>
|
||||||
info!(path);
|
}
|
||||||
// TODO this is just pseudo-code
|
#[rocket::async_trait]
|
||||||
let url = "en.wikipedia.org/wiki/CNBC";
|
impl<'r> FromRequest<'r> for Og{
|
||||||
if let Some(resp) = db.get(&url).await {
|
type Error = Error;
|
||||||
return resp
|
|
||||||
|
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
|
||||||
|
if let Some(h) = req.headers().get("og-domain").next() {
|
||||||
|
debug!(h);
|
||||||
|
return Outcome::Success(Og { og: Some(h.to_string()) });
|
||||||
|
}
|
||||||
|
Outcome::Success(Og { og: None })
|
||||||
}
|
}
|
||||||
"Hello world.".to_owned()
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[get("/s3/<domain>/<file>")]
|
||||||
|
async fn get_s3_content(domain: &str, file: &str, db: &State<S3>, og: Og) -> (Status, (ContentType, String)) {
|
||||||
|
use base64::prelude::*;
|
||||||
|
let filename = domain.to_owned() + "/" + file;
|
||||||
|
|
||||||
|
info!("{:?}", og.og);
|
||||||
|
|
||||||
|
if let Some(resp) = db.get(&filename).await {
|
||||||
|
let content_type = if let Ok(url) = BASE64_URL_SAFE.decode(file) {
|
||||||
|
let url = url.into_iter().map(|f| f as char).collect::<String>();
|
||||||
|
if let Some(filetype) = url.split('.').collect::<Vec<&str>>().last() {
|
||||||
|
ContentType::from_extension(&filetype).unwrap_or(ContentType::HTML)
|
||||||
|
} else {
|
||||||
|
ContentType::HTML
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ContentType::Any
|
||||||
|
};
|
||||||
|
trace!("{:?}", content_type);
|
||||||
|
|
||||||
|
return (Status::Ok, (content_type, resp))
|
||||||
|
}
|
||||||
|
// instead of some/none I would rather this be 200/404
|
||||||
|
// TODO should tell surreal to get this url next.
|
||||||
|
(Status::NotFound, (ContentType::Text, "Resource not found in offline cache".to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// CORS, to allow other sites to request this from their front-end
|
// CORS, to allow other sites to request this from their front-end
|
||||||
pub struct CORS;
|
// pub struct CORS;
|
||||||
|
|
||||||
#[rocket::async_trait]
|
// #[rocket::async_trait]
|
||||||
impl Fairing for CORS {
|
// impl Fairing for CORS {
|
||||||
fn info(&self) -> Info {
|
// fn info(&self) -> Info {
|
||||||
Info {
|
// Info {
|
||||||
name: "Add CORS headers to responses",
|
// name: "Add CORS headers to responses",
|
||||||
kind: Kind::Response
|
// kind: Kind::Response
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
|
// async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
|
||||||
response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
|
// response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
|
||||||
response.set_header(Header::new("Access-Control-Allow-Methods", "POST, GET, PATCH, OPTIONS"));
|
// response.set_header(Header::new("Access-Control-Allow-Methods", "POST, GET, PATCH, OPTIONS"));
|
||||||
response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
|
// response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
|
||||||
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
|
// response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
|
||||||
}
|
// // TODO set this automatically like how Rocket/core/lib/src/fs/server.rs does (bottom of file)
|
||||||
}
|
// response.set_header(Header::new("Content-Type", "text/html"));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|||||||
46
src/s3.rs
46
src/s3.rs
@@ -1,8 +1,13 @@
|
|||||||
use minio::s3::{
|
use minio::s3::{
|
||||||
args::{BucketExistsArgs, MakeBucketArgs}, client::ClientBuilder, creds::StaticProvider, error::Error, http::BaseUrl, types::S3Api, Client
|
args::{BucketExistsArgs, MakeBucketArgs},
|
||||||
|
client::ClientBuilder,
|
||||||
|
creds::StaticProvider,
|
||||||
|
error::Error,
|
||||||
|
http::BaseUrl,
|
||||||
|
types::S3Api,
|
||||||
|
Client,
|
||||||
};
|
};
|
||||||
use tracing::{instrument, trace};
|
use tracing::{error, instrument, trace, warn};
|
||||||
use url::Url;
|
|
||||||
|
|
||||||
use crate::Config;
|
use crate::Config;
|
||||||
|
|
||||||
@@ -35,24 +40,45 @@ impl S3 {
|
|||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
trace!("Connection successfull");
|
trace!("Connection successful");
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
bucket_name: config.s3_bucket.to_owned(),
|
bucket_name: config.s3_bucket.to_owned(),
|
||||||
client: client,
|
client: client,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get(&self, filename: &str) -> Option<String> {
|
pub async fn get(&self, filename: &str) -> Option<String> {
|
||||||
let data = self
|
match self
|
||||||
.client
|
.client
|
||||||
.get_object(&self.bucket_name, &filename)
|
.get_object(&self.bucket_name, &filename)
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
{
|
||||||
|
Ok(data) => {
|
||||||
if let Ok(segments )= data.content.to_segmented_bytes().await {
|
if let Ok(segments) = data.content.to_segmented_bytes().await {
|
||||||
return Some(segments.to_bytes().iter().map(|c| *c as char).collect::<String>())
|
return Some(
|
||||||
|
segments
|
||||||
|
.to_bytes()
|
||||||
|
.iter()
|
||||||
|
.map(|c| *c as char)
|
||||||
|
.collect::<String>(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
match e {
|
||||||
|
Error::S3Error(error_response) => {
|
||||||
|
if error_response.code == "NoSuchKey" {
|
||||||
|
warn!("Resource not found")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
let error = e.to_string();
|
||||||
|
error!("S3 Error: {error}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user