{"id":22350671,"url":"https://github.com/iamsauravsharma/tower_allowed_hosts","last_synced_at":"2025-03-26T11:27:41.899Z","repository":{"id":186737247,"uuid":"675691191","full_name":"iamsauravsharma/tower_allowed_hosts","owner":"iamsauravsharma","description":"Tower service which limit host to only allowed domains","archived":false,"fork":false,"pushed_at":"2024-03-14T06:17:55.000Z","size":1415,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-03-15T06:46:34.559Z","etag":null,"topics":["allowedhosts","hacktoberfest","rust","tower"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iamsauravsharma.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-07T13:55:56.000Z","updated_at":"2023-09-30T03:24:33.000Z","dependencies_parsed_at":"2024-01-23T10:05:47.367Z","dependency_job_id":"9324379f-31fa-456e-9f4b-cf985416dc2b","html_url":"https://github.com/iamsauravsharma/tower_allowed_hosts","commit_stats":null,"previous_names":["iamsauravsharma/tower_allowed_hosts"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsauravsharma%2Ftower_allowed_hosts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsauravsharma%2Ftower_allowed_hosts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsauravsharma%2Ftower_allowed_hosts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsauravsharma%2Ftower_allowed_hosts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iamsauravsharma","download_url":"https://codeload.github.com/iamsauravsharma/tower_allowed_hosts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245643473,"owners_count":20649042,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["allowedhosts","hacktoberfest","rust","tower"],"created_at":"2024-12-04T11:12:45.527Z","updated_at":"2025-03-26T11:27:41.876Z","avatar_url":"https://github.com/iamsauravsharma.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TOWER ALLOWED HOSTS\n\n**Project status \u0026 info:**\n\n|                    License                     |              Crates Version               |                 Docs                 |\n| :--------------------------------------------: | :---------------------------------------: | :----------------------------------: |\n| [![License: MIT][license_badge]][license_link] | [![Crate][cratesio_badge]][cratesio_link] | [![Docs][docsrs_badge]][docsrs_link] |\n\nTower service which limits request from only specified hosts.\n\n## Add as dependencies\n\nIn your `Cargo.toml` file, add `tower_allowed_hosts` as a dependency:\n\n```toml\n[dependencies]\ntower_allowed_hosts = \"0.9.0\"\n```\n\n# Usage\n\n### Basic\n\nTo restrict access to specific basic hosts, you can use the following code:\n\n```rust\nlet tower_layer = tower_allowed_hosts::AllowedHostLayer::default()\n    .extend_hosts(\u0026[\"127.0.0.1\".to_string()]);\n```\n\n### Wildcard\n\nIf you need wildcard-based host matching, enable the `wildcard` feature in your `Cargo.toml`:\n\n```toml\n[dependencies]\ntower_allowed_hosts = { version = \"0.9.0\", features = [\"wildcard\"] }\n```\n\nYou can then restrict hosts using wildcards:\n\n```rust\nlet tower_layer = tower_allowed_hosts::AllowedHostLayer::default()\n    .extend_hosts(\u0026[wildmatch::WildMatch::new(\"127.0.0.*\")]);\n```\n\n### Regex\n\nIf you need regex-based host matching, enable the `regex` feature in your `Cargo.toml`:\n\n```toml\n[dependencies]\ntower_allowed_hosts = { version = \"0.9.0\", features = [\"regex\"] }\n```\n\nYou can then restrict hosts using regex patterns:\n\n```rust\nlet tower_layer = tower_allowed_hosts::AllowedHostLayer::new(\u0026[regex::Regex::new(\"^127.0.0.1$\")?]);\n```\n\n# Integrating with a Tower-Compatible Library\n\nAfter creating the `AllowedHostLayer`, it can be integrated into any library that supports `tower` components. Here's an example of how to use this layer in an `axum` application. You will also need to handle errors properly using `HandleErrorLayer`:\n\n```rust\nuse axum::{\n    error_handling::HandleErrorLayer,\n    http::StatusCode,\n    Router\n};\nuse tower::ServiceBuilder;\nuse tower_allowed_hosts::AllowedHostLayer;\n\nfn router() -\u003e Router {\n    let handle_error_layer = HandleErrorLayer::new(handle_box_error);\n\n    let allowed_hosts_layer = AllowedHostLayer::default()\n        .extend_hosts(\u0026[wildmatch::WildMatch::new(\"127.0.0.*\")]);\n\n     let layer = ServiceBuilder::new()\n        .layer(handle_error_layer)\n        .layer(allowed_hosts_layer);\n\n    Router::new().layer(layer)\n}\n\nasync fn handle_box_error(err: tower::BoxError) -\u003e (StatusCode, String) {\n    if err.is::\u003ctower_allowed_hosts::error::Error\u003e() {\n        return (StatusCode::BAD_REQUEST, err.to_string());\n    }\n    return (StatusCode::INTERNAL_SERVER_ERROR, \"\".to_string())\n}\n```\n\nThere is also extension added after successfully parsing allowed host and allowing host which can be access using\n`tower_allowed_hosts::Host` struct Extension\n\n[license_badge]: https://img.shields.io/github/license/iamsauravsharma/tower_allowed_hosts.svg?style=for-the-badge\n[license_link]: LICENSE\n[cratesio_badge]: https://img.shields.io/crates/v/tower_allowed_hosts.svg?style=for-the-badge\n[cratesio_link]: https://crates.io/crates/tower_allowed_hosts\n[docsrs_badge]: https://img.shields.io/docsrs/tower_allowed_hosts/latest?style=for-the-badge\n[docsrs_link]: https://docs.rs/tower_allowed_hosts\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamsauravsharma%2Ftower_allowed_hosts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiamsauravsharma%2Ftower_allowed_hosts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamsauravsharma%2Ftower_allowed_hosts/lists"}