From 7ddc9f498cabbbe9c84e025498ad806213ee3d2d Mon Sep 17 00:00:00 2001 From: Ari Havlin Date: Tue, 29 Oct 2024 20:54:41 +0200 Subject: [PATCH 1/3] feat(localhost): add custom host binding to allow external access --- .changes/localhost-custom-host-binding.md | 5 +++++ plugins/localhost/src/lib.rs | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 .changes/localhost-custom-host-binding.md diff --git a/.changes/localhost-custom-host-binding.md b/.changes/localhost-custom-host-binding.md new file mode 100644 index 0000000000..b5bd3b53d2 --- /dev/null +++ b/.changes/localhost-custom-host-binding.md @@ -0,0 +1,5 @@ +--- +'localhost': 'minor' +--- + +Add custom host binding to allow external access \ No newline at end of file diff --git a/plugins/localhost/src/lib.rs b/plugins/localhost/src/lib.rs index a0c4c794f0..1bfa3b2993 100644 --- a/plugins/localhost/src/lib.rs +++ b/plugins/localhost/src/lib.rs @@ -46,6 +46,7 @@ type OnRequest = Option>; pub struct Builder { port: u16, + host: Option, on_request: OnRequest, } @@ -53,10 +54,16 @@ impl Builder { pub fn new(port: u16) -> Self { Self { port, + host: None, on_request: None, } } + pub fn host>(mut self, host: H) -> Self { + self.host = Some(host.as_ref().to_string()); + self + } + pub fn on_request( mut self, f: F, @@ -67,6 +74,7 @@ impl Builder { pub fn build(mut self) -> TauriPlugin { let port = self.port; + let host = self.host.unwrap_or("localhost".to_string()); let on_request = self.on_request.take(); PluginBuilder::new("localhost") @@ -74,7 +82,7 @@ impl Builder { let asset_resolver = app.asset_resolver(); std::thread::spawn(move || { let server = - Server::http(format!("localhost:{port}")).expect("Unable to spawn server"); + Server::http(format!("{host}:{port}")).expect("Unable to spawn server"); for req in server.incoming_requests() { let path = req .url() From 85d37060c5f2fe2ac2bdbe649048af713e2cc300 Mon Sep 17 00:00:00 2001 From: Ari Havlin Date: Wed, 30 Oct 2024 20:50:17 +0200 Subject: [PATCH 2/3] rust fmt --- plugins/localhost/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/localhost/src/lib.rs b/plugins/localhost/src/lib.rs index 1bfa3b2993..ddf4eb2bdc 100644 --- a/plugins/localhost/src/lib.rs +++ b/plugins/localhost/src/lib.rs @@ -63,7 +63,7 @@ impl Builder { self.host = Some(host.as_ref().to_string()); self } - + pub fn on_request( mut self, f: F, From 28467d98019c21c5fbff340a0f849a09aab2a946 Mon Sep 17 00:00:00 2001 From: Ari <67370329+arihav@users.noreply.github.com> Date: Wed, 6 Nov 2024 11:55:55 +0200 Subject: [PATCH 3/3] Update plugins/localhost/src/lib.rs Co-authored-by: Fabian-Lars --- plugins/localhost/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/localhost/src/lib.rs b/plugins/localhost/src/lib.rs index ddf4eb2bdc..ae77cd8060 100644 --- a/plugins/localhost/src/lib.rs +++ b/plugins/localhost/src/lib.rs @@ -59,8 +59,9 @@ impl Builder { } } - pub fn host>(mut self, host: H) -> Self { - self.host = Some(host.as_ref().to_string()); + // Change the host the plugin binds to. Defaults to `localhost`. + pub fn host>(mut self, host: H) -> Self { + self.host = Some(host.into()); self }