Bind to all interfaces (#5)

* Bind to all interfaces

When running under WSL2, binding only to localhost via the 127.0.0.1 address
only lets you access the server from within WSL2 itself. It does not allow you
to access the server from Windows.

With this change, you can access the web server from the WSL ip address. You can
find your WSL ip address with

```
$ ip a | grep eth0
4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 192.168.161.12/20 brd 192.168.175.255 scope global eth0
```

* Add comment about binding to localhost

When running under WSL2, binding only to localhost via the 127.0.0.1 address
only lets you access the server from within WSL2 itself. It does not allow you
to access the server from Windows.

Add a comment explaining how to work around this, by binding to all
interfaces, instead of only to localhost.
This commit is contained in:
Jes Bak Hansen 2020-09-01 16:54:53 +02:00 committed by GitHub
parent 24dccda00c
commit f4ba849d91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,6 +11,13 @@ async fn main() -> Result<(), anyhow::Error> {
.await .await
.map_err(anyhow::Error::from) .map_err(anyhow::Error::from)
.with_context(|| "Failed to connect to Postgres.")?; .with_context(|| "Failed to connect to Postgres.")?;
// Here we choose to bind explicitly to localhost, 127.0.0.1, for security
// reasons. This binding may cause issues in some environments. For example,
// it causes connectivity issues running in WSL2, where you cannot reach the
// server when it is bound to WSL2's localhost interface. As a workaround,
// you can choose to bind to all interfaces, 0.0.0.0, instead, but be aware
// of the security implications when you expose the server on all interfaces.
let address = format!("127.0.0.1:{}", configuration.application_port); let address = format!("127.0.0.1:{}", configuration.application_port);
let listener = TcpListener::bind(address)?; let listener = TcpListener::bind(address)?;
run(listener, connection_pool)?.await?; run(listener, connection_pool)?.await?;