2022-08-21 12:44:53 +03:00
|
|
|
use super::errors::FrontendResult;
|
2022-09-10 03:12:14 +03:00
|
|
|
use super::rate_limit::{rate_limit_by_ip, rate_limit_by_key};
|
2022-08-16 22:29:00 +03:00
|
|
|
use crate::{app::Web3ProxyApp, jsonrpc::JsonRpcRequestEnum};
|
2022-09-09 00:01:36 +03:00
|
|
|
use axum::extract::{Host, Path};
|
|
|
|
use axum::headers::{Referer, UserAgent};
|
|
|
|
use axum::TypedHeader;
|
2022-09-07 06:54:16 +03:00
|
|
|
use axum::{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-09-09 00:01:36 +03:00
|
|
|
use tracing::{debug_span, error_span, Instrument};
|
2022-08-06 04:17:25 +03:00
|
|
|
use uuid::Uuid;
|
2022-06-05 22:58:47 +03:00
|
|
|
|
2022-08-05 22:22:23 +03:00
|
|
|
pub async fn public_proxy_web3_rpc(
|
2022-07-07 06:22:09 +03:00
|
|
|
Extension(app): Extension<Arc<Web3ProxyApp>>,
|
2022-09-09 00:01:36 +03:00
|
|
|
Host(host): Host,
|
2022-07-07 06:22:09 +03:00
|
|
|
ClientIp(ip): ClientIp,
|
2022-09-09 00:01:36 +03:00
|
|
|
Json(payload): Json<JsonRpcRequestEnum>,
|
|
|
|
referer: Option<TypedHeader<Referer>>,
|
|
|
|
user_agent: Option<TypedHeader<UserAgent>>,
|
2022-08-21 12:39:38 +03:00
|
|
|
) -> FrontendResult {
|
2022-09-09 00:01:36 +03:00
|
|
|
let request_span = debug_span!("request", host, ?referer, ?user_agent);
|
2022-08-16 07:56:01 +03:00
|
|
|
|
2022-09-09 00:01:36 +03:00
|
|
|
let ip = rate_limit_by_ip(&app, ip)
|
|
|
|
.instrument(request_span.clone())
|
|
|
|
.await?;
|
2022-08-13 01:12:46 +03:00
|
|
|
|
2022-09-09 00:01:36 +03:00
|
|
|
let user_span = error_span!("ip", %ip);
|
2022-08-13 01:12:46 +03:00
|
|
|
|
2022-09-09 00:01:36 +03:00
|
|
|
let f = tokio::spawn(async move {
|
|
|
|
app.proxy_web3_rpc(payload)
|
|
|
|
.instrument(request_span)
|
|
|
|
.instrument(user_span)
|
|
|
|
.await
|
|
|
|
});
|
2022-08-16 03:33:26 +03:00
|
|
|
|
2022-09-09 00:01:36 +03:00
|
|
|
let response = f.await.unwrap()?;
|
2022-08-21 12:39:38 +03:00
|
|
|
|
2022-09-07 06:54:16 +03:00
|
|
|
Ok(Json(&response).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(
|
|
|
|
Extension(app): Extension<Arc<Web3ProxyApp>>,
|
2022-09-09 00:01:36 +03:00
|
|
|
Host(host): Host,
|
|
|
|
Json(payload): Json<JsonRpcRequestEnum>,
|
|
|
|
user_agent: Option<TypedHeader<UserAgent>>,
|
2022-08-06 04:17:25 +03:00
|
|
|
Path(user_key): Path<Uuid>,
|
2022-09-09 00:01:36 +03:00
|
|
|
referer: Option<TypedHeader<Referer>>,
|
2022-08-21 12:39:38 +03:00
|
|
|
) -> FrontendResult {
|
2022-09-09 00:01:36 +03:00
|
|
|
let request_span = debug_span!("request", host, ?referer, ?user_agent);
|
|
|
|
|
2022-09-10 03:12:14 +03:00
|
|
|
let user_id = rate_limit_by_key(&app, user_key)
|
2022-09-09 00:01:36 +03:00
|
|
|
.instrument(request_span.clone())
|
|
|
|
.await?;
|
2022-07-07 06:22:09 +03:00
|
|
|
|
2022-09-09 00:01:36 +03:00
|
|
|
let user_span = error_span!("user", user_id);
|
2022-08-16 07:56:01 +03:00
|
|
|
|
2022-09-09 00:01:36 +03:00
|
|
|
let f = tokio::spawn(async move {
|
|
|
|
app.proxy_web3_rpc(payload)
|
|
|
|
.instrument(request_span)
|
|
|
|
.instrument(user_span)
|
|
|
|
.await
|
|
|
|
});
|
2022-08-16 07:56:01 +03:00
|
|
|
|
2022-09-09 00:01:36 +03:00
|
|
|
let response = f.await.unwrap()?;
|
2022-08-21 12:39:38 +03:00
|
|
|
|
2022-09-07 06:54:16 +03:00
|
|
|
Ok(Json(&response).into_response())
|
2022-06-05 22:58:47 +03:00
|
|
|
}
|