[GH-ISSUE #1102] Speed Limit for Inbound and Client #748

Closed
opened 2026-10-31 20:14:09 +07:00 by bsakaguchi · 10 comments
Owner

Originally created by @pro9793 on GitHub (Oct 3, 2023).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/1102

I have to speed limit my clients. May be you can make this new feature to low the cpu usage of the server. To make this I do make the new Inbound for every client, to use different port number, because Ip is dynamic. So that I can limit the speed by tc commands. I use this code to limit 2 clients on ports 64001 and 64002 for example up to 20mbit download rate:
tc qdisc add dev ens3 root handle 1: htb default 1000
tc class add dev ens3 parent 1: classid 1:1 htb rate 20mbit
tc class add dev ens3 parent 1: classid 1:2 htb rate 20mbit
tc filter add dev ens3 protocol ip parent 1:0 prio 1 u32 match ip sport 64001 0xffff flowid 1:1
tc filter add dev ens3 protocol ip parent 1:0 prio 1 u32 match ip sport 64002 0xffff flowid 1:2
You can use these commands to limit the Inbound download speed.

But to limit the clients you need to use Ip instead of port. I used this code in Ikev2 protocol. But in x-ray Ip is dynamic and there is no tunnel interface.
iptables -I FORWARD -s $client1_IP -j MARK --set-mark 1
iptables -I FORWARD -d $client1_IP -j MARK --set-mark 1
tc qdisc add dev ens3 root handle 1: htb
tc class add dev ens3 parent 1:1 classid 1:100 htb rate 20mbit ceil 20mbit
tc qdisc add dev ens3 parent 1:100 sfq perturb 10
tc filter add dev ens3 protocol ip parent 1: prio 1 handle 1 fw flowid 1:100

iptables -I FORWARD -s $client2_IP -j MARK --set-mark 2
iptables -I FORWARD -d $client2_IP -j MARK --set-mark 2
tc class add dev ens3 parent 1:2 classid 1:200 htb rate 20mbit ceil 20mbit
tc qdisc add dev ens3 parent 1:200 sfq perturb 10
tc filter add dev ens3 protocol ip parent 1: prio 1 handle 2 fw flowid 1:200

Originally created by @pro9793 on GitHub (Oct 3, 2023). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/1102 I have to speed limit my clients. May be you can make this new feature to low the cpu usage of the server. To make this I do make the new Inbound for every client, to use different port number, because Ip is dynamic. So that I can limit the speed by tc commands. I use this code to limit 2 clients on ports 64001 and 64002 for example up to 20mbit download rate: tc qdisc add dev ens3 root handle 1: htb default 1000 tc class add dev ens3 parent 1: classid 1:1 htb rate 20mbit tc class add dev ens3 parent 1: classid 1:2 htb rate 20mbit tc filter add dev ens3 protocol ip parent 1:0 prio 1 u32 match ip sport 64001 0xffff flowid 1:1 tc filter add dev ens3 protocol ip parent 1:0 prio 1 u32 match ip sport 64002 0xffff flowid 1:2 You can use these commands to limit the Inbound download speed. But to limit the clients you need to use Ip instead of port. I used this code in Ikev2 protocol. But in x-ray Ip is dynamic and there is no tunnel interface. iptables -I FORWARD -s $client1_IP -j MARK --set-mark 1 iptables -I FORWARD -d $client1_IP -j MARK --set-mark 1 tc qdisc add dev ens3 root handle 1: htb tc class add dev ens3 parent 1:1 classid 1:100 htb rate 20mbit ceil 20mbit tc qdisc add dev ens3 parent 1:100 sfq perturb 10 tc filter add dev ens3 protocol ip parent 1: prio 1 handle 1 fw flowid 1:100 iptables -I FORWARD -s $client2_IP -j MARK --set-mark 2 iptables -I FORWARD -d $client2_IP -j MARK --set-mark 2 tc class add dev ens3 parent 1:2 classid 1:200 htb rate 20mbit ceil 20mbit tc qdisc add dev ens3 parent 1:200 sfq perturb 10 tc filter add dev ens3 protocol ip parent 1: prio 1 handle 2 fw flowid 1:200
bsakaguchi 2026-10-31 20:14:09 +07:00
Author
Owner

@kalilovers commented on GitHub (Oct 9, 2023):

yeah i think this is necessary too

<!-- gh-comment-id:1752846327 --> @kalilovers commented on GitHub (Oct 9, 2023): yeah i think this is necessary too
Author
Owner

@yatakoi commented on GitHub (Oct 16, 2023):

Hello.

Could you please tell me how you configured static client ports?

My one client generates several random UDP ports at once:

root@2078029-cc18895:~/3x-ui# netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      549/zabbix_agentd   
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      516/systemd-resolve 
tcp        0      0 127.0.0.1:62789         0.0.0.0:*               LISTEN      3247/bin/xray-linux 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      579/sshd: /usr/sbin 
tcp6       0      0 :::2053                 :::*                    LISTEN      3221/x-ui           
tcp6       0      0 :::22                   :::*                    LISTEN      579/sshd: /usr/sbin 
tcp6       0      0 :::11948                :::*                    LISTEN      3247/bin/xray-linux 
udp        0      0 127.0.0.53:53           0.0.0.0:*                           516/systemd-resolve 
udp        0      0 82.xx.71.xx:68        0.0.0.0:*                           514/systemd-network 
udp6       0      0 :::55217                :::*                                3247/bin/xray-linux 
udp6       0      0 :::58346                :::*                                3247/bin/xray-linux 
udp6       0      0 :::57846                :::*                                3247/bin/xray-linux 
udp6       0      0 fe80::xx:78ff:xx:546 :::*                                692/dhclient        
udp6       0      0 :::44633                :::*                                3247/bin/xray-linux 
udp6       0      0 :::55416                :::*                                3247/bin/xray-linux 
udp6       0      0 :::55577                :::*                                3247/bin/xray-linux 
udp6       0      0 :::40767                :::*                                3247/bin/xray-linux 
udp6       0      0 :::60265                :::*                                3247/bin/xray-linux 
udp6       0      0 :::40318                :::*                                3247/bin/xray-linux

I use VLESS and every time a client connects, a dynamic port is allocated for it.

<!-- gh-comment-id:1763947312 --> @yatakoi commented on GitHub (Oct 16, 2023): Hello. Could you please tell me how you configured static client ports? My one client generates several random UDP ports at once: ``` root@2078029-cc18895:~/3x-ui# netstat -tulpn Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 549/zabbix_agentd tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 516/systemd-resolve tcp 0 0 127.0.0.1:62789 0.0.0.0:* LISTEN 3247/bin/xray-linux tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 579/sshd: /usr/sbin tcp6 0 0 :::2053 :::* LISTEN 3221/x-ui tcp6 0 0 :::22 :::* LISTEN 579/sshd: /usr/sbin tcp6 0 0 :::11948 :::* LISTEN 3247/bin/xray-linux udp 0 0 127.0.0.53:53 0.0.0.0:* 516/systemd-resolve udp 0 0 82.xx.71.xx:68 0.0.0.0:* 514/systemd-network udp6 0 0 :::55217 :::* 3247/bin/xray-linux udp6 0 0 :::58346 :::* 3247/bin/xray-linux udp6 0 0 :::57846 :::* 3247/bin/xray-linux udp6 0 0 fe80::xx:78ff:xx:546 :::* 692/dhclient udp6 0 0 :::44633 :::* 3247/bin/xray-linux udp6 0 0 :::55416 :::* 3247/bin/xray-linux udp6 0 0 :::55577 :::* 3247/bin/xray-linux udp6 0 0 :::40767 :::* 3247/bin/xray-linux udp6 0 0 :::60265 :::* 3247/bin/xray-linux udp6 0 0 :::40318 :::* 3247/bin/xray-linux ``` I use VLESS and every time a client connects, a dynamic port is allocated for it.
Author
Owner

@pro9793 commented on GitHub (Oct 16, 2023):

Hello.

Could you please tell me how you configured static client ports?

My one client generates several random UDP ports at once:

root@2078029-cc18895:~/3x-ui# netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      549/zabbix_agentd   
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      516/systemd-resolve 
tcp        0      0 127.0.0.1:62789         0.0.0.0:*               LISTEN      3247/bin/xray-linux 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      579/sshd: /usr/sbin 
tcp6       0      0 :::2053                 :::*                    LISTEN      3221/x-ui           
tcp6       0      0 :::22                   :::*                    LISTEN      579/sshd: /usr/sbin 
tcp6       0      0 :::11948                :::*                    LISTEN      3247/bin/xray-linux 
udp        0      0 127.0.0.53:53           0.0.0.0:*                           516/systemd-resolve 
udp        0      0 82.xx.71.xx:68        0.0.0.0:*                           514/systemd-network 
udp6       0      0 :::55217                :::*                                3247/bin/xray-linux 
udp6       0      0 :::58346                :::*                                3247/bin/xray-linux 
udp6       0      0 :::57846                :::*                                3247/bin/xray-linux 
udp6       0      0 fe80::xx:78ff:xx:546 :::*                                692/dhclient        
udp6       0      0 :::44633                :::*                                3247/bin/xray-linux 
udp6       0      0 :::55416                :::*                                3247/bin/xray-linux 
udp6       0      0 :::55577                :::*                                3247/bin/xray-linux 
udp6       0      0 :::40767                :::*                                3247/bin/xray-linux 
udp6       0      0 :::60265                :::*                                3247/bin/xray-linux 
udp6       0      0 :::40318                :::*                                3247/bin/xray-linux

I use VLESS and every time a client connects, a dynamic port is allocated for it.

vless не пробовал,я на shadowsocks настроил - порты слушает те что в панели управления выставил - не рандомные, поэтому tc комманды работают для Inbound. для каждого клиента свой inbound со своим портом

<!-- gh-comment-id:1764323774 --> @pro9793 commented on GitHub (Oct 16, 2023): > Hello. > > Could you please tell me how you configured static client ports? > > My one client generates several random UDP ports at once: > > ``` > root@2078029-cc18895:~/3x-ui# netstat -tulpn > Active Internet connections (only servers) > Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name > tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 549/zabbix_agentd > tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 516/systemd-resolve > tcp 0 0 127.0.0.1:62789 0.0.0.0:* LISTEN 3247/bin/xray-linux > tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 579/sshd: /usr/sbin > tcp6 0 0 :::2053 :::* LISTEN 3221/x-ui > tcp6 0 0 :::22 :::* LISTEN 579/sshd: /usr/sbin > tcp6 0 0 :::11948 :::* LISTEN 3247/bin/xray-linux > udp 0 0 127.0.0.53:53 0.0.0.0:* 516/systemd-resolve > udp 0 0 82.xx.71.xx:68 0.0.0.0:* 514/systemd-network > udp6 0 0 :::55217 :::* 3247/bin/xray-linux > udp6 0 0 :::58346 :::* 3247/bin/xray-linux > udp6 0 0 :::57846 :::* 3247/bin/xray-linux > udp6 0 0 fe80::xx:78ff:xx:546 :::* 692/dhclient > udp6 0 0 :::44633 :::* 3247/bin/xray-linux > udp6 0 0 :::55416 :::* 3247/bin/xray-linux > udp6 0 0 :::55577 :::* 3247/bin/xray-linux > udp6 0 0 :::40767 :::* 3247/bin/xray-linux > udp6 0 0 :::60265 :::* 3247/bin/xray-linux > udp6 0 0 :::40318 :::* 3247/bin/xray-linux > ``` > > I use VLESS and every time a client connects, a dynamic port is allocated for it. vless не пробовал,я на shadowsocks настроил - порты слушает те что в панели управления выставил - не рандомные, поэтому tc комманды работают для Inbound. для каждого клиента свой inbound со своим портом
Author
Owner

@pro9793 commented on GitHub (Oct 16, 2023):

The best way to limit the speed is to add this code automatically on every change of the client IPs (don't look at for-do code and vars, just for example):
for $client$N_ip in $client$N_ips
do
iptables -I FORWARD -s $client$N_IP -j MARK --set-mark $N
iptables -I FORWARD -d $client$N_IP -j MARK --set-mark $N
done
tc qdisc add dev ens3 root handle 1: htb
tc class add dev ens3 parent 1:$N classid 1:$N00 htb rate 20mbit ceil 20mbit
tc qdisc add dev ens3 parent 1:$N00 sfq perturb 10
tc filter add dev ens3 protocol ip parent 1: prio 1 handle $N fw flowid 1:$N00

Also need to clear the old Ips first with iptables -D or rewrite them with new Ip.

<!-- gh-comment-id:1765158197 --> @pro9793 commented on GitHub (Oct 16, 2023): The best way to limit the speed is to add this code automatically on every change of the client IPs (don't look at for-do code and vars, just for example): for $client$N_ip in $client$N_ips do iptables -I FORWARD -s $client$N_IP -j MARK --set-mark $N iptables -I FORWARD -d $client$N_IP -j MARK --set-mark $N done tc qdisc add dev ens3 root handle 1: htb tc class add dev ens3 parent 1:$N classid 1:$N00 htb rate 20mbit ceil 20mbit tc qdisc add dev ens3 parent 1:$N00 sfq perturb 10 tc filter add dev ens3 protocol ip parent 1: prio 1 handle $N fw flowid 1:$N00 Also need to clear the old Ips first with iptables -D or rewrite them with new Ip.
Author
Owner

@yatakoi commented on GitHub (Oct 17, 2023):

Спасибо, добрый человек )

<!-- gh-comment-id:1765476073 --> @yatakoi commented on GitHub (Oct 17, 2023): Спасибо, добрый человек )
Author
Owner

@drno88 commented on GitHub (Oct 21, 2023):

personally, I just set up 3proxy and redirect the client to a local socks proxy, but it already limits the speed)

