Xray-core: How to block Bittorrent traffic via nDPI module (IPtables)
Most VPS service providers do not allow Bittorrent downloading due copyright issues. However, some people are not aware of this. And no matter how much this is explained there is always somebody using torrents to download movies. The best way is to stop the torrent traffic within xray-server altogether. But this is not easy.
In theory the protocol could be blocked via this config below.
Inbounds:
"sniffing":{
"enabled":true,
"destOverride":[
"http",
"tls"
]
}
"routing":{
"domainStrategy":"IPIfNonMatch",
"rules":[
{
"type":"field",
"outboundTag":"block",
"protocol":[
"bittorrent"
]
}
]
}
However this doesn’t work reliably with VLESS-VISION-TCP-REALITY.
Looking at the /var/log/xray/access.log I can see some packages are blocked and others go through. It can’t block all of the connections, it will only slow this down.
2023/07/07 16:39:13 140.228.xx:32368 accepted tcp:51.159.4.xxx:55463 [block]
2023/07/07 16:39:13 140.228.xx:32012 accepted tcp:96.244.189.xxx:58946 [direct]
2023/07/07 16:39:18 140.228.xx:29768 accepted tcp:ipv6.torrent.ubuntu.com:443 [block]
2023/07/07 16:39:18 140.228.xx:29378 accepted tcp:torrent.ubuntu.com:443 [block]
I’m using Bittorrent app on Android (from Playstore):
As you can see there is no way to stop the peer-to-peer traffic.
I know from experience, that this nDPI project works with Wireguard.
But because V2Ray/Xray operates at the application layer of the network stack (as opposed to the network layer for Wireguard/OpenVPN), it doesn’t need to alter IP packet headers, so there’s no need for NAT or masquerading.
The core challenge here is that the nDPI module is designed to operate at the network layer, inspecting packets directly. While Xray operates at the application layer, it doesn’t mean nDPI or any L7 filter couldn’t analyze its traffic. Xray, as a proxy tool, encrypts the traffic between the client and the server. This makes it challenging for a DPI tool like nDPI to determine the application-level protocol, because it cannot see into the encrypted data.
Unless there was a way to hook that into the unencrypted traffic within Xray.
So what can we really do to prevent Bittorrent properly?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 32 (1 by maintainers)
Ok I will document it well but I will share it encrypted, but it really is very good, the best solution I have found, it works for any ovpn v2ray protocol etc.I will let you know
https://github.com/UnknownDev2018/block-p2p-UDPGuard
Sorry for the delay. I’ve been very busy. This is my other account. I’m the same @freecode505
Here’s my free script that has managed to block 99% of torrents on my VPN users.
ENJOY!!..
@houmie @kastov @timi-owo @Fangliding @donnyxray
A simplified example would be:
This way it’s not necessary to identify all torrent traffic. You just need to detect 1 single connection to block the user who makes a mistake. In my experience this is 100% effective in avoiding DMCA notices.
Regarding PEX, DHT, etc: I’m sure if someone nefariously wanted to shut down your VPS they could succeed. But this is an unlikely scenario. In my experience it’s just people who make a mistake and are easily detected.
@houmie
your description of traffic flow is correct. client configs stay entirely the same, there is no restriction on inbound protocols that I have seen. the maximal potential impact is that they can’t access certain websites that might have mullvad blocked, and performance
latency/ping is slightly worse, but in the end a lot of VPN providers give you a lot of server choices, and the connection between two western european servers is not that bad.
throughput/bandwidth is usually bottlenecked by GFW. Honestly, latency is also already ruined by GFW. I am not talking about the chinese GFW though.
"outbounds": [ {"protocol": "freedom", ...}, ..., ]withI believe you can have multiple “outbounds” to use multiple mullvad servers, but I haven’t tried, and I don’t understand what the ramifications are if your IP keeps changing during usage. you can use xray to do some custom routing, it’s the same mechanism with which you send certain IPs into “blackhole” outbound.
Keep in mind that the CPU usage increases a lot. I can’t remember how much, probably less than doubled CPU usage. From what I remember, xray’s wireguard support is still a lot more efficient than connecting via SOCKS to a separate wireguard client.
Well, I have tried with Utorrents that according to encrypts the traffic and it is still detected. I have used several clients, blocking torrents little by little, it blocks each p2p IP that shares packages or processes packages on the server, once all are blocked the download will be 0 bytes and finally blocking the IP of the user who downloads these packages. This script has several patterns to detect torrent traffic. In the same way, if you find a client that downloads without being detected, let me know and I will solve it.
Where could i find your solution for v2ray protocol?🙏
@houmie I have created the issue https://github.com/XTLS/Xray-core/issues/4354