forked from shihaam/bml-proxy
fix: add Content-Type
This commit is contained in:
parent
4142aa289c
commit
699c3c07b1
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -45,6 +45,7 @@ dependencies = [
|
|||||||
"bytes",
|
"bytes",
|
||||||
"curl",
|
"curl",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"unicase",
|
||||||
"warp",
|
"warp",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -7,4 +7,5 @@ edition = "2021"
|
|||||||
bytes = "1.2.1"
|
bytes = "1.2.1"
|
||||||
curl = "0.4.44"
|
curl = "0.4.44"
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
unicase = "2.6.0"
|
||||||
warp = "0.3"
|
warp = "0.3"
|
||||||
|
34
src/main.rs
34
src/main.rs
@ -1,16 +1,16 @@
|
|||||||
#![feature(slice_pattern)]
|
|
||||||
|
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
use core::slice::SlicePattern;
|
use curl::easy::{Easy, List};
|
||||||
use curl::easy::Easy;
|
|
||||||
use std::env::var;
|
use std::env::var;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
use std::{error::Error, net::SocketAddr};
|
use std::{error::Error, net::SocketAddr};
|
||||||
use utils::QueryParameters;
|
use unicase::Ascii;
|
||||||
|
use warp::hyper::HeaderMap;
|
||||||
use warp::{http::Method, hyper::body::Bytes, path::FullPath, Filter};
|
use warp::{http::Method, hyper::body::Bytes, path::FullPath, Filter};
|
||||||
|
|
||||||
|
const FOR_COPY: [&'static str; 1] = ["Content-Type"];
|
||||||
|
|
||||||
const BASE: &str = "https://www.bankofmaldives.com.mv/internetbanking/api/";
|
const BASE: &str = "https://www.bankofmaldives.com.mv/internetbanking/api/";
|
||||||
const ERR: &str = "{ code: 407, message: \"Proxy failed\" }";
|
const ERR: &str = "{ code: 407, message: \"Proxy failed\" }";
|
||||||
|
|
||||||
@ -24,15 +24,29 @@ fn get_base_url() -> String {
|
|||||||
fn get_proxied_body(
|
fn get_proxied_body(
|
||||||
uri: &String,
|
uri: &String,
|
||||||
method: &Method,
|
method: &Method,
|
||||||
|
headers: &HeaderMap,
|
||||||
body: &Bytes,
|
body: &Bytes,
|
||||||
base: &String,
|
base: &String,
|
||||||
) -> Result<String, Box<dyn Error>> {
|
) -> Result<String, Box<dyn Error>> {
|
||||||
let mut handle = Easy::new();
|
let mut handle = Easy::new();
|
||||||
handle.url(format!("{}{}", base, uri).as_str())?;
|
handle.url(format!("{}{}", base, uri).as_str())?;
|
||||||
|
|
||||||
|
let mut custom_headers = List::new();
|
||||||
|
for (name, value) in headers.iter() {
|
||||||
|
if FOR_COPY.iter().any(|v| Ascii::new(v) == &name) {
|
||||||
|
match value.to_str() {
|
||||||
|
Ok(val) => custom_headers.append(format!("{}: {}", name.as_str(), val).as_str())?,
|
||||||
|
Err(_) => (),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
handle.http_headers(custom_headers)?;
|
||||||
|
|
||||||
|
let body = std::str::from_utf8(body)?;
|
||||||
|
let mut body_bytes = body.as_bytes();
|
||||||
|
|
||||||
if method == Method::POST {
|
if method == Method::POST {
|
||||||
handle.post(true)?;
|
handle.post_field_size(body_bytes.len() as u64)?;
|
||||||
handle.post_field_size(body.len() as u64)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
@ -41,7 +55,7 @@ fn get_proxied_body(
|
|||||||
|
|
||||||
if method == Method::POST {
|
if method == Method::POST {
|
||||||
transfer.read_function(|into| {
|
transfer.read_function(|into| {
|
||||||
let read = body.as_slice().read(into).unwrap_or(0);
|
let read = body_bytes.read(into).unwrap_or(0);
|
||||||
Ok(read)
|
Ok(read)
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
@ -62,7 +76,7 @@ fn get_proxied_body(
|
|||||||
|
|
||||||
fn handler(
|
fn handler(
|
||||||
uri: FullPath,
|
uri: FullPath,
|
||||||
_: QueryParameters,
|
headers: HeaderMap,
|
||||||
method: Method,
|
method: Method,
|
||||||
body: Bytes,
|
body: Bytes,
|
||||||
ip: Option<SocketAddr>,
|
ip: Option<SocketAddr>,
|
||||||
@ -76,7 +90,7 @@ fn handler(
|
|||||||
|
|
||||||
println!("({}) {}", ip_str, uri);
|
println!("({}) {}", ip_str, uri);
|
||||||
|
|
||||||
let proxy_response = get_proxied_body(&uri, &method, &body, &base);
|
let proxy_response = get_proxied_body(&uri, &method, &headers, &body, &base);
|
||||||
|
|
||||||
let res = match proxy_response {
|
let res = match proxy_response {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
|
18
src/utils.rs
18
src/utils.rs
@ -1,19 +1,17 @@
|
|||||||
use warp::{http::Method, hyper::body::Bytes, path::FullPath, Filter};
|
use warp::{
|
||||||
|
http::Method,
|
||||||
|
hyper::{body::Bytes, HeaderMap},
|
||||||
|
path::FullPath,
|
||||||
|
Filter,
|
||||||
|
};
|
||||||
|
|
||||||
pub type QueryParameters = Option<String>;
|
pub type QueryParameters = Option<String>;
|
||||||
pub type Request = (FullPath, QueryParameters, Method, Bytes);
|
pub type Request = (FullPath, HeaderMap, Method, Bytes);
|
||||||
|
|
||||||
pub fn query_params_filter(
|
|
||||||
) -> impl Filter<Extract = (QueryParameters,), Error = std::convert::Infallible> + Clone {
|
|
||||||
warp::query::raw()
|
|
||||||
.map(Some)
|
|
||||||
.or_else(|_| async { Ok::<(QueryParameters,), std::convert::Infallible>((None,)) })
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn extract_request_data_filter(
|
pub fn extract_request_data_filter(
|
||||||
) -> impl Filter<Extract = Request, Error = warp::Rejection> + Clone {
|
) -> impl Filter<Extract = Request, Error = warp::Rejection> + Clone {
|
||||||
warp::path::full()
|
warp::path::full()
|
||||||
.and(query_params_filter())
|
.and(warp::header::headers_cloned())
|
||||||
.and(warp::method())
|
.and(warp::method())
|
||||||
.and(warp::body::bytes())
|
.and(warp::body::bytes())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user