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

121 lines
3.7 KiB
Rust
Raw Normal View History

2022-06-05 22:58:47 +03:00
/// this should move into web3-proxy once the basics are working
mod errors;
mod http;
mod http_proxy;
2022-07-14 00:49:57 +03:00
mod users;
2022-06-05 22:58:47 +03:00
mod ws_proxy;
2022-07-07 06:22:09 +03:00
2022-06-05 22:58:47 +03:00
use axum::{
handler::Handler,
2022-08-04 04:10:27 +03:00
response::IntoResponse,
2022-06-05 22:58:47 +03:00
routing::{get, post},
Extension, Router,
};
2022-08-05 22:22:23 +03:00
use entities::user_keys;
2022-08-04 04:10:27 +03:00
use reqwest::StatusCode;
2022-08-05 22:22:23 +03:00
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter, QuerySelect};
2022-08-04 04:10:27 +03:00
use std::net::{IpAddr, SocketAddr};
2022-06-05 22:58:47 +03:00
use std::sync::Arc;
2022-06-29 21:26:52 +03:00
use tracing::debug;
2022-06-15 04:02:26 +03:00
2022-06-05 22:58:47 +03:00
use crate::app::Web3ProxyApp;
2022-08-04 04:10:27 +03:00
use self::errors::handle_anyhow_error;
pub async fn rate_limit_by_ip(app: &Web3ProxyApp, ip: &IpAddr) -> Result<(), impl IntoResponse> {
let rate_limiter_key = format!("ip:{}", ip);
rate_limit_by_key(app, &rate_limiter_key).await
}
2022-08-05 22:22:23 +03:00
/// if Ok(()), rate limits are acceptable
/// if Err(response), rate limits exceeded
2022-08-04 04:10:27 +03:00
pub async fn rate_limit_by_key(
app: &Web3ProxyApp,
user_key: &str,
) -> Result<(), impl IntoResponse> {
let db = app.db_conn();
2022-08-05 22:22:23 +03:00
// query the db to make sure this key is active
// TODO: probably want a cache on this
match user_keys::Entity::find()
.select_only()
.column(user_keys::Column::UserId)
2022-08-05 22:22:23 +03:00
.filter(user_keys::Column::ApiKey.eq(user_key))
.filter(user_keys::Column::Active.eq(true))
.one(db)
.await
{
Ok(Some(_)) => {
// user key is valid
}
Ok(None) => {
// invalid user key
// TODO: rate limit by ip here, too? maybe tarpit?
return Err(handle_anyhow_error(
Some(StatusCode::FORBIDDEN),
None,
anyhow::anyhow!("unknown api key"),
)
.await
.into_response());
}
Err(e) => {
return Err(handle_anyhow_error(
Some(StatusCode::INTERNAL_SERVER_ERROR),
None,
e.into(),
)
.await
.into_response());
}
}
2022-08-04 04:10:27 +03:00
if let Some(rate_limiter) = app.rate_limiter() {
if rate_limiter.throttle_key(user_key).await.is_err() {
// TODO: set headers so they know when they can retry
// warn!(?ip, "public rate limit exceeded");
// TODO: use their id if possible
return Err(handle_anyhow_error(
Some(StatusCode::TOO_MANY_REQUESTS),
None,
anyhow::anyhow!("too many requests"),
)
.await
.into_response());
}
} else {
// TODO: if no redis, rate limit with a local cache?
}
Ok(())
}
2022-06-05 22:58:47 +03:00
pub async fn run(port: u16, proxy_app: Arc<Web3ProxyApp>) -> anyhow::Result<()> {
// build our application with a route
2022-07-26 08:01:08 +03:00
// order most to least common
2022-06-05 22:58:47 +03:00
let app = Router::new()
2022-08-05 22:22:23 +03:00
.route("/", post(http_proxy::public_proxy_web3_rpc))
.route("/", get(ws_proxy::public_websocket_handler))
.route("/u/:key", post(http_proxy::user_proxy_web3_rpc))
.route("/u/:key", get(ws_proxy::user_websocket_handler))
2022-06-29 21:22:53 +03:00
.route("/health", get(http::health))
2022-06-05 22:58:47 +03:00
.route("/status", get(http::status))
2022-07-14 00:49:57 +03:00
.route("/users", post(users::create_user))
2022-06-05 22:58:47 +03:00
.layer(Extension(proxy_app));
// 404 for any unknown routes
let app = app.fallback(errors::handler_404.into_service());
// run our app with hyper
// `axum::Server` is a re-export of `hyper::Server`
2022-08-05 22:22:23 +03:00
// TODO: allow only listening on localhost?
2022-06-05 22:58:47 +03:00
let addr = SocketAddr::from(([0, 0, 0, 0], port));
2022-06-29 21:26:52 +03:00
debug!("listening on port {}", port);
2022-07-07 06:22:09 +03:00
// TODO: into_make_service is enough if we always run behind a proxy. make into_make_service_with_connect_info optional?
2022-06-05 22:58:47 +03:00
axum::Server::bind(&addr)
2022-07-07 06:22:09 +03:00
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
2022-06-05 22:58:47 +03:00
.await
.map_err(Into::into)
}