diff --git a/docs/configuration.md b/docs/configuration.md index 791d4114d..1ffd82509 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1561,8 +1561,9 @@ supported on the current platform. and want to restrict Sunshine to a specific one. If not set, Sunshine will bind to all available interfaces (0.0.0.0 for IPv4 or :: for IPv6).

- Note: The address must be valid for the system and must match the address family - being used. When using IPv6, you can specify an IPv6 address even with address_family set to "both". + Note: The address must exist on the host and be compatible with address_family. + An IPv4 address works with either "ipv4" or "both"; when used with "both", Sunshine listens only + on that IPv4 address. An IPv6 address requires address_family to be set to "both". diff --git a/src/confighttp.cpp b/src/confighttp.cpp index 9bd3cbc22..f21c6af8c 100644 --- a/src/confighttp.cpp +++ b/src/confighttp.cpp @@ -2320,9 +2320,7 @@ namespace confighttp { server.config.address = net::get_bind_address(address_family); server.config.port = port_https; - // Store bind address for logging, use "localhost" as fallback for wildcard addresses - const auto bind_addr = server.config.address; - const auto display_addr = config::sunshine.bind_address.empty() ? "localhost"sv : std::string_view {bind_addr}; + const auto display_addr = net::get_bind_address_url_host(); auto accept_and_run = [&](auto *server) { try { diff --git a/src/entry_handler.cpp b/src/entry_handler.cpp index e5e18920b..c2249af0a 100644 --- a/src/entry_handler.cpp +++ b/src/entry_handler.cpp @@ -26,12 +26,16 @@ extern "C" { using namespace std::literals; -void launch_ui(const std::optional &path) { - std::string url = std::format("https://localhost:{}", static_cast(net::map_port(confighttp::PORT_HTTPS))); +std::string get_launch_ui_url(const std::optional &path) { + std::string url = std::format("https://{}:{}", net::get_bind_address_url_host(), static_cast(net::map_port(confighttp::PORT_HTTPS))); if (path) { url += *path; } - platf::open_url(url); + return url; +} + +void launch_ui(const std::optional &path) { + platf::open_url(get_launch_ui_url(path)); } namespace args { diff --git a/src/entry_handler.h b/src/entry_handler.h index 32aba38e6..886c76c04 100644 --- a/src/entry_handler.h +++ b/src/entry_handler.h @@ -6,19 +6,28 @@ // standard includes #include +#include +#include #include // local includes #include "thread_pool.h" #include "thread_safe.h" +/** + * @brief Build the URL used to launch the Web UI. + * @param path Optional path to append to the base URL. + * @return HTTPS URL using the configured bind address and Web UI port. + * @examples + * get_launch_ui_url(); + * get_launch_ui_url("/pin"); + * @examples_end + */ +std::string get_launch_ui_url(const std::optional &path = std::nullopt); + /** * @brief Launch the Web UI. * @param path Optional path to append to the base URL. - * @examples - * launch_ui(); - * launch_ui("/pin"); - * @examples_end */ void launch_ui(const std::optional &path = std::nullopt); diff --git a/src/network.cpp b/src/network.cpp index 2a78b2f95..b04d6f888 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -126,6 +126,16 @@ namespace net { return BOTH; } + af_e get_effective_address_family(const af_e af) { + if (af != BOTH || config::sunshine.bind_address.empty()) { + return af; + } + + boost::system::error_code ec; + const auto bind_address = ip::make_address(config::sunshine.bind_address, ec); + return !ec && bind_address.is_v4() ? IPV4 : af; + } + std::string_view af_to_any_address_string(const af_e af) { switch (af) { case IPV4: @@ -175,6 +185,16 @@ namespace net { } } + std::string get_bind_address_url_host() { + if (config::sunshine.bind_address.empty()) { + return "localhost"; + } + + boost::system::error_code ec; + const auto bind_address = ip::make_address(config::sunshine.bind_address, ec); + return ec ? config::sunshine.bind_address : addr_to_url_escaped_string(bind_address); + } + int encryption_mode_for_address(boost::asio::ip::address address) { auto nettype = net::from_address(address.to_string()); if (nettype == net::net_e::PC || nettype == net::net_e::LAN) { @@ -193,6 +213,7 @@ namespace net { enet_initialize(); }); + af = get_effective_address_family(af); const auto bind_addr = net::get_bind_address(af); enet_address_set_host(&addr, bind_addr.c_str()); enet_address_set_port(&addr, port); diff --git a/src/network.h b/src/network.h index 679f35b8a..137dcdef8 100644 --- a/src/network.h +++ b/src/network.h @@ -104,6 +104,14 @@ namespace net { */ af_e af_from_enum_string(const std::string_view &view); + /** + * @brief Get the effective address family for listening sockets. + * @details An IPv4 bind address narrows dual-stack mode to IPv4 so the socket protocol matches the bind endpoint. + * @param af Configured address family. + * @return IPv4 for a valid IPv4 bind address in dual-stack mode, or `af` otherwise. + */ + af_e get_effective_address_family(af_e af); + /** * @brief Get the wildcard binding address for a given address family. * @param af Address family. @@ -118,6 +126,13 @@ namespace net { */ std::string get_bind_address(af_e af); + /** + * @brief Get the configured bind address formatted as a URL host. + * @details IPv6 addresses are enclosed in brackets. An empty bind address is represented as `localhost`. + * @return URL host for links to services bound by Sunshine. + */ + std::string get_bind_address_url_host(); + /** * @brief Convert an address to a normalized form. * @details Normalization converts IPv4-mapped IPv6 addresses into IPv4 addresses. diff --git a/src/rtsp.cpp b/src/rtsp.cpp index 423df9d4c..c7135ddf0 100644 --- a/src/rtsp.cpp +++ b/src/rtsp.cpp @@ -482,6 +482,7 @@ namespace rtsp_stream { * @return Network operation status. */ int bind(net::af_e af, std::uint16_t port, boost::system::error_code &ec) { + af = net::get_effective_address_family(af); acceptor.open(af == net::IPV4 ? tcp::v4() : tcp::v6(), ec); if (ec) { return -1; diff --git a/src/stream.cpp b/src/stream.cpp index 5c232c4f1..1291c9c62 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -1956,7 +1956,7 @@ namespace stream { * @brief Bind the GameStream UDP and control sockets used for a streaming session. */ int start_broadcast(broadcast_ctx_t &ctx) { - auto address_family = net::af_from_enum_string(config::sunshine.address_family); + auto address_family = net::get_effective_address_family(net::af_from_enum_string(config::sunshine.address_family)); auto protocol = address_family == net::IPV4 ? udp::v4() : udp::v6(); auto control_port = net::map_port(CONTROL_PORT); auto video_port = net::map_port(VIDEO_STREAM_PORT); diff --git a/src/upnp.cpp b/src/upnp.cpp index 7d46d73e4..ca20e9856 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -314,7 +314,7 @@ namespace upnp { bool mapped = false; IGDdatas data; urls_t mapped_urls; - auto address_family = net::af_from_enum_string(config::sunshine.address_family); + auto address_family = net::get_effective_address_family(net::af_from_enum_string(config::sunshine.address_family)); // Refresh UPnP rules every few minutes. They can be lost if the router reboots, // WAN IP address changes, or various other conditions. diff --git a/src_assets/common/assets/web/public/assets/locale/en.json b/src_assets/common/assets/web/public/assets/locale/en.json index 718e88a37..81308fd57 100644 --- a/src_assets/common/assets/web/public/assets/locale/en.json +++ b/src_assets/common/assets/web/public/assets/locale/en.json @@ -166,7 +166,7 @@ "back_button_timeout": "Home/Guide Button Emulation Timeout", "back_button_timeout_desc": "If the Back/Select button is held down for the specified number of milliseconds, a Home/Guide button press is emulated. If set to a value < 0 (default), holding the Back/Select button will not emulate the Home/Guide button.", "bind_address": "Bind address", - "bind_address_desc": "Set the specific IP address Sunshine will bind to. If left blank, Sunshine will bind to all available addresses.", + "bind_address_desc": "Set an IP address that exists on this host. An IPv4 address works with either address-family mode; an IPv6 address requires dual-stack mode. If left blank, Sunshine will bind to all available addresses for the configured address family.", "capture": "Force a Specific Capture Method", "capture_desc": "On automatic mode Sunshine will use the first one that works. NvFBC requires patched nvidia drivers.", "category_advanced": "Advanced", diff --git a/tests/unit/test_entry_handler.cpp b/tests/unit/test_entry_handler.cpp index bef36f213..9fb64d210 100644 --- a/tests/unit/test_entry_handler.cpp +++ b/tests/unit/test_entry_handler.cpp @@ -10,6 +10,57 @@ // local includes #include +/** + * @brief Test fixture that restores Web UI URL configuration after each test. + */ +class LaunchUiUrlTest: public BaseTest { +private: + std::string original_bind_address; + std::uint16_t original_port; + +protected: + void SetUp() override { + BaseTest::SetUp(); + original_bind_address = config::sunshine.bind_address; + original_port = config::sunshine.port; + config::sunshine.port = 47989; + } + + void TearDown() override { + config::sunshine.bind_address = original_bind_address; + config::sunshine.port = original_port; + BaseTest::TearDown(); + } +}; + +/** + * @brief Test that Web UI URLs use localhost when no bind address is configured. + */ +TEST_F(LaunchUiUrlTest, UsesLocalhostWithoutBindAddress) { + config::sunshine.bind_address = ""; + + EXPECT_EQ(get_launch_ui_url(), "https://localhost:47990"); + EXPECT_EQ(get_launch_ui_url("/pin"), "https://localhost:47990/pin"); +} + +/** + * @brief Test that Web UI URLs use the configured IPv4 bind address. + */ +TEST_F(LaunchUiUrlTest, UsesConfiguredIPv4Address) { + config::sunshine.bind_address = "198.51.100.56"; + + EXPECT_EQ(get_launch_ui_url(), "https://198.51.100.56:47990"); +} + +/** + * @brief Test that Web UI URLs enclose configured IPv6 bind addresses in brackets. + */ +TEST_F(LaunchUiUrlTest, EscapesConfiguredIPv6Address) { + config::sunshine.bind_address = "2001:db8::1"; + + EXPECT_EQ(get_launch_ui_url("/config"), "https://[2001:db8::1]:47990/config"); +} + TEST(EntryHandlerTests, LogPublisherDataTest) { // call log_publisher_data log_publisher_data(); diff --git a/tests/unit/test_network.cpp b/tests/unit/test_network.cpp index b46670ba6..f3a8987e1 100644 --- a/tests/unit/test_network.cpp +++ b/tests/unit/test_network.cpp @@ -72,6 +72,63 @@ TEST_F(BindAddressTest, DefaultBehaviorIPv6) { ASSERT_EQ(bind_addr, "::"); } +/** + * @brief Test that an empty bind address preserves the configured address family. + */ +TEST_F(BindAddressTest, EmptyAddressPreservesConfiguredFamily) { + config::sunshine.bind_address = ""; + + EXPECT_EQ(net::get_effective_address_family(net::af_e::IPV4), net::af_e::IPV4); + EXPECT_EQ(net::get_effective_address_family(net::af_e::BOTH), net::af_e::BOTH); +} + +/** + * @brief Test that an explicit IPv4 bind address selects an IPv4 socket. + */ +TEST_F(BindAddressTest, IPv4AddressSelectsIPv4Family) { + config::sunshine.bind_address = "192.0.2.101"; + + EXPECT_EQ(net::get_effective_address_family(net::af_e::IPV4), net::af_e::IPV4); + EXPECT_EQ(net::get_effective_address_family(net::af_e::BOTH), net::af_e::IPV4); +} + +/** + * @brief Test that an explicit IPv6 bind address does not override the configured family. + */ +TEST_F(BindAddressTest, IPv6AddressPreservesConfiguredFamily) { + config::sunshine.bind_address = "2001:db8::1"; + + EXPECT_EQ(net::get_effective_address_family(net::af_e::IPV4), net::af_e::IPV4); + EXPECT_EQ(net::get_effective_address_family(net::af_e::BOTH), net::af_e::BOTH); +} + +/** + * @brief Test that invalid bind addresses preserve the configured family for downstream error reporting. + */ +TEST_F(BindAddressTest, InvalidAddressPreservesConfiguredFamily) { + config::sunshine.bind_address = "not-an-address"; + + EXPECT_EQ(net::get_effective_address_family(net::af_e::IPV4), net::af_e::IPV4); + EXPECT_EQ(net::get_effective_address_family(net::af_e::BOTH), net::af_e::BOTH); +} + +/** + * @brief Test that bind addresses are formatted for use as URL hosts. + */ +TEST_F(BindAddressTest, AddressFormatsAsUrlHost) { + config::sunshine.bind_address = ""; + EXPECT_EQ(net::get_bind_address_url_host(), "localhost"); + + config::sunshine.bind_address = "198.51.100.56"; + EXPECT_EQ(net::get_bind_address_url_host(), "198.51.100.56"); + + config::sunshine.bind_address = "2001:db8::1"; + EXPECT_EQ(net::get_bind_address_url_host(), "[2001:db8::1]"); + + config::sunshine.bind_address = "not-an-address"; + EXPECT_EQ(net::get_bind_address_url_host(), "not-an-address"); +} + /** * @brief Test that get_bind_address returns configured IPv4 address */