From defda719f4dce1e64f7d813cdbc90bb680cd6465 Mon Sep 17 00:00:00 2001 From: bytedream Date: Wed, 4 May 2022 17:03:32 +0200 Subject: [PATCH] Add custom hosts and https only mode --- .example.env | 11 ++++--- README.md | 14 ++++++++ src/main.rs | 93 ++++++++++++++++++++++++++++++++++------------------ 3 files changed, 82 insertions(+), 36 deletions(-) diff --git a/.example.env b/.example.env index 58fb01f..cbca922 100644 --- a/.example.env +++ b/.example.env @@ -1,5 +1,8 @@ -HOST = 0.0.0.0 -PORT = 8080 +HOST=0.0.0.0 +PORT=8080 -ENABLE_REGEX = false -MAX_PATTER_LEN = 70 +ENABLE_REGEX=false +MAX_PATTER_LEN=70 + +HTTPS_ONLY=true +ENABLE_CUSTOM_HOSTS=false diff --git a/README.md b/README.md index 948944d..1e34406 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,20 @@ Default is `false`. Limits the maximal length the pattern can have. Default is `70`. +### `HTTPS_ONLY` + +If requests should always be made with https connections. Default is `true`. + +### `ENABLE_CUSTOM_HOSTS` + +If custom hosted instances of git servers should be supported too. Default is `false`. + +The url scheme of using a custom git server follows this: +
+https://example.com/custom/:host/:platform/:owner/:repository/:pattern
+
+**:host** is the url of the custom git server host. Subpaths are currently not supported. + ## Warnings It is recommended to limit the pattern length with [`MAX_PATTER_LEN`](#max_patter_len) if [`ENABLE_REGEX`](#enable_regex) is enabled since a too long pattern which is too complex could lead to an, wanted or unwanted, [ReDoS](https://en.wikipedia.org/wiki/ReDoS) attack. diff --git a/src/main.rs b/src/main.rs index d0f4d62..0b72aa4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use std::str::FromStr; use std::time::Duration; use actix_web::{App, dev, Error, get, http, HttpRequest, HttpResponse, HttpServer, Result, web}; use actix_web::client::{Client, Connector}; -use actix_web::error::{ErrorInternalServerError, ErrorNotFound, ErrorUriTooLong}; +use actix_web::error::{ErrorBadRequest, ErrorInternalServerError, ErrorNotFound, ErrorUriTooLong}; use actix_web::middleware::errhandlers::{ErrorHandlerResponse, ErrorHandlers}; use dotenv::dotenv; use lazy_static::lazy_static; @@ -15,13 +15,15 @@ use regex::{escape, Regex}; use serde::Deserialize; lazy_static! { - pub static ref ENABLE_REGEX: bool = env_lookup::("ENABLE_REGEX", false).unwrap(); - pub static ref MAX_PATTERN_LEN: i32 = env_lookup::("MAX_PATTER_LEN", 70).unwrap(); + static ref ENABLE_REGEX: bool = env_lookup("ENABLE_REGEX", false); + static ref MAX_PATTERN_LEN: i32 = env_lookup("MAX_PATTER_LEN", 70); - pub static ref TAG_PATTERN: Regex = Regex::new(r"(?P\d+)([.-_](?P\d+)([.-_](?P\d+))?([.-_]?(?P
[\w\d]+))?)?").unwrap();
-    pub static ref REPLACE_PATTERN: Regex = Regex::new(r"\{\w*?}").unwrap();
+    static ref ENABLE_CUSTOM_HOSTS: bool = env_lookup("ENABLE_CUSTOM_HOSTS", false);
 
-    pub static ref USER_AGENT: String = format!("smartrelease/{}", env_lookup::("CARGO_PKG_VERSION", "".to_string()).unwrap());
+    static ref TAG_PATTERN: Regex = Regex::new(r"(?P\d+)([.-_](?P\d+)([.-_](?P\d+))?([.-_]?(?P
[\w\d]+))?)?").unwrap();
+    static ref REPLACE_PATTERN: Regex = Regex::new(r"\{\w*?}").unwrap();
+
+    static ref USER_AGENT: String = format!("smartrelease/{}", env_lookup::("CARGO_PKG_VERSION", "".to_string()));
 }
 
 #[derive(Deserialize)]
@@ -58,19 +60,7 @@ async fn github(
     web::Path((user, repo, pattern)): web::Path<(String, String, String)>,
     query: web::Query
 ) -> Result {
-    if let Some(err) = pre_check(&pattern) {
-        return Err(err)
-    }
-
-    let mut res = client()
-        .get(format!("https://api.github.com/repos/{}/{}/releases/latest", user, repo))
-        .header("Accept", "application/vnd.github.v3+json")
-        .header("User-Agent", USER_AGENT.as_str())
-        .send()
-        .await?;
-    let mut github = res.json::().await?;
-
-    process(&pattern, &mut github.assets, query.into_inner(), &github.tag_name)
+    request_github(user.as_str(), repo.as_str(), pattern.as_str(), query).await
 }
 
 #[derive(Deserialize)]
