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

libtorrent有无考虑开发一下基于UTP的UDP NAT1打洞 #6331

Closed
1265578519 opened this issue Jul 23, 2021 · 44 comments
Closed

libtorrent有无考虑开发一下基于UTP的UDP NAT1打洞 #6331

1265578519 opened this issue Jul 23, 2021 · 44 comments
Labels

Comments

@1265578519
Copy link

1265578519 commented Jul 23, 2021

libtorrent有无考虑开发一下基于UTP的UDP NAT1打洞

比特彗星那边也在做了,不过开发进度有点慢,,而且做出来的效果不尽人意,还在等待后续更新
https://www.cometbbs.com/t/178%E6%B5%8B%E8%AF%95%E7%89%88/59260

以前我也测过utorrent,是根本无法NAT打洞的。。。包括 qBittorrent 也不行,可能开发者都搞错了,现在市面上所有的BT软件,,基本都是需要对方为外部网络,TCP端口开放,然后通过升级到UTP传输而已,而不是打洞

这就是我上面说的,其实目前市面上没有任何一款软件做了打洞,单纯的只是升级为同端口的udp传输而已。
比特彗星那边正在实行的就是基于BEP55来实现PEX报文传递的是客户端的监听端口,这种打洞是没有意义的

测试NAT1打洞需要至少3台设备,测试环境如下
做种服务器A
防火墙开放TCP端口,做种客户端设置为UTP启用

下载服务器B
B服务器在路由器中封堵TCP端口,NAT值为1
一样设置为UTP启用,执行下载,此时服务器A可以看到一个来源为被动的服务器B请求。

下载服务器C
C服务器在路由器中封堵TCP端口,NAT值为4
设置UTP启用,执行下载,测试是否与服务器B连接成功

服务器A和服务器B可为公网IP的云VPS,服务器B在系统防火墙高级设置封堵TCP入站即可,服务器C可为家中电脑设备

顺便吐槽一下,,qBittorrent 这么多年了,单线程传输速率还是这么烂,5MB/S极限了,比特彗星可以跑到80MB/S
https://www.youtube.com/watch?v=pO_nIMSKWd8

translate
Is libtorrent considering developing UTP-based UDP NAT1 hole punching?

Bit Comet is also doing it there, but the development progress is a bit slow, and the results are not satisfactory, and we are still waiting for follow-up updates.
https://www.cometbbs.com/t/178%E6%B5%8B%E8%AF%95%E7%89%88/59260

I have tested utorrent before, and it is impossible to make NAT holes at all. . . Including qBittorrent also does not work, maybe the developers are mistaken, all BT software on the market now basically requires the other party to be an external network, open the TCP port, and then upgrade to UTP for transmission, instead of making holes

This is what I said above. Actually, there is not any software on the market that does hole punching. It is simply upgraded to udp transmission on the same port.
What Bit Comet is implementing is based on BEP55 to realize the PEX message transmission is the listening port of the client. This kind of hole punching is meaningless.

At least 3 devices are required to test NAT1 hole punching. The test environment is as follows
Seed server A
The firewall opens the TCP port, and the client is set to UTP to enable

Download server B
Server B blocks the TCP port in the router, and the NAT value is 1
The same is set to enable UTP, and download is performed. At this time, server A can see a request from server B whose source is passive.

Download server C
Server C blocks the TCP port in the router, and the NAT value is 4
Set UTP to enable, execute download, and test whether the connection with server B is successful

Server A and Server B can be cloud VPS with public IP, server B can block TCP inbound in the advanced settings of the system firewall, and server C can be a home computer device

By the way, qBittorrent has been around for so many years, the single-threaded transmission rate is still so bad, 5MB/S is the limit, BitComet can run to 80MB/S
https://www.youtube.com/watch?v=pO_nIMSKWd8

@arvidn
Copy link
Owner

arvidn commented Jul 23, 2021

Is libtorrent considering developing UTP-based UDP NAT1 hole punching?

Unless "NAT1 hole punching" is a term of art describing some specific kind of hole punching, it already is.

It's limited to full cone NATs though.

@1265578519
Copy link
Author

我使用qbittorrent测试,并不支持nat1打洞

translate
I use qbittorrent to test, it does not support nat1 hole punching

youtube
https://www.youtube.com/watch?v=pO_nIMSKWd8

@arvidn
Copy link
Owner

arvidn commented Jul 23, 2021

so, is this a bug report?

Can you provide more details of your test case, such as logs from all 3 peers (the two NATed as well as the rendezvous peer) and the kind of NATs the peers are behind. Perhaps some independent test report showing that the NATs are indeed full cone (or at least one of them).

@arvidn
Copy link
Owner

arvidn commented Jul 23, 2021

also, you keep saying "nat1" is the different from just "NAT"?

@1265578519
Copy link
Author

1265578519 commented Jul 24, 2021

also, you keep saying "nat1" is the different from just "NAT"?