<!-- gh-comment-id:1773866700 --> @drno88 commented on GitHub (Oct 21, 2023): personally, I just set up 3proxy and redirect the client to a local socks proxy, but it already limits the speed)
Author
Owner

@limjianhui789 commented on GitHub (Dec 22, 2023):

Need this function too

<!-- gh-comment-id:1867891994 --> @limjianhui789 commented on GitHub (Dec 22, 2023): Need this function too
Author
Owner

@drno88 commented on GitHub (Dec 22, 2023):

The best way to limit the speed is to add this code automatically on every change of the client IPs (don't look at for-do code and vars, just for example): for $client$N_ip in $client$N_ips do iptables -I FORWARD -s $client$N_IP -j MARK --set-mark $N iptables -I FORWARD -d $client$N_IP -j MARK --set-mark $N done tc qdisc add dev ens3 root handle 1: htb tc class add dev ens3 parent 1:$N classid 1:$N00 htb rate 20mbit ceil 20mbit tc qdisc add dev ens3 parent 1:$N00 sfq perturb 10 tc filter add dev ens3 protocol ip parent 1: prio 1 handle $N fw flowid 1:$N00

Also need to clear the old Ips first with iptables -D or rewrite them with new Ip.

Hello. Can you tell me how to call it? Or is it suggested to add this to the panel code?

<!-- gh-comment-id:1867916246 --> @drno88 commented on GitHub (Dec 22, 2023): > The best way to limit the speed is to add this code automatically on every change of the client IPs (don't look at for-do code and vars, just for example): for $client$N_ip in $client$N_ips do iptables -I FORWARD -s $client$N_IP -j MARK --set-mark $N iptables -I FORWARD -d $client$N_IP -j MARK --set-mark $N done tc qdisc add dev ens3 root handle 1: htb tc class add dev ens3 parent 1:$N classid 1:$N00 htb rate 20mbit ceil 20mbit tc qdisc add dev ens3 parent 1:$N00 sfq perturb 10 tc filter add dev ens3 protocol ip parent 1: prio 1 handle $N fw flowid 1:$N00 > > Also need to clear the old Ips first with iptables -D or rewrite them with new Ip. Hello. Can you tell me how to call it? Or is it suggested to add this to the panel code?
Author
Owner

@evilnero commented on GitHub (Apr 18, 2025):

FireQOS in Debian/others (part of firehol package) can limit total speed based on souce port, destinatin port...
It sets tc, qdisc, filters, htb and all things automatically. But can harm high-speed channels, and lower their speed in general. If you have 1+ gbit/s channell, using FireQOS will probably limit it, must check enabled/disabled.

For example, use this as fireqos config to set limit to 50 mbit/s:
(change 123.4.5.6 to real ip, as well as network interface and speed)

DEVICE=ens3
INPUT_SPEED=100mbit
OUTPUT_SPEED=100mbit

interface $DEVICE world-in input rate $INPUT_SPEED $LINKTYPE

  class default

  class vless-in max 50mbit

    match dports 443 dst 123.4.5.6 prio 1

interface $DEVICE world-out output rate $OUTPUT_SPEED $LINKTYPE

  class default

  class vless-out max 50mbit

    match sports 443 src 123.4.5.6 prio 1

(docs: https://firehol.org/tutorial/fireqos-new-user/)

<!-- gh-comment-id:2816126975 --> @evilnero commented on GitHub (Apr 18, 2025): FireQOS in Debian/others (part of firehol package) can limit total speed based on souce port, destinatin port... It sets tc, qdisc, filters, htb and all things automatically. But can harm high-speed channels, and lower their speed in general. If you have 1+ gbit/s channell, using FireQOS will probably limit it, must check enabled/disabled. For example, use this as fireqos config to set limit to 50 mbit/s: (change 123.4.5.6 to real ip, as well as network interface and speed) ``` DEVICE=ens3 INPUT_SPEED=100mbit OUTPUT_SPEED=100mbit interface $DEVICE world-in input rate $INPUT_SPEED $LINKTYPE class default class vless-in max 50mbit match dports 443 dst 123.4.5.6 prio 1 interface $DEVICE world-out output rate $OUTPUT_SPEED $LINKTYPE class default class vless-out max 50mbit match sports 443 src 123.4.5.6 prio 1 ``` (docs: https://firehol.org/tutorial/fireqos-new-user/)
Author
Owner

@myxeni64b commented on GitHub (Jun 8, 2026):

Try https://github.com/myxeni64b/xui-bw-guard (https://github.com/myxeni64b/xui-bw-guard)

<!-- gh-comment-id:4644617338 --> @myxeni64b commented on GitHub (Jun 8, 2026): Try https://github.com/myxeni64b/xui-bw-guard (https://github.com/myxeni64b/xui-bw-guard)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#748
No description provided.