xjasonlyu / tun2socks

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

[Bug] Memory leaks on Windows 11 using Tun2socks 2.5.2 (wintun 0.14.1) with Xray 1.8.17 #390

Closed egormzln closed 1 month ago

egormzln commented 3 months ago

Verify steps

Version

2.5.2

What OS are you seeing the problem on?

Windows

Description

I am faced with the problem of large memory leaks when using the Xray-client with tun2socks (for local socks proxy) on Windows 11, here is my Xray config:

Xray-client config.json
{
  "dns": {
    "disableCache": true,
    "hosts": {
      "one.one.one.one": [
        "1.1.1.1"
      ]
    },
    "queryStrategy": "UseIPv4",
    "servers": [
      {
        "address": "https://one.one.one.one/dns-query"
      },
      {
        "address": "https+local://dns.quad9.net/dns-query",
        "domains": [
          "regexp:\\.ru$",
          "geosite:category-gov-ru",
          "geosite:yandex",
          "geosite:mailru"
        ]
      },
      "1.1.1.1"
    ]
  },
  "inbounds": [
    {
      "listen": "127.0.0.1",
      "port": 10808,
      "protocol": "socks",
      "settings": {
        "auth": "noauth",
        "udp": true,
        "userLevel": 8
      },
      "sniffing": {
        "destOverride": [
          "http",
          "tls"
        ],
        "enabled": true
      },
      "tag": "socks"
    },
    {
      "listen": "127.0.0.1",
      "port": 10809,
      "protocol": "http",
      "settings": {
        "userLevel": 8
      },
      "tag": "http"
    }
  ],
  "log": {
    "dnsLog": true,
    "loglevel": "debug"
  },
  "outbounds": [
    {
      "mux": {
        "concurrency": 8,
        "enabled": false
      },
      "protocol": "vless",
      "settings": {
        "vnext": [
          {
            "address": "my-server-address",
            "port": 443,
            "users": [
              {
                "encryption": "none",
                "flow": "xtls-rprx-vision",
                "id": "my-server-id",
                "level": 8,
                "security": "auto"
              }
            ]
          }
        ]
      },
      "streamSettings": {
        "network": "tcp",
        "realitySettings": {
          "allowInsecure": false,
          "fingerprint": "chrome",
          "publicKey": "my-server-key",
          "serverName": "my-server-name",
          "shortId": "my-server-short-id",
          "show": false,
          "spiderX": ""
        },
        "security": "reality",
        "tcpSettings": {
          "header": {
            "type": "none"
          }
        }
      },
      "tag": "proxy"
    },
    {
      "protocol": "freedom",
      "settings": {},
      "tag": "direct"
    },
    {
      "protocol": "blackhole",
      "settings": {
        "response": {
          "type": "http"
        }
      },
      "tag": "block"
    }
  ],
  "routing": {
    "domainStrategy": "IPIfNonMatch",
    "rules": [
      {
        "ip": [
          "1.1.1.1"
        ],
        "outboundTag": "proxy",
        "port": "53",
        "type": "field"
      },
      {
        "inboundTag": [
          "dnsQuery"
        ],
        "outboundTag": "proxy",
        "type": "field"
      },
      {
        "outboundTag": "dnsOut",
        "port": "53",
        "type": "field"
      },
      {
        "ip": [
          "1.1.1.1"
        ],
        "outboundTag": "proxy",
        "type": "field"
      },
      {
        "outboundTag": "block",
        "protocol": [
          "bittorrent"
        ],
        "type": "field"
      },
      {
        "ip": [
          "geoip:ru"
        ],
        "outboundTag": "direct",
        "type": "field"
      },
      {
        "ip": [
          "geoip:private"
        ],
        "outboundTag": "direct",
        "type": "field"
      },
      {
        "domain": [
          "regexp:\\.ru$"
        ],
        "outboundTag": "direct",
        "type": "field"
      },
      {
        "domain": [
          "geosite:category-gov-ru"
        ],
        "outboundTag": "direct",
        "type": "field"
      },
      {
        "domain": [
          "geosite:yandex"
        ],
        "outboundTag": "direct",
        "type": "field"
      },
      {
        "domain": [
          "geosite:mailru"
        ],
        "outboundTag": "direct",
        "type": "field"
      }
    ]
  }
}

Leaks reaches 2GB+ on Tun2socks.exe and 1GB+ on Xray.exe

tun2socks-memory-leak xray-memory-leak

I guess my virtual adapter is being created incorrectly in some way and traffic just starts to overflow the Tun2socks and Xray stacks. Maybe someone can provide me with other tools for local socks-proxying to the xray-client (at 127.0.0.1:10808) I will be very grateful!

CLI or Config

.\tun2socks.exe -device tun-device -proxy 127.0.0.1:10808

Logs

[Tun2socks] time="2024-08-16T10:48:56+03:00" level=warning msg="[UDP] dial "server-ip": connect to 127.0.0.1:10808: dial tcp 127.0.0.1:10808: bind: An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full."
[Tun2socks] time="2024-08-16T10:48:58+03:00" level=warning msg="[UDP] dial "server-ip": connect to 127.0.0.1:10808: dial tcp 127.0.0.1:10808: connectex: Only one usage of each socket address (protocol/network address/port) is normally permitted."

How to Reproduce

  1. In cmd run Xray-core as admin with config from the description :

    .\xray.exe
    
  2. In another cmd run Tun2socks as admin:

    .\tun2socks.exe -device tun-device -proxy 127.0.0.1:10808
    
  3. In another cmd configure the virtual adapter:

    netsh interface ip set address name=tun-device addr=10.0.236.10 source=static mask=255.255.255.0 gateway=none
    
  4. Set DNS from the xray-config for tun-device:

    netsh interface ip set dns name=tun-device static (dns-ip-address, ex: 1.1.1.1)
    
  5. Adding routes to the local DNS table:

    route add (xray-server-ip, ex: 172.210.11.100) mask 255.255.255.255 (windows-default-gateway-ip, ex: 192.168.0.1)
    
    route add 0.0.0.0 mask 0.0.0.0 10.0.236.10
    
xjasonlyu commented 3 months ago

Just wondering, were you able to connect to internet after your setup using tun2socks and xray?

egormzln commented 3 months ago

Yes, it was successful, what could be the problem?

github-actions[bot] commented 1 month ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days