NAT大致分为下面四类

  1. Full Cone
    这种NAT内部的机器A连接过外网机器C后,NAT会打开一个端口.然后外网的任何发到这个打开的端口的UDP数据报都可以到达A.不管是不是C发过来的.
    例如 A:192.168.8.100 NAT:202.100.100.100 C:292.88.88.88
    A(192.168.8.100:5000) -> NAT(202.100.100.100 : 8000) -> C(292.88.88.88:2000)
    任何发送到 NAT(202.100.100.100:8000)的数据都可以到达A(192.168.8.100:5000)

  2. Restricted Cone
    这种NAT内部的机器A连接过外网的机器C后,NAT打开一个端口.然后C可以用任何端口和A通信.其他的外网机器不行.
    例如 A:192.168.8.100 NAT:202.100.100.100 C:292.88.88.88
    A(192.168.8.100:5000) -> NAT(202.100.100.100 : 8000) -> C(292.88.88.88:2000)
    任何从C发送到 NAT(202.100.100.100:8000)的数据都可以到达A(192.168.8.100:5000)

  3. Port Restricted Cone
    这种NAT内部的机器A连接过外网的机器C后,NAT打开一个端口.然后C可以用原来的端口和A通信.其他的外网机器不行.
    例如 A:192.168.8.100 NAT:202.100.100.100 C:292.88.88.88
    A(192.168.8.100:5000) -> NAT(202.100.100.100 : 8000) -> C(292.88.88.88:2000)
    C(202.88.88.88:2000)发送到 NAT(202.100.100.100:8000)的数据都可以到达A(192.168.8.100:5000)
    以上三种NAT通称Cone NAT.我们只能用这3种NAT进行UDP打洞.

  4. Symmetic
    对于这种NAT.连接不同的外部目标.原来NAT打开的端口会变化.而Cone NAT不会.虽然可以用端口猜测.但是成功的概率很小.因此放弃这种NAT的UDP打洞.

我们看看不同NAT之间的NAT打洞。NAT打洞需要Server配合,需要2种Server:

  1. 类似WebRTC中的信令服务器,作用是帮助客户机沟通IP和PORT信息;
  2. STUN Server,用来让客户机判断自己所在的NAT环境。

维基百科:https://zh.wikipedia.org/wiki/%E7%BD%91%E7%BB%9C%E5%9C%B0%E5%9D%80%E8%BD%AC%E6%8D%A2

translate
NAT is roughly divided into the following four categories

  1. Full Cone
    After machine A inside this NAT connects to machine C on the external network, NAT will open a port. Then any UDP datagram from the external network sent to this open port can reach A. Whether it is sent by C or not.
    For example, A: 192.168.8.100 NAT: 202.100.100.100 C: 292.88.88.88
    A(192.168.8.100:5000) -> NAT(202.100.100.100: 8000) -> C(292.88.88.88:2000)
    Any data sent to NAT (202.100.100.100:8000) can reach A (192.168.8.100:5000)

  2. Restricted Cone
    After machine A inside this NAT connects to machine C on the external network, NAT opens a port. Then C can communicate with A using any port. Other external machines cannot.
    For example A: 192.168.8.100 NAT: 202.100.100.100 C: 292.88.88.88
    A(192.168.8.100:5000) -> NAT(202.100.100.100: 8000) -> C(292.88.88.88:2000)
    Any data sent from C to NAT (202.100.100.100:8000) can reach A (192.168.8.100:5000)

  3. Port Restricted Cone
    After machine A inside this NAT connects to machine C on the external network, NAT opens a port. Then C can communicate with A using the original port. Other external machines cannot.
    For example, A: 192.168.8.100 NAT: 202.100.100.100 C: 292.88.88.88
    A(192.168.8.100:5000) -> NAT(202.100.100.100: 8000) -> C(292.88.88.88:2000)
    Data sent from C (202.88.88.88:2000) to NAT (202.100.100.100:8000) can reach A (192.168.8.100:5000)
    The above three types of NAT are generally called Cone NAT. We can only use these three types of NAT for UDP hole punching.

  4. Symmetic
    For this kind of NAT, connect to different external destinations. The original port opened by the NAT will change. Cone NAT will not. Although you can use the port to guess. But the probability of success is very small. So give up this NAT UDP hole punching.

Let's look at NAT hole punching between different NATs. NAT hole punching requires the cooperation of the server, and two types of servers are required:

  1. Similar to the signaling server in WebRTC, its role is to help clients communicate IP and PORT information;
  2. STUN Server, used to let clients judge their NAT environment.

Wikipedia: https://zh.wikipedia.org/wiki/%E7%BD%91%E7%BB%9C%E5%9C%B0%E5%9D%80%E8%BD%AC%E6%8D%A2

@1265578519
Copy link
Author

so, is this a bug report?

Can you provide more details of your test case, such as logs from all 3 peers (the two NATed as well as the rendezvous peer) and the kind of NATs the peers are behind. Perhaps some independent test report showing that the NATs are indeed full cone (or at least one of them).

如果libtorrent以前支持nat,那则是一个bug报告
在上方帖子有说明,测试机器为NAT1,也就是Full Cone,并且提供了youtube链接。

