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

Serve from other IPs #142

Merged
merged 8 commits into from
Jan 19, 2024
Merged

Conversation

vocksel
Copy link
Contributor

@vocksel vocksel commented Jan 15, 2024

This PR introduces a new address property to net.serve to change which IP address to serve off of

Usage:

-- Continues to function like normal and binds to loopback interface
net.serve(8080, function(request)
  -- ...
end)

-- Use the config object to serve on a different IP
net.serve(8080, {
  address = "http://0.0.0.0",
  handleRequest = function(request)
    -- ...
  end,
})

Resolves #120

Was confused why these tests were failing on my end. Turned out I had a service running on 8080 and had to kill it to get these tests to pass
Copy link
Collaborator

@filiptibell filiptibell left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! Left just one comment to resolve and this should be good to go.

Comment on lines 147 to 162
let address_pattern = Regex::new(r"(?:.*:\/\/)?(\d+\.\d+\.\d+\.\d+)(?::\d+)?").unwrap();

let address: Ipv4Addr = match &config.address {
Some(addr) => {
let addr_str = addr.to_str()?;

if let Some(caps) = address_pattern.captures(addr_str) {
caps[1].parse::<Ipv4Addr>()?
} else {
return Err(LuaError::runtime(format!(
"IP address format is incorrect (expected an IP in the form 'http://0.0.0.0' or '0.0.0.0', got '{addr_str}')"
)));
}
}
None => DEFAULT_IP_ADDRESS,
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like maybe regex is a bit overkill here, if all we're really doing is stripping out the http:// prefix we should be able to trim it down to something like

let addr_str = addr.to_str()?;
addr_str.trim_start_matches("http://").parse()?

and we leave the rest of the validation and error handling to the parse implementation that Ipv4Addr has. If you want to keep the nice descriptive error message maybe also .parse().map_err(|| LuaError::runtime(...))?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, I was hesitant to add regex here. We need to at least trim http://, https://, and the port :xxxx from the address. I opted for regex since at the time it was the easiest way to capture the IP.

Would it be worthwhile to consider trimming any other protocols? The current pattern could match things like ssh://, ftp://, etc. but I can't see those actually being used.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this is explicitly just an http server, I don't think we need to do any other protocols. I missed that port was included in that pattern - unsure about what to do about that, maybe we should just let the parse error there if port is given, since port must be given as net.serve first arg anyway?

@filiptibell
Copy link
Collaborator

Seems like the new test cases on Windows run forever, which is a bit strange..

@vocksel
Copy link
Contributor Author

vocksel commented Jan 16, 2024

Seems like the new test cases on Windows run forever, which is a bit strange..

I'm developing on macOS, so this will be a bit hard for me to test. Any suggestions on debugging that?

@filiptibell
Copy link
Collaborator

Seems like the new test cases on Windows run forever, which is a bit strange..

I'm developing on macOS, so this will be a bit hard for me to test. Any suggestions on debugging that?

I think manually calling process.exit(0) at the end of the test is an okay solution for now. Looks similar to other scheduler issues we've had with process not exiting from net.serve not shutting down, those just need to be fixed in general

Copy link
Collaborator

@filiptibell filiptibell left a comment

Choose a reason for hiding this comment

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

LGTM 👍 seems like the test on Windows still doesnt finish running but I'm positive it will be solved with some scheduler fixes that are in progress.

@filiptibell filiptibell merged commit 3c68d8e into lune-org:main Jan 19, 2024
4 of 5 checks passed
@vocksel vocksel deleted the serve-from-other-ips branch January 19, 2024 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

net.serve only binds to loopback interface
2 participants