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

添加 tun 支持 #1814

Closed
wants to merge 1 commit into from
Closed

添加 tun 支持 #1814

wants to merge 1 commit into from

Conversation

nekohasekai
Copy link
Collaborator

用例:

{
  "tun": {
    "inet4_address": "172.19.0.1/30",
    "stack": "system",
    "auto_route": true,
    "auto_detect_interface": true,
    "override_android_vpn": false
  },
  "outbounds": [
    {
      "protocol": "freedom"
    }
  ]
}

其他参数参考 https://sing-box.sagernet.org/configuration/inbound/tun/

@nekohasekai nekohasekai requested review from RPRX and yuhan6665 March 18, 2023 06:18
@sougouzhinan

This comment was marked as off-topic.

Copy link
Member

@yuhan6665 yuhan6665 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢大佬!有一个问题麻烦看下

我要学习一下如何在 windows 上开 tun。。是否按用例配置并确保 core 有管理员权限即可?另外这个 tun 在安卓上使用 只需要开 override_android_vpn = true?有没有其它需要注意的?

app/dispatcher/default.go Outdated Show resolved Hide resolved
@nekohasekai
Copy link
Collaborator Author

nekohasekai commented Mar 24, 2023

是否按用例配置并确保 core 有管理员权限即可?

是的。

另外这个 tun 在安卓上使用 只需要开 override_android_vpn = true?

override_android_vpn 应该作为选项提供, 在 android 使用时您需要自定义 core 以提供由平台创建的 tun 文件描述符。

有没有其它需要注意的?

确保启用 auto_route 代理所有流量时打开 auto_detect_interface 以避免流量循环。

@yuhan6665
Copy link
Member

在 windows 试了一下遇到两个问题
一个是 sing-tun 需要开 cgo?这个具体是为什么?目前 xray 的编译都是关掉的。我自己试验的时候交叉编译会有一点问题。
二是编译之后成功打开了 tun 但是 dispatch 似乎有点问题:

panic: file already closed

goroutine 13 [running]:
github.com/xtls/xray-core/common.Must(...)
	github.com/xtls/xray-core/common/common.go:23
github.com/xtls/xray-core/app/proxyman/outbound.(*Handler).Dispatch(0xc000300300?, {0x11fef00, 0xc000210180}, 0xc000228060)
	github.com/xtls/xray-core/app/proxyman/outbound/handler.go:160 +0x465
github.com/xtls/xray-core/app/dispatcher.(*DefaultDispatcher).routedDispatch(0xc0002ee1e0, {0x11fef00, 0xc000210180}, 0xc000228060, {{0x1200e80, 0xc000380040}, 0x89, 0x3})
	github.com/xtls/xray-core/app/dispatcher/default.go:490 +0xbfe
github.com/xtls/xray-core/app/dispatcher.(*DefaultDispatcher).DispatchLink(0x0?, {0x11fef00, 0xc000210120}, {{0x1200e80?, 0xc000380040?}, 0x0?, 0x0?}, 0xc000228060)
	github.com/xtls/xray-core/app/dispatcher/default.go:345 +0x686
github.com/xtls/xray-core/app/tun.(*Tun).NewPacketConnection(0xc000324000, {0x11fef00, 0xc0003143c0}, {0x1205020?, 0xc00033e000}, {{0x0, 0x0}, {{{0x0, 0xffffac130001}, 0xc000008180}, ...}, ...})
	github.com/xtls/xray-core/app/tun/tun.go:228 +0x3c4
github.com/sagernet/sing/common/udpnat.(*Service[...]).NewContextPacket.func2()
	github.com/sagernet/sing@v0.2.1/common/udpnat/service.go:83 +0xe6
created by github.com/sagernet/sing/common/udpnat.(*Service[...]).NewContextPacket
	github.com/sagernet/sing@v0.2.1/common/udpnat/service.go:82 +0x3d2

@nekohasekai
Copy link
Collaborator Author

没有依赖 CGO,关于第二个问题,请将 common.Must 删除。

@yuhan6665
Copy link
Member

没有依赖 CGO,关于第二个问题,请将 common.Must 删除。

测试可以了! 测了一下 vmess 通 但是 reality 好像还有点问题。。
编译后大小 27.4 -> 28.5 压缩后 9.23 -> 9.62
CC @RPRX

@RPRX
Copy link
Member

RPRX commented Mar 26, 2023

但是 reality 好像还有点问题。。

是什么现象

@yuhan6665
Copy link
Member

yuhan6665 commented Mar 26, 2023

proxy/vless/outbound: Reader is not timeout reader, will send out vless header separately from first payload
破案了 是因为 tun interface 不支持读 timeout 所以 vless 无法粘首包 那块代码有点问题所以不通
@nekohasekai tun 有可能支持 timeout 吗?

