Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inbound config without sockopt will block inbound listener from setting SO_REUSEPORT #69

Closed
Vigilans opened this issue Jul 30, 2020 · 0 comments · Fixed by #70
Closed

Comments

@Vigilans
Copy link
Contributor

Vigilans commented Jul 30, 2020

你正在使用哪个版本的 V2Ray?

4.26.0。

你的使用场景是什么?

透明代理。

你看到的不正常的现象是什么?

当Inbound的JSON配置中,若sockopt一项没有设置的话,会导致Inbound服务器的监听socket的SO_REUSEPORT不被设置。

这可以在Inbound设置SO_REUSEPORT的代码中:

if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
return newError("failed to set SO_REUSEPORT").Base(err).AtWarning()
}

通过GetSockopt在日志中追加打印SO_REUSEPORT的值来验证:

if value, err := syscall.GetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT); err == nil {
	newError("Reuseport: " + strconv.Itoa(value)).AtWarning().WriteToLog()
}

验证结果如下:

[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 0.0.0.0:53
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:53
[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 0.0.0.0:3423
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:3423
[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 0.0.0.0:3424
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:36106
[Info] v2ray.com/core/transport/internet/kcp: listening on 0.0.0.0:36106
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:32758
[Info] v2ray.com/core/transport/internet/kcp: listening on 0.0.0.0:32758
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 0.0.0.0:821
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:821
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 192.168.200.8:1080
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 192.168.200.8:1080
[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 0.0.0.0:8123
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:3421
[Info] v2ray.com/core/transport/internet/kcp: listening on 0.0.0.0:3421
[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 0.0.0.0:3422
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:3422
[Warning] v2ray.com/core: V2Ray 4.25.1 started

根据下文配置可以看到,821、1080、3421、32758、36106(30000-40000端口范围)端口对应的Inbound设置了sockopt,它们也打印了相应的Reuseport输出,而剩余未设置sockopt的Inbound全部未打印。

这也导致了尽管v2ray/v2ray-core#2228 为Inbound引入了SO_REUSEPORT能力,但依然不能修复v2ray/v2ray-core#1971 中的failed to bind source address问题,因为没有为dokodemo-door inbound写sockopt时,Socket 0.0.0.0:53SO_REUSEPORT完全无缘。

你期待看到的正确表现是怎样的?

正确改动后,应该会有如下输出:

[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 0.0.0.0:821
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:821
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 0.0.0.0:8123
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 0.0.0.0:53
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:53
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:3421
[Info] v2ray.com/core/transport/internet/kcp: listening on 0.0.0.0:3421
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 0.0.0.0:3422
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:3422
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 192.168.200.8:1080
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 192.168.200.8:1080
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 0.0.0.0:3423
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:3423
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/tcp: listening TCP on 0.0.0.0:3424
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:36613
[Info] v2ray.com/core/transport/internet/kcp: listening on 0.0.0.0:36613
[Warning] v2ray.com/core/transport/internet: Reuseport: 1
[Info] v2ray.com/core/transport/internet/udp: listening UDP on 0.0.0.0:32783
[Info] v2ray.com/core/transport/internet/kcp: listening on 0.0.0.0:32783
[Warning] v2ray.com/core: V2Ray 4.25.1 started

除了KCP监听外,TCP与UDP监听均成功设置了SO_REUSEPORT(并没有去研究KCP是否应与这个选项搭上关系……)。

在本地运行https://rextester.com/BUAFK86204 中的测试代码,可以看到其他Socket能够正常绑定53端口了:

Test port is 53...
Primary address: 192.168.200.8...
(none)     UDP    Port(2)    0.0.0.0          0.0.0.0          ->  OK
(none)     UDP    Port(2)    0.0.0.0          127.0.0.1        ->  OK
(none)     UDP    Port(2)    0.0.0.0          192.168.200.8    ->  OK
(none)     UDP    Port(2)    127.0.0.1        0.0.0.0          ->  OK
(none)     UDP    Port(2)    127.0.0.1        127.0.0.1        ->  OK
(none)     UDP    Port(2)    127.0.0.1        192.168.200.8    ->  OK
(none)     UDP    Port(2)    192.168.200.8    0.0.0.0          ->  OK
(none)     UDP    Port(2)    192.168.200.8    127.0.0.1        ->  OK
(none)     UDP    Port(2)    192.168.200.8    192.168.200.8    ->  OK
$ netstat -nap | grep :53
tcp6       0      0 :::53                  :::*                    LISTEN      348552/v2ray        
udp        0      0 192.168.200.8:53       0.0.0.0:*                           348569/./sock_reuse 
udp        0      0 0.0.0.0:53             0.0.0.0:*                           348569/./sock_reuse 
udp6       0      0 :::53                  :::*                                348552/v2ray 

服务器端配置:

"inbounds": [
  { 
    "tag":"transparent",
    "protocol": "dokodemo-door",
    "port": 821,
    "sniffing": { "enabled": true, "destOverride": ["http", "tls"] },
    "settings": {
      "network": "tcp,udp",
      "followRedirect": true
    },
    "streamSettings": {
      "sockopt": {
        "tproxy": "tproxy"
      }
    }
  }, {
    "tag": "socks5",
    "protocol": "socks",
    "port": 1080,
    "sniffing": { "enabled": true, "destOverride": ["http", "tls"] },
    "settings": {
      "ip": "127.0.0.1",
      "auth": "noauth",
      "udp": true
    },
    "streamSettings": {
      "sockopt": { "mark": 255 }
    }
  }, { 
    "tag": "http",
    "protocol": "http",
    "port": 8123,
    "sniffing": { "enabled": true, "destOverride": ["http", "tls"] },
    "settings": {
      "timeout": 0
    }
  }, {
    "tag": "dns-in",
    "protocol": "dokodemo-door",
    "port": 53,
    "sniffing": { "enabled": true, "destOverride": ["http", "tls"] },
    "settings": {
      "address": "127.0.0.53",
      "port": 53,
      "network": "tcp,udp"
    }
  }, {
    "tag": "mkcp-remote",
    "protocol": "vmess",
    "port": 3421,
    "sniffing": { "enabled": true, "destOverride": ["http", "tls"] },
    "streamSettings": {
      "network": "mkcp",
      "sockopt": { "tcpFastOpen": true },
      "kcpSettings": {
        "uplinkCapacity": 100,
        "downlinkCapacity": 100,
        "congestion": true,
        "header": { "type": "utp" }
      }
    },
    "settings": {
      "detour": { "to": "vmess-dynamic" },
      "clients": []
    }
  }, {
    "tag": "ss-remote",
    "protocol": "shadowsocks",
    "port": 3422,
    "sniffing": { "enabled": true, "destOverride": ["http", "tls"] },
    "settings": {
      "method": "aes-256-cfb",
      "network": "tcp,udp",
      "email": "",
      "password": ""
    }
  }, {
    "tag": "socks5-remote",
    "protocol": "socks",
    "port": 3423,
    "sniffing": { "enabled": true, "destOverride": ["http", "tls"] },
    "settings": {
      "auth": "password",
      "accounts": [{
        "user": "",
        "pass": ""
      }],
      "ip": "127.0.0.1",
      "udp": true
    }
  }, {
    "tag": "http-remote",
    "protocol": "http",
    "port": 3424,
    "sniffing": { "enabled": true, "destOverride": ["http", "tls"] },
    "settings": {
      "accounts": [{
        "user": "",
        "pass": ""
      }]
    }
  }, {
    "tag": "vmess-dynamic",
    "protocol": "vmess",
    "port": "30000-40000",
    "allocate": {
      "strategy": "random",
      "concurrency": 2,
      "refresh": 3
    },
    "streamSettings": {
      "network": "mkcp",
      "sockOpt": { "tcpFastOpen": true },
      "kcpSettings": {
        "uplinkCapacity": 100,
        "downlinkCapacity": 100,
        "congestion": true,
        "header": { "type": "utp" }
      }
    },
    "sniffing": { "enabled": true, "destOverride": ["http", "tls"] },
    "settings": {
      "default": { "level": 2, "alterId": 64 }
    }
  }
],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant