Skip to content

Commit e0f180d

Browse files
committed
+ Implement support for setting listening ports in config (fixes #9)
1 parent 487c8ea commit e0f180d

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

doc/config-reference.md

+12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ bridge:
3333
gateway: 10.0.0.1
3434
timezone: Europe/Copenhagen
3535

36+
# http port for emulated bridge
37+
#
38+
# beware: most client programs do NOT support non-standard ports.
39+
# This is for advanced users (e.g. bifrost behind a reverse proxy)
40+
http_port: 80
41+
42+
# https port for emulated bridge
43+
#
44+
# beware: most client programs do NOT support non-standard ports.
45+
# This is for advanced users (e.g. bifrost behind a reverse proxy)
46+
https_port: 443
47+
3648
# Zigbee2mqtt section
3749
#
3850
# Make a sub-section for each zigbee2mqtt server you want to connect

src/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub struct BridgeConfig {
1212
pub name: String,
1313
pub mac: MacAddress,
1414
pub ipaddress: Ipv4Addr,
15+
pub http_port: u16,
16+
pub https_port: u16,
1517
pub netmask: Ipv4Addr,
1618
pub gateway: Ipv4Addr,
1719
pub timezone: String,
@@ -53,6 +55,8 @@ pub fn parse(filename: &Utf8Path) -> Result<AppConfig, ConfigError> {
5355
let settings = Config::builder()
5456
.set_default("bifrost.state_file", "state.yaml")?
5557
.set_default("bifrost.cert_file", "cert.pem")?
58+
.set_default("bridge.http_port", 80)?
59+
.set_default("bridge.https_port", 443)?
5660
.add_source(config::File::with_name(filename.as_str()))
5761
.build()?;
5862

src/main.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,17 @@ async fn build_tasks(appstate: AppState) -> ApiResult<JoinSet<ApiResult<()>>> {
6969
let tls_config = appstate.tls_config().await?;
7070
let state_file = appstate.config().bifrost.state_file.clone();
7171

72-
tasks.spawn(server::http_server(bconf.ipaddress, svc.clone()));
73-
tasks.spawn(server::https_server(bconf.ipaddress, svc, tls_config));
72+
tasks.spawn(server::http_server(
73+
bconf.ipaddress,
74+
bconf.http_port,
75+
svc.clone(),
76+
));
77+
tasks.spawn(server::https_server(
78+
bconf.ipaddress,
79+
bconf.https_port,
80+
svc,
81+
tls_config,
82+
));
7483
tasks.spawn(server::config_writer(appstate.res.clone(), state_file));
7584

7685
for (name, server) in &appstate.config().z2m.servers {

src/server/mod.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,30 @@ pub fn build_service(appstate: AppState) -> IntoMakeService<NormalizePath<Router
6161
ServiceExt::<Request>::into_make_service(normalized)
6262
}
6363

64-
pub async fn http_server<S>(listen_addr: Ipv4Addr, svc: S) -> ApiResult<()>
64+
pub async fn http_server<S>(listen_addr: Ipv4Addr, listen_port: u16, svc: S) -> ApiResult<()>
6565
where
6666
S: Send + MakeService<SocketAddr, Request<Incoming>>,
6767
S::MakeFuture: Send,
6868
{
69-
let addr = SocketAddr::from((listen_addr, 80));
69+
let addr = SocketAddr::from((listen_addr, listen_port));
7070
log::info!("http listening on {}", addr);
7171

7272
axum_server::bind(addr).serve(svc).await?;
7373

7474
Ok(())
7575
}
7676

77-
pub async fn https_server<S>(listen_addr: Ipv4Addr, svc: S, config: RustlsConfig) -> ApiResult<()>
77+
pub async fn https_server<S>(
78+
listen_addr: Ipv4Addr,
79+
listen_port: u16,
80+
svc: S,
81+
config: RustlsConfig,
82+
) -> ApiResult<()>
7883
where
7984
S: Send + MakeService<SocketAddr, Request<Incoming>>,
8085
S::MakeFuture: Send,
8186
{
82-
let addr = SocketAddr::from((listen_addr, 443));
87+
let addr = SocketAddr::from((listen_addr, listen_port));
8388
log::info!("https listening on {}", addr);
8489

8590
axum_server::bind_rustls(addr, config).serve(svc).await?;

0 commit comments

Comments
 (0)