forked from shihaam/bml-proxy
feat: add info text and env var support
This commit is contained in:
parent
60cbb78354
commit
4142aa289c
52
src/main.rs
52
src/main.rs
@ -4,6 +4,7 @@ pub mod utils;
|
|||||||
|
|
||||||
use core::slice::SlicePattern;
|
use core::slice::SlicePattern;
|
||||||
use curl::easy::Easy;
|
use curl::easy::Easy;
|
||||||
|
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};
|
||||||
@ -13,9 +14,21 @@ use warp::{http::Method, hyper::body::Bytes, path::FullPath, Filter};
|
|||||||
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\" }";
|
||||||
|
|
||||||
fn get_proxied_body(uri: &String, method: &Method, body: &Bytes) -> Result<String, Box<dyn Error>> {
|
fn get_base_url() -> String {
|
||||||
|
match var("BML_PROXY_BASE") {
|
||||||
|
Ok(base) => base,
|
||||||
|
Err(_) => BASE.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_proxied_body(
|
||||||
|
uri: &String,
|
||||||
|
method: &Method,
|
||||||
|
body: &Bytes,
|
||||||
|
base: &String,
|
||||||
|
) -> 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())?;
|
||||||
|
|
||||||
if method == Method::POST {
|
if method == Method::POST {
|
||||||
handle.post(true)?;
|
handle.post(true)?;
|
||||||
@ -53,6 +66,7 @@ fn handler(
|
|||||||
method: Method,
|
method: Method,
|
||||||
body: Bytes,
|
body: Bytes,
|
||||||
ip: Option<SocketAddr>,
|
ip: Option<SocketAddr>,
|
||||||
|
base: String,
|
||||||
) -> String {
|
) -> String {
|
||||||
let uri = uri.as_str().clone().to_string();
|
let uri = uri.as_str().clone().to_string();
|
||||||
let ip_str = match ip {
|
let ip_str = match ip {
|
||||||
@ -62,7 +76,7 @@ fn handler(
|
|||||||
|
|
||||||
println!("({}) {}", ip_str, uri);
|
println!("({}) {}", ip_str, uri);
|
||||||
|
|
||||||
let proxy_response = get_proxied_body(&uri, &method, &body);
|
let proxy_response = get_proxied_body(&uri, &method, &body, &base);
|
||||||
|
|
||||||
let res = match proxy_response {
|
let res = match proxy_response {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
@ -80,6 +94,36 @@ async fn main() {
|
|||||||
let proxy_handler = warp::any()
|
let proxy_handler = warp::any()
|
||||||
.and(utils::extract_request_data_filter())
|
.and(utils::extract_request_data_filter())
|
||||||
.and(warp::filters::addr::remote())
|
.and(warp::filters::addr::remote())
|
||||||
|
.and(warp::any().map(move || get_base_url()))
|
||||||
.map(handler);
|
.map(handler);
|
||||||
warp::serve(proxy_handler).run(([0, 0, 0, 0], 3030)).await;
|
|
||||||
|
let log_warning = || {
|
||||||
|
println!("This binary is meant to be used with LD_PRELOAD=\"/path/to/libcurl-impersonate-chrome.so\"");
|
||||||
|
println!("If you're on Arch Linux, this library can be obtained via the AUR package 'libcurl-impersonate-bin'");
|
||||||
|
println!(
|
||||||
|
"A common location for this library is '/usr/local/lib/libcurl-impersonate-chrome.so'"
|
||||||
|
);
|
||||||
|
println!("\n[WARN] proceeding without libcurl-impersonate, expect cloudflare blockages\n");
|
||||||
|
};
|
||||||
|
|
||||||
|
match var("LD_PRELOAD") {
|
||||||
|
Ok(v) => {
|
||||||
|
if !v.contains("libcurl-impersonate-chrome.so") {
|
||||||
|
log_warning();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(_) => log_warning(),
|
||||||
|
}
|
||||||
|
|
||||||
|
let port = var("BML_PROXY_PORT")
|
||||||
|
.unwrap_or_default()
|
||||||
|
.parse::<u16>()
|
||||||
|
.unwrap_or(3030);
|
||||||
|
|
||||||
|
println!("Proxy running on port: {}", port);
|
||||||
|
println!("Proxy base URL: {}", get_base_url());
|
||||||
|
println!("Available environment variables for modification: BML_PROXY_PORT, BML_PROXY_BASE\n");
|
||||||
|
|
||||||
|
warp::serve(proxy_handler).run(([0, 0, 0, 0], port)).await;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user