translate
If libtorrent previously supported nat, it is a bug report
As stated in the above post, the test machine is NAT1, which is Full Cone, and a youtube link is provided.

@1265578519
Copy link
Author

使用的是谷歌翻译,可能存在语言互通的情况,如果有人理解中文,可以跟进该issues #6331

translate
Google Translate is used, and there may be language interoperability. If someone understands Chinese, you can follow up on this issue #6331

@arvidn
Copy link
Owner

arvidn commented Jul 24, 2021

Ok, so when you say "NAT1", you mean "full cone NAT". So when you say just "NAT", do you mean all kinds of NATs?

if so, libtorrent (nor uTorrent) has ever supported "NAT", but supports "NAT1".

a youtube link is not an effective medium to provide details on what's happening in libtorrent.

@arvidn
Copy link
Owner

arvidn commented Jul 24, 2021

I suspect that you are unsure under what scenarios hole-punching is expected to work. Let's start there. What scenario are you testing?

See my questions in #6331 (comment)

@1265578519
Copy link
Author

At least 3 devices are required to test NAT1 hole punching. The test environment is as follows
Seed server A
The firewall opens the TCP port, and the client is set to UTP to enable

Download server B
Server B blocks the TCP port in the router, and the NAT value is 1
The same is set to enable UTP, and download is performed. At this time, server A can see a request from server B whose source is passive.

Download server C
Server C blocks the TCP port in the router, and the NAT value is 4
Set UTP to enable, execute download, and test whether the connection with server B is successful

Server A and Server B can be cloud VPS with public IP, server B can block TCP inbound in the advanced settings of the system firewall, and server C can be a home computer device

如果你对这个 issues 感兴趣的话,可以根据我提供的测试方法测试一下,我不是开发人员不太知道如何描述,测试场景在上方有描述过

translate
If you are interested in this issue, you can test it according to the test method I provided. I am not a developer and I don’t know how to describe it. The test scenario is described above.

@1265578519
Copy link
Author

需要设备C能连接上设备B

translate
Requires device C to connect to device B

@paullouisageneau
Copy link
Contributor

paullouisageneau commented Jul 25, 2021

NAT hole punching requires the cooperation of the server, and two types of servers are required:

1. Similar to the signaling server in WebRTC, its role is to help clients communicate IP and PORT information;

2. STUN Server, used to let clients judge their NAT environment.

@1265578519 What you call UDP hole-punching here is more precisely the ICE connection negotiation technique, as used by WebRTC. If this is what you want, you can enable WebTorrent support in libtorrent (WebTorrent is basically BitTorrent over WebRTC Data Channels).

What Bit Comet is implementing is based on BEP55 to realize the PEX message transmission is the listening port of the client. This kind of hole punching is meaningless.

It's not meaningless, it's basically a simpler implementation of a UDP hole-punching process similar to ICE. The relaying peer acts as both the STUN server (to get the external endpoint after NAT mapping) and the signaling server (to make the other peer connect at the same time to overcome NAT filtering).

NAT is roughly divided into the following four categories

Note those categories should be considered legacy terminology as they are confusing and do not really reflect real implementations. The IETF clarified this with new terminology in RFC 4787. See https://en.wikipedia.org/wiki/Network_address_translation#Methods_of_translation :

Many NAT implementations combine these types, and it is, therefore, better to refer to specific individual NAT behavior instead of using the Cone/Symmetric terminology. RFC 4787 attempts to alleviate confusion by introducing standardized terminology for observed behaviors. [...] The RFC would characterize Full-Cone, Restricted-Cone, and Port-Restricted Cone NATs as having an Endpoint-Independent Mapping, whereas it would characterize a Symmetric NAT as having an Address- and Port-Dependent Mapping. [...] RFC 4787 would also label Full-Cone NAT as having an Endpoint-Independent Filtering, Restricted-Cone NAT as having an Address-Dependent Filtering, Port-Restricted Cone NAT as having an Address and Port-Dependent Filtering, and Symmetric NAT as having either an Address-Dependent Filtering or Address and Port-Dependent Filtering.

With this terminology, UDP hole-punching techniques require at least one of the two NATs to feature Endpoint-Independent Mapping.

@1265578519
Copy link
Author

感谢楼上回复issues,具体怎么实现NAT1打洞就要看各位开发者提交pr。
只要能够实现设备C能连接上设备B,具体怎么开发都行

translate
Thank you for replying to the issues upstairs. How to implement NAT1 hole punching depends on developers submitting pr.
As long as it can be realized that device C can be connected to device B, any specific development will do.

@paullouisageneau
Copy link
Contributor

My point is that UDP hole-punching is already implemented: BEP55 implements it already, and if you want ICE specifically, WebTorrent, which you can activate in the settings, implements it already.

