forked from shihaam/bml-proxy
init
This commit is contained in:
commit
1394301ffc
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
1182
Cargo.lock
generated
Normal file
1182
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
10
Cargo.toml
Normal file
10
Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "bml_proxy"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
bytes = "1.2.1"
|
||||
curl = "0.4.44"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
warp = "0.3"
|
BIN
libs/libcurl-impersonate-chrome.so
Executable file
BIN
libs/libcurl-impersonate-chrome.so
Executable file
Binary file not shown.
71
src/main.rs
Normal file
71
src/main.rs
Normal file
@ -0,0 +1,71 @@
|
||||
pub mod utils;
|
||||
|
||||
use curl::easy::Easy;
|
||||
use std::str::from_utf8;
|
||||
use std::{error::Error, net::SocketAddr};
|
||||
use utils::QueryParameters;
|
||||
use warp::{http::Method, hyper::body::Bytes, path::FullPath, Filter};
|
||||
|
||||
const BASE: &str = "https://www.bankofmaldives.com.mv/internetbanking/api/";
|
||||
const ERR: &str = "{ code: 407, message: \"Proxy failed\" }";
|
||||
|
||||
fn get_proxied_body(uri: &String) -> Result<String, Box<dyn Error>> {
|
||||
let mut handle = Easy::new();
|
||||
|
||||
handle.url(format!("{}{}", BASE, uri).as_str())?;
|
||||
|
||||
let mut buf = Vec::new();
|
||||
|
||||
{
|
||||
let mut transfer = handle.transfer();
|
||||
|
||||
transfer.write_function(|data| {
|
||||
buf.extend_from_slice(data);
|
||||
Ok(data.len())
|
||||
})?;
|
||||
|
||||
transfer.perform()?;
|
||||
}
|
||||
|
||||
let cloned_buf = buf.clone();
|
||||
let s = from_utf8(&cloned_buf)?;
|
||||
|
||||
Ok(s.to_string())
|
||||
}
|
||||
|
||||
fn handler(
|
||||
uri: FullPath,
|
||||
_: QueryParameters,
|
||||
_: Method,
|
||||
_: Bytes,
|
||||
ip: Option<SocketAddr>,
|
||||
) -> String {
|
||||
let uri = uri.as_str().clone().to_string();
|
||||
let ip_str = match ip {
|
||||
Some(v) => format!("{}", v),
|
||||
None => "unknown".to_string(),
|
||||
};
|
||||
|
||||
println!("({}) {}", ip_str, uri);
|
||||
|
||||
let proxy_response = get_proxied_body(&uri);
|
||||
|
||||
let res = match proxy_response {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
println!("error: {}", e);
|
||||
ERR.to_string()
|
||||
}
|
||||
};
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let proxy_handler = warp::any()
|
||||
.and(utils::extract_request_data_filter())
|
||||
.and(warp::filters::addr::remote())
|
||||
.map(handler);
|
||||
warp::serve(proxy_handler).run(([0, 0, 0, 0], 3030)).await;
|
||||
}
|
19
src/utils.rs
Normal file
19
src/utils.rs
Normal file
@ -0,0 +1,19 @@
|
||||
use warp::{http::Method, hyper::body::Bytes, path::FullPath, Filter};
|
||||
|
||||
pub type QueryParameters = Option<String>;
|
||||
pub type Request = (FullPath, QueryParameters, 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(
|
||||
) -> impl Filter<Extract = Request, Error = warp::Rejection> + Clone {
|
||||
warp::path::full()
|
||||
.and(query_params_filter())
|
||||
.and(warp::method())
|
||||
.and(warp::body::bytes())
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user