Add CloseWrite() method to rwcConn in cnet/conn_rwc.go to support proper TCP connection closure in SOCKS5 mode. #548
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I encountered an issue while using Chisel in SOCKS5 mode, where the following connection flow caused unexpected behavior:
tcpclient ---use-socks5--> chisel-client --> chisel-server --> tcpserver
When the tcpserver actively closes the TCP connection, the chisel-server is notified correctly. However, the chisel-client does not receive the closure notification, which I believe is a bug.
If the tcpclient actively closes the TCP connection, the chisel-server successfully closes the connection with tcpserver, and everything works as expected.
Root Cause: Upon investigation, I found that when a channel object is passed through the go-socks5 process, the conversion to NewRWCConn(stream) prevents the CloseWrite method from being called on the connection.
In go-socks5/request.go, the following code is responsible for handling the connection closure:
Here, the dst object needs to implement the closeWriter interface. When tcpserver actively closes the TCP connection, the chisel-server attempts to close the channel. However, since the object net.Conn from NewRWCConn does not implement CloseWrite, the connection closure does not propagate properly.
Solution: I believe adding a CloseWrite() method to cnet/conn_rwc.go would resolve this issue. The method would attempt to call CloseWrite on the underlying ReadWriteCloser object, if it supports it.
The proposed method is:
This change ensures that the rwcConn type can correctly close connections when necessary, especially when the connection involves a channel object that supports the CloseWrite() method. This will fix the issue where chisel-client fails to receive the connection closure notification when tcpserver closes the connection.