If it doesn't work with you setup, it's most probably a problem with the setup itself, or a bug with libtorrent in a pinch. (Since you said "there is not any software on the market that does hole punching", it's probably a problem with your setup or your understanding of hole-punching.) Like said before in #6331 (comment), you need to provide more information, like logs.

A wild guess is that peer A has only the TCP port open, and therefore can't be used as relaying peer for hole-punching (You need a least one already-connected uTP peer for BEP55 hole-punching).

@1265578519
Copy link
Author

1265578519 commented Jul 26, 2021

我需要的不是WebTorrent,而是基于NAT1的打洞
这个就需要各位测试一下了,上面提供了测试方法,服务器A的TCP和UDP都是通的,并且是NAT1(Full Cone),并不是UdpBlocked,打洞这个很轻松就可以复现出这个问题。

At least 3 devices are required to test NAT1 hole punching. The test environment is as follows
Seed server A
The firewall opens the TCP port, and the client is set to UTP to enable

Download server B
Server B blocks the TCP port in the router, and the NAT value is 1
The same is set to enable UTP, and download is performed. At this time, server A can see a request from server B whose source is passive.

Download server C
Server C blocks the TCP port in the router, and the NAT value is 4
Set UTP to enable, execute download, and test whether the connection with server B is successful

Server A and Server B can be cloud VPS with public IP, server B can block TCP inbound in the advanced settings of the system firewall, and server C can be a home computer device

translate
What I need is not WebTorrent, but hole punching based on NAT1
You need to test this. The test methods are provided above. Server A's TCP and UDP are both connected, and it is NAT1 (Full Cone), not UdpBlocked. This problem can be easily reproduced by drilling holes.

@escape0707
Copy link

escape0707 commented Oct 17, 2021

I watched the youtube vid OP provided, and basically all OP did is that:

  1. He RDP connected to two remote VPS from his host machine.
  2. He ran a software to test the machines' NAT type using a public STUN server. The software is somewhat similar to: https://github.com/HMBSbige/NatTypeTester
  3. The software said that one of the machine is not behind any NAT, and the other one, OP manually set up a restriction to force a FullCone NAT on it. (You can san-check this assertion with the public / local socket info the software reported back.)
  4. He let these two machine and his host machine to transfer one same torrent in between, and theoretically no matter what type of NAT the host machine is behind of, it should be able to connect the machine with FullCone NAT.
  5. The host machine indeed tried to connect to the FullCone one but failed.

One interested thing is that his host machine is only connecting to the seeding machine with TCP/BT. In OP's vid the host machine tried to connect to the NATed peer with only TCP connection, too, which is guaranteed to fail in this case.

But anyway, he didn't provide further detailed information like "what peer info did the host machine get", "which method / protocol did the host machine tried to use to connect to the NATed peer", and "what kind of failure the connection from his host machine to the FullCone one ended up with".

Another thing is that OP's host machine is located in China mainland, (actually OP should try to disguise your IP with VPN / proxy or people will find out that this machine is probably located at someplace near Times Garden Kweilin.) and as far as I know, ISPs at China mainland restrict UDP connection so hard that you can't really use UDP to connect / transfer sh*t abroad, sometimes not even usable with another peer inside China mainland as well. I tried similar things using qTox with my friend while we are in the same province and the UDP connection is pretty f*cked up. I also tried restricting qBittorrent to only allow μTP / UDP to seed 2 TB in demand files consisted of over 120 hot torrents, and can only get about ten active UDP connected peers, let along five of them reside in China, while when I use TCP_bbr, I can seed to over 154 peers at full upload bandwidth.

I think OP should use another abroad VPS or a proxied one to replace the machine in China in this test.

@1265578519
Copy link
Author

感谢回复跟进issues,目前比特彗星1.81版本是全世界第一款支持NAT1 UDP打洞的软件,可以体验
https://download.bitcomet.com/achive/BitComet_1.81.zip
更新记录中可以看到相关的更新信息,已经测试完美支持UDP打洞,这是至关重要的更新,通过它我实现了此特性

核心改进:添加对DHT协议里的implied_port 参数支持,以增强对透过NAT网络进行uTP传输的支持
核心改进:通过PEX发送peer列表时,对已连接的uTP peer提供其UDP外网端口,而不采用其本地监听端口

Thanks for replying to follow up issues. At present, BitComet 1.81 is the first software in the world to support NAT1 UDP hole punching. You can experience it
https://download.bitcomet.com/achive/BitComet_1.81.zip
You can see the relevant update information in the update record. It has been tested to perfectly support UDP hole punching. This is a crucial update, through which I realized this feature

Core Improve: support implied_port parameter in DHT protocol to help improve uTP transport via NAT
Core Improve: use UDP source port instead of listening port for uTP peers when sending peer list through PEX

一些相关使用方法
内网IP UDP打洞使用说明.txt
确保运营商提供为NAT1网络
NAT1有几种对应的说法,例如:NAT1、Full Cone、全锥型

检测自身网络是NAT几
工具菜单选中NAT类型检测对话框,点击开始检测即可

公网IP绿灯下是否还需要进行UDP打洞
不需要,TCP直连端口开放连接性效果最好,有公网单独走TCP更好,就没必要使用UDP传输协议(具体原因下方有说,UDP会受QOS影响)

UTP和UDP有什么关系
UDP是四层传输协议,UTP是七层应用协议,建立在UDP基础上实现,打洞是UDP实现的,UTP自然就也可以同步打洞了

为什么启用UTP后连接成功后传输速度这么慢
UTP是建立在UDP基础上的,UDP经常被黑客利用作于DDOS攻击,自然在全世界任何一个网络提供商都不受欢迎,所以UTP确实存在被运营商QOS问题,需要后续版本加强加密然后传输

其它BT软件支持UDP打洞吗
没有,经过严格测试过utorrent,qbittorrent,deluge,transmission,libtorrent均不支持NAT1 UDP打洞
比特彗星1.81版本是全世界第一款成功做出来支持NAT1 UDP打洞的BT软件

如何启用该版本的UDP打洞功能
内网IP用户想尝试,请在”选项-BT下载-为BT连接启用UTP传输协议“改为自动检测(目前版本保持默认值为禁止状态,等待后续优化传输速率)

Some related usage methods
Intranet IP UDP hole punching instructions.txt
Ensure that the operator provides the network for NAT1
NAT1 has several corresponding terms, such as: NAT1, Full Cone, Full Cone

Check if your own network is NAT
Select the NAT type detection dialog box in the Tools menu, and click Start detection.

Do you still need to perform UDP hole punching under the green light of the public network IP?
No, the TCP direct connection port has the best open connection effect. It is better to have the public network to use TCP alone, so there is no need to use the UDP transmission protocol (the specific reason is mentioned below, UDP will be affected by QOS)

What is the relationship between UTP and UDP
UDP is a four-layer transmission protocol, and UTP is a seven-layer application protocol. It is implemented on the basis of UDP. Hole punching is implemented by UDP. UTP can naturally also punch holes simultaneously.

Why is the transmission speed so slow after a successful connection after UTP is enabled
UTP is based on UDP. UDP is often used by hackers for DDOS attacks. Naturally, it is not popular with any network provider in the world. Therefore, UTP does have the problem of being QOS by operators, and subsequent versions need to be encrypted and transmitted.

Does other BT software support UDP hole punching?
No, after rigorous testing, utorrent, qbittorrent, deluge, transmission, libtorrent do not support NAT1 UDP hole punching
BitComet 1.81 version is the world's first BT software successfully made to support NAT1 UDP hole punching

How to enable this version of UDP hole punching
Intranet IP users want to try, please change "Options-BT download-enable UTP transmission protocol for BT connection" to automatic detection (the current version keeps the default value forbidden, and waits for the subsequent optimization of the transmission rate)

@escape0707
Copy link

I'll be a little bit disappointed but also amused if all you prepared is only for self advertising your "mod" to a closed source software.

@1265578519
Copy link
Author

1265578519 commented Oct 17, 2021

所以我才来libtorrent发issues,希望开源软件能够支持NAT1 UDP打洞,只要libtorrent能够支持,qbittorrent就可以更新libtorrent实现支持

That’s why I came to libtorrent to post issues, hoping that the open source software can support NAT1 UDP hole punching
As long as libtorrent can support, qbittorrent can update libtorrent to support

@escape0707
Copy link

Can you provide more details of your test case, such as logs from all 3 peers (the two NATed as well as the rendezvous peer) and the kind of NATs the peers are behind.

Please do help as you claimed.

@1265578519
Copy link
Author

在我发这个issues之前,bitcomet并不支持NAT1打洞,他们是2021年10月1日更新支持,我是在2021年7月23日发的issues
我并不是广告一个封闭源代码软件,而是他们第一个实现了NAT1 UDP打洞,我的想法是libtorrent可以参照他们提供的更新说明来移植到开源软件中
image
image

Before I posted this issue, bitcomet did not support NAT1 hole punching. They updated support on October 1, 2021. I posted the issue on July 23, 2021.
I am not advertising a closed source software, but they are the first to implement NAT1 UDP hole punching. My idea is that libtorrent can be ported to open source software by referring to the update instructions provided by them.

@1265578519
Copy link
Author

您能否提供有关测试用例的更多详细信息,例如来自所有 3 个对等方(两个 NAT 以及集合点对等方)的日志以及对等方背后的 NAT 类型。

按照您的要求提供帮助

我在上面描述的非常清楚了,你可以看一下之前的issues信息
可能因为一个中英文翻译问题引起不适
A=NAT1, AND open tcp port
B=NAT1, AND clos tcp port
C=NAT4, AND clos tcp port

What I described above is very clear, you can look at the previous issues information
It may cause discomfort due to a Chinese-English translation problem

@escape0707
Copy link

escape0707 commented Oct 17, 2021

我就是因为仔细看了所有前文才如此不适,你应该提供 @arvidn 需要的日志和详细重现步骤作为你非要断定 libtorrent 有漏洞的根据。你没有提供日志的问题开发者已经提过了,你的测试步骤需要拓展我也已经提过了。

I feel so uncomfortable just because I've read all the previous posts carefully. You are the one who should provide the logs and detailed reproduction steps that @arvidn requested as the basis for your conclusion that libtorrent is bugged. The problem that you didn't provide logs has already been mentioned by the developers. Also, I've already suggested that your test steps might need to be expanded as well.

@stale
Copy link

stale bot commented Jan 15, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 15, 2022
@stale stale bot closed this as completed Feb 5, 2022
@1265578519
Copy link
Author

目前libtorrent并未支持nat1打洞,已经被机器人标记过时关闭,估计以后也不会支持了。。毕竟开发难度巨大

@Seeker2
Copy link

Seeker2 commented Feb 6, 2022

Google Translate of previous post:
"At present, libtorrent does not support nat1 hole punching, which has been marked as obsolete and closed by the robot, and it is estimated that it will not be supported in the future. . After all, it is very difficult to develop"

...Has already been proven to be incorrect.
libtorrent DOES support NAT hole-punching under some circumstances.
The problem is when software and/or hardware firewalls intervene to block libtorrent's NAT hole-punching attempts...but that is NOT the same thing as hole-punching NAT alone!

Until the poster does the work required by:
#6331 (comment)
...this is not proven to be a real issue caused by libtorrent bugs/problems.

@1265578519
Copy link
Author

https://www.youtube.com/watch?v=wOLp7f-gb1M
使用比特彗星根据一楼提供方法的NAT1打洞成功例子视频

@k79e
Copy link

k79e commented Oct 2, 2023

Are you joking?
Nat1 don't need puncturehole. Just detect the external port and report the correct port to tracker and it's fine.

@Mythologyli
Copy link

Mythologyli commented Nov 14, 2023

Are you joking? Nat1 don't need puncturehole. Just detect the external port and report the correct port to tracker and it's fine.

I'm afraid not. If you don't perform hole puncturing, you won't even have a port because you're only listening on your router, not the ISP's NAT device.

You may want to read this:
https://www.v2ex.com/t/879549
https://zhuanlan.zhihu.com/p/40816201

@escape0707
Copy link

escape0707 commented Nov 14, 2023

I'm afraid not. If you don't perform hole puncturing, you won't even have a port because you're only listening on your router, not the ISP's NAT device.

Sorry, but your understanding is actually the wrong one. Once your p2p client's NAT status is determined, it includes your Carrier Grade NAT restriction as well. So if you actually did a NAT behavior test and got so called "NAT1", you can just keep your external endpoint alive and just report it to the tracker. By the definition of so called "NAT1", other clients will be able to communicate with you by talking to that endpoint, and it won't change too easily.

Nat FullCone or NAT1 is not the modern terminology for describing these behaviors anymore. Should be "Endpoint-Independent NAT mapping", see RFC-4878 or Tailscale's blog for an easier explanation.

But since you've written "not the ISP's NAT device", I'd suggest you to first do research about how to test your NAT behavior, and how can a STUN server help you with that.

@Mythologyli
Copy link

Mythologyli commented Nov 14, 2023

I'm afraid not. If you don't perform hole puncturing, you won't even have a port because you're only listening on your router, not the ISP's NAT device.

Sorry, but your understanding is actually the wrong one.

Thanks for your reply, I totally understand what you mean. My reply just now was to explain to k79e that hole punching (which in my understanding includes communicating with the STUN server) is necessary. Without this process, the ISP's NAT device would not be aware of the existence of this service. I haven't looked into libtorrent's support for this, but it would obviously require some extra work rather than being as easy as k79e describes

Nat1 don't need puncturehole.

@escape0707
Copy link

My reply just now was to explain to k79e that hole punching (which in my understanding includes communicating with the STUN server) is necessary. Without this process, the ISP's NAT device would not be aware of the existence of this service.

Well, for the case of bittorrent clients, they actually don't communicate to STUN servers. They fetch a list of living peers from dht or pex or tracker, then use other globally routable (or maybe Endpoint-Independet-Mapping) peers as a STUN server equivalent, which is already implemented and mentioned previously. See BEP55

@1265578519
Copy link
Author

截止2023-11-14, libtorrent 2.0.9依旧不支持使用UDP的NAT1打洞

As of 2023-11-14, libtorrent 2.0.9 still does not support UDP NAT1 hole punching

@Mythologyli
Copy link

Well, for the case of bittorrent clients, they actually don't communicate to STUN servers.

Thanks for the wonderful explanation!

By the way, considering the restrictions you mentioned on UDP by ISPs in mainland China, is it possible to implement a TCP hole punching function similar to natter in libtorrent #7207? Currently, we need to use a script like this.

@escape0707
Copy link

escape0707 commented Nov 14, 2023

TCP hole punching is way more difficult to be implemented correctly. And because BitTorrent is a protocol which needs all (different brands of) clients to support it for that feature to function as expected, I highly doubt whether if it will be accepted and popularized even if you created a perfect draft BEP and submitted it.

With that said, as HTTP3 uses QUIC thus UDP right now, I doubt if you will still experience severe degrade of UDP connection quality. What's more is that, most main stream ISP in China now support ipv6 fully. So it's not hard at all now for normal users in China to obtain a globally routable IPv6 address and setup correct firewall rules to allow incoming connection. It's already a consensus (from even IPFilter's devs years ago) that there shouldn't be more resource put into NAT workarounds.

