snsinfu / reverse-tunnel

Reverse tunnel TCP and UDP
MIT License
178 stars 38 forks source link

Handle broken connection to avoid: Agent error "websocket: close 1000 (normal)" loop #11

Closed snsinfu closed 3 years ago

snsinfu commented 3 years ago

rtun does not handle signals and just terminates immediately without cleanup. This results in dangling WebSocket connections (and tunneling sessions) on rtun-server and renders the server temporarily unusable (see https://github.com/snsinfu/reverse-tunnel/issues/9#issuecomment-773375412). The dead connections will be cleared up in ~20 sec. or so, but it's frustrating to see errors like this:

2021/02/04 17:07:24 Listening on remote port: 8080/tcp
2021/02/04 17:07:24 Agent error "websocket: close 1000 (normal)" - recovering...
2021/02/04 17:07:34 Listening on remote port: 8080/tcp
2021/02/04 17:07:34 Agent error "websocket: close 1000 (normal)" - recovering...
2021/02/04 17:07:44 Listening on remote port: 8080/tcp

So, handle signals in agent (and also in server maybe) and make sure to close connections cleanly.


Edit: The issue was partly due to the server not handling broken connections correctly. I think this is a more serious bug and thus I change the title.

SahajJain01 commented 3 years ago

When there is a network drop on my agent, and then network comes back online, it shows the above websocket close error, and I need to kill rtun-server and start again. I wait for 20+ seconds but agent never connects back unless I restart rtun-server. Can you please suggest how to go about resolving this,

snsinfu commented 3 years ago

Okay, I reproduced your issue. I'll look into it.

For reference, the following is the agent log I saw. I turned off wifi while rtun is running, waited for a while and then turned wifi back on. Looks like rtun-server can't detect connection loss in this case. ``` 2021/03/10 23:21:11 Listening on remote port: 10000/tcp 2021/03/10 23:21:11 Agent error "websocket: close 1008 (policy violation): error: listen tcp :10000: bind: address already in use" - recovering... 2021/03/10 23:21:21 Listening on remote port: 10000/tcp 2021/03/10 23:23:52 Agent error "read tcp 10.1.10.26:63171->192.168.1.18:8888: read: operation timed out" - recovering... 2021/03/10 23:23:52 Agent error "dial tcp 192.168.1.18:8888: connect: network is unreachable" - recovering... 2021/03/10 23:24:01 Agent error "dial tcp 192.168.1.18:8888: connect: network is unreachable" - recovering... ... 2021/03/10 23:32:11 Agent error "dial tcp 192.168.1.18:8888: connect: network is unreachable" - recovering... 2021/03/10 23:33:14 Listening on remote port: 10000/tcp 2021/03/10 23:33:14 Agent error "websocket: close 1008 (policy violation): error: listen tcp :10000: bind: address already in use" - recovering... 2021/03/10 23:33:24 Listening on remote port: 10000/tcp 2021/03/10 23:33:24 Agent error "websocket: close 1008 (policy violation): error: listen tcp :10000: bind: address already in use" - recovering... 2021/03/10 23:33:34 Listening on remote port: 10000/tcp 2021/03/10 23:33:34 Agent error "websocket: close 1008 (policy violation): error: listen tcp :10000: bind: address already in use" - recovering... 2021/03/10 23:33:44 Listening on remote port: 10000/tcp ... ```
snsinfu commented 3 years ago

I believe v1.3.0 fixes the issue: https://github.com/snsinfu/reverse-tunnel/releases/tag/v1.3.0

snsinfu commented 3 years ago

Fixed in release v1.3.2. The server-side issue (handling broken connection) is fixed in 61ddce32520a60712c6cd21818494eff1140888e and the agent-side issue (handling signal) is fixed in ade3e8d7450b471752a38278ae32660f8e0153cc.