Skip to content

Commit

Permalink
feat(serverless): forward X-Real-Ip as X-Forwarded-For & X-Lagon-Regi…
Browse files Browse the repository at this point in the history
…on (#379)
  • Loading branch information
QuiiBz authored Dec 12, 2022
1 parent cf89165 commit d48f00c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-wombats-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/serverless': patch
---

Forward X-Real-Ip header to X-Forwarded-For
5 changes: 5 additions & 0 deletions .changeset/wise-cooks-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/serverless': patch
---

Forward X-Lagon-Region header
1 change: 0 additions & 1 deletion packages/runtime/tests/disallow_codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,3 @@ async fn disallow_function() {
RunResult::Error("Uncaught EvalError: Code generation from strings disallowed for this context, at:\n const result = new Function('return 1 + 1')".into())
);
}

11 changes: 10 additions & 1 deletion packages/serverless/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ mod logger;
lazy_static! {
pub static ref ISOLATES: RwLock<HashMap<usize, HashMap<String, Isolate>>> =
RwLock::new(HashMap::new());
static ref X_FORWARDED_FOR: String = String::from("X-Forwarded-For");
pub static ref REGION: String = env::var("LAGON_REGION").expect("LAGON_REGION must be set");
}

const POOL_SIZE: usize = 8;
const PAGE_404: &str = include_str!("../public/404.html");
const PAGE_502: &str = include_str!("../public/502.html");
const PAGE_500: &str = include_str!("../public/500.html");
const X_FORWARDED_FOR: &str = "x-forwarded-for";
const X_LAGON_REGION: &str = "x-lagon-region";
const X_REAL_IP: &str = "x-real-ip";

async fn handle_request(
req: HyperRequest<Body>,
Expand Down Expand Up @@ -156,7 +158,14 @@ async fn handle_request(
};

counter!("lagon_bytes_in", request.len() as u64, &thread_labels);

// Try to Extract the X-Real-Ip header or fallback to remote addr IP
let ip = request.headers
.as_ref()
.map_or(&ip, |headers| headers.get(X_REAL_IP).unwrap_or(&ip)).to_string();

request.add_header(X_FORWARDED_FOR.to_string(), ip);
request.add_header(X_LAGON_REGION.to_string(), REGION.to_string());

// Only acquire the lock when we are sure we have a
// deployment and that the isolate should be called.
Expand Down

4 comments on commit d48f00c

@vercel
Copy link

@vercel vercel bot commented on d48f00c Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./packages/docs

lagon-docs.vercel.app
docs.lagon.app
docs-git-main-lagon.vercel.app
docs-lagon.vercel.app

@vercel
Copy link

@vercel vercel bot commented on d48f00c Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

www – ./www

www-lagon.vercel.app
lagon.app
www-git-main-lagon.vercel.app

@vercel
Copy link

@vercel vercel bot commented on d48f00c Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

storybook – ./packages/ui

storybook-lagon.vercel.app
storybook-git-main-lagon.vercel.app
storybook-swart-eight.vercel.app
ui.lagon.app

@vercel
Copy link

@vercel vercel bot commented on d48f00c Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

dashboard – ./packages/dashboard

dashboard-git-main-lagon.vercel.app
dashboard-lagon.vercel.app
dash.lagon.app

Please sign in to comment.