Even then, most of the time when I know some Chinese users who don't have a usable Endpoint-Independent-Mapping environment, it's because of bad router / modem settings and port forwarding / UPnP settings. The three mainstream ISP in China only have CGNAT for IPv4 and it's doing Endpoint-Independent-Mapping already.

@arvidn
Copy link
Owner

arvidn commented Nov 14, 2023

As of 2023-11-14, libtorrent 2.0.9 still does not support UDP NAT1 hole punching

Would you care to define "UDP NAT1 hole punching"?
Especially comparing it to the "UDP NAT hole punching" that's implemented in libtorrent.

@faithleysath
Copy link

faithleysath commented Oct 27, 2024

Understanding Full Cone CGNAT

If a peer is behind a full cone CGNAT, each of its communications will be assigned a shorter lease external port that does not match its listening local port. If the peer reports its local port to the tracker and other peers, the connection will fail to establish.

Requirements for Successful Connections

A peer behind a full cone NAT must have a way to:

  1. Determine the external port mapped by the CGNAT for its listening port.
  2. Relay that information correctly to other peers.

This process is referred to as hole punching.

Current Limitations of qbittorrent

Based on my experience, qbittorrent (which is based on libtorrent) does not currently support hole punching for full cone NAT. It reports the incorrect local listening port to the tracker instead of the external mapped port.

