web3-proxy/web3_proxy/src/frontend/http_proxy.rs

87 lines
2.8 KiB
Rust
Raw Normal View History

2022-08-06 04:17:25 +03:00
use axum::extract::Path;
2022-08-11 03:16:13 +03:00
use axum::response::Response;
2022-06-05 22:58:47 +03:00
use axum::{http::StatusCode, response::IntoResponse, Extension, Json};
2022-07-07 06:22:09 +03:00
use axum_client_ip::ClientIp;
2022-06-05 22:58:47 +03:00
use std::sync::Arc;
2022-08-06 04:17:25 +03:00
use uuid::Uuid;
2022-06-05 22:58:47 +03:00
2022-08-11 04:53:27 +03:00
use super::errors::anyhow_error_into_response;
use super::rate_limit::RateLimitResult;
use crate::stats::{Protocol, ProxyRequestLabels};
2022-06-16 05:53:37 +03:00
use crate::{app::Web3ProxyApp, jsonrpc::JsonRpcRequestEnum};
2022-08-05 22:22:23 +03:00
pub async fn public_proxy_web3_rpc(
2022-07-07 06:22:09 +03:00
Json(payload): Json<JsonRpcRequestEnum>,
Extension(app): Extension<Arc<Web3ProxyApp>>,
ClientIp(ip): ClientIp,
2022-08-11 03:16:13 +03:00
) -> Response {
2022-08-11 04:53:27 +03:00
let _ip = match app.rate_limit_by_ip(ip).await {
Ok(x) => match x.try_into_response().await {
Ok(RateLimitResult::AllowedIp(x)) => x,
Err(err_response) => return err_response,
_ => unimplemented!(),
},
Err(err) => return anyhow_error_into_response(None, None, err).into_response(),
};
2022-08-04 04:10:27 +03:00
let user_id = 0;
let protocol = Protocol::HTTP;
match &payload {
JsonRpcRequestEnum::Batch(batch) => {
// TODO: use inc_by if possible?
for single in batch {
let rpc_method = single.method.clone();
let _count = app
.stats
.proxy_requests
.get_or_create(&ProxyRequestLabels {
protocol: protocol.clone(),
rpc_method,
user_id,
})
.inc();
}
}
JsonRpcRequestEnum::Single(single) => {
let rpc_method = single.method.clone();
let _count = app
.stats
.proxy_requests
.get_or_create(&ProxyRequestLabels {
protocol,
rpc_method,
user_id,
})
.inc();
}
};
match app.proxy_web3_rpc(payload, user_id).await {
2022-08-04 04:10:27 +03:00
Ok(response) => (StatusCode::OK, Json(&response)).into_response(),
2022-08-11 04:53:27 +03:00
Err(err) => anyhow_error_into_response(None, None, err).into_response(),
2022-08-04 04:10:27 +03:00
}
}
2022-07-07 06:22:09 +03:00
2022-08-04 04:10:27 +03:00
pub async fn user_proxy_web3_rpc(
Json(payload): Json<JsonRpcRequestEnum>,
Extension(app): Extension<Arc<Web3ProxyApp>>,
2022-08-06 04:17:25 +03:00
Path(user_key): Path<Uuid>,
2022-08-11 03:16:13 +03:00
) -> Response {
let user_id = match app.rate_limit_by_key(user_key).await {
2022-08-11 04:53:27 +03:00
Ok(x) => match x.try_into_response().await {
Ok(RateLimitResult::AllowedUser(x)) => x,
Err(err_response) => return err_response,
_ => unimplemented!(),
},
Err(err) => return anyhow_error_into_response(None, None, err).into_response(),
};
2022-07-07 06:22:09 +03:00
match app.proxy_web3_rpc(payload, user_id).await {
2022-06-05 22:58:47 +03:00
Ok(response) => (StatusCode::OK, Json(&response)).into_response(),
2022-08-11 04:53:27 +03:00
Err(err) => anyhow_error_into_response(None, None, err),
2022-06-05 22:58:47 +03:00
}
}