rustdesk / rustdesk-server

RustDesk Server Program
https://rustdesk.com/server
GNU Affero General Public License v3.0
6.49k stars 1.36k forks source link

ipv6 potential fix #395

Closed rogerjames99 closed 5 months ago

rogerjames99 commented 6 months ago

This pull request is about fixing a problem in the open source version of rustdesk-server when run on a bare metal linux server. The problem manifests itself as the rendezvous server only listening on ipv6 ports. My lack of knowledge of the rust programming language means that while this pull request will build. However the rendezvous will fail connection requests with the following trace.

[2024-04-01 12:38:53.722551 +01:00] TRACE [/home/roger/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-tungstenite-0.17.1/src/handshake.rs:157] Setting context in handshake
[2024-04-01 12:38:53.722641 +01:00] TRACE [/home/roger/.cargo/registry/src/github.com-1ecc6299db9ec823/tungstenite-0.17.2/src/handshake/machine.rs:40] Doing handshake round.
[2024-04-01 12:38:53.722694 +01:00] TRACE [/home/roger/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-tungstenite-0.17.1/src/compat.rs:149] /home/roger/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-tungstenite-0.17.1/src/compat.rs:149 Read.read
[2024-04-01 12:38:53.723568 +01:00] TRACE [/home/roger/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-tungstenite-0.17.1/src/compat.rs:126] /home/roger/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-tungstenite-0.17.1/src/compat.rs:126 AllowStd.with_context
[2024-04-01 12:38:53.723651 +01:00] TRACE [/home/roger/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-tungstenite-0.17.1/src/compat.rs:152] /home/roger/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-tungstenite-0.17.1/src/compat.rs:152 Read.with_context read -> poll_read
[2024-04-01 12:38:53.723728 +01:00] TRACE [/home/roger/.cargo/registry/src/github.com-1ecc6299db9ec823/mio-0.8.3/src/poll.rs:630] deregistering event source from poller
[2024-04-01 12:38:53.723859 +01:00] DEBUG [src/rendezvous_server.rs:1101] WebSocket protocol error: httparse error: invalid token

Caused by:
    0: httparse error: invalid token
    1: invalid token, hbbs::rendezvous_server:src/rendezvous_server.rs:1101:13

If only the patch in src/rendezvous_server.rs is applied then listening ports will be open on both ipv4 and ipv6. If the patch in libs/hbb_common/src/tcp.rs is applied as well then no ipv6 ports will be created at on either the rendezvous server or the relay server.

Please can this pull request be reviewed by someone who is familiar with rustdesk-server code.

eltorio commented 6 months ago

@rogerjames99 Commenting out return Ok(l); in tcp.rs causes the TCP connection to close. In Rust, you can use the Result enum for error handling:

enum Result<T, E> {
    Ok(T),
    Err(E),
}

This enum provides a way to handle errors. The function listen_any(port) must return a Result<TcpListener, E>, in other words, Ok(something), where something is a TcpListener. In your case, you’re not returning anything.

rogerjames99 commented 5 months ago

Sorry for the delay in replying to this. I have been unwell. I will have a look at this over the next few days. Once again I have no knowledge of the rust language.

The correct behaviour for handling IPv4 and IPv6 connections on linux systems is to open two listening sockets one bound to the IPv4 stack and the other to the IPv6 stack.

Once again I reiterate that I have zero knowledge of the Rust language. So this needs someone who knows rust Rust to look at the code areas I have highlighted and provide the code changes necessary to the acheive the correct behaviour. I will then remove my hacks from the pull request and resubmit it with the correct changes.

rogerjames99 commented 5 months ago

Mea culpa,

This pull request is currently nonsensensical.

I am closing it down.

On linux systems a single socket can listen to both IPv6 and IPv4 connections but only if a number of conditions are met.

  1. The socket is created as an IPv6 server socket.
  2. The socket option IPV6_V6ONLY is set to off.
  3. The socket is bound to the "any" address.

If any of these conditions are not met then the socket will be not accept IPv4 connections.

N.B. Incoming IPv4 addresses will be mapped into IPv6 addresses using the standard rules.