Solutions Found by Users in China

Users in China have discovered methods to address this issue, such as:

  1. Using a specialized STUN client to hole punch on port A to obtain external port C.
  2. Using a webhook to modify qbittorrent's listening port to C, establishing a port mapping of C -> A -> C.

This way, qbittorrent will report the correct port number C to the tracker.

Additional Tools and Scripts

I noticed that qbittorrent supports UPnP/NAT-PMP protocols. In response, I wrote a Python script that runs on the gateway device to:

  • Receive NAT-PMP requests from the qbittorrent client.
  • Automatically perform STUN hole punching.
  • Relay the correct port number back to qbittorrent.

The script is available at: https://github.com/faithleysath/natpmp-stun.

Invitation for Discussion

If anyone has more questions, please reply to let me know. I am happy to discuss.

理解全锥CGNAT

如果一个节点位于全锥型CGNAT后面,每次通信都会被分配一个与其本地监听端口不匹配的短期租约外部端口。如果节点将其本地端口报告给跟踪器和其他节点,连接将无法建立。

成功连接的要求

位于全锥NAT后的节点必须能够:

  1. 确定 CGNAT为其监听端口映射的外部端口。
  2. 正确传递该信息给其他节点。

这个过程称为打洞

qbittorrent当前的限制

根据我的经验,qbittorrent(基于libtorrent)当前不支持全锥NAT的打洞。它向跟踪器报告了错误的本地监听端口,而不是映射的外部端口。

