This commit is contained in:
mereith 2024-11-20 20:18:22 +08:00
parent 01f7c57d72
commit 9f9244278d
4 changed files with 76 additions and 1 deletions

View File

@ -4,5 +4,8 @@ FROM nginx:alpine
# 将当前目录下的 index.html 复制到 nginx 的默认网页目录
COPY index.html /usr/share/nginx/html/
# 复制自定义的 nginx 配置文件
COPY nginx.conf /etc/nginx/conf.d/default.conf
# 暴露 80 端口
EXPOSE 80

View File

@ -1,7 +1,7 @@
#!/bin/bash
IMAGE=mereith/tcp-cal
VERSION=v0.0.1
VERSION=v0.1.0
echo "Building Docker image: ${IMAGE}:${VERSION}"

10
nginx.conf Normal file
View File

@ -0,0 +1,10 @@
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}

62
tc_limit.sh Normal file
View File

@ -0,0 +1,62 @@
#!/bin/bash
# 检查是否以root权限运行
if [ "$EUID" -ne 0 ]; then
echo "请使用root权限运行此脚本"
exit 1
fi
# 设置网络参数
echo "正在配置系统网络参数..."
cat >> /etc/sysctl.conf << EOF
net.ipv4.tcp_wmem = 4096 16384 67108864
net.ipv4.tcp_rmem = 4096 87380 67108864
EOF
# 使网络参数生效
sysctl -p
# 获取网络接口名称
echo "可用的网络接口:"
ip a
read -p "请输入要限速的网络接口名称默认eth0: " INTERFACE
INTERFACE=${INTERFACE:-eth0}
# 获取限速值
read -p "请输入限速值Mbit: " SPEED_LIMIT
# 清除现有TC配置
tc qdisc del dev $INTERFACE root 2>/dev/null
# 配置TC限速
echo "正在配置TC限速..."
tc qdisc add dev $INTERFACE root handle 1:0 htb default 10
tc class add dev $INTERFACE parent 1:0 classid 1:1 htb rate ${SPEED_LIMIT}mbit ceil ${SPEED_LIMIT}mbit
tc filter add dev $INTERFACE protocol ip parent 1:0 prio 1 u32 match ip src 0.0.0.0/0 flowid 1:1
tc class add dev $INTERFACE parent 1:0 classid 1:2 htb rate ${SPEED_LIMIT}mbit ceil ${SPEED_LIMIT}mbit
tc filter add dev $INTERFACE protocol ip parent 1:0 prio 1 u32 match ip dst 0.0.0.0/0 flowid 1:2
# 显示TC配置结果
echo "当前TC配置"
tc qdisc show dev $INTERFACE
tc class show dev $INTERFACE
tc -s filter show dev $INTERFACE
# 配置开机自启
echo "正在配置开机自启..."
cat > /etc/rc.local << EOF
#!/bin/bash
tc qdisc add dev $INTERFACE root handle 1:0 htb default 10
tc class add dev $INTERFACE parent 1:0 classid 1:1 htb rate ${SPEED_LIMIT}mbit ceil ${SPEED_LIMIT}mbit
tc filter add dev $INTERFACE protocol ip parent 1:0 prio 1 u32 match ip src 0.0.0.0/0 flowid 1:1
tc class add dev $INTERFACE parent 1:0 classid 1:2 htb rate ${SPEED_LIMIT}mbit ceil ${SPEED_LIMIT}mbit
tc filter add dev $INTERFACE protocol ip parent 1:0 prio 1 u32 match ip dst 0.0.0.0/0 flowid 1:2
exit 0
EOF
# 设置rc.local可执行权限
chmod +x /etc/rc.local
echo "配置完成!系统将在重启后自动应用限速设置"
echo "当前限速值为: ${SPEED_LIMIT}Mbit"