-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetwork.sh
More file actions
855 lines (734 loc) · 29.2 KB
/
network.sh
File metadata and controls
855 lines (734 loc) · 29.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
networkfun() {
beforeMenu(){
_blue "> --- 当前目录: [ $(pwd) ] ---- < v:${branch}-$selfversion"
echo
_yellow "当前菜单: $menuname "
echo
}
#获取网卡
getnetcard() {
# 获取系统中可用的网卡名称
interfaces=$(ifconfig -a | sed -nE 's/^([^[:space:]]+).*$/\1/p')
# 输出供用户选择的网卡名称列表
PS3="请选择网卡名称: "
select interface in $interfaces; do
if [[ -n "$interface" ]]; then
break
fi
done
# 去掉网卡名称后面的冒号,并输出用户选择的网卡名称
interface=$(echo "$interface" | sed 's/://')
echo $interface
}
#实时网速
Realtimenetworkspeedfun() {
check_and_install bmon
bmon
}
#网络信息
netinfo() {
echo
_blue "--本机IP--"
ifconfig -a | grep "inet "
_blue "--路由表--"
route -n
_blue "--活动连接--"
tcpcount=$(netstat -antp | grep ESTABLISHED | wc -l)
udpcount=$(netstat -antp | grep -v ESTABLISHED | wc -l)
#计算百分比
#计算总连接数
totalcount=$(($tcpcount + $udpcount))
check_and_install bc
#计算百分比
cent=$(echo "scale=2; $totalcount / 65535 * 100" | bc)
#最低1%
if [ $(echo "$cent < 1" | bc) -eq 1 ]; then
cent=1
fi
#输出结果
echo
echo "TCP连接数: $tcpcount UDP连接数: $udpcount 总计:$totalcount /65535 ($cent%)"
echo
_blue "--监听端口--"
netstat -tunlp
echo
_blue "--公网IP--"
echo "From cip.cc:" $(curl cip.cc)
echo
echo "From ifconfig.me:" $(curl ifconfig.me)
echo
_blue "--ip地区--"
local org city country region
org="$(wget -q -T10 -O- ipinfo.io/org)"
city="$(wget -q -T10 -O- ipinfo.io/city)"
country="$(wget -q -T10 -O- ipinfo.io/country)"
region="$(wget -q -T10 -O- ipinfo.io/region)"
if [[ -n "${org}" ]]; then
echo "Organization : $(_blue "${org}")"
fi
if [[ -n "${city}" && -n "${country}" ]]; then
echo "Location : $(_blue "${city} / ${country}")"
fi
if [[ -n "${region}" ]]; then
echo "Region : $(_yellow "${region}")"
fi
if [[ -z "${org}" ]]; then
echo "Region : $(_red "No ISP detected")"
fi
_blue "--IP连接数--"
waitinput
echo ' 数量 ip'
netstat -na | grep ESTABLISHED | awk '{print$5}' | awk -F : '{print$1}' | sort | uniq -c | sort -r
echo
_blue "--ssh失败记录--"
waitinput
lastb | grep root | awk '{print $3}' | sort | uniq
echo
}
#ufw防火墙
ufwfun() {
ufwopen() {
check_and_install ufw
ufw enable
echo "ufw已开启"
}
ufwdefault() {
ufw allow 22
echo "已配置允许 22 端口"
ufw default deny
echo "拒绝全部传入"
ufwstatus
}
ufwadd() {
read -ep "请输入端口号 (0-65535): " port
until [[ -n "$port" || "$port" =~ ^[0-9]+$ && "$port" -le 65535 ]]; do
echo "$port: 无效端口."
read -ep "请输入端口号 (0-65535): " port
done
ufw allow $port
echo "端口 $port 已放行"
ufwstatus
}
ufwstatus() {
ufw status verbose
echo "提示:inactive 关闭状态 , active 开启状态"
}
ufwclose() {
read -ep "请输入端口号 (0-65535): " unport
until [[ -n "$unport" || "$unport" =~ ^[0-9]+$ && "$unport" -le 65535 ]]; do
echo "$unport: 无效端口."
read -ep "请输入端口号 (0-65535): " unport
done
ufw delete allow $unport
echo "端口 $unport 已关闭"
ufwstatus
}
ufwdisablefun() {
ufw disable
echo "ufw已关闭"
ufwstatus
}
ufwlogtail() {
tail -f /var/log/ufw.log
}
setufwfromip() {
# 函数:允许特定 IP 和端口的入站流量
allow_ip_port() {
read -ep "请输入ip: " ip
read -ep "请输入端口号 (0-65535): " unport
until [[ -n "$unport" || "$unport" =~ ^[0-9]+$ && "$unport" -le 65535 ]]; do
echo "$unport: 无效端口."
read -ep "请输入端口号 (0-65535): " unport
done
echo "ip:$ip 端口:$unport"
waitinput
ufw allow from $ip to any port $unport
_blue 'ok'
ufwstatus
}
# 函数:拒绝特定 IP 和端口的入站流量
deny_ip_port() {
read -ep "请输入ip: " ip
read -ep "请输入端口号 (0-65535): " unport
until [[ -n "$unport" || "$unport" =~ ^[0-9]+$ && "$unport" -le 65535 ]]; do
echo "$unport: 无效端口."
read -ep "请输入端口号 (0-65535): " unport
done
echo "ip:$ip 端口:$unport"
waitinput
ufw deny from $ip to any port $unport
_blue 'ok'
ufwstatus
}
menuname='首页/网络/ufw/特定ip操作'
options=("允许特定IP和端口的入站流量" allow_ip_port "拒绝特定IP和端口的入站流量" deny_ip_port)
menu "${options[@]}"
}
menuname='首页/网络/ufw'
options=("开启ufw" ufwopen "关闭ufw" ufwdisablefun "ufw默认配置仅ssh" ufwdefault "ufw状态" ufwstatus "查看实时日志" ufwlogtail "添加端口" ufwadd "关闭端口" ufwclose "对特定ip操作" setufwfromip)
menu "${options[@]}"
}
fail2banfun() {
fail2banstatusfun() {
fail2ban-client status sshd
}
installfail2ban() {
check_and_install fail2ban
echo "开始配置fail2ban"
waitinput
read -ep "请输入尝试次数 (直接回车默认4次): " retry
read -ep "请输入拦截后禁止访问的时间 (直接回车默认604800s): " timeban
if [[ "$retry" = "" ]]; then
retry=4
fi
if [[ "$timeban" = "" ]]; then
timeban=604800
fi
rm /etc/fail2ban/jail.d/sshd.local
echo "[ssh-iptables]" >>/etc/fail2ban/jail.d/sshd.local
echo "enabled = true" >>/etc/fail2ban/jail.d/sshd.local
echo "filter = sshd" >>/etc/fail2ban/jail.d/sshd.local
echo "action = iptables[name=SSH, port=ssh, protocol=tcp]" >>/etc/fail2ban/jail.d/sshd.local
echo "logpath = /var/log/auth.log" >>/etc/fail2ban/jail.d/sshd.local
echo "maxretry = $retry" >>/etc/fail2ban/jail.d/sshd.local
echo "bantime = $timeban" >>/etc/fail2ban/jail.d/sshd.local
service fail2ban start
echo "服务已开启"
echo
echo "----服务状态----"
fail2banstatusfun
}
menuname='首页/网络/fail2ban'
options=("安装配置sshd" installfail2ban "查看状态" fail2banstatusfun)
menu "${options[@]}"
}
#iperf3打流
iperftest() {
check_and_install iperf3
iperf3client() {
until [[ "$serversip" ]]; do
read -ep "请输入服务器ip: " serversip
done
_blue '默认udp 手动执行'
next
_yellow "iperf3 -u -c $serversip -b 2000M -t 40"
next
iperf3 -u -c $serversip -b 2000M -t 40
}
echo "请选择运行模式 1.服务端 2.客户端"
until [[ $PROTOCOL_CHOICE =~ ^[1-2]$ ]]; do
read -rp "Protocol [1-2]: " PROTOCOL_CHOICE
done
case $PROTOCOL_CHOICE in
1)
_blue '端口为 5201 请放行端口'
iperf3 -s
;;
2)
iperf3client
;;
esac
}
#nmap扫描
nmapfun() {
check_and_install nmap
nmapdetection() {
echo '本地网络:'
ip addr show | grep "inet " | grep -v "127.0.0.1"
echo
until [[ -n "$ips" ]]; do
read -ep "请输入网段x.x.x.x/x: " ips
done
nmap -sP $ips
}
nmapportcat() {
read -ep "请输入ip: " ip
read -ep "请输入端口(1-65535): " port
nmap "$ip" -p "$port" -Pn
}
echo "1.主机探测 2.端口扫描"
until [[ $PROTOCOL_CHOICE =~ ^[1-2]$ ]]; do
read -rp "Protocol [1-2]: " PROTOCOL_CHOICE
done
case $PROTOCOL_CHOICE in
1)
_blue '扫描网段中有哪些主机在线,本质上是Ping扫描'
nmapdetection
;;
2)
_blue '示例 nmap ip -p 1-2000 -Pn'
nmapportcat
;;
esac
}
#外网测速
publicnettest() {
netfast() {
check_and_install speedtest-cli
echo "开始测速"
speedtest-cli
echo "测速完成"
}
#SpeedCLI 测速
netfast2() {
echo "开始测速"
curl -fsSL git.io/speedtest-cli.sh | sudo bash
speedtest
echo "测速完成"
}
#三网测速
sanwang() {
bash <(curl -Lso- https://down.wangchao.info/sh/superspeed.sh)
}
#多地区测速
netfast3() {
if [ ! -e "./speedtest-cli/speedtest" ]; then
sys_bit=""
local sysarch
sysarch="$(uname -m)"
if [ "${sysarch}" = "unknown" ] || [ "${sysarch}" = "" ]; then
sysarch="$(arch)"
fi
if [ "${sysarch}" = "x86_64" ]; then
sys_bit="x86_64"
fi
if [ "${sysarch}" = "i386" ] || [ "${sysarch}" = "i686" ]; then
sys_bit="i386"
fi
if [ "${sysarch}" = "armv8" ] || [ "${sysarch}" = "armv8l" ] || [ "${sysarch}" = "aarch64" ] || [ "${sysarch}" = "arm64" ]; then
sys_bit="aarch64"
fi
if [ "${sysarch}" = "armv7" ] || [ "${sysarch}" = "armv7l" ]; then
sys_bit="armhf"
fi
if [ "${sysarch}" = "armv6" ]; then
sys_bit="armel"
fi
[ -z "${sys_bit}" ] && _red "Error: Unsupported system architecture (${sysarch}).\n" && exit 1
url1="https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-linux-${sys_bit}.tgz"
url2="https://dl.lamp.sh/files/ookla-speedtest-1.2.0-linux-${sys_bit}.tgz"
if ! wget --no-check-certificate -q -T10 -O speedtest.tgz ${url1}; then
if ! wget --no-check-certificate -q -T10 -O speedtest.tgz ${url2}; then
_red "Error: Failed to download speedtest-cli.\n" && exit 1
fi
fi
mkdir -p speedtest-cli && tar zxf speedtest.tgz -C ./speedtest-cli && chmod +x ./speedtest-cli/speedtest
rm -f speedtest.tgz
fi
printf "%-18s%-18s%-20s%-12s\n" " Node Name" "Upload Speed" "Download Speed" "Latency"
speed_test() {
local nodeName="$2"
if [ -z "$1" ]; then
./speedtest-cli/speedtest --progress=no --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1
else
./speedtest-cli/speedtest --progress=no --server-id="$1" --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1
fi
if [ $? -eq 0 ]; then
local dl_speed up_speed latency
dl_speed=$(awk '/Download/{print $3" "$4}' ./speedtest-cli/speedtest.log)
up_speed=$(awk '/Upload/{print $3" "$4}' ./speedtest-cli/speedtest.log)
latency=$(awk '/Latency/{print $3" "$4}' ./speedtest-cli/speedtest.log)
if [[ -n "${dl_speed}" && -n "${up_speed}" && -n "${latency}" ]]; then
printf "\033[0;33m%-18s\033[0;32m%-18s\033[0;31m%-20s\033[0;36m%-12s\033[0m\n" " ${nodeName}" "${up_speed}" "${dl_speed}" "${latency}"
fi
fi
}
speed_test '' 'Speedtest.net'
speed_test '21541' 'Los Angeles, US'
speed_test '43860' 'Dallas, US'
speed_test '40879' 'Montreal, CA'
speed_test '24215' 'Paris, FR'
speed_test '28922' 'Amsterdam, NL'
speed_test '24447' 'Shanghai, CN'
speed_test '5530' 'Chongqing, CN'
speed_test '60572' 'Guangzhou, CN'
speed_test '32155' 'Hongkong, CN'
speed_test '23647' 'Mumbai, IN'
speed_test '13623' 'Singapore, SG'
speed_test '21569' 'Tokyo, JP'
rm -rf speedtest-cli
}
menuname='首页/网络/外网测速'
options=("测速1" netfast "测速2-SpeedCLI" netfast2 "多地区测速" netfast3 "三网测速" sanwang)
menu "${options[@]}"
}
#配置局域网ip
lanfun() {
cidr2mask() {
# 参数: CIDR (如 24),返回 netmask (如 255.255.255.0)
local cidr=$1
local mask=""
local i
for ((i = 0; i < 4; i++)); do
if (( cidr >= 8 )); then
mask+="255"
cidr=$((cidr-8))
else
if (( cidr > 0 )); then
val=$((256 - 2**(8-cidr)))
mask+="$val"
cidr=0
else
mask+="0"
fi
fi
if (( i < 3 )); then
mask+="."
fi
done
echo "$mask"
}
# Debian (ifupdown) 方式:/etc/network/interfaces
staticip_debian() {
echo "备份原文件 /etc/network/interfaces"
cp /etc/network/interfaces /etc/network/interfaces.bak."$datevar" 2>/dev/null || true
ens=$(getnetcard)
until [[ -n "$ipaddresses" ]]; do
read -ep "请输入ip地址+网络号 (x.x.x.x/x): " ipaddresses
done
ip=${ipaddresses%%/*}
cidr=${ipaddresses#*/}
netmask=$(cidr2mask "$cidr")
until [[ -n "$gateway" ]]; do
read -ep "请输入网关(x.x.x.x): " gateway
done
until [[ -n "$nameservers" ]]; do
read -ep "请输入DNS(多个空格分隔): " nameservers
done
_red "请仔细检查配置是否正确!"
echo "网卡: $ens"
echo "地址: $ip"
echo "CIDR: /$cidr netmask: $netmask"
echo "网关: $gateway"
echo "DNS: $nameservers"
waitinput
cat <<EOF >/etc/network/interfaces
# This file is auto-generated by script (backup at /etc/network/interfaces.bak.*)
auto lo
iface lo inet loopback
auto $ens
iface $ens inet static
address $ip
netmask $netmask
gateway $gateway
dns-nameservers $nameservers
EOF
# 尝试重启网络服务
if command -v systemctl &>/dev/null; then
systemctl restart networking || true
fi
ifdown "$ens" 2>/dev/null || true
ifup "$ens" 2>/dev/null || true
echo "配置已写入 /etc/network/interfaces ,请验证网络连接(若 SSH 中断,使用新 IP 登录)"
}
dhcpip_debian() {
echo "备份原文件 /etc/network/interfaces"
cp /etc/network/interfaces /etc/network/interfaces.bak."$datevar" 2>/dev/null || true
ens=$(getnetcard)
_red "请仔细检查配置是否正确!"
echo "网卡: $ens"
waitinput
cat <<EOF >/etc/network/interfaces
# This file is auto-generated by script (backup at /etc/network/interfaces.bak.*)
auto lo
iface lo inet loopback
auto $ens
iface $ens inet dhcp
EOF
if command -v systemctl &>/dev/null; then
systemctl restart networking || true
fi
ifdown "$ens" 2>/dev/null || true
ifup "$ens" 2>/dev/null || true
echo "DHCP 配置已写入"
}
# netplan (Ubuntu 等) 保持原有实现
staticip_netplan() {
echo "备份原文件 /etc/netplan/00-installer-config.yaml"
cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak."$datevar" 2>/dev/null || true
ens=$(getnetcard)
until [[ -n "$ipaddresses" ]]; do
read -ep "请输入ip地址+网络号 (x.x.x.x/x): " ipaddresses
done
until [[ -n "$gateway" ]]; do
read -ep "请输入网关(x.x.x.x): " gateway
done
until [[ -n "$nameservers" ]]; do
read -ep "请输入DNS(x.x.x.x): " nameservers
done
_red "请仔细检查配置是否正确!"
echo "网卡为 $ens"
echo "网络地址为 $ipaddresses"
echo "网关为 $gateway"
echo "DNS地址为 $nameservers"
waitinput
cat <<EOM >/etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
version: 2
ethernets:
$ens:
dhcp4: no
addresses: [$ipaddresses]
gateway4: $gateway
nameservers:
addresses: [$nameservers]
EOM
echo "配置信息成功写入,若ssh断开,请使用设置的ip重新登录"
netplan apply
echo "ok"
}
dhcpip_netplan() {
echo "开始配置DHCP (netplan)"
echo "备份原文件 /etc/netplan/00-installer-config.yaml"
cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak."$datevar" 2>/dev/null || true
ens=$(getnetcard)
_red "请仔细检查配置是否正确!"
echo "网卡为 $ens"
waitinput
cat <<EOM >/etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
$ens:
dhcp4: true
version: 2
EOM
echo "配置信息成功写入"
netplan apply
echo "ok"
}
# 根据系统选择实现
if [[ -f /etc/os-release ]]; then
. /etc/os-release
ID_LC="${ID,,}"
IDLIKE_LC="${ID_LIKE,,}"
else
ID_LC=""
IDLIKE_LC=""
fi
if [[ "$ID_LC" == "debian" || "$IDLIKE_LC" =~ debian ]]; then
menuname='首页/网络/配置局域网ip (Debian)'
options=("配置静态ip (Debian)" staticip_debian "配置dhcp (Debian)" dhcpip_debian)
else
menuname='首页/网络/配置局域网ip'
options=("配置静态ip" staticip_netplan "配置dhcp" dhcpip_netplan)
fi
menu "${options[@]}"
}
#配置临时代理
http_proxy() {
_blue '配置后仅当前窗口生效,需手动执行'
echo 'export http_proxy=http://x.x.x.x:x'
}
#系统网络配置优化
system_best() {
sed -i '/net.ipv4.tcp_retries2/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_slow_start_after_idle/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_fastopen/d' /etc/sysctl.conf
sed -i '/fs.file-max/d' /etc/sysctl.conf
sed -i '/fs.inotify.max_user_instances/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_syncookies/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_fin_timeout/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_tw_reuse/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_max_syn_backlog/d' /etc/sysctl.conf
sed -i '/net.ipv4.ip_local_port_range/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_max_tw_buckets/d' /etc/sysctl.conf
sed -i '/net.ipv4.route.gc_timeout/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_synack_retries/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_syn_retries/d' /etc/sysctl.conf
sed -i '/net.core.somaxconn/d' /etc/sysctl.conf
sed -i '/net.core.netdev_max_backlog/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_timestamps/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_max_orphans/d' /etc/sysctl.conf
sed -i '/net.ipv4.ip_forward/d' /etc/sysctl.conf
echo "net.ipv4.tcp_retries2 = 8
net.ipv4.tcp_slow_start_after_idle = 0
fs.file-max = 1000000
fs.inotify.max_user_instances = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 32768
net.core.netdev_max_backlog = 32768
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_max_orphans = 32768
# forward ipv4
#net.ipv4.ip_forward = 1" >>/etc/sysctl.conf
sysctl -p
echo "* soft nofile 1000000
* hard nofile 1000000" >/etc/security/limits.conf
echo "ulimit -SHn 1000000" >>/etc/profile
read -p "需要重启VPS后,才能生效系统优化配置,是否现在重启 ? [Y/n] :" yn
[ -z "${yn}" ] && yn="y"
if [[ $yn == [Yy] ]]; then
echo -e "${Info} VPS 重启中..."
reboot
fi
}
portforward() {
check_and_install socat
# 定义函数:启动端口转发
start_port_forward() {
# 获取监听端口和目标地址
read -p "请输入监听端口: " listen_port
read -p "请输入目标 IP: " target_addr
read -p "请输入目标端口: " target_port
# 服务名称
service_name="port-forwarding-$listen_port"
# 创建 systemd 服务文件
service_file="/etc/systemd/system/$service_name.service"
cat <<EOF >$service_file
[Unit]
Description=lookname
After=network.target
[Service]
ExecStart=/usr/bin/socat TCP-LISTEN:$listen_port,fork,reuseaddr TCP:$target_addr:$target_port
Restart=always
RestartSec=5
StandardOutput=file:$HOME/$service_name.log
StandardError=file:$HOME/$service_name.log
[Install]
WantedBy=multi-user.target
EOF
# 重载 systemd 管理器配置
systemctl daemon-reload
# 启动服务
systemctl start $service_name
_blue "服务 $service_name 已启动。"
# 启用服务,使其开机自启
systemctl enable $service_name
_green "服务 $service_name 已设置为开机自启。"
}
# 定义函数:停止端口转发
stop_port_forward() {
# 获取所有以 port-forwarding 开头的服务
services=($(systemctl list-units --full -all | grep 'port-forwarding' | awk '{print $1}'))
if [ ${#services[@]} -eq 0 ]; then
_red "未找到任何 port-forwarding 相关的服务。"
return
fi
echo "以下是所有 port-forwarding 相关的服务:"
for i in "${!services[@]}"; do
status=$(systemctl is-active "${services[$i]}")
echo "$((i + 1)). ${services[$i]} - $status"
done
read -p "请输入要删除的服务编号(输入 0 退出): " choice
if [ "$choice" -eq 0 ]; then
return
fi
index=$((choice - 1))
if [ $index -ge 0 ] && [ $index -lt ${#services[@]} ]; then
service_to_delete="${services[$index]}"
systemctl stop $service_to_delete
systemctl disable $service_to_delete
rm -f "/etc/systemd/system/$service_to_delete"
systemctl daemon-reload
_blue "服务 $service_to_delete 已停止并删除"
else
_red "输入的编号无效,请重新运行该函数。"
fi
}
# 定义函数:检查端口转发状态
list_port_forward() {
_blue "列出所有端口转发:"
pgrep -f "socat TCP-LISTEN:" | xargs -I {} ps -p {} -o pid,cmd
# 获取所有以 port-forwarding 开头的服务
services=($(systemctl list-units --full -all | grep 'port-forwarding' | awk '{print $1}'))
if [ ${#services[@]} -eq 0 ]; then
_red "未找到任何 port-forwarding 相关的服务。"
return
fi
echo "以下是所有 port-forwarding 相关的服务:"
for i in "${!services[@]}"; do
status=$(systemctl is-active "${services[$i]}")
echo "$((i + 1)). ${services[$i]} - $status"
done
}
# 定义函数:主菜单
menuname='首页/网络/端口转发服务'
options=("开启转发" start_port_forward "终止转发" stop_port_forward "列出所有转发" list_port_forward)
menu "${options[@]}"
}
#测试端口延迟
testport(){
# 提示用户输入 IP 地址和端口号
read -p "请输入目标 IP 地址: " ip
read -p "请输入目标端口号: " port
# 记录开始时间
start_time=$(date +%s%N)
# 尝试连接到指定的 IP 和端口
nc -z -w 2 $ip $port
result=$?
# 记录结束时间
end_time=$(date +%s%N)
# 计算延迟(毫秒)
latency=$(( (end_time - start_time) / 1000000 ))
if [ $result -eq 0 ]; then
echo "端口 $port 在 $ip 上开放,延迟约为 $latency 毫秒。"
else
echo "端口 $port 在 $ip 上未开放或连接超时,延迟计算失败。"
fi
}
#切换TCP拥塞控制
switchTCPctrl(){
tcpcc=$(sysctl net.ipv4.tcp_congestion_control | awk -F ' ' '{print $3}')
beforeMenu(){
_blue "> --- 当前目录: [ $(pwd) ] ---- < v:${branch}-$selfversion"
echo
_yellow "当前菜单: $menuname "
echo
_green "当前TCP CC: $tcpcc"
echo
}
tcpccbbr() {
# 检查是否为root权限(修改sysctl.conf需要)
[[ $EUID -ne 0 ]] && { echo "错误:需要root权限,请使用sudo执行"; return 1; }
# 替换或添加配置(避免重复写入)
# 处理默认队列管理算法
if grep -q "^net.core.default_qdisc" /etc/sysctl.conf; then
sed -i 's/^net.core.default_qdisc=.*/net.core.default_qdisc=fq/' /etc/sysctl.conf
else
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
fi
# 处理拥塞控制算法
if grep -q "^net.ipv4.tcp_congestion_control" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.tcp_congestion_control=.*/net.ipv4.tcp_congestion_control=bbr/' /etc/sysctl.conf
else
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
fi
sysctl -p
_blue "已设置TCP拥塞控制为BBR"
}
tcpcccubic() {
[[ $EUID -ne 0 ]] && { echo "错误:需要root权限,请使用sudo执行"; return 1; }
# 处理默认队列管理算法(CUBIC也可搭配fq,或根据需求调整为pfifo_fast)
if grep -q "^net.core.default_qdisc" /etc/sysctl.conf; then
sed -i 's/^net.core.default_qdisc=.*/net.core.default_qdisc=fq/' /etc/sysctl.conf
else
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
fi
# 处理拥塞控制算法
if grep -q "^net.ipv4.tcp_congestion_control" /etc/sysctl.conf; then
sed -i 's/^net.ipv4.tcp_congestion_control=.*/net.ipv4.tcp_congestion_control=cubic/' /etc/sysctl.conf
else
echo "net.ipv4.tcp_congestion_control=cubic" >> /etc/sysctl.conf
fi
sysctl -p
_blue "已设置TCP拥塞控制为CUBIC"
}
menuname='首页/网络/切换TCP拥塞控制'
options=("切换成bbr" tcpccbbr "切换成cubic" tcpcccubic )
menu "${options[@]}"
}
menuname='首页/网络'
echo "networkfun" >$installdir/config/lastfun
options=("网络信息" netinfo "实时网速" Realtimenetworkspeedfun "外网测速" publicnettest "iperf3打流" iperftest "临时http代理" http_proxy "配置局域网ip" lanfun "nmap扫描" nmapfun "ufw" ufwfun "fail2ban" fail2banfun "系统网络配置优化" system_best "端口转发服务" portforward "测试端口延迟" testport "切换TCP拥塞控制" switchTCPctrl)
menu "${options[@]}"
}