Skip to content

Commit beb6bdf

Browse files
author
loki
committed
Allow end user to configure what ports to listen on
1 parent 8bf4ade commit beb6bdf

File tree

19 files changed

+149
-69
lines changed

19 files changed

+149
-69
lines changed

assets/sunshine.conf

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# external_ip = 123.456.789.12
33

44
# Set the familly of ports used by Sunshine
5-
# port = 47984
5+
# port = 47989
66

77
# The private key must be 2048 bits
88
# pkey = /dir/pkey.pem
@@ -35,7 +35,16 @@
3535
# lan: Only those in LAN may access /pin
3636
# wan: Anyone may access /pin
3737
#
38-
# origin_pin_allowed = lan
38+
# origin_pin_allowed = pc
39+
40+
# The origin of the remote endpoint address that is not denied for HTTPS Web UI
41+
# Could be any of the following values:
42+
# pc|lan|wan
43+
# pc: Only localhost may access the Web Manager
44+
# lan: Only those in LAN may access the Web Manager
45+
# wan: Anyone may access the Web Manager
46+
#
47+
# origin_web_ui_allowed = lan
3948

4049
# If UPnP is enabled, Sunshine will attempt to open ports for streaming over the internet
4150
# To enable it, uncomment the following line:

assets/web/config.html

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ <h1 class="my-4">Configuration</h1>
3232
</select>
3333
<div class="form-text">The minimum log level printed to standard out</div>
3434
</div>
35-
<!--Origin PIN Allowed-->
35+
<!--Origin Web UI Allowed-->
3636
<div class="mb-3">
37-
<label for="origin_pin_allowed" class="form-label">Origin PIN Allowed</label>
38-
<select id="origin_pin_allowed" class="form-select" v-model="config.origin_pin_allowed">
39-
<option value="pc">Only localhost may access /pin and Web UI</option>
40-
<option value="lan">Only those in LAN may access /pin and Web UI</option>
41-
<option value="wan">Anyone may access /pin and Web UI</option>
37+
<label for="origin_web_ui_allowed" class="form-label">Origin Web UI Allowed</label>
38+
<select id="origin_web_ui_allowed" class="form-select" v-model="config.origin_web_ui_allowed">
39+
<option value="pc">Only localhost may access Web UI</option>
40+
<option value="lan">Only those in LAN may access Web UI</option>
41+
<option value="wan">Anyone may access Web UI</option>
4242
</select>
43-
<div class="form-text">The origin of the remote endpoint address that is not denied for HTTP method /pin
43+
<div class="form-text">The origin of the remote endpoint address that is not denied access to Web UI
4444
</div>
4545
</div>
4646
<!--UPnP-->
@@ -238,6 +238,15 @@ <h1 class="my-4">Configuration</h1>
238238
</div>
239239
</div>
240240
<div v-if="currentTab === 'advanced'" class="config-page">
241+
<!--Port familly-->
242+
<div class="mb-3">
243+
<label for="port" class="form-label">Port</label>
244+
<input type="number" min="0" max="65529" class="form-control" id="port" placeholder="47989"
245+
v-model="config.port">
246+
<div class="form-text">
247+
Set the familly of ports used by Sunshine
248+
</div>
249+
</div>
241250
<!--Constant Rate Factor-->
242251
<div class="mb-3">
243252
<label for="crf" class="form-label">Constant Rate Factor</label>
@@ -341,6 +350,17 @@ <h1 class="my-4">Configuration</h1>
341350
Store Username/Password seperately from Sunshine's state file.
342351
</div>
343352
</div>
353+
<!--Origin PIN Allowed-->
354+
<div class="mb-3">
355+
<label for="origin_pin_allowed" class="form-label">Origin PIN Allowed</label>
356+
<select id="origin_pin_allowed" class="form-select" v-model="config.origin_pin_allowed">
357+
<option value="pc">Only localhost may access /pin</option>
358+
<option value="lan">Only those in LAN may access /pin</option>
359+
<option value="wan">Anyone may access /pin</option>
360+
</select>
361+
<div class="form-text">The origin of the remote endpoint address that is not denied for HTTP method /pin
362+
</div>
363+
</div>
344364
<!--External IP-->
345365
<div class="mb-3">
346366
<label for="external_ip" class="form-label">External IP</label>
@@ -517,7 +537,8 @@ <h1 class="my-4">Configuration</h1>
517537
//Populate default values if not present in config
518538
this.config.upnp = this.config.upnp || 'disabled';
519539
this.config.min_log_level = this.config.min_log_level || 2;
520-
this.config.origin_pin_allowed = this.config.origin_pin_allowed || "lan";
540+
this.config.origin_pin_allowed = this.config.origin_pin_allowed || "pc";
541+
this.config.origin_web_ui_allowed = this.config.origin_web_manager_allowed || "lan";
521542
this.config.hevc_mode = this.config.hevc_mode || 0;
522543
this.config.encoder = this.config.encoder || '';
523544
this.config.nv_preset = this.config.nv_preset || 'default';

