Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

motis-proxy: Allow to configure the list allowed endpoints #26

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions motis-proxy/Rocket.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
[debug]
log_level = "debug"

[release.proxy]
proxy_assets = false
allowed_endpoints = [ "/intermodal","/guesser", "/address",
"/railviz/get_trains", "/railviz/get_trips", "/railviz/get_station",
"/lookup/schedule_info", "/gbfs/info", "/ppr/route", "/trip_to_connection" ]


[debug.proxy]
proxy_assets = true
motis_address = "https://europe.motis-project.de"
27 changes: 23 additions & 4 deletions motis-proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ fn default_motis_address() -> String {
fn default_proxy_assets() -> bool {
false
}
fn default_allowed_endpoints() -> Option<Vec<Endpoint>> {
None
}

#[derive(Deserialize, DefaultFromSerde)]
struct Config {
Expand All @@ -47,11 +50,16 @@ struct Config {
/// Proxy endpoints other than `/`. This should only ever be used for debugging.
/// It is slow and incomplete.
#[serde(default = "default_proxy_assets" )]
proxy_assets: bool
proxy_assets: bool,

/// List of endpoints (by path) that should be allowed through the proxy.
/// If this option is not set, all known endpoints will be allowed.
#[serde(default = "default_allowed_endpoints")]
allowed_endpoints: Option<Vec<Endpoint>>
}

#[derive(Deserialize, Serialize)]
enum AllowedEndpoints {
#[derive(Deserialize, Serialize, PartialEq, Eq)]
enum Endpoint {
#[serde(rename = "/intermodal")]
Intermodal,
#[serde(rename = "/guesser")]
Expand All @@ -77,7 +85,7 @@ enum AllowedEndpoints {
#[derive(Deserialize, Serialize)]
#[serde(tag = "type")]
enum RequestDestination {
Module { target: AllowedEndpoints },
Module { target: Endpoint },
}

#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -313,6 +321,17 @@ async fn proxy_api(
) -> ResultResponse<Custom<Json<serde_json::Value>>> {
let request = request.into_inner();

// Check if the requested endpoint is allowed
match &request.destination {
RequestDestination::Module { target } => {
if let Some(allowed_endpoints) = &config.allowed_endpoints {
if !allowed_endpoints.contains(&target) {
return Err(Custom(Status::UnprocessableEntity, ()))
}
}
}
}

trace!("MOTIS Request <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
trace!("{}", serde_json::to_string_pretty(&request).unwrap());

Expand Down
Loading