中国用户找到的解决方案

中国用户发现了解决此问题的方法,例如:

  1. 使用专门的STUN客户端在端口A上打洞以获取外部端口C。
  2. 使用webhook来修改qbittorrent的监听端口为C,以建立C -> A -> C的端口映射。

这样,qbittorrent将向跟踪器报告正确的端口号C。

其他工具和脚本

我注意到qbittorrent支持UPnP/NAT-PMP协议。对此,我编写了一个运行在网关设备上的Python脚本:

  • 接收来自qbittorrent客户端的NAT-PMP请求。
  • 自动执行STUN打洞。
  • 将正确的端口号传递回qbittorrent。

脚本可在此处获得:https://github.com/faithleysath/natpmp-stun

讨论邀请

如果有人有更多问题,请回复让我知道。我很乐意讨论。

@1265578519
Copy link
Author

1265578519 commented Oct 27, 2024

比特彗星使用UTP协议进行NAT1 UDP打洞实现原理

比特彗星NAT1打洞主要为以下两个方面
核心改进:添加对DHT协议里的implied_port 参数支持,以增强对透过NAT网络进行uTP传输的支持
核心改进:通过PEX发送peer列表时,对已连接的uTP peer提供其UDP外网端口,而不采用其本地监听端口

对于DHT
发送包
1

回复包
2

对方peer获得端口号与NAT1端口一致
3

对于PEX
与另一个peer建立连接后,双方都会把NAT1正确的端口通过PEX传递给对方与其它所有人,实现打洞

比特彗星的打洞和tracker汇报时发送为监听端口,而不是NAT端口,当前版本没有借助tracker去打洞

PT打洞原理
私有种子根据规范,使用BEP55协议,打洞需要等待2分钟左右
UTP直接发起请求连接失败后,则对自身客户端已经建立TCP和UTP传输连接的所有peer发起BEP55协议请求,随后响应可用于打洞的peer

截至目前libtorrent-2.0.10版本,依旧无法进行NAT1打洞

