This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async fn tunnel(mut upgraded: Upgraded, addr: String) -> std::io::Result<()> { | |
| let mut server = TcpStream::connect(addr).await?; | |
| let (from_client, from_server) = | |
| tokio::io::copy_bidirectional(&mut upgraded, &mut server).await?; | |
| tracing::debug!( | |
| "client wrote {} bytes and received {} bytes", | |
| from_client, | |
| from_server |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async fn proxy(req: Request<Body>) -> Result<Response, hyper::Error> { | |
| tracing::trace!(?req); | |
| if let Some(host_addr) = req.uri().authority().map(|auth| auth.to_string()) { | |
| if check_address_block(&host_addr) == true { | |
| println!("This site is blocked") | |
| } else { | |
| tokio::task::spawn(async move { | |
| match hyper::upgrade::on(req).await { | |
| Ok(upgraded) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub fn check_address_block(address_to_check: &str) -> bool { | |
| let addresses_blocked = read_file_lines_to_vec(&"./blacklist.txt".to_string()); | |
| let addresses_blocked_iter: Vec<String> = match addresses_blocked { | |
| Ok(vector) => vector, | |
| Err(_) => vec!["Error".to_string()] | |
| }; | |
| let address_in = addresses_blocked_iter.contains(&address_to_check.to_string()); | |
| return address_in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::fs; | |
| use std::io::BufReader; | |
| use std::io::BufRead; | |
| use std::io; | |
| pub fn read_file_lines_to_vec(filename: &str) -> io::Result<Vec<String>> { | |
| let file_in = fs::File::open(filename)?; | |
| let file_reader = BufReader::new(file_in); | |
| Ok(file_reader.lines().filter_map(io::Result::ok).collect()) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| use tower_http::trace::{self, TraceLayer}; | |
| use tracing::Level; | |
| #[tokio::main] | |
| async fn main() { | |
| tracing_subscriber::registry() | |
| .with(tracing_subscriber::fmt::layer()) | |
| .init(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use axum::{ | |
| body::{self, Body}, | |
| http::{Method, Request, StatusCode}, | |
| response::{IntoResponse, Response}, | |
| routing::get, | |
| Router, | |
| }; | |
| use hyper::upgrade::Upgraded; | |
| use std::net::SocketAddr; | |
| use tokio::net::TcpStream; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "net/http" | |
| "github.com/gin-gonic/gin" | |
| "github.com/prometheus/client_golang/prometheus" | |
| "github.com/prometheus/client_golang/prometheus/promhttp" | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async fn track_metrics<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse { | |
| let start = Instant::now(); | |
| let path = if let Some(matched_path) = req.extensions().get::<MatchedPath>() { | |
| matched_path.as_str().to_owned() | |
| } else { | |
| req.uri().path().to_owned() | |
| }; | |
| let method = req.method().clone(); | |
| let response = next.run(req).await; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn setup_metrics_recorder() -> PrometheusHandle { | |
| const EXPONENTIAL_SECONDS: &[f64] = &[ | |
| 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, | |
| ]; | |
| PrometheusBuilder::new() | |
| .set_buckets_for_metric( | |
| Matcher::Full("http_requests_duration_seconds".to_string()), | |
| EXPONENTIAL_SECONDS, | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async fn start_metrics_server() { | |
| let app = metrics_app(); | |
| // NOTE: expose metrics endpoint on a different port | |
| let addr = SocketAddr::from(([127, 0, 0, 1], 3001)); | |
| tracing::debug!("listening on {}", addr); | |
| axum::Server::bind(&addr) | |
| .serve(app.into_make_service()) | |
| .await | |
| .unwrap() |
NewerOlder