@@ -84,15 +74,54 @@ async fn gitea(
     web::Path((user, repo, pattern)): web::Path<(String, String, String)>,
     query: web::Query
 ) -> Result {
+    request_gitea("gitea.com", user.as_str(), repo.as_str(), pattern.as_str(), query).await
+}
+
+#[get("/custom/{host}/{platform}/{user}/{repo}/{pattern}")]
+async fn custom(
+    web::Path((host, platform, user, repo, pattern)): web::Path<(String, String, String, String, String)>,
+    query: web::Query
+) -> Result {
+    if *ENABLE_CUSTOM_HOSTS {
+        match platform.as_str() {
+            "gitea" => request_gitea(host.as_str(), user.as_str(), repo.as_str(), pattern.as_str(), query).await,
+            _ => Err(ErrorBadRequest("Invalid host"))
+        }
+    } else {
+        Err(ErrorNotFound("Custom hosts are disabled"))
+    }
+}
+
+async fn request_github(user: &str, repo: &str, pattern: &str, query: web::Query) -> Result {
+    if let Some(err) = pre_check(pattern) {
+        return Err(err)
+    }
+
     let mut res = client()
-        .get(format!("https://gitea.com/api/v1/repos/{}/{}/releases?limit=1", user, repo))
+        .get(format!("https://api.github.com/repos/{}/{}/releases/latest", user, repo))
+        .header("Accept", "application/vnd.github.v3+json")
+        .header("User-Agent", USER_AGENT.as_str())
+        .send()
+        .await?;
+    let mut result = res.json::().await?;
+
+    process(&pattern, &mut result.assets, query.into_inner(), &result.tag_name)
+}
+
+async fn request_gitea(host: &str, user: &str, repo: &str, pattern: &str, query: web::Query) -> Result {
+    if let Some(err) = pre_check(pattern) {
+        return Err(err)
+    }
+
+    let mut res = client()
+        .get(format!("{}://{}/api/v1/repos/{}/{}/releases?limit=1", if env_lookup("HTTPS_ONLY", true) { "HTTPS" } else { "HTTP" }, host, user, repo))
         .header(http::header::CONTENT_TYPE, "application/json")
         .header(http::header::USER_AGENT, USER_AGENT.as_str())
         .send()
         .await?;
-    let mut gitea = res.json::<[Gitea; 1]>().await?;
+    let mut result = res.json::<[Gitea; 1]>().await?;
 
-    return process(&pattern, &mut gitea[0].assets, query.into_inner(), &gitea[0].tag_name)
+    return process(pattern, &mut result[0].assets, query.into_inner(), &result[0].tag_name )
 }
 
 fn redirect_error(res: dev::ServiceResponse) -> Result> {
@@ -102,8 +131,6 @@ fn redirect_error(res: dev::ServiceResponse) -> Result = res.request().uri().path().split("/").collect();
 
-
-
     info!("{} {}: got {} ({})",
         ip(res.request()),
         res.request().path(),
@@ -147,11 +174,12 @@ fn client() -> Client {
         .finish();
 }
 
-fn env_lookup(name: &str, default: F) -> std::result::Result {
+fn env_lookup(name: &str, default: F) -> F {
     if let Ok(envvar) = env::var(name) {
-        return envvar.parse::();
+        envvar.parse::().unwrap_or(default)
+    } else {
+        default
     }
-    Ok(default)
 }
 
 fn ip(request: &HttpRequest) -> String {
@@ -160,15 +188,15 @@ fn ip(request: &HttpRequest) -> String {
         .rsplit_once(":").unwrap().0.to_string()
 }
 
-fn pre_check(pattern: &String) -> Option {
+fn pre_check(pattern: &str) -> Option {
     // if MAX_PATTERN_LEN is -1 or below the len checking is disabled
-    if *MAX_PATTERN_LEN > -1 && REPLACE_PATTERN.replace_all(pattern.as_str(), "").len() > *MAX_PATTERN_LEN as usize {
+    if *MAX_PATTERN_LEN > -1 && REPLACE_PATTERN.replace_all(pattern, "").len() > *MAX_PATTERN_LEN as usize {
         return Some(ErrorUriTooLong(format!("Pattern / last url path must not exceed {} characters", *MAX_PATTERN_LEN)))
     }
     None
 }
 
-fn process(pattern: &String, assets: &mut Vec, query: Query, tag_name: &String) -> Result {
+fn process(pattern: &str, assets: &mut Vec, query: Query, tag_name: &String) -> Result {
     let re: Regex;
     let mut replaced = replace(
         pattern.to_string(),
@@ -253,13 +281,14 @@ async fn main() -> io::Result<()> {
         .filter_level(Info)
         .init();
 
-    let host = env_lookup::("HOST", "0.0.0.0".to_string()).unwrap();
-    let port = env_lookup::("PORT", 8080).unwrap();
+    let host = env_lookup::("HOST", "0.0.0.0".to_string());
+    let port = env_lookup::("PORT", 8080);
 
     let server = HttpServer::new(|| {
         App::new()
             .service(github)
             .service(gitea)
+            .service(custom)
             .service(
                 web::resource("/").route(web::get().to(|| async {
                     HttpResponse::Found()