1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
use http::uri::{Parts, Uri}; use hyper::header::HeaderValue; use hyper::{Body, Request, StatusCode}; use regex::Regex; use crate::proxy::error::MiddlewareError; use crate::proxy::middleware::MiddlewareResult::Next; use crate::proxy::middleware::{Middleware, MiddlewareResult}; use crate::proxy::service::{ServiceContext, State}; use serde_json; #[derive(Clone)] pub struct Router { routes: RouterRules, name: String, } #[derive(Debug, Clone, Deserialize)] pub struct RouteRegex { #[serde(with = "serde_regex")] pub host: Regex, #[serde(with = "serde_regex")] pub path: Regex, } #[derive(Debug, Clone, Deserialize)] pub struct Route { pub from: RouteRegex, pub to: RouteRegex, pub public: bool, } #[derive(Debug, Clone, Deserialize)] pub struct RouterRulesWrapper { pub rules: RouterRules, } pub type RouterRules = Vec<Route>; #[derive(Serialize, Deserialize, Debug)] pub struct MatchedRoute { pub uri: String, pub public: bool, } pub trait RouterConfig { fn get_router_filename(&self) -> &str; } fn get_host_and_path(req: &mut Request<Body>) -> Result<(String, String), MiddlewareError> { let uri = req.uri(); let path = uri .path_and_query() .map(ToString::to_string) .unwrap_or_else(|| String::from("")); match uri.host() { Some(host) => Ok((String::from(host), path)), None => Ok(( String::from(req.headers().get("host").unwrap().to_str()?), path, )), } } fn inject_new_uri( req: &mut Request<Body>, old_host: &str, host: &str, path: &str, ) -> Result<(), MiddlewareError> { { let headers = req.headers_mut(); headers.insert("X-Forwarded-Host", HeaderValue::from_str(old_host).unwrap()); headers.insert("host", HeaderValue::from_str(host).unwrap()); } let mut parts = Parts::default(); parts.scheme = Some("http".parse()?); parts.authority = Some(host.parse()?); parts.path_and_query = Some(path.parse()?); debug!("Found a route to {:?}", parts); *req.uri_mut() = Uri::from_parts(parts)?; Ok(()) } impl Middleware for Router { fn name() -> String { String::from("Router") } fn before_request( &mut self, req: &mut Request<Body>, context: &ServiceContext, state: &State, ) -> Result<MiddlewareResult, MiddlewareError> { let routes = &self.routes; let (host, path) = get_host_and_path(req)?; debug!("Routing => Host: {} Path: {}", host, path); for route in routes { let (re_host, re_path) = (&route.from.host, &route.from.path); let to = &route.to; let public = route.public; debug!("Trying to convert from {} / {:?}", &re_host, &re_path); if re_host.is_match(&host) { let new_host = re_host.replace(&host, to.host.as_str()); let new_path = if re_path.is_match(&path) { re_path.replace(&path, to.path.as_str()) } else { continue; }; debug!("Proxying to {}", &new_host); inject_new_uri(req, &host, &new_host, &new_path)?; self.set_state( context.req_id, state, serde_json::to_string(&MatchedRoute { uri: req.uri().to_string(), public, })?, )?; return Ok(Next); } } Err(MiddlewareError::new( String::from("No route matched"), Some(String::from("Not found")), StatusCode::NOT_FOUND, )) } } fn read_routes(config: &RouterConfig) -> RouterRules { use std::fs::File; use std::io::prelude::Read; let mut f = File::open(config.get_router_filename()).expect("Router config not found !"); let mut data = String::new(); f.read_to_string(&mut data) .expect("Cannot read Router config !"); let rules: RouterRulesWrapper = serde_json::from_str(&data).expect("Cannot parse Router config file !"); rules.rules } impl Router { pub fn new(config: &RouterConfig) -> Self { Router { routes: read_routes(config), name: String::from("Router"), } } }