sunshine/config.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ stream_t stream {
185185
};
186186

187187
nvhttp_t nvhttp {
188-
"lan", // origin_pin
188+
"pc", // origin_pin
189+
"lan", // origin web manager
190+
189191
PRIVATE_KEY_FILE,
190192
CERTIFICATE_FILE,
191193

@@ -222,7 +224,8 @@ sunshine_t sunshine {
222224
{}, // Password
223225
{}, // Password Salt
224226
SUNSHINE_ASSETS_DIR "/sunshine.conf", // config file
225-
{} // cmd args
227+
{}, // cmd args
228+
47989,
226229
};
227230

228231
bool endline(char ch) {
@@ -610,6 +613,7 @@ void apply_config(std::unordered_map<std::string, std::string> &&vars) {
610613
string_f(vars, "virtual_sink", audio.virtual_sink);
611614

612615
string_restricted_f(vars, "origin_pin_allowed", nvhttp.origin_pin_allowed, { "pc"sv, "lan"sv, "wan"sv });
616+
string_restricted_f(vars, "origin_web_ui_allowed", nvhttp.origin_web_ui_allowed, { "pc"sv, "lan"sv, "wan"sv });
613617

614618
int to = -1;
615619
int_between_f(vars, "ping_timeout", to, { -1, std::numeric_limits<int>::max() });
@@ -642,6 +646,10 @@ void apply_config(std::unordered_map<std::string, std::string> &&vars) {
642646
input.key_repeat_delay = std::chrono::milliseconds { to };
643647
}
644648

649+
int port = sunshine.port;
650+
int_f(vars, "port"s, port);
651+
sunshine.port = (std::uint16_t)port;
652+
645653
bool upnp = false;
646654
bool_f(vars, "upnp"s, upnp);
647655

sunshine/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct nvhttp_t {
5959
// Could be any of the following values:
6060
// pc|lan|wan
6161
std::string origin_pin_allowed;
62+
std::string origin_web_ui_allowed;
6263

6364
std::string pkey; // must be 2048 bits
6465
std::string cert; // must be signed with a key of 2048 bits

sunshine/confighttp.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030
#include "utility.h"
3131
#include "uuid.h"
3232

33-
namespace confighttp {
3433
using namespace std::literals;
35-
constexpr auto PORT_HTTPS = 47990;
3634

35+
namespace confighttp {
3736
namespace fs = std::filesystem;
3837
namespace pt = boost::property_tree;
3938

@@ -67,7 +66,7 @@ void print_req(const req_https_t &request) {
6766

6867
void send_unauthorized(resp_https_t response, req_https_t request) {
6968
auto address = request->remote_endpoint_address();
70-
BOOST_LOG(info) << '[' << address << "] -- denied"sv;
69+
BOOST_LOG(info) << "Web UI: ["sv << address << "] -- not authorized"sv;
7170
const SimpleWeb::CaseInsensitiveMultimap headers {
7271
{ "WWW-Authenticate", R"(Basic realm="Sunshine Gamestream Host", charset="UTF-8")" }
7372
};
@@ -78,8 +77,8 @@ bool authenticate(resp_https_t response, req_https_t request) {
7877
auto address = request->remote_endpoint_address();
7978
auto ip_type = net::from_address(address);
8079

81-
if(ip_type > http::origin_pin_allowed) {
82-
BOOST_LOG(info) << '[' << address << "] -- denied"sv;
80+
if(ip_type > http::origin_web_ui_allowed) {
81+
BOOST_LOG(info) << "Web UI: ["sv << address << "] -- denied"sv;
8382
response->write(SimpleWeb::StatusCode::client_error_forbidden);
8483
return false;
8584
}
@@ -455,6 +454,8 @@ void savePin(resp_https_t response, req_https_t request) {
455454
void start() {
456455
auto shutdown_event = mail::man->event<bool>(mail::shutdown);
457456

457+
auto port_https = map_port(PORT_HTTPS);
458+
458459
auto ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tls);
459460
ctx->use_certificate_chain_file(config::nvhttp.cert);
460461
ctx->use_private_key_file(config::nvhttp.pkey, boost::asio::ssl::context::pem);
@@ -475,14 +476,14 @@ void start() {
475476
server.resource["^/api/apps/([0-9]+)$"]["DELETE"] = deleteApp;
476477
server.config.reuse_address = true;
477478
server.config.address = "0.0.0.0"s;
478-
server.config.port = PORT_HTTPS;
479+
server.config.port = port_https;
479480

480481
try {
481482
server.bind();
482-
BOOST_LOG(info) << "Configuration UI available at [https://localhost:"sv << PORT_HTTPS << "]";
483+
BOOST_LOG(info) << "Configuration UI available at [https://localhost:"sv << port_https << "]";
483484
}
484485
catch(boost::system::system_error &err) {
485-
BOOST_LOG(fatal) << "Couldn't bind http server to ports ["sv << PORT_HTTPS << "]: "sv << err.what();
486+
BOOST_LOG(fatal) << "Couldn't bind http server to ports ["sv << port_https << "]: "sv << err.what();
486487

487488
shutdown_event->raise(true);
488489
return;
@@ -497,7 +498,7 @@ void start() {
497498
return;
498499
}
499500

500-
BOOST_LOG(fatal) << "Couldn't start Configuration HTTP server to ports ["sv << PORT_HTTPS << ", "sv << PORT_HTTPS << "]: "sv << err.what();
501+
BOOST_LOG(fatal) << "Couldn't start Configuration HTTPS server to port ["sv << port_https << "]: "sv << err.what();
501502
shutdown_event->raise(true);
502503
return;
503504
}

sunshine/confighttp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515

1616
namespace confighttp {
17+
constexpr auto PORT_HTTPS = 1;
1718
void start();
18-
}
19+
} // namespace confighttp
1920

2021
#endif //SUNSHINE_CONFIGHTTP_H

sunshine/httpcommon.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ bool user_creds_exist(const std::string &file);
3535

3636
std::string unique_id;
3737
net::net_e origin_pin_allowed;
38+
net::net_e origin_web_ui_allowed;
3839

3940
int init() {
40-
bool clean_slate = config::sunshine.flags[config::flag::FRESH_STATE];
41-
origin_pin_allowed = net::from_enum_string(config::nvhttp.origin_pin_allowed);
41+
bool clean_slate = config::sunshine.flags[config::flag::FRESH_STATE];
42+
origin_pin_allowed = net::from_enum_string(config::nvhttp.origin_pin_allowed);
43+
origin_web_ui_allowed = net::from_enum_string(config::nvhttp.origin_web_ui_allowed);
44+
4245
if(clean_slate) {
4346
unique_id = util::uuid_t::generate().string();
4447
auto dir = std::filesystem::temp_directory_path() / "Sushine"sv;

sunshine/httpcommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ int save_user_creds(
1414
int reload_user_creds(const std::string &file);
1515
extern std::string unique_id;
1616
extern net::net_e origin_pin_allowed;
17+
extern net::net_e origin_web_ui_allowed;
1718

1819
} // namespace http

sunshine/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,8 @@ int write_file(const char *path, const std::string_view &contents) {
270270
out << contents;
271271

272272
return 0;
273+
}
274+
275+
std::uint16_t map_port(int port) {
276+
return (std::uint16_t)((int)config::sunshine.port + port);
273277
}

sunshine/main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ void print_help(const char *name);
2929
std::string read_file(const char *path);
3030
int write_file(const char *path, const std::string_view &contents);
3131

32+
std::uint16_t map_port(int port);
33+
3234
namespace mail {
3335
#define MAIL(x) \
3436
constexpr auto x = std::string_view { #x }

0 commit comments

Comments
 (0)