xjasonlyu / tun2socks

tun2socks - powered by gVisor TCP/IP stack
https://github.com/xjasonlyu/tun2socks/wiki
GNU General Public License v3.0
3.12k stars 433 forks source link

[Bug] -tcp-sndBuf does not appear to change the send buffer size on Linux #335

Closed niconorsk closed 7 months ago

niconorsk commented 7 months ago

Verify steps

Version

2.5.1

What OS are you seeing the problem on?

No response

Description

When starting tun2socks with the -tcp-sndbuf argument, this does not actually appear to work. The command runs fine but if I look at the strace output the SO_SNDBUF getsockopt option is never called.

I've done a dive in the source code and have found one bug in core/tcp.go however even with this fix, it still does not actually seem to change the senb buffer size

diff --git a/core/tcp.go b/core/tcp.go
index 7195fa1..f387f8f 100644
--- a/core/tcp.go
+++ b/core/tcp.go
@@ -101,12 +101,14 @@ func setSocketOptions(s *stack.Stack, ep tcpip.Endpoint) tcpip.Error {
        { /* TCP recv/send buffer size */
                var ss tcpip.TCPSendBufferSizeRangeOption
                if err := s.TransportProtocolOption(header.TCPProtocolNumber, &ss); err == nil {
-                       ep.SocketOptions().SetReceiveBufferSize(int64(ss.Default), false)
+                       ep.SocketOptions().SetSendBufferSize(int64(ss.Default), false)
                }

I'm happy to test any other suggested fixes but further understanding is a bit beyond me

CLI or Config

No response

Logs

No response

How to Reproduce

No response

xjasonlyu commented 7 months ago

Hi there,

The -tcp-sndbuf argument is used to set the send buffer size of the internal/userspace gVisor TCP/IP stack, and it doesn't affect the Linux kernel setting. That's why there is no output on the strace.

xjasonlyu commented 7 months ago

But apparently there is a bug on this line:

https://github.com/xjasonlyu/tun2socks/blob/2334083cf9b8afea7caf7679a17e66b6d15c504e/core/tcp.go#L104

And you are welcome to submit a PR to fix it. :-P