From 0e7ba839ed97f2fe80ab6954888b83d42d11a2e8 Mon Sep 17 00:00:00 2001 From: MSAA <27443298+4xMSAA@users.noreply.github.com> Date: Fri, 19 Mar 2021 04:41:31 +0200 Subject: [PATCH] change server bind address (#403) * web/mod.rs - change server bind address 127.0.0.1 is a loopback interface, and only works on the same host 0.0.0.0 will allow connections from other hosts ideally, this should be a console arg - but it's a quick fix * implement --address option, revert default bind address to 127.0.0.1 * revert silly autoformatting * ok, actually using rustfmt now * More precise --address flag description * Use SocketAddr where available, take advantage of const-ness * Display 'localhost' if address is loopback * Update Changelog Co-authored-by: Lucien Greathouse --- CHANGELOG.md | 3 +++ src/cli/mod.rs | 7 ++++++- src/cli/serve.rs | 18 ++++++++++++++---- src/web/mod.rs | 6 ++---- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c20c76076..c813e23c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Rojo Changelog ## Unreleased Changes +* Added `--address` flag to `rojo serve` to allow for external connections. ([#403][pr-403]) + +[pr-403]: https://github.com/rojo-rbx/rojo/pull/403 ## [7.0.0-alpha.2][7.0.0-alpha.2] (February 19, 2021) * Fixed incorrect protocol version between the client and server. diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 5bd69a1db..330ac46d3 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -12,6 +12,7 @@ use std::{ env, error::Error, fmt, + net::IpAddr, path::{Path, PathBuf}, str::FromStr, }; @@ -186,7 +187,11 @@ pub struct ServeCommand { #[structopt(default_value = "")] pub project: PathBuf, - /// The port to listen on. Defaults to the project's preference, or 34872 if + /// The IP address to listen on. Defaults to `127.0.0.1`. + #[structopt(long)] + pub address: Option, + + /// The port to listen on. Defaults to the project's preference, or `34872` if /// it has none. #[structopt(long)] pub port: Option, diff --git a/src/cli/serve.rs b/src/cli/serve.rs index 07ae85b35..bc2cc1aaf 100644 --- a/src/cli/serve.rs +++ b/src/cli/serve.rs @@ -1,5 +1,7 @@ use std::{ io::{self, Write}, + net::IpAddr, + net::Ipv4Addr, sync::Arc, }; @@ -13,6 +15,7 @@ use crate::{ web::LiveServer, }; +const DEFAULT_BIND_ADDRESS: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1); const DEFAULT_PORT: u16 = 34872; pub fn serve(global: GlobalOptions, options: ServeCommand) -> Result<()> { @@ -20,6 +23,8 @@ pub fn serve(global: GlobalOptions, options: ServeCommand) -> Result<()> { let session = Arc::new(ServeSession::new(vfs, &options.absolute_project())?); + let ip = options.address.unwrap_or(DEFAULT_BIND_ADDRESS.into()); + let port = options .port .or_else(|| session.project_port()) @@ -27,13 +32,13 @@ pub fn serve(global: GlobalOptions, options: ServeCommand) -> Result<()> { let server = LiveServer::new(session); - let _ = show_start_message(port, global.color.into()); - server.start(port); + let _ = show_start_message(ip, port, global.color.into()); + server.start((ip, port).into()); Ok(()) } -fn show_start_message(port: u16, color: ColorChoice) -> io::Result<()> { +fn show_start_message(bind_address: IpAddr, port: u16, color: ColorChoice) -> io::Result<()> { let writer = BufferWriter::stdout(color); let mut buffer = writer.buffer(); @@ -41,7 +46,12 @@ fn show_start_message(port: u16, color: ColorChoice) -> io::Result<()> { write!(&mut buffer, " Address: ")?; buffer.set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true))?; - writeln!(&mut buffer, "localhost")?; + + if bind_address.is_loopback() { + writeln!(&mut buffer, "localhost")?; + } else { + writeln!(&mut buffer, "{}", bind_address)?; + } buffer.set_color(&ColorSpec::new())?; write!(&mut buffer, " Port: ")?; diff --git a/src/web/mod.rs b/src/web/mod.rs index e9a032bcf..512532caf 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -4,7 +4,7 @@ pub mod interface; mod ui; mod util; -use std::sync::Arc; +use std::{net::SocketAddr, sync::Arc}; use futures::{ future::{self, FutureResult}, @@ -57,9 +57,7 @@ impl LiveServer { LiveServer { serve_session } } - pub fn start(self, port: u16) { - let address = ([127, 0, 0, 1], port).into(); - + pub fn start(self, address: SocketAddr) { let server = Server::bind(&address) .serve(move || { let service: FutureResult<_, hyper::Error> =