由于Unbinilium的Twist 脚本长时间不更新且已删库,想自己维护但确实不太会写shell脚本,只好把他脚本里的内容一步步写下来,当作日记,也方便自己日后使用。
前置准备 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 cd /tmpsudo mkdir /etc/twistsudo apt updatesudo apt upgradesudo apt install -y wget gawk grep curl sed git gcc swig gettext autoconf \ automake make libtool perl cpio xmlto asciidoc cron net-tools dnsutils \ rng-tools libc-ares-dev libev-dev openssl libssl-dev zlib1g-dev libpcre3-dev \ libevent-dev build-essential python3-dev python3-pip python3-setuptools \ python3-qrcode nginx fail2ban certbot python3-certbot-nginx tmux btop tree route | grep '^default' | grep -o '[^ ]*$' ip -4 route list 0/0 | grep -Po '(?<=dev )(\S+)' cat /sys/class/net/eth0/operstatedig @resolver1.opendns.com -t A -4 myip.opendns.com +short ip -6 addr show eth0 curl -s diagnostic.opendns.com/myip cat /sys/class/net/eth0/mtu
其他
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 sudo useradd shadowsockssudo swaponsudo swapoff /tmp/swapfilesudo rm /tmp/swapfilesudo vi /etc/fstabselect-editor
开启bbr算法 如果内核大于等于4.8,可以直接开启bbr算法,否则可以升级内核后开启
1 2 3 4 5 6 [ -e /proc/user_beancounters ] && echo "存在" || echo "不存在" uname -r | grep -oE '[0-9]+\.[0-9]+'
内核版本大于等于4.8或者内核已经升级
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 cp /etc/sysctl.conf /etc/twist/sysctl.conf.old-$(date +%Y%m%d%H%M%S)sudo sysctl net.ipv4.tcp_available_congestion_controlls /lib/modules/$(uname -r)/kernel/net/ipv4/tcp_bbr.ko*sudo modprobe tcp_bbrsed -i '/net.ipv4.tcp_congestion_control/d' /etc/sysctl.conf echo "net.ipv4.tcp_congestion_control = bbr" >> /etc/sysctl.conf
更新内核(我没尝试过)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 KERNELURL="https://kernel.ubuntu.com/~kernel-ppa/mainline/" wget -qO- "$KERNELURL " | awk -F'\"v' '/v[4-9]./{print $2}' | cut -d/ -f1 | grep -v - | sort -V | tail -1 dpkg --print-architecture KERNELVER="$(wget -qO- ${KERNELURL} | awk -F'\"v' '/v[4-9]./{print $2}' | cut -d/ -f1 | grep -v - | sort -V | tail -1) " for pkg in linux-headers linux-headers-all linux-modules linux-image-unsigned; do FILE=$(wget -qO- "${KERNELURL} v${KERNELVER} /" | grep "$pkg " | grep "generic" | grep "amd64.deb" | awk -F'\">' '{print $2}' | cut -d'<' -f1 | head -1) [ -n "$FILE " ] && wget -c "${KERNELURL} v${KERNELVER} /${FILE} " done sudo dpkg -i *.debsudo update-grubsudo reboot
安装shadowsocks-rust shadowsocks-rust跟shadowsocks-libev不同,不支持xchacha20-ietf-poly1305算法
1 2 3 4 5 6 7 8 9 10 curl -LO https://github.com/shadowsocks/shadowsocks-rust/releases/download/v1.24.0/shadowsocks-v1.24.0.x86_64-unknown-linux-gnu.tar.xz sha256sum shadowsocks-v1.24.0.x86_64-unknown-linux-gnu.tar.xztar Jxf shadowsocks-v1.24.0.x86_64-unknown-linux-gnu.tar.xz -C /usr/local/bin
创建systemd服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [Unit] Description =Shadowsocks-Rust ServiceAfter =network.target[Service] User =shadowsocksGroup =shadowsocksType =simpleExecStart =/usr/local/bin/ssserver -c /etc/shadowsocks-rust/config.jsonRestart =on -failureLimitNOFILE =512000 [Install] WantedBy =multi-user.target
安装simple-obfs:simple-obfs已经不更新了,各大发行版也移除了,但目前还是需要用到
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 git clone https://github.com/shadowsocks/simple-obfs.git cd simple-obfsgit submodule update --init --recursive ./autogen.sh ./configure --prefix=/usr/local/simple-obfs && make sudo make install
配置shadowsocks-rust 确定监听地址
DNS
密码生成
1 2 3 4 5 PASSWORD="$(< /dev/urandom tr -dc 'A-HJ-NPR-Za-km-z2-9-._+?%^&*() ' | head -c 8)" PASSWORD="$(ssservice genkey -m "aes-256-gcm" ) "
生成json文件
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 mkdir -p /etc/shadowsocks-rustcat > /etc/shadowsocks-rust/config.json <<-EOF { "server":"0.0.0.0", "server_port":443, "password":"${PASSWORD}", "method":"aes-256-gcm", "timeout":1800, "udp_timeout":1800, "plugin":"/usr/local/simple-obfs/bin/obfs-server", "plugin_opts":"obfs=tls;obfs-host=microsoft.com;obfs-uri=/", "fast_open":true, "reuse_port":true, "nofile":512000, "nameserver":"8.8.8.8,8.8.4.4", "dscp":"EF", "mode":"tcp_and_udp", "mtu":1500, "mptcp":false, "ipv6_first":false, "use_syslog":true, "no_delay":true, } EOF
配置内核参数
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 cat >> /etc/sysctl.conf <<-EOF # Twist fs.file-max = 512000 net.core.rmem_max = 67108864 net.core.wmem_max = 67108864 net.core.netdev_max_backlog = 256000 net.core.somaxconn = 4096 net.ipv4.udp_mem = 25600 51200 102400 net.ipv4.tcp_mem = 25600 51200 102400 net.ipv4.tcp_rmem = 4096 87380 67108864 net.ipv4.tcp_wmem = 4096 65536 67108864 net.ipv4.ip_local_port_range = 49152 65535 net.ipv4.tcp_max_syn_backlog = 8192 net.ipv4.tcp_max_tw_buckets = 4096 net.core.default_qdisc = fq net.ipv4.ip_forward = 1 net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_timestamps = 1 net.ipv4.tcp_fack = 1 net.ipv4.tcp_sack = 1 net.ipv4.tcp_dsack = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fwmark_accept = 1 net.ipv4.tcp_stdurg = 1 net.ipv4.tcp_synack_retries = 30 net.ipv4.tcp_syn_retries = 30 net.ipv4.tcp_rfc1337 = 1 net.ipv4.tcp_fin_timeout = 60 net.ipv4.tcp_keepalive_time = 1800 net.ipv4.tcp_mtu_probing = 2 net.ipv4.tcp_fastopen = 3 net.ipv4.tcp_low_latency = 1 net.ipv4.udp_l3mdev_accept = 1 net.ipv4.fib_multipath_hash_policy = 1 net.ipv4.fib_multipath_use_neigh = 1 net.ipv4.cipso_rbm_optfmt = 1 net.ipv4.fwmark_reflect = 1 net.ipv4.conf.all.accept_source_route = 1 net.ipv4.conf.all.accept_redirects = 1 net.ipv4.conf.all.send_redirects = 1 net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.all.arp_accept = 1 net.ipv4.conf.all.arp_announce = 1 net.ipv4.conf.all.proxy_arp = 1 net.ipv4.conf.all.proxy_arp_pvlan = 1 net.ipv4.conf.all.mc_forwarding = 1 net.ipv6.conf.all.forwarding = 1 net.ipv6.conf.all.accept_source_route = 1 net.ipv6.conf.all.accept_redirects = 1 net.ipv6.conf.all.autoconf = 1 net.ipv6.conf.all.accept_ra = 2 net.ipv6.conf.all.seg6_enabled = 1 # conntrack accounting # net.netfilter.nf_conntrack_acct=1 EOF
配置用户或进程的资源限制
1 2 3 4 5 6 7 cp /etc/security/limits.conf /etc/security/limits.conf.old-$(date +%Y%m%d%H%M%S)echo "* soft nofile 512000" >> /etc/security/limits.confecho "* hard nofile 512000" >> /etc/security/limits.confecho "" >> /etc/security/limits.conf
DNS 配置
1 2 3 4 5 6 7 8 9 echo "nameserver 8.8.8.8" > /etc/resolv.confecho "nameserver 8.8.8.4" >> /etc/resolv.confecho "" >> /etc/resolv.conf
配置防火墙 ufw twist用的是iptables,比较专业,但我用ufw比较多,下面是ufw的同等配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 sudo ufw allow 22/tcpsudo ufw allow 80/tcpsudo ufw allow 80/udpsudo ufw allow 443/tcpsudo ufw allow 443/udpsudo ufw deny out to 10.0.0.0/8sudo ufw deny out to 172.16.0.0/12sudo ufw deny out to 192.168.0.0/16sudo ufw deny out to 169.254.0.0/16sudo ufw deny out to 100.64.0.0/10sudo ufw deny out to 198.18.0.0/15sudo ufw deny out to fc00::/7sudo ufw deny out to fe80::/10
内核参数配置
1 2 3 4 net/ipv4/ip_forward=1 net/ipv6/conf/all/forwarding=1
细粒度的iptables修改
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 *nat :POSTROUTING ACCEPT [0:0] -A POSTROUTING -o ${ETH} -j MASQUERADE COMMIT -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
启动ufw并设置开机启动
1 2 3 4 5 6 sudo ufw enable sudo systemctl enable --now ufwsudo ufw status verbose
nftables 注意:nftables跟iptables一样,很危险,不要随意操作
临时修改 使用 nft 命令
rule 的顺序很重要。add 默认将规则插入到最后,insert 默认插入到最前面,replace 直接替换指定 rule。操作前先用 nft -a list ruleset 查看 handle
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 nft add table inet filter nft add table ip nat nft add table ip6 nat nft add chain inet filter input '{ type filter hook input priority 0; policy drop; }' nft add chain inet filter before-input nft add chain inet filter main-input nft add chain inet filter after-input nft add chain inet filter forward '{ type filter hook forward priority 0; policy drop; }' nft add chain inet filter output '{ type filter hook output priority 0; policy accept; }' nft add chain inet filter before-output nft add chain inet filter main-output nft add rule inet filter input jump before-input nft add rule inet filter input jump main-input nft add rule inet filter input jump after-input nft add rule inet filter input drop nft add rule inet filter before-input iifname "lo" counter accept nft add rule inet filter before-input ct state established,related counter accept nft add rule inet filter before-input ct state invalid counter drop nft add rule inet filter before-input ip protocol icmp icmp type echo-request limit rate 10/second burst 20 packets counter accept nft add rule inet filter before-input ip protocol icmp icmp type { destination-unreachable, time-exceeded, parameter-problem } counter accept nft add rule inet filter before-input meta l4proto ipv6-icmp icmpv6 type echo-request limit rate 10/second burst 20 packets counter accept nft add rule inet filter before-input meta l4proto ipv6-icmp icmpv6 type { echo-reply, destination-unreachable, packet-too-big, time-exceeded, parameter-problem } counter accept nft add rule inet filter main-input tcp dport 22 ct state new limit rate 5/minute burst 5 packets counter accept comment "input-tcp-22-limited" nft add rule inet filter main-input tcp dport 80 counter accept comment "input-tcp-80" nft add rule inet filter main-input tcp dport 443 counter accept comment "input-tcp-443" nft add rule inet filter main-input udp dport 443 counter accept comment "input-udp-443" nft add rule inet filter main-input tcp dport 30001 meta mark set 1 counter accept comment "input-tcp-30001" nft add rule inet filter main-input udp dport 30001 meta mark set 1 counter accept comment "input-udp-30001" nft add rule inet filter main-input tcp dport 30002 meta mark set 2 counter accept comment "input-tcp-30002" nft add rule inet filter main-input udp dport 30002 meta mark set 2 counter accept comment "input-udp-30002" nft add rule inet filter main-input tcp dport 30003 meta mark set 3 counter accept comment "input-tcp-30003" nft add rule inet filter main-input udp dport 30003 meta mark set 3 counter accept comment "input-udp-30003" nft add rule inet filter after-input limit rate 3/minute burst 10 packets counter log prefix "[nft BLOCK INPUT] " nft add rule inet filter forward tcp flags syn tcp option maxseg size set rt mtu nft add rule inet filter forward ct state established,related counter accept nft add rule inet filter output jump before-output nft add rule inet filter output jump main-output nft add rule inet filter before-output oifname != "eth0" counter accept nft add rule inet filter before-output udp dport 53 counter accept comment "Allow outbound DNS" nft add rule inet filter before-output tcp dport 53 counter accept comment "Allow outbound DNS" nft add rule inet filter before-output udp sport 68 udp dport 67 counter accept comment "Allow outbound DHCP" nft add rule inet filter before-output ip daddr 169.254.169.254 limit rate 3/minute burst 5 packets log prefix "[nft BLOCK METADATA] " nft add rule inet filter before-output ip daddr 169.254.169.254 counter reject comment "Block Cloud Metadata API" nft add rule inet filter before-output ip daddr 10.0.0.0/8 counter reject comment "Block Class A (Cloud VPCs & Large Enterprise Intranets)" nft add rule inet filter before-output ip daddr 172.16.0.0/12 counter reject comment "Block Class B (Docker bridge & Container networks)" nft add rule inet filter before-output ip daddr 192.168.0.0/16 counter reject comment "Block Class C (Local & Management networks)" nft add rule inet filter before-output ip daddr 100.64.0.0/10 counter reject comment "Block CGNAT (Cloud infrastructure routing)" nft add rule inet filter before-output ip6 daddr fc00::/7 counter reject comment "Block IPv6 ULA (Unique Local Addresses)" nft add rule inet filter before-output ip6 daddr fe80::/10 counter reject comment "Block IPv6 Link-Local (Subnet-only addresses)" nft add rule inet filter main-output tcp sport 30001 meta mark set 1 counter accept comment "output-tcp-30001" nft add rule inet filter main-output udp sport 30001 meta mark set 1 counter accept comment "output-udp-30001" nft add rule inet filter main-output tcp sport 30002 meta mark set 2 counter accept comment "output-tcp-30002" nft add rule inet filter main-output udp sport 30002 meta mark set 2 counter accept comment "output-udp-30002" nft add rule inet filter main-output tcp sport 30003 meta mark set 3 counter accept comment "output-tcp-30003" nft add rule inet filter main-output udp sport 30003 meta mark set 3 counter accept comment "output-udp-30003" nft add chain ip nat postrouting '{ type nat hook postrouting priority 100; policy accept; }' nft add rule ip nat postrouting oifname "eth0" masquerade nft add chain ip6 nat postrouting '{ type nat hook postrouting priority 100; policy accept; }' nft add rule ip6 nat postrouting oifname "eth0" masquerade
验证
1 2 3 4 5 6 7 8 9 10 11 sudo nft list tablessudo nft -a list ruleset
永久修改 备份
1 sudo cp /etc/nftables.conf /etc/twist/nftables.conf.old-$(date +%Y%m%d%H%M%S)
编辑 /etc/nftables.conf 配置文件
一定要小心使用 flush 命令,flush ruleset会清空其他table,如fail2ban的table。flush table inet filter 会导致当前连接直接中断,无法再连接上。
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 #!/usr/sbin/nft -f flush ruleset table inet filter { chain input { type filter hook input priority 0; policy drop; jump before-input jump main-input jump after-input drop } chain before-input { iifname "lo" counter accept ct state established,related counter accept ct state invalid counter drop ip protocol icmp icmp type echo-request limit rate 10/second burst 20 packets counter accept ip protocol icmp icmp type { destination-unreachable, time-exceeded, parameter-problem } counter accept meta l4proto ipv6-icmp icmpv6 type echo-request limit rate 10/second burst 20 packets counter accept meta l4proto ipv6-icmp icmpv6 type { echo-reply, destination-unreachable, packet-too-big, time-exceeded, parameter-problem } counter accept } chain main-input { tcp dport 22 ct state new limit rate 5/minute burst 5 packets counter accept comment "input-tcp-22-limited" tcp dport 80 counter accept comment "input-tcp-80" tcp dport 443 counter accept comment "input-tcp-443" udp dport 443 counter accept comment "input-udp-443" tcp dport 30001 meta mark set 1 counter accept comment "input-tcp-30001" udp dport 30001 meta mark set 1 counter accept comment "input-udp-30001" tcp dport 30002 meta mark set 2 counter accept comment "input-tcp-30002" udp dport 30002 meta mark set 2 counter accept comment "input-udp-30002" tcp dport 30003 meta mark set 3 counter accept comment "input-tcp-30003" udp dport 30003 meta mark set 3 counter accept comment "input-udp-30003" } chain after-input { limit rate 3/minute burst 10 packets counter log prefix "[nft BLOCK INPUT] " } chain forward { type filter hook forward priority 0; policy drop; tcp flags syn tcp option maxseg size set rt mtu ct state established,related counter accept } chain output { type filter hook output priority 0; policy accept; jump before-output jump main-output } chain before-output { oifname != "eth0" counter accept udp dport 53 counter accept comment "Allow outbound DNS" tcp dport 53 counter accept comment "Allow outbound DNS" udp sport 68 udp dport 67 counter accept comment "Allow outbound DHCP" ip daddr 169.254.169.254 limit rate 3/minute burst 5 packets log prefix "[nft BLOCK METADATA] " ip daddr 169.254.169.254 counter reject comment "Block Cloud Metadata API" ip daddr 10.0.0.0/8 counter reject comment "Block Class A (Cloud VPCs & Large Enterprise Intranets)" ip daddr 172.16.0.0/12 counter reject comment "Block Class B (Docker bridge & Container networks)" ip daddr 192.168.0.0/16 counter reject comment "Block Class C (Local & Management networks)" ip daddr 100.64.0.0/10 counter reject comment "Block CGNAT (Cloud infrastructure routing)" ip6 daddr fc00::/7 counter reject comment "Block IPv6 ULA (Unique Local Addresses)" ip6 daddr fe80::/10 counter reject comment "Block IPv6 Link-Local (Subnet-only addresses)" } chain main-output { tcp sport 30001 meta mark set 1 counter accept comment "output-tcp-30001" udp sport 30001 meta mark set 1 counter accept comment "output-udp-30001" tcp sport 30002 meta mark set 2 counter accept comment "output-tcp-30002" udp sport 30002 meta mark set 2 counter accept comment "output-udp-30002" tcp sport 30003 meta mark set 3 counter accept comment "output-tcp-30003" udp sport 30003 meta mark set 3 counter accept comment "output-udp-30003" } } table ip nat { chain postrouting { type nat hook postrouting priority 100; policy accept; oifname "eth0" masquerade } } table ip6 nat { chain postrouting { type nat hook postrouting priority 100; policy accept; oifname "eth0" masquerade } }
验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 sudo nft -c -f /etc/nftables.confsudo systemctl restart nftables fail2bansudo nft list tablessudo nft -a list rulesetsudo systemctl enable --now nftables.service
iptables 下面是iptables的配置,有点老了,最新的是使用nftables,目前还是兼容的
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 iptables-save > /etc/twist/iptables.rules iptables-save > /etc/twist/iptables.rules.old-$(date +%Y%m%d%H%M%S) iptables -I INPUT -m conntrack --ctstate INVALID -j DROP iptables -I INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT iptables -I INPUT -p tcp -m multiport --dports $PORT -j ACCEPT iptables -I INPUT -p udp -m multiport --dports $PORT -j ACCEPT iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport $PORT -j ACCEPT iptables -I INPUT -m state --state NEW -m udp -p udp --dport $PORT -j ACCEPT iptables -I FORWARD -m conntrack --ctstate INVALID -j DROP iptables -I FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu iptables -t nat -A POSTROUTING -o $ETH -j MASQUERADE iptables-save > /etc/iptables.rules cp /etc/ip6tables.rules /etc/twist/ip6tables.rules.old-$(date +%Y%m%d%H%M%S)cp -f /etc/iptables.rules /etc/ip6tables.rulesmkdir -p /etc/network/if-pre-up.dcp /etc/network/if-pre-up.d/iptablesload /etc/twist/iptablesload.old-$(date +%Y%m%d%H%M%S)cat > /etc/network/if-pre-up.d/iptablesload <<-EOF #!/bin/sh iptables-restore < /etc/iptables.rules exit 0 EOF cp /etc/network/if-pre-up.d/ip6tablesload /etc/twist/ip6tablesload.old-$(date +%Y%m%d%H%M%S)cat > /etc/network/if-pre-up.d/ip6tablesload <<-EOF #!/bin/sh ip6tables-restore < /etc/ip6tables.rules exit 0 EOF
开机启动 原版 可以忽略,现在用systemd,没怎么用rc.local
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 cp /etc/rc.local /etc/twist/rc.local.old-$(date +%Y%m%d%H%M%S)cat >> /etc/rc.local <<-EOF # 但有些发行版或特殊场景(比如 rc.local 早于 sysctl 服务执行),可能会导致参数未及时生效,所以脚本里加 sysctl -q -p 是保险做法,确保参数一定被加载 sysctl -q -p # 只有在你直接用 iptables 命令自定义规则时,才需要 iptables-save/iptables-restore 来持久化和恢复 iptables-restore < /etc/iptables.rules ip6tables-restore < /etc/ip6tables.rules # 启动服务 systemctl restart fail2ban cron nginx shadowsocks-rust exit 0 EOF
systemd 1 2 3 4 sudo systemctl enable --now shadowsocks-rustsudo systemctl enable --now nginxsudo systemctl enable --now fail2bansudo systemctl enable --now cron
打印输出 以下仅供参考,相关变量请改成实际的变量值。注意:如果是http方式的混淆,echo部分也需要做相应修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 METHOD=aes-256-gcm PASSWORD=abcdefg PUBLICIP=1.2.3.4 PORT=443 OBFSHOST=microsoft.com OBFSURI=/ OBFS=tls IPV6ENABLE=false PUBLICIPv6= BASE64=$(echo -n "${METHOD} :${PASSWORD} " | base64 -w 0) echo "ss://${BASE64} @${PUBLICIP} :${PORT} ?plugin=obfs-local;obfs-host=${OBFSHOST} ;obfs-uri=${OBFSURI} ;obfs=${OBFS} #Twist" | qrecho -e "# [\033[32;1mss://\033[0m\033[34;1m$(echo "${BASE64} @${PUBLICIP} :${PORT} ?plugin=obfs-local;obfs-host=${OBFSHOST} ;obfs-uri=${OBFSURI} ;obfs=${OBFS} #Twist" ) \033[0m]" echo -e "# [\033[32;1mServer IP:\033[0m \033[34;1m${PUBLICIP} \033[0m\c" [ ! "$IPV6ENABLE " = "false" ] && echo -e "(\033[34;1m${PUBLICIPv6} \033[0m)\c" echo -e " \033[32;1mPassWord:\033[0m \033[34;1m${PASSWORD} \033[0m \033[32;1mEncryption:\033[0m \033[34;1m${METHOD} \033[0m \033[32;1mOBFS:\033[0m \033[34;1m${OBFS} \033[0m \033[32;1mOBFS-HOST:\033[0m \033[34;1m${OBFSHOST} \033[0m \033[32;1mOBFS-URI:\033[0m \033[34;1m${OBFSURI} \033[0m]"
配置Nginx 伪装为微软的服务器
1 2 3 4 5 6 7 8 # /etc/nginx/sites-enabled/default server { listen 80; server_name _; location / { return 301 http://microsoft.com$request_uri; } }
重启nginx
1 2 nginx -t sudo systemctl restart nginx
letsencrypt如果有通过软链共享证书给其他app,那么最好创建一个certs组,全部加入这个组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 sudo groupadd certssudo chown -R root:certs /etc/letsencryptsudo usermod -aG certs www-datasudo find /etc/letsencrypt -type d -exec chmod 750 {} \;sudo find /etc/letsencrypt -type f -exec chmod 640 {} \;su - www-data cat /etc/letsencrypt/live/you_domain.com/fullchain.pem
配置fail2ban fail2ban自带了很多规则,自己可以根据nginx的日志添加一些,放在/etc/fail2ban/filter.d/下面不过默认的也够了
添加过滤器
1 2 3 4 # 仅用作参考,默认的够用了 # /etc/fail2ban/filter.d/nginx-badurl.conf [Definition] failregex = <HOST> -.*"(GET|POST|HEAD) (/admin.*)
添加jail
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 # /etc/fail2ban/jail.d/nginx-all.local [nginx-bad-request] enabled = true filter = nginx-bad-request port = http,https logpath = /var/log/nginx/access.log backend = auto maxretry = 1 bantime = 86400 findtime = 600 [nginx-botsearch] enabled = true filter = nginx-botsearch port = http,https logpath = /var/log/nginx/access.log backend = auto maxretry = 1 bantime = 86400 findtime = 600 [nginx-http-auth] enabled = true filter = nginx-http-auth port = http,https logpath = /var/log/nginx/error.log backend = auto maxretry = 1 bantime = 86400 findtime = 600 [nginx-limit-req] enabled = true filter = nginx-limit-req port = http,https logpath = /var/log/nginx/error.log backend = auto maxretry = 1 bantime = 86400 findtime = 600
重启验证
1 2 3 4 sudo systemctl restart fail2banfail2ban-client status fail2ban-client status nginx-bad-request
重启下服务器,完成
注意:国内电信/联通网络的DPI检测很严格,混淆经常失效,目前还在调查原因
其他 定时任务
内容如下
1 2 3 # 定时更新,避免系统漏洞,时区只影响一条 CRON_TZ=Asia/Shanghai 0 3 * * 0 /usr/bin/apt-get update && DEBIAN_FRONTEND=noninteractive /usr/bin/apt-get upgrade -y && /usr/bin/apt-get autoremove -y >> /var/log/apt-cron.log 2>&1
融合怪测评项目 - GO版本 ecs 这个项目很有趣,用于测试你的vps各项指标,包括服务器性能、三网路由测试、流媒体解锁测试等