Search code examples
curlnetworkingipnatwindows-subsystem-for-linux

Cannot access service bound on 0.0.0.0 from wsl2


I have an ssh tunnel that I did with the help of MobaXterm that looks like this

local pc     <= remote server
0.0.0.0:8545 <= 127.0.0.1:8545

So I did some tests and I can reach the service on the remote server with curl at http://127.0.0.1:8545 on the local pc. I tried curl on git bash and cmd and both work but curl on wsl2 claims that connection has been refused.

cmd:

>curl -H "Content-Type: application/json" -X POST --data "{\"jsonrpc\":\"2.0\",\"method\":\"eth_blockNumber\",\"params\":[],\"id\":83}" http://127.0.0.1:8545
{"jsonrpc":"2.0","id":83,"result":"0x6c360c"}

git bash:

$ curl -XPOST -H "Content-type: application/json" -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83}' 'http://127.0.0.1:8545'
{"jsonrpc":"2.0","id":83,"result":"0x6c35b8"}

wsl2:

$ curl -XPOST -H "Content-type: application/json" -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83}' 'http://127.0.0.1:8545'
curl: (7) Failed to connect to 127.0.0.1 port 8545: Connection refused

What I also tried was to bind the tunnel at 127.0.0.1:8545 and 172.27.96.1:8545. Since those didn't work I thought to try making 0.0.0.0:8545 work first before trying something more specific.

How can I reach at that service from wsl2?


Solution

  • I made it work by binding to http://172.20.144.1:8545 (the gateway address of wsl network). I could then reach the service at http://172.20.144.1:8545 both from wsl and windows (cmd).

    The only issue is that the gateway ip for wsl network changes on each reboot.

    I'm not yet sure if windows provides an address resolution to wsl's network gateway.