借助其它脚本实现外部方式的打洞也是可以的,但是实现过程比较复杂,不如直接在软件中内置PEX、DHT打洞算法,毕竟讲究的是默认值,不可能人人都会怎么去使用脚本

@faithleysath
Copy link

比特彗星使用UTP协议进行NAT1 UDP打洞实现原理

比特彗星NAT1打洞主要为以下两个方面
核心改进:添加对DHT协议里的implied_port 参数支持,以增强对透过NAT网络进行uTP传输的支持
核心改进:通过PEX发送peer列表时,对已连接的uTP peer提供其UDP外网端口,而不采用其本地监听端口

对于DHT
发送包
1

回复包
2

对方peer获得端口号与NAT1端口一致
3

对于PEX
与另一个peer建立连接后,双方都会把NAT1正确的端口通过PEX传递给对方与其它所有人,实现打洞

比特彗星的打洞和tracker汇报时发送为监听端口,而不是NAT端口,当前版本没有借助tracker去打洞

PT打洞原理
私有种子根据规范,使用BEP55协议,打洞需要等待2分钟左右
UTP直接发起请求连接失败后,则对自身客户端已经建立TCP和UTP传输连接的所有peer发起BEP55协议请求,随后响应可用于打洞的peer

截至目前libtorrent-2.0.10版本,依旧无法进行NAT1打洞

借助其它脚本实现外部方式的打洞也是可以的,但是实现过程比较复杂,不如直接在软件中内置PEX、DHT打洞算法,毕竟讲究的是默认值,不可能人人都会怎么去使用脚本

Although libtorrent still hasn't implemented what you expected, with the widespread adoption of IPv6 in China and the strict regulation of v4 NAT, full cone NAT (which you referred to as NAT1) is becoming increasingly irrelevant.

@k79e
Copy link

k79e commented Oct 28, 2024

行了行了 真想要的话自己动手吧. 找些pex方面的信息, 留结后人.
不要自己发明这些可用的办法. 那是可以, 但不够好. 为什么不留结其它人彻底解决问题的招术?
就像有人说的, 那个implied开头字段可以修. 但是方法不止一种.
udp tracker上开nat 兼容mode也可以.

udp tracker忽略一切报文里面的port, 只用连接信息里面的port, 这样一定不会错. 还不需要client修改.

dht和pex就不清楚. 没见过文档. 有信息谁都能设计出方案.

dht还是老了, 要是能加个显式nat tag, 也不会闹的错的信息满天飞.
假如我是nat , 但是我不知道我是什么nat. 那就当我是nat1行了, 反正别的也没法用.
此时一个显式, 永久的tag. 一经client设定, 则一直给dht加这个标记.
此时对端见到信息, 忽略port字段. 使用连接信息中的对端port.(IP头那个信息就是nat1的. 不用任何探测) .
同时去nat标记. 改成真的port. 存入buffer. 这不就大功告成. 既不会造成错信息散播 也不会影响正常使用.

@k79e
Copy link

k79e commented Oct 28, 2024

天哪只要ut_holepunch的logic修一下就能支持nat1.
如果不会可以找合作么. 这个工作量又不大. 就是交换连接信息时写正确信息.

If the relaying peer is connected to the target peer, and the target peer supports this extension, the relaying peer sends a connect message to both the initiating peer and the target peer, each containing the endpoint of the other.
//utp 打洞可以看作"我是nat标记"
Upon receiving the connect message, each peer initiates a uTP [2] connection to the other peer.
//这个连接, 收到打洞的人 必需要是公网的 , 否则不行. 很简单. 不需处部服务.
直接用ip头信息回连一下, nat1都能连成!!!!.
nat client 报的信息都是错的. 所以就不要管. 忽略nat user的ip和port字段. 直接用收到的ip头就行了.

这都是什么事哇, 跟本不需要修改. 都不需要新加元素, 己有的就够用了. 但是就是有问题.
可能是老的打洞程序本身就有问题. 所以才有现在的事情.

有人作准备么? 连接头信息叫什么? 是那个文件里的? 己有打洞程序位置是哪里?

@1265578519
Copy link
Author

1265578519 commented Oct 28, 2024

多半美国佬人人都是公网ip,没有体验过nat1
这个修改难度确实不大,但是你看这issues发了几年了,也没更新支持。。。

@k79e
Copy link

k79e commented Oct 28, 2024

@1265578519 他们一开始不知道谁写的logic, 这个200x年就不应该是问题. 但是没人测试才这样!!!!!

@faithleysath
Copy link

faithleysath commented Oct 29, 2024

多半美国佬人人都是公网ip,没有体验过nat1
这个修改难度确实不大,但是你看这issues发了几年了,也没更新支持。。。

Actually, the author is a Swedish guy, not American, haha. But you're right, the author probably didn't have the chance to test this situation; they can't imagine how strict the internet in China is.

实际上作者是瑞典老哥,哈哈

不过你说的对,作者大概率没有办法测试这种情况,他们无法想象中国的网络有多严格。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants