准备
安装 Hysteria 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| sudo useradd hysteria
bash <(curl -fsSL https://get.hy2.sh/)
curl -L https://github.com/apernet/hysteria/releases/download/app%2Fv2.9.1/hysteria-linux-amd64 -o hysteria
sha256sum hysteria
chmod +x hysteria
sudo mv hysteria /usr/local/bin
|
安装 AnyTLS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| sudo useradd mihomo
curl -LO https://github.com/MetaCubeX/mihomo/releases/download/v1.19.24/mihomo-linux-amd64-v3-v1.19.24.gz
sha256sum mihomo-linux-amd64-v3-v1.19.24.gz
gzip -d mihomo-linux-amd64-v3-v1.19.24.gz
mv mihomo-linux-amd64-v3-v1.19.24 mihomo
chmod +x mihomo
sudo mv mihomo /usr/local/bin
|
安装SSL证书
主服务器
建立nginx配置文件
1
| sudo vi /etc/nginx/sites-enabled/<your_domain.com>
|
内容,先别配置ssl,让certbot自己设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| server { listen 80; server_name <your_domain.com>;
# 用于 Let’s Encrypt 证书验证 location /.well-known/acme-challenge/ { root /var/www/folder_name; }
# 其它 HTTP 请求重定向到 HTTPS location / { return 301 https://$host$request_uri; } }
server { listen 443 ssl http2; server_name <your_domain.com>;
ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; }
|
执行certbot申请证书
1
| sudo certbot --nginx -d <your_domain.com>
|
将证书软链接到指定目录
1 2 3 4 5 6 7 8 9 10 11 12 13
| sudo mkdir -p /etc/hysteria/certs
sudo ln -s /etc/letsencrypt/live/<your_domain.com>/fullchain.pem /etc/hysteria/certs/ sudo ln -s /etc/letsencrypt/live/<your_domain.com>/privkey.pem /etc/hysteria/certs/
sudo mkdir -p /etc/mihomo/certs
sudo ln -s /etc/letsencrypt/live/<your_domain.com>/fullchain.pem /etc/mihomo/certs/ sudo ln -s /etc/letsencrypt/live/<your_domain.com>/privkey.pem /etc/mihomo/certs/
|
我们可以将主服务器申请的证书通过rsync同步到其他服务器,当然,也可以为每个服务器申请不同的证书
1 2 3 4
|
rsync -avzL /etc/letsencrypt/live/<your_domain.com>/fullchain.pem user@host:/data/certs/ rsync -avzL /etc/letsencrypt/live/<your_domain.com>/privkey.pem user@host:/data/certs/
|
定时设置
创建一个脚本certbot_post_hook.sh
1 2 3 4 5 6
| #!/bin/bash systemctl reload nginx rsync -avzL /etc/letsencrypt/live/<your_domain.com>/ user@host:/data/certs/ >> /var/log/certbot_rsync.log 2>&1 if [ $? -ne 0]; then echo "[`date '+%F %T'`] rsync failed" >> /var/log/certbot_rsync.log fi
|
给脚本加执行权限
1
| chmod +x certbot_post_hook.sh
|
测试验证
1
| certbot renew --quite --post-hook "/path/to/certbot_post_hook.sh"
|
设置crontab
1
| 0 2 * * * certbot renew --quite --post-hook "/path/to/certbot_post_hook.sh"
|
其他服务器
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
| sudo groupadd certs
sudo usermod -aG certs hysteria sudo usermod -aG certs mihomo
sudo mkdir -p /etc/hysteria/certs sudo mkdir -p /etc/mihomo/certs
sudo chown -R root:certs /data/certs sudo chmod 750 /data/certs sudo chmod 640 /data/certs/*
sudo ln -s /data/certs/fullchain.pem /etc/hysteria/certs/ sudo ln -s /data/certs/privkey.pem /etc/hysteria/certs/
sudo chown hysteria:certs /etc/hysteria/certs
sudo chmod 750 /etc/hysteria/certs
sudo chown hysteria /etc/hysteria
sudo ln -s /data/certs/fullchain.pem /etc/mihomo/certs/ sudo ln -s /data/certs/privkey.pem /etc/mihomo/certs/
sudo chown mihomo:certs /etc/mihomo/certs
sudo chmod 750 /etc/mihomo/certs
sudo chown mihomo /etc/mihomo
|
Hysteria 2
配置
服务端
1
| sudo vi /etc/hysteria/config.yaml
|
内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| trafficStats: listen: 127.0.0.1:10002
listen: :30002
tls: cert: /etc/hysteria/certs/fullchain.pem key: /etc/hysteria/certs/privkey.pem
auth: type: userpass userpass: user1: <64_character_password(openssl rand -hex 32)>
obfs: type: salamander salamander: password: <32_character_password(openssl rand -hex 16)>
bandwidth: up: 10 mbps down: 50 mbps
|
注意:iOS的stash还不支持salamander混淆方式,但是shadowrocket支持
客户端
1
| hysteria2://user1:64_character_password@your_domain.com:30002/?sni=your_domain.com&insecure=0&udp=true&alpn=h3&obfs=salamander&obfs-password=32_character_password#Hy2
|
Systemd
创建service文件
1
| sudo vi /etc/systemd/system/hysteria-server.service
|
内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [Unit] Description=Hysteria2 Service After=network.target
[Service] User=hysteria Group=hysteria Type=simple ExecStart=/usr/local/bin/hysteria server -c /etc/hysteria/config.yaml Restart=on-failure LimitNOFILE=512000
[Install] WantedBy=multi-user.target
|
启动/验证
1 2 3 4 5 6 7 8
| sudo systemctl start hysteria-server
sudo systemctl status hysteria-server sudo journalctl -u hysteria-server -f
sudo systemctl enable hysteria-server
|
查看流量消耗
1 2 3 4
| curl http://127.0.0.1:10002/traffic
|
AnyTLS
配置
服务端
1
| sudo vi /etc/mihomo/config.yaml
|
内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| mode: direct
external-controller: 127.0.0.1:10003
listeners: - name: anytls-in type: anytls listen: 0.0.0.0 port: 30003
users: user1: <uuidgen>
certificate: /etc/mihomo/certs/fullchain.pem private-key: /etc/mihomo/certs/privkey.pem
udp: true
|
客户端
1
| anytls://uuid@your_domain.com:30003?security=tls&sni=your_domain.com&insecure=0&udp=true&fp=chrome#AnyTLS
|
Systemd
创建service文件
1
| sudo vi /etc/systemd/system/mihomo-anytls.service
|
内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [Unit] Description=Mihomo AnyTLS Service After=network.target
[Service] User=mihomo Group=mihomo Type=simple ExecStart=/usr/local/bin/mihomo -d /etc/mihomo Restart=on-failure LimitNOFILE=512000
[Install] WantedBy=multi-user.target
|
启动/验证
1 2 3 4 5 6 7 8
| sudo systemctl start mihomo-anytls
sudo systemctl status mihomo-anytls sudo journalctl -u mihomo-anytls -f
sudo systemctl enable mihomo-anytls
|
查看流量消耗
1 2 3 4
| curl http://127.0.0.1:10003/traffic
|