@nekohasekai
Copy link
Collaborator Author

是支持的,看起来只是没有实现 xray 的 interface。

@yuhan6665
Copy link
Member

感谢 测试无问题

@yuhan6665
Copy link
Member

As discussed, plan to merge for v1.9.0

@hosseinkhojany
Copy link

hosseinkhojany commented May 14, 2023

这是一个了不起的功能
我在 REALITY 设置上进行了测试,几秒钟后出现了这个错误
Xray 1.8.1 (Xray, Penetrates Everything.) 6032640 (go1.20.3 windows/amd64)
A unified platform for anti-censorship.
2023/05/14 19:53:03 [Info] infra/conf/serial: Reading config: c.json
2023/05/14 19:53:04 [Warning] core: Xray 1.8.1 started
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x20 pc=0x156044c]

goroutine 416 [running]:
github.com/xtls/xray-core/common/singbridge.ToSocksaddr({{0x0?, 0x0?}, 0x0?, 0x0?})
github.com/xtls/xray-core/common/singbridge/destination.go:38 +0x6c
github.com/xtls/xray-core/app/tun.(*PacketConn).WriteMultiBuffer(0xc000353210, {0xc00000a060?, 0x1, 0xc0001f9cf0?})
github.com/xtls/xray-core/app/tun/packet_conn.go:36 +0x1d8
github.com/xtls/xray-core/common/buf.copyInternal({0x1f73fc0, 0xc00023af60}, {0x1f73060, 0xc000353210}, 0xc00025e1e0)
github.com/xtls/xray-core/common/buf/copy.go:87 +0x175
github.com/xtls/xray-core/common/buf.Copy({0x1f73fc0, 0xc00023af60}, {0x1f73060, 0xc000353210}, {0xc0001f9eb8, 0x1, 0xc00004e048?})
github.com/xtls/xray-core/common/buf/copy.go:104 +0xa5
github.com/xtls/xray-core/proxy/vless/outbound.(*Handler).Process.func4()
github.com/xtls/xray-core/proxy/vless/outbound/outbound.go:304 +0x5d5
github.com/xtls/xray-core/common/task.OnSuccess.func1()
github.com/xtls/xray-core/common/task/task.go:12 +0x25
github.com/xtls/xray-core/common/task.Run.func1(0x0?)
github.com/xtls/xray-core/common/task/task.go:28 +0x2e
created by github.com/xtls/xray-core/common/task.Run
github.com/xtls/xray-core/common/task/task.go:27 +0xde

@charliez0
Copy link

charliez0 commented May 18, 2023

遇到了同样的问题,貌似是发生在内置dns请求完dns返回的时候 @RPRX

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x20 pc=0x76044c]

goroutine 191 [running]:
github.com/xtls/xray-core/common/singbridge.ToSocksaddr({{0x0?, 0x0?}, 0x0?, 0x0?})
        github.com/xtls/xray-core/common/singbridge/destination.go:38 +0x6c
github.com/xtls/xray-core/app/tun.(*PacketConn).WriteMultiBuffer(0xc0003356e0, {0xc0001081f0?, 0x1, 0x1700000000000000?})
        github.com/xtls/xray-core/app/tun/packet_conn.go:36 +0x1d8
github.com/xtls/xray-core/common/protocol/dns.(*UDPWriter).WriteMessage(0xc000335770, 0xc0011e8630)
        github.com/xtls/xray-core/common/protocol/dns/io.go:125 +0x6a
github.com/xtls/xray-core/proxy/dns.(*Handler).handleIPQuery(0xc0008b0b40?, 0x3186, 0x1, {0xc000b92000, 0x17}, {0x1173640, 0xc000335770})
        github.com/xtls/xray-core/proxy/dns/dns.go:290 +0x8a3
created by github.com/xtls/xray-core/proxy/dns.(*Handler).Process.func2
        github.com/xtls/xray-core/proxy/dns/dns.go:176 +0x295

@yuhan6665 yuhan6665 force-pushed the main branch 2 times, most recently from c73e413 to a4e80f0 Compare June 7, 2023 16:27
@A5DkjGQUZx
Copy link

@RPRX
Would it be possible to have a fully tested and working TUN support in the next release please?
Thanks :)

@A5DkjGQUZx
Copy link

As discussed, plan to merge for v1.9.0

Hi

  1. Any ETA for the release?
  2. What would be the performance of this TUN interface e.g. in comparison to https://github.com/heiher/hev-socks5-tunnel
    Thanks :)

@zhouxinghong
Copy link

+1

@XTLS XTLS locked as off-topic and limited conversation to collaborators Oct 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants