From 9f9244278da2473a75fb7ae2b30757378ae837b4 Mon Sep 17 00:00:00 2001 From: mereith Date: Wed, 20 Nov 2024 20:18:22 +0800 Subject: [PATCH] fix --- Dockerfile | 3 +++ build.sh | 2 +- nginx.conf | 10 +++++++++ tc_limit.sh | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 nginx.conf create mode 100644 tc_limit.sh diff --git a/Dockerfile b/Dockerfile index f4ea213..f59319a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ No newline at end of file diff --git a/build.sh b/build.sh index 63f4980..07f56d0 100644 --- a/build.sh +++ b/build.sh @@ -1,7 +1,7 @@ #!/bin/bash IMAGE=mereith/tcp-cal -VERSION=v0.0.1 +VERSION=v0.1.0 echo "Building Docker image: ${IMAGE}:${VERSION}" diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..61628b8 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,10 @@ +server { + listen 80; + server_name _; + root /usr/share/nginx/html; + index index.html; + + location / { + try_files $uri $uri/ /index.html; + } +} \ No newline at end of file diff --git a/tc_limit.sh b/tc_limit.sh new file mode 100644 index 0000000..5cf095c --- /dev/null +++ b/tc_limit.sh @@ -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" \ No newline at end of file