Theldus / wsServer

wsServer - a tiny WebSocket server library written in C
https://theldus.github.io/wsServer
GNU General Public License v3.0
427 stars 82 forks source link

Only works on a loopback adapter #81

Closed doka1997 closed 10 months ago

doka1997 commented 10 months ago

I'm trying to use your wsServer with a small RPi application, but I'm having an issue that the server is only available on the 127.0.0.1 address...

How can I bind it to the eth0 interface (instead of the loopback adapter)?

Theldus commented 10 months ago

Hello @doka1997, It should work in addition to localhost... what is your output for:

$ sudo netstat -ntlp

here I have something like:

$ sudo netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      4510/./examples/ech

Anyway, I'm working on a related issue (#80 and #56), and maybe by tomorrow I'll have something done, maybe it'll fix your problem too =).

doka1997 commented 10 months ago

Thanks for your reply @Theldus

Here's the output of the command on my system:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:40419         0.0.0.0:*               LISTEN      1530/node
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      2781/./c2
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      915/sshd: /usr/sbin
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      525/systemd-resolve
tcp6       0      0 :::22                   :::*                    LISTEN      915/sshd: /usr/sbin

The second process listed (c2) is my application using wsServer.

I can connect to the server by using the ws://127.0.0.1:8080 in the client, but not if I try the local IP address ws://192.168.0.21:8080. The firewall is disabled and should not be an issue.

To me it looks like the server binds to the loopback adapter, and not the ethernet adapter.

Theldus commented 10 months ago

Your netstat output at:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      2781/./c2

clearly states (0.0.0.0:8080/0.0.0.0:*) that it is binding to all network interfaces, and therefore, not restricted to localhost only.

I also ran a quick test on my Raspberry Pi, and I could access it without any issues. I'm not sure what the problem might be on your end...

Theldus commented 10 months ago

Hi @doka1997, I finally finished implementing binding support in PR #82, let me know if this works for you.

Please note that the ws_socket() signature has been updated, but all the example files and manpages have been updated as well, so just follow what's ready =).

doka1997 commented 10 months ago

Thanks for your efforts @Theldus

I'm using your echo example and this website https://websocketking.com/ as a client.

With the default host = "0.0.0.0" or a host = "::" I can access the server running on the same computer with ws://localhost:8080, but not with the IP address. So ws://192.168.0.24:8080 doesn't work for me.

If I set the host to host = "192.168.0.24" then ws://192.168.0.24:8080 still doesn't work and ws://localhost:8080 stops working as well. Basically I can't access the server in that case.

I don't know what is the problem and why with the host = "0.0.0.0" I can access it with ws://localhost:8080 but not with ws://192.168.0.24:8080. The latter is what I really need to able able to access the server from other computers on the network...

Theldus commented 10 months ago

Hi @doka1997, I believe I've managed to reproduce your issue...

The point is that you're testing directly from the website https://websocketking.com/, and it seems that browsers block access to an insecure WebSocket server (ws:// instead of wss://) from a secure site (https). The only exception appears to be when connecting to localhost.

I received the following error in the Chrome console when attempting to connect to my Pi:

Mixed Content: The page at 'https://websocketking.com/' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://192.168.100.40:8080/'. This request has been blocked; this endpoint must be available over WSS.

Could you please test the connection to your Pi using the example file available at examples/echo/echo.html? Just open the HTML in your browser (without running it on a server) and try to connect to the Pi.

If you indeed require the wsServer to run under TLS, please refer to the SSL/TLS Support guide.

doka1997 commented 10 months ago

Hi @Theldus

You are right, it does work work with echo.html! And from another computer on the network.

Thanks fo the tip, I will look into adding TLS to use with the websocketing.com

I appreciate your help!