├── README.markdown
├── hysteria-v1.0.4.sh
└── hysteria.sh
/README.markdown:
--------------------------------------------------------------------------------
1 | # Hysteria 2 一键安装脚本(Alpine Linux 移植版) 🚀
2 | # Hysteria 2 One-Click Install Script (Alpine Linux Port)
3 |
4 | 
5 |
6 | 🎉 这是一个为 **Alpine Linux** 和低内存环境(最低 128MB)优化的 [MisakaNo Hysteria 2 一键安装脚本](https://github.com/Misaka-blog) 移植版,**运行时仅需约 20MB 内存**!专为资源受限的 VPS 或嵌入式系统设计,完美适配轻量级 Alpine Linux,支持**端口跳跃**等高级功能!当前版本为 **V1.0.5**,带来 NAT VPS 支持和灵活的证书选择!🔥
7 |
8 | 🎉 This is a port of [MisakaNo's Hysteria 2 one-click install script](https://github.com/Misaka-blog), optimized for **Alpine Linux** and low-memory environments (minimum 128MB), **requiring only ~20MB of memory to run**! Designed for resource-constrained VPS or embedded systems, it’s tailored for lightweight Alpine Linux with advanced features like **port hopping**! Current version: **V1.0.5**, introducing NAT VPS support and flexible certificate options! 🔥
9 |
10 | ## 项目信息 / Project Info 📋
11 |
12 | - **原作者 / Original Author**: MisakaNo の 小破站
13 | - **原博客 / Original Blog**: [https://blog.misaka.cyou](https://blog.misaka.cyou)
14 | - **原 GitHub / Original GitHub**: [https://github.com/Misaka-blog](https://github.com/Misaka-blog)
15 | - **移植作者 / Port Author**: TheX
16 | - **移植 GitHub / Port GitHub**: [https://github.com/MEILOI/HYTWOALPINE](https://github.com/MEILOI/HYTWOALPINE)
17 | - **移植版本 / Port Version**: v1.0.5
18 | - **许可证 / License**: MIT 📜
19 |
20 | ## 移植目的 / Port Purpose 🎯
21 |
22 | 原 Hysteria 2 脚本因 Alpine Linux 的独特包管理器(`apk`)、OpenRC 初始化系统和极简环境而不兼容。此移植旨在:
23 | The original Hysteria 2 script was incompatible with Alpine Linux due to its unique package manager (`apk`), OpenRC init system, and minimal environment. This port aims to:
24 |
25 | - 在 Alpine Linux 上启用 Hysteria 2 部署,最低 128MB 内存,运行仅需 ~20MB!💾
26 | Enable Hysteria 2 deployment on Alpine Linux with a minimum of 128MB memory, requiring only ~20MB to run! 💾
27 | - 保留交互式菜单,新增**端口跳跃**支持,提升网络灵活性和安全性!🌐
28 | Retain the interactive menu, with added **port hopping** support for enhanced network flexibility and security! 🌐
29 | - 优化资源占用,适配内存受限环境。
30 | Optimize resource usage for memory-constrained environments.
31 | - 确保与 BusyBox 系统的兼容性。
32 | Ensure compatibility with BusyBox systems.
33 | - **V1.0.5 新增 / New in V1.0.5**: 支持 NAT VPS 环境,新增证书选择(自签名、ACME 自动证书、自定义证书)。
34 | Support for NAT VPS environments and new certificate options (self-signed, ACME auto-cert, custom cert).
35 |
36 | ## 与原版的差异 / Differences from Original ⚖️
37 |
38 | | 功能/方面
Feature | 原版
Original | V1.0.4 移植版
V1.0.4 Port | V1.0.5 移植版
V1.0.5 Port |
39 | |----------------------|---------------------------------------------|-----------------------------------------------|-----------------------------------------------|
40 | | **支持系统**
Supported Systems | Debian、Ubuntu、CentOS 等 | Alpine Linux(及其他 BusyBox 系统) | Alpine Linux(及其他 BusyBox 系统) |
41 | | **系统检测**
System Check | 检查特定 Linux 发行版 | 移除系统检测,支持通用 Linux | 移除系统检测,支持通用 Linux |
42 | | **初始化系统**
Init System | Systemd | 替换为 `nohup`(移除 OpenRC) | 替换为 `nohup`(移除 OpenRC) |
43 | | **包管理器**
Package Manager | `apt`、`yum`、`dnf` | 支持 `apk`,`apt`/`yum` 回退 | 仅支持 `apk`,移除 `apt`/`yum` 回退 |
44 | | **依赖安装**
Dependency Install | 需要手动安装 | 自动安装 `bash`、`openssl`、`curl`、`wget`、`iptables`、`qrencode` | 自动安装 `bash`、`openssl`、`curl`、`wget` |
45 | | **内存优化**
Memory Optimization | 未针对低内存优化 | QUIC 窗口减小,占用 ~15-20MB | QUIC 窗口减小,占用 ~20MB |
46 | | **默认伪装网站**
Default Masquerade Site | `maimai.sega.jp` | 改为 `www.bing.com` | 改为 `www.bing.com` |
47 | | **防火墙管理**
Firewall Management | 假设 `iptables` 存在 | 检查 `iptables`,缺失时提示 | NAT VPS 跳过 `iptables`,需手动放行 |
48 | | **进程检查**
Process Check | 使用 `ps -p` | 使用 `ps | grep`,兼容 BusyBox | 使用 `ps | grep`,兼容 BusyBox |
49 | | **证书生成**
Certificate Generation | 假设 `openssl` 存在 | 显式检查 `openssl`,自动安装 | 支持自签名、ACME、自定义证书 |
50 | | **端口跳跃**
Port Hopping | 支持 | 移除以简化 | **强化支持**,通过配置启用 🌟 |
51 | | **ACME 证书**
ACME Certificates | 支持 | 移除以减少依赖 | 重新引入 ACME 证书支持 |
52 |
53 | ## 主要优化 / Key Optimizations 🛠️
54 |
55 | - **超低内存占用 / Ultra-Low Memory Usage**: QUIC 窗口大小减小(`initStreamReceiveWindow: 8388608`, `maxStreamReceiveWindow: 8388608`),Hysteria 2 运行仅需 ~20MB 内存,最低支持 128MB 设备!💾
56 | - **BusyBox 兼容性 / BusyBox Compatibility**: 调整命令(如 `ps`)以适配 BusyBox 的精简工具集。
57 | - **端口跳跃支持 / Port Hopping Support**: **V1.0.5 强化**,通过配置文件启用端口跳跃,动态切换端口以提升安全性和抗封锁能力!🔄
58 | - **简化依赖 / Simplified Dependencies**: 移除复杂功能,仅需核心工具(`bash`、`curl`、`wget`、`openssl`)。
59 | - **通用 Linux 支持 / Universal Linux Support**: 移除严格的系统检查,脚本可在任何 Linux 系统上运行(需基本工具)。
60 | - **V1.0.5 新增 / New in V1.0.5**:
61 | - 支持 NAT VPS 环境,跳过 `iptables`,提示手动放行端口。
62 | - 灵活证书选择:必应自签名、ACME 自动证书、自定义证书路径。📜
63 | - 改进错误提示和分享链接同步。
64 |
65 | ## 修复的 Bug 和变更 / Bug Fixes and Changes 🐞
66 |
67 | ### v1.0.5 (2025-05-19)
68 | - **新增 NAT VPS 支持 / NAT VPS Support** 🌐:
69 | - **变更 / Change**: 移除 `iptables` 和 `ip6tables` 依赖,跳过防火墙配置。
70 | - **影响 / Impact**: 用户需在 VPS 控制面板或主机防火墙手动放行 UDP 端口。
71 | - **细节 / Details**: 安装和端口修改时提示 NAT VPS 用户确保端口映射。
72 | - **新增证书选择功能 / Certificate Selection** 📜:
73 | - **变更 / Change**: 支持必应自签名(默认)、ACME 自动证书、自定义证书路径。
74 | - **影响 / Impact**: 增强灵活性,适合需要正规证书或自定义证书的用户。
75 | - **细节 / Details**: ACME 使用 `acme.sh` 自动申请,需提供域名;自定义证书需提供证书和私钥路径。
76 | - **强化端口跳跃 / Enhanced Port Hopping** 🔄:
77 | - **变更 / Change**: 通过配置文件启用端口跳跃,动态切换端口以提升安全性。
78 | - **影响 / Impact**: 增强抗封锁能力,适合复杂网络环境。
79 | - **细节 / Details**: 在 `hy-client.yaml` 中配置 `transport.udp.hopInterval`(默认 30s)。
80 | - **优化依赖管理 / Optimized Dependency Management**:
81 | - **变更 / Change**: 移除 `iptables`、`ip6tables`、`qrencode` 依赖,仅保留核心依赖。
82 | - **影响 / Impact**: 简化安装流程,移除二维码生成功能。
83 | - **细节 / Details**: 若 `apk update` 失败,提示更换镜像源(`mirrors.tuna.tsinghua.edu.cn`)。
84 | - **改进错误提示 / Improved Error Handling**:
85 | - **变更 / Change**: 增强 root 权限检查,显示当前用户和 UID。
86 | - **影响 / Impact**: 更清晰的错误信息,便于排查。
87 | - **改进配置更新 / Improved Config Update**:
88 | - **变更 / Change**: 端口和密码修改时同步更新 `url.txt` 分享链接。
89 | - **影响 / Impact**: 确保客户端配置文件和分享链接一致。
90 |
91 | ### v1.0.4 (2025-05-19)
92 | - 改进依赖安装,分步处理核心(`curl`, `wget`, `bash`, `openssl`, `iptables`, `ip6tables`)和可选依赖(`qrencode`)。
93 | - 解决 `qrencode` 缺失导致安装失败的问题,失败时仅警告。
94 | - 添加 `apk cache clean`,提供备用镜像源提示。
95 |
96 | ### v1.0.3 (2025-05-19)
97 | - 自动添加 `community` 仓库,尝试安装所有依赖。
98 |
99 | ### v1.0.2 (2025-05-19)
100 | - 集成 `openssl` 安装,增强“一键”体验。
101 |
102 | ### v1.0.1 (2025-05-19)
103 | - 修复 `check_dependencies` 语法错误和 `menu` 函数笔误。
104 |
105 | ### v1.0 (初始移植 / Initial Port)
106 | - 移除系统检测,用 `nohup` 替换 OpenRC,适配 BusyBox。
107 |
108 | ## 前置条件 / Prerequisites ⚙️
109 |
110 | - **系统 / System**: Alpine Linux(建议 3.18 或更高版本)或任何 BusyBox 系统。
111 | - **内存 / Memory**: 最低 128MB(Hysteria 2 运行仅需 ~20MB,64MB 内存不足以稳定运行)。
112 | - **权限 / Permissions**: 需要 root 权限。
113 | - **架构 / Architecture**: 默认 x86_64(arm64/armv7 需修改二进制 URL)。
114 | - **网络 / Network**: 需要公网 IP 或 NAT,UDP 端口已开放。
115 | - **依赖 / Dependencies**: `bash`、`curl`、`wget`、`openssl`(自动安装)。
116 | - **NAT VPS 注意 / NAT VPS Note**: 确保 UDP 端口已由 VPS 提供商映射到公网!🌍
117 |
118 | ## 安装 / Installation 🚀
119 |
120 | ### 一键安装 / One-Click Install
121 | ```bash
122 | curl -o hysteria.sh -fsSL https://raw.githubusercontent.com/MEILOI/HYTWOALPINE/main/hysteria.sh && chmod +x hysteria.sh && ./hysteria.sh
123 | ```
124 |
125 | ### 手动安装 / Manual Install
126 | ```bash
127 | curl -o hysteria.sh -fsSL https://raw.githubusercontent.com/MEILOI/HYTWOALPINE/main/hysteria.sh
128 | chmod +x hysteria.sh
129 | ./hysteria.sh
130 | ```
131 |
132 | ### 依赖安装(若失败) / Dependency Install (If Failed)
133 | 如果脚本提示依赖安装失败,运行以下命令:
134 | If the script reports dependency installation failure, run:
135 | ```bash
136 | echo 'https://dl-cdn.alpinelinux.org/alpine/v3.21/main' > /etc/apk/repositories
137 | echo 'https://dl-cdn.alpinelinux.org/alpine/v3.21/community' >> /etc/apk/repositories
138 | apk update
139 | apk add --no-cache bash curl wget openssl
140 | ```
141 | 替换 `v3.21` 为你的 Alpine 版本(查看:`cat /etc/alpine-release`)。
142 | Replace `v3.21` with your Alpine version (check: `cat /etc/alpine-release`).
143 |
144 | #### 更换镜像源 / Change Mirror Source
145 | 如果默认镜像源(`dl-cdn.alpinelinux.org`)不可用,尝试以下镜像:
146 | If the default mirror (`dl-cdn.alpinelinux.org`) is unavailable, try:
147 | ```bash
148 | sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
149 | apk update
150 | apk add --no-cache bash curl wget openssl
151 | ```
152 | 其他可选镜像源 / Other mirror options:
153 | - `mirrors.aliyun.com`
154 | - `mirrors.ustc.edu.cn`
155 | 运行以下命令切换:
156 | Run to switch:
157 | ```bash
158 | sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
159 | apk update
160 | apk add --no-cache bash curl wget openssl
161 | ```
162 |
163 | ### 防火墙(NAT VPS) / Firewall (NAT VPS) 🔒
164 | - NAT VPS 用户需在 VPS 控制面板或主机防火墙手动放行 UDP 端口(例如 55555)。
165 | NAT VPS users must manually open UDP ports (e.g., 55555) in the VPS control panel or host firewall.
166 | - 示例(使用 `nftables`) / Example (using `nftables`):
167 | ```bash
168 | nft add rule ip filter input udp dport 55555 accept
169 | ```
170 | - 或使用 `iptables`(若主机支持) / Or use `iptables` (if supported):
171 | ```bash
172 | iptables -I INPUT -p udp --dport 55555 -j ACCEPT
173 | ```
174 |
175 | ## 使用方法 / Usage Guide 🎮
176 |
177 | 1. 运行 `./hysteria.sh`,选择选项 `1` 安装。
178 | Run `./hysteria.sh` and select option `1` to install.
179 | 2. 配置 / Configure:
180 | - **证书 / Certificate**: 选择必应自签名(默认)、ACME 自动证书或自定义证书路径。📜
181 | - **端口 / Port**: 输入 UDP 端口(1-65535)或回车随机分配。
182 | - **密码 / Password**: 设置密码或回车生成随机密码。
183 | - **伪装网站 / Masquerade Site**: 输入网站(如 `www.bing.com`)或回车使用默认。
184 | - **端口跳跃 / Port Hopping**: 默认启用,配置 `hopInterval`(如 30s)以动态切换端口!🔄
185 | 3. 脚本生成 / Script Output:
186 | - 客户端配置文件 / Client Config: `/root/hy/hy-client.yaml`
187 | - 分享链接 / Share Link: `/root/hy/url.txt`
188 | 4. 导入客户端 / Import to Client:
189 | - 自签名证书需启用 `insecure: true`。
190 | - ACME 或自定义证书需确保域名正确解析。
191 | 5. 使用菜单(选项 3-6)管理服务:启动/停止、修改配置、更新内核。
192 | Use the menu (options 3-6) to manage the service: start/stop, modify config, update core.
193 | 6. **NAT VPS 注意 / NAT VPS Note**: 确保端口已映射到公网,否则客户端无法连接!🌐
194 | 7. **端口跳跃提示 / Port Hopping Tip**: 检查 `hy-client.yaml` 中的 `transport.udp.hopInterval`,调整间隔(30s-60s)以优化性能!
195 |
196 | ## 安装后操作 / Post-Installation Steps 🛠️
197 |
198 | - **验证服务 / Verify Service**:
199 | ```bash
200 | ps | grep hysteria
201 | cat /var/log/hysteria.log
202 | netstat -tunlp | grep
203 | ```
204 | - **添加开机启动 / Add Auto-Start**:
205 | ```bash
206 | crontab -e
207 | @reboot nohup /usr/local/bin/hysteria server --config /etc/hysteria/config.yaml > /var/log/hysteria.log 2>&1 &
208 | ```
209 | - **故障排除 / Troubleshoot**:
210 | - 查看 `/var/log/hysteria.log` 检查错误。
211 | - 确保 UDP 端口已放行(NAT VPS 用户联系提供商)。
212 | - 验证依赖:`command -v bash curl wget openssl`。
213 |
214 | ## 故障排除 / Troubleshooting 🐞
215 |
216 | 1. **依赖安装失败 / Dependency Installation Failure**:
217 | - **问题 / Issue**: `apk` 提示 `no such package` 或网络超时。
218 | - **解决 / Solution**:
219 | - 确保网络正常,检查 `/etc/apk/repositories` 是否包含 `main` 和 `community` 仓库:
220 | ```bash
221 | cat /etc/apk/repositories
222 | ```
223 | - 手动配置仓库:
224 | ```bash
225 | echo 'https://dl-cdn.alpinelinux.org/alpine/v3.21/main' > /etc/apk/repositories
226 | echo 'https://dl-cdn.alpinelinux.org/alpine/v3.21/community' >> /etc/apk/repositories
227 | apk update
228 | apk add --no-cache bash curl wget openssl
229 | ```
230 | - 如果仍失败,切换镜像源:
231 | ```bash
232 | sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
233 | apk update
234 | apk add --no-cache bash curl wget openssl
235 | ```
236 | - 其他镜像源选项:`mirrors.aliyun.com`, `mirrors.ustc.edu.cn`。
237 | - **验证 / Verify**:
238 | ```bash
239 | command -v bash curl wget openssl
240 | ```
241 | 2. **内存不足 / Insufficient Memory**:
242 | - **问题 / Issue**: 64MB 内存设备运行失败或崩溃。
243 | - **解决 / Solution**:
244 | - 本脚本最低要求 128MB 内存,Hysteria 2 运行占用 ~20MB。64MB 内存不足以稳定运行,可能导致进程终止。
245 | - 建议升级到 128MB 或更高内存的 VPS。
246 | - 检查内存使用:
247 | ```bash
248 | free -m
249 | ```
250 | - 释放内存(若可能):
251 | ```bash
252 | sync; echo 3 > /proc/sys/vm/drop_caches
253 | ```
254 | 3. **网络问题 / Network Issues**:
255 | - **问题 / Issue**: 无法下载 Hysteria 二进制或获取 IP。
256 | - **解决 / Solution**:
257 | - 检查网络连通性:
258 | ```bash
259 | ping dl-cdn.alpinelinux.org
260 | curl -I https://github.com
261 | ```
262 | - 使用备用 DNS:
263 | ```bash
264 | echo "nameserver 8.8.8.8" > /etc/resolv.conf
265 | ```
266 | - 尝试其他网络源:
267 | ```bash
268 | wget -O /usr/local/bin/hysteria https://github.com/apernet/hysteria/releases/latest/download/hysteria-linux-amd64
269 | ```
270 | 4. **端口未放行 / Port Not Opened**:
271 | - **问题 / Issue**: 客户端无法连接。
272 | - **解决 / Solution**:
273 | - NAT VPS 用户需在控制面板或主机防火墙放行 UDP 端口:
274 | ```bash
275 | nft add rule ip filter input udp dport 55555 accept
276 | ```
277 | - 或使用 `iptables`(若支持):
278 | ```bash
279 | iptables -I INPUT -p udp --dport 55555 -j ACCEPT
280 | ```
281 | - 联系 VPS 提供商确保端口映射到公网。
282 | - 验证端口开放:
283 | ```bash
284 | netstat -tunlp | grep 55555
285 | ```
286 | 5. **ACME 证书申请失败 / ACME Certificate Failure**:
287 | - **问题 / Issue**: 域名未解析或网络问题。
288 | - **解决 / Solution**:
289 | - 确保域名已解析到服务器 IP:
290 | ```bash
291 | ping
292 | ```
293 | - 检查 80 端口是否开放(ACME 需要):
294 | ```bash
295 | netstat -tunlp | grep :80
296 | ```
297 | - 重新运行 ACME:
298 | ```bash
299 | ~/.acme.sh/acme.sh --issue -d "" --standalone
300 | ```
301 | 6. **端口跳跃问题 / Port Hopping Issues**:
302 | - **问题 / Issue**: 端口切换不稳定或客户端断连。
303 | - **解决 / Solution**:
304 | - 检查 `hy-client.yaml` 中的 `hopInterval`,建议 30s-60s:
305 | ```bash
306 | cat /root/hy/hy-client.yaml | grep hopInterval
307 | ```
308 | - 调整间隔(例如 60s):
309 | ```bash
310 | sed -i 's/hopInterval: 30s/hopInterval: 60s/' /root/hy/hy-client.yaml
311 | ```
312 | - 重启服务:
313 | ```bash
314 | kill $(cat /run/hysteria.pid); nohup /usr/local/bin/hysteria server --config /etc/hysteria/config.yaml > /var/log/hysteria.log 2>&1 &
315 | ```
316 |
317 | ## 贡献 / Contributing 🤝
318 |
319 | 欢迎提交问题或拉取请求至 [https://github.com/MEILOI/HYTWOALPINE](https://github.com/MEILOI/HYTWOALPINE)!
320 | Welcome to submit issues or pull requests to [https://github.com/MEILOI/HYTWOALPINE](https://github.com/MEILOI/HYTWOALPINE)!
321 |
322 | ## 致谢 / Acknowledgments 🙏
323 |
324 | - 感谢 MisakaNo 提供的原脚本和灵感!
325 | Thanks to MisakaNo for the original script and inspiration!
326 | - 感谢 Hysteria 团队开发的 [Hysteria 2](https://github.com/apernet/hysteria) 代理!
327 | Thanks to the Hysteria team for developing [Hysteria 2](https://github.com/apernet/hysteria)!
328 |
329 | ## 许可证 / License 📜
330 |
331 | 本项目采用 MIT 许可证,详情见 [LICENSE](LICENSE)。
332 | This project is licensed under the MIT License, see [LICENSE](LICENSE) for details.
333 |
334 | 🌟 感谢使用 HYTWOALPINE!期待你的反馈和贡献!🚀
335 | 🌟 Thank you for using HYTWOALPINE! Looking forward to your feedback and contributions! 🚀
--------------------------------------------------------------------------------
/hysteria-v1.0.4.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | export LANG=en_US.UTF-8
4 |
5 | RED="\033[31m"
6 | GREEN="\033[32m"
7 | YELLOW="\033[33m"
8 | PLAIN="\033[0m"
9 |
10 | red() { echo -e "\033[31m\033[01m$1\033[0m"; }
11 | green() { echo -e "\033[32m\033[01m$1\033[0m"; }
12 | yellow() { echo -e "\033[33m\033[01m$1\033[0m"; }
13 |
14 | # 判断是否为 root 用户
15 | [[ $EUID -ne 0 ]] && red "注意: 请在 root 用户下运行脚本" && exit 1
16 |
17 | # 检查并安装依赖
18 | check_dependencies() {
19 | local core_deps="curl wget bash openssl iptables ip6tables"
20 | local optional_deps="qrencode"
21 | local missing_core_deps=""
22 | local missing_optional_deps=""
23 |
24 | # 检查核心依赖
25 | for dep in $core_deps; do
26 | if ! command -v $dep >/dev/null 2>&1; then
27 | missing_core_deps="$missing_core_deps $dep"
28 | fi
29 | done
30 | # 检查可选依赖
31 | for dep in $optional_deps; do
32 | if ! command -v $dep >/dev/null 2>&1; then
33 | missing_optional_deps="$missing_optional_deps $dep"
34 | fi
35 | done
36 |
37 | if [[ -n $missing_core_deps || -n $missing_optional_deps ]]; then
38 | yellow "以下依赖缺失:核心依赖 [$missing_core_deps] 可选依赖 [$missing_optional_deps]"
39 | if command -v apk >/dev/null 2>&1; then
40 | yellow "检测到 apk 包管理器,配置仓库并安装依赖..."
41 | # 动态获取 Alpine 版本
42 | local alpine_version=$(cat /etc/alpine-release 2>/dev/null | cut -d'.' -f1,2 || echo "3.21")
43 | local repo_file="/etc/apk/repositories"
44 | local main_repo="https://dl-cdn.alpinelinux.org/alpine/v${alpine_version}/main"
45 | local community_repo="https://dl-cdn.alpinelinux.org/alpine/v${alpine_version}/community"
46 |
47 | # 确保 main 和 community 仓库存在
48 | if ! grep -q "$main_repo" "$repo_file"; then
49 | echo "$main_repo" >> "$repo_file"
50 | yellow "已添加 main 仓库:$main_repo"
51 | fi
52 | if ! grep -q "$community_repo" "$repo_file"; then
53 | echo "$community_repo" >> "$repo_file"
54 | yellow "已添加 community 仓库:$community_repo"
55 | fi
56 |
57 | # 清理缓存并更新仓库
58 | apk cache clean
59 | apk update || {
60 | red "apk update 失败,可能是网络或镜像源问题"
61 | red "请尝试更换镜像源(如 mirrors.tuna.tsinghua.edu.cn):"
62 | red " sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories"
63 | red " apk update && apk add --no-cache $core_deps $optional_deps"
64 | exit 1
65 | }
66 |
67 | # 安装核心依赖
68 | if [[ -n $missing_core_deps ]]; then
69 | apk add --no-cache $core_deps || {
70 | red "核心依赖 [$missing_core_deps] 安装失败"
71 | red "请手动运行:apk add --no-cache $core_deps"
72 | exit 1
73 | }
74 | green "核心依赖 [$core_deps] 安装成功"
75 | fi
76 |
77 | # 安装可选依赖(qrencode)
78 | if [[ -n $missing_optional_deps ]]; then
79 | apk add --no-cache $optional_deps || {
80 | yellow "可选依赖 [$missing_optional_deps] 安装失败,二维码生成功能可能不可用"
81 | yellow "请手动运行:apk add --no-cache $optional_deps"
82 | yellow "或更换镜像源:sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories"
83 | }
84 | fi
85 | elif command -v apt >/dev/null 2>&1; then
86 | yellow "检测到 apt 包管理器,尝试安装缺失依赖..."
87 | apt update && apt install -y $missing_core_deps $missing_optional_deps || {
88 | red "apt 安装依赖失败,请手动运行:apt update && apt install -y $missing_core_deps $missing_optional_deps"
89 | exit 1
90 | }
91 | elif command -v yum >/dev/null 2>&1; then
92 | yellow "检测到 yum 包管理器,尝试安装缺失依赖..."
93 | yum install -y $missing_core_deps $missing_optional_deps || {
94 | red "yum 安装依赖失败,请手动运行:yum install -y $missing_core_deps $missing_optional_deps"
95 | exit 1
96 | }
97 | else
98 | red "未检测到包管理器,请手动安装以下依赖:"
99 | red "核心依赖:$missing_core_deps"
100 | red "可选依赖:$missing_optional_deps"
101 | red "例如,在 Alpine Linux 上运行:apk add --no-cache $core_deps $optional_deps"
102 | exit 1
103 | fi
104 | fi
105 | }
106 |
107 | check_dependencies
108 |
109 | realip() {
110 | ip=$(curl -s4m8 ip.gs -k) || ip=$(curl -s6m8 ip.gs -k)
111 | if [[ -z $ip ]]; then
112 | red "无法获取服务器 IP,请检查网络连接"
113 | exit 1
114 | fi
115 | }
116 |
117 | inst_cert() {
118 | if ! command -v openssl >/dev/null 2>&1; then
119 | red "openssl 未安装,请先安装 openssl"
120 | exit 1
121 | fi
122 | green "将使用必应自签证书作为 Hysteria 2 的节点证书"
123 | cert_path="/etc/hysteria/cert.crt"
124 | key_path="/etc/hysteria/private.key"
125 | mkdir -p /etc/hysteria
126 | openssl ecparam -genkey -name prime256v1 -out /etc/hysteria/private.key
127 | openssl req -new -x509 -days 36500 -key /etc/hysteria/private.key -out /etc/hysteria/cert.crt -subj "/CN=www.bing.com"
128 | if [[ ! -f $cert_path || ! -f $key_path ]]; then
129 | red "证书生成失败,请检查 openssl 是否正常工作"
130 | exit 1
131 | fi
132 | chmod 644 /etc/hysteria/cert.crt /etc/hysteria/private.key
133 | hy_domain="www.bing.com"
134 | domain="www.bing.com"
135 | }
136 |
137 | inst_port() {
138 | read -p "设置 Hysteria 2 端口 [1-65535](回车则随机分配端口):" port
139 | [[ -z $port ]] && port=$(shuf -i 2000-65535 -n 1)
140 | until [[ -z $(ss -tunlp 2>/dev/null | grep -w udp | awk '{print $5}' | sed 's/.*://g' | grep -w "$port") ]]; do
141 | if [[ -n $(ss -tunlp 2>/dev/null | grep -w udp | awk '{print $5}' | sed 's/.*://g' | grep -w "$port") ]]; then
142 | echo -e "${RED} $port ${PLAIN} 端口已经被其他程序占用,请更换端口重试!"
143 | read -p "设置 Hysteria 2 端口 [1-65535](回车则随机分配端口):" port
144 | [[ -z $port ]] && port=$(shuf -i 2000-65535 -n 1)
145 | fi
146 | done
147 | yellow "将在 Hysteria 2 节点使用的端口是:$port"
148 |
149 | # 防火墙放行端口
150 | if command -v iptables >/dev/null 2>&1; then
151 | iptables -I INPUT -p udp --dport $port -j ACCEPT
152 | ip6tables -I INPUT -p udp --dport $port -j ACCEPT 2>/dev/null || true
153 | iptables-save >/etc/iptables.rules 2>/dev/null || true
154 | ip6tables-save >/etc/ip6tables.rules 2>/dev/null || true
155 | else
156 | yellow "未检测到 iptables,需手动配置防火墙放行 UDP 端口 $port"
157 | yellow "请在 VPS 控制面板或主机防火墙中放行 UDP 端口 $port"
158 | fi
159 | }
160 |
161 | inst_pwd() {
162 | read -p "设置 Hysteria 2 密码(回车跳过为随机字符):" auth_pwd
163 | [[ -z $auth_pwd ]] && auth_pwd=$(date +%s%N | md5sum | cut -c 1-8)
164 | yellow "使用在 Hysteria 2 节点的密码为:$auth_pwd"
165 | }
166 |
167 | inst_site() {
168 | read -rp "请输入 Hysteria 2 的伪装网站地址 (去除https://) [回车默认 www.bing.com]:" proxysite
169 | [[ -z $proxysite ]] && proxysite="www.bing.com"
170 | yellow "使用在 Hysteria 2 节点的伪装网站为:$proxysite"
171 | }
172 |
173 | insthysteria() {
174 | realip
175 |
176 | # 下载 Hysteria 2 二进制 (x86_64)
177 | HYSTERIA_VERSION=$(curl -s https://api.github.com/repos/apernet/hysteria/releases/latest | grep tag_name | cut -d '"' -f 4)
178 | if [[ -z $HYSTERIA_VERSION ]]; then
179 | red "无法获取 Hysteria 最新版本,请检查网络连接"
180 | exit 1
181 | fi
182 | wget -O /usr/local/bin/hysteria https://github.com/apernet/hysteria/releases/download/${HYSTERIA_VERSION}/hysteria-linux-amd64
183 | chmod +x /usr/local/bin/hysteria
184 |
185 | if [[ -f "/usr/local/bin/hysteria" ]]; then
186 | green "Hysteria 2 安装成功!"
187 | else
188 | red "Hysteria 2 安装失败,请检查网络或磁盘空间"
189 | exit 1
190 | fi
191 |
192 | # 询问用户配置
193 | inst_cert
194 | inst_port
195 | inst_pwd
196 | inst_site
197 |
198 | # 设置 Hysteria 配置文件
199 | cat << EOF > /etc/hysteria/config.yaml
200 | listen: :$port
201 |
202 | tls:
203 | cert: $cert_path
204 | key: $key_path
205 |
206 | quic:
207 | initStreamReceiveWindow: 8388608
208 | maxStreamReceiveWindow: 8388608
209 | initConnReceiveWindow: 16777216
210 | maxConnReceiveWindow: 16777216
211 |
212 | auth:
213 | type: password
214 | password: $auth_pwd
215 |
216 | masquerade:
217 | type: proxy
218 | proxy:
219 | url: https://$proxysite
220 | rewriteHost: true
221 | EOF
222 |
223 | # 生成客户端配置文件
224 | last_port=$port
225 | if [[ -n $(echo $ip | grep ":") ]]; then
226 | last_ip="[$ip]"
227 | else
228 | last_ip=$ip
229 | fi
230 |
231 | mkdir -p /root/hy
232 | cat << EOF > /root/hy/hy-client.yaml
233 | server: $last_ip:$last_port
234 |
235 | auth: $auth_pwd
236 |
237 | tls:
238 | sni: $hy_domain
239 | insecure: true
240 |
241 | quic:
242 | initStreamReceiveWindow: 8388608
243 | maxStreamReceiveWindow: 8388608
244 | initConnReceiveWindow: 16777216
245 | maxConnReceiveWindow: 16777216
246 |
247 | fastOpen: true
248 |
249 | socks5:
250 | listen: 127.0.0.1:5080
251 |
252 | transport:
253 | udp:
254 | hopInterval: 30s
255 | EOF
256 |
257 | url="hysteria2://$auth_pwd@$last_ip:$last_port/?insecure=1&sni=$hy_domain#Misaka-Hysteria2"
258 | echo $url > /root/hy/url.txt
259 |
260 | # 启动 Hysteria 2
261 | nohup /usr/local/bin/hysteria server --config /etc/hysteria/config.yaml > /var/log/hysteria.log 2>&1 &
262 | echo $! > /run/hysteria.pid
263 | sleep 2
264 | if ps | grep -q "[h]ysteria.*server"; then
265 | green "Hysteria 2 服务启动成功"
266 | else
267 | red "Hysteria 2 服务启动失败,请检查 /var/log/hysteria.log"
268 | exit 1
269 | fi
270 |
271 | red "======================================================================================"
272 | green "Hysteria 2 代理服务安装完成"
273 | yellow "Hysteria 2 客户端 YAML 配置文件 hy-client.yaml 内容如下,并保存到 /root/hy/hy-client.yaml"
274 | red "$(cat /root/hy/hy-client.yaml)"
275 | yellow "Hysteria 2 节点分享链接如下,并保存到 /root/hy/url.txt"
276 | red "$(cat /root/hy/url.txt)"
277 | }
278 |
279 | unsthysteria() {
280 | kill $(cat /run/hysteria.pid) >/dev/null 2>&1
281 | rm -f /run/hysteria.pid
282 | rm -rf /usr/local/bin/hysteria /etc/hysteria /root/hy
283 | if command -v iptables >/dev/null 2>&1; then
284 | iptables -D INPUT -p udp --dport $port -j ACCEPT >/dev/null 2>&1
285 | ip6tables -D INPUT -p udp --dport $port -j ACCEPT >/dev/null 2>&1
286 | iptables-save >/etc/iptables.rules 2>/dev/null || true
287 | ip6tables-save >/etc/ip6tables.rules 2>/dev/null || true
288 | fi
289 | green "Hysteria 2 已彻底卸载完成!"
290 | }
291 |
292 | starthysteria() {
293 | nohup /usr/local/bin/hysteria server --config /etc/hysteria/config.yaml > /var/log/hysteria.log 2>&1 &
294 | echo $! > /run/hysteria.pid
295 | sleep 2
296 | if ps | grep -q "[h]ysteria.*server"; then
297 | green "Hysteria 2 服务已启动"
298 | else
299 | red "Hysteria 2 服务启动失败,请检查 /var/log/hysteria.log"
300 | fi
301 | }
302 |
303 | stophysteria() {
304 | kill $(cat /run/hysteria.pid) >/dev/null 2>&1
305 | rm -f /run/hysteria.pid
306 | green "Hysteria 2 服务已停止"
307 | }
308 |
309 | hysteriaswitch() {
310 | yellow "请选择你需要的操作:"
311 | echo -e " ${GREEN}1.${PLAIN} 启动 Hysteria 2"
312 | echo -e " ${GREEN}2.${PLAIN} 关闭 Hysteria 2"
313 | echo -e " ${GREEN}3.${PLAIN} 重启 Hysteria 2"
314 | read -rp "请输入选项 [0-3]: " switchInput
315 | case $switchInput in
316 | 1) starthysteria ;;
317 | 2) stophysteria ;;
318 | 3) stophysteria && starthysteria ;;
319 | *) exit 1 ;;
320 | esac
321 | }
322 |
323 | changeport() {
324 | oldport=$(cat /etc/hysteria/config.yaml 2>/dev/null | sed -n 1p | awk '{print $2}' | awk -F ":" '{print $2}')
325 | read -p "设置 Hysteria 2 端口[1-65535](回车则随机分配端口):" port
326 | [[ -z $port ]] && port=$(shuf -i 2000-65535 -n 1)
327 | until [[ -z $(ss -tunlp 2>/dev/null | grep -w udp | awk '{print $5}' | sed 's/.*://g' | grep -w "$port") ]]; do
328 | if [[ -n $(ss -tunlp 2>/dev/null | grep -w udp | awk '{print $5}' | sed 's/.*://g' | grep -w "$port") ]]; then
329 | echo -e "${RED} $port ${PLAIN} 端口已经被其他程序占用,请更换端口重试!"
330 | read -p "设置 Hysteria 2 端口 [1-65535](回车则随机分配端口):" port
331 | [[ -z $port ]] && port=$(shuf -i 2000-65535 -n 1)
332 | fi
333 | done
334 | sed -i "1s#$oldport#$port#g" /etc/hysteria/config.yaml
335 | sed -i "1s#$oldport#$port#g" /root/hy/hy-client.yaml
336 | if command -v iptables >/dev/null 2>&1; then
337 | iptables -D INPUT -p udp --dport $oldport -j ACCEPT >/dev/null 2>&1
338 | ip6tables -D INPUT -p udp --dport $oldport -j ACCEPT >/dev/null 2>&1
339 | iptables -I INPUT -p udp --dport $port -j ACCEPT
340 | ip6tables -I INPUT -p udp --dport $port -j ACCEPT 2>/dev/null || true
341 | iptables-save >/etc/iptables.rules 2>/dev/null || true
342 | ip6tables-save >/etc/ip6tables.rules 2>/dev/null || true
343 | else
344 | yellow "未检测到 iptables,需手动放行新端口 $port"
345 | fi
346 | stophysteria && starthysteria
347 | green "Hysteria 2 端口已成功修改为:$port"
348 | yellow "请手动更新客户端配置文件以使用节点"
349 | showconf
350 | }
351 |
352 | changepasswd() {
353 | oldpasswd=$(cat /etc/hysteria/config.yaml 2>/dev/null | grep password: | awk '{print $2}')
354 | read -p "设置 Hysteria 2 密码(回车跳过为随机字符):" passwd
355 | [[ -z $passwd ]] && passwd=$(date +%s%N | md5sum | cut -c 1-8)
356 | sed -i "s/password: $oldpasswd/password: $passwd/g" /etc/hysteria/config.yaml
357 | sed -i "s/auth: $oldpasswd/auth: $passwd/g" /root/hy/hy-client.yaml
358 | stophysteria && starthysteria
359 | green "Hysteria 2 节点密码已成功修改为:$passwd"
360 | yellow "请手动更新客户端配置文件以使用节点"
361 | showconf
362 | }
363 |
364 | changeconf() {
365 | green "Hysteria 2 配置变更选择如下:"
366 | echo -e " ${GREEN}1.${PLAIN} 修改端口"
367 | echo -e " ${GREEN}2.${PLAIN} 修改密码"
368 | read -p " 请选择操作 [1-2]:" confAnswer
369 | case $confAnswer in
370 | 1) changeport ;;
371 | 2) changepasswd ;;
372 | *) exit 1 ;;
373 | esac
374 | }
375 |
376 | showconf() {
377 | yellow "Hysteria 2 客户端 YAML 配置文件 hy-client.yaml 内容如下,并保存到 /root/hy/hy-client.yaml"
378 | red "$(cat /root/hy/hy-client.yaml)"
379 | yellow "Hysteria 2 节点分享链接如下,并保存到 /root/hy/url.txt"
380 | red "$(cat /root/hy/url.txt)"
381 | }
382 |
383 | update_core() {
384 | HYSTERIA_VERSION=$(curl -s https://api.github.com/repos/apernet/hysteria/releases/latest | grep tag_name | cut -d '"' -f 4)
385 | if [[ -z $HYSTERIA_VERSION ]]; then
386 | red "无法获取 Hysteria 最新版本,请检查网络连接"
387 | exit 1
388 | fi
389 | wget -O /usr/local/bin/hysteria https://github.com/apernet/hysteria/releases/download/${HYSTERIA_VERSION}/hysteria-linux-amd64
390 | chmod +x /usr/local/bin/hysteria
391 | stophysteria && starthysteria
392 | green "Hysteria 2 内核已更新到最新版本"
393 | }
394 |
395 | menu() {
396 | clear
397 | echo "#############################################################"
398 | echo -e "# ${RED}Hysteria 2 一键安装脚本${PLAIN} #"
399 | echo -e "# ${GREEN}原作者${PLAIN}: MisakaNo の 小破站 #"
400 | echo -e "# ${GREEN}原博客${PLAIN}: https://blog.misaka.cyou #"
401 | echo -e "# ${GREEN}原GitHub 项目${PLAIN}: https://github.com/Misaka-blog #"
402 | echo -e "# ${GREEN}移植作者${PLAIN}: TheX #"
403 | echo -e "# ${GREEN}移植项目${PLAIN}: https://github.com/MEILOI/HYTWOALPINE #"
404 | echo -e "# ${GREEN}移植版本${PLAIN}: v1.0.4 #"
405 | echo "#############################################################"
406 | echo ""
407 | echo -e " ${GREEN}1.${PLAIN} 安装 Hysteria 2"
408 | echo -e " ${GREEN}2.${PLAIN} ${RED}卸载 Hysteria 2${PLAIN}"
409 | echo " -------------"
410 | echo -e " ${GREEN}3.${PLAIN} 关闭、开启、重启 Hysteria 2"
411 | echo -e " ${GREEN}4.${PLAIN} 修改 Hysteria 2 配置"
412 | echo -e " ${GREEN}5.${PLAIN} 显示 Hysteria 2 配置文件"
413 | echo -e " ${GREEN}6.${PLAIN} 更新 Hysteria 2 内核"
414 | echo -e " ${GREEN}0.${PLAIN} 退出脚本"
415 | read -rp "请输入选项 [0-6]: " menuInput
416 | case $menuInput in
417 | 1) insthysteria ;;
418 | 2) unsthysteria ;;
419 | 3) hysteriaswitch ;;
420 | 4) changeconf ;;
421 | 5) showconf ;;
422 | 6) update_core ;;
423 | *) exit 1 ;;
424 | esac
425 | }
426 |
427 | menu
428 |
--------------------------------------------------------------------------------
/hysteria.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | export LANG=en_US.UTF-8
4 |
5 | RED="\033[31m"
6 | GREEN="\033[32m"
7 | YELLOW="\033[33m"
8 | PLAIN="\033[0m"
9 |
10 | red() { echo -e "\033[31m\033[01m$1\033[0m"; }
11 | green() { echo -e "\033[32m\033[01m$1\033[0m"; }
12 | yellow() { echo -e "\033[33m\033[01m$1\033[0m"; }
13 |
14 | # 判断是否为 root 用户
15 | if [[ $EUID -ne 0 ]]; then
16 | red "错误: 请以 root 用户或使用 sudo 运行脚本"
17 | yellow "当前用户: $(whoami) (UID: $EUID)"
18 | yellow "尝试: sudo $0 或 su - 后重新运行"
19 | exit 1
20 | fi
21 |
22 | # 检查并安装依赖
23 | check_dependencies() {
24 | local core_deps="curl wget bash openssl"
25 | local missing_core_deps=""
26 |
27 | for dep in $core_deps; do
28 | if ! command -v $dep >/dev/null 2>&1; then
29 | missing_core_deps="$missing_core_deps $dep"
30 | fi
31 | done
32 |
33 | if [[ -n $missing_core_deps ]]; then
34 | yellow "以下核心依赖缺失:[$missing_core_deps]"
35 | if command -v apk >/dev/null 2>&1; then
36 | yellow "检测到 apk 包管理器,配置仓库并安装依赖..."
37 | local alpine_version=$(cat /etc/alpine-release 2>/dev/null | cut -d'.' -f1,2 || echo "3.21")
38 | local repo_file="/etc/apk/repositories"
39 | local main_repo="https://dl-cdn.alpinelinux.org/alpine/v${alpine_version}/main"
40 | local community_repo="https://dl-cdn.alpinelinux.org/alpine/v${alpine_version}/community"
41 |
42 | if ! grep -q "$main_repo" "$repo_file"; then
43 | echo "$main_repo" >> "$repo_file"
44 | yellow "已添加 main 仓库:$main_repo"
45 | fi
46 | if ! grep -q "$community_repo" "$repo_file"; then
47 | echo "$community_repo" >> "$repo_file"
48 | yellow "已添加 community 仓库:$community_repo"
49 | fi
50 |
51 | apk cache clean
52 | apk update || {
53 | red "apk update 失败,尝试更换镜像源:"
54 | red "sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories"
55 | exit 1
56 | }
57 |
58 | apk add --no-cache $core_deps || {
59 | red "核心依赖 [$missing_core_deps] 安装失败"
60 | red "请手动运行:apk add --no-cache $core_deps"
61 | exit 1
62 | }
63 | green "核心依赖 [$core_deps] 安装成功"
64 | else
65 | red "未检测到 apk 包管理器,请手动安装依赖:$missing_core_deps"
66 | exit 1
67 | fi
68 | fi
69 | }
70 |
71 | # 获取服务器 IP
72 | realip() {
73 | ip=$(curl -s4m8 ip.gs -k) || ip=$(curl -s6m8 ip.gs -k)
74 | if [[ -z $ip ]]; then
75 | red "无法获取服务器 IP,请检查网络连接"
76 | exit 1
77 | fi
78 | }
79 |
80 | # 必应自签证书
81 | inst_cert() {
82 | if ! command -v openssl >/dev/null 2>&1; then
83 | red "openssl 未安装,请先安装 openssl"
84 | exit 1
85 | fi
86 | green "将使用必应自签证书作为 Hysteria 2 的节点证书"
87 | cert_path="/etc/hysteria/cert.crt"
88 | key_path="/etc/hysteria/private.key"
89 | mkdir -p /etc/hysteria
90 | openssl ecparam -genkey -name prime256v1 -out "$key_path"
91 | openssl req -new -x509 -days 36500 -key "$key_path" -out "$cert_path" -subj "/CN=www.bing.com"
92 | if [[ ! -f $cert_path || ! -f $key_path ]]; then
93 | red "证书生成失败,请检查 openssl 是否正常工作"
94 | exit 1
95 | fi
96 | chmod 644 "$cert_path" "$key_path"
97 | hy_domain="www.bing.com"
98 | domain="www.bing.com"
99 | }
100 |
101 | # Acme 自动证书
102 | inst_acme() {
103 | green "将使用 Acme 脚本自动申请证书"
104 | if ! command -v curl >/dev/null 2>&1; then
105 | red "curl 未安装,请先安装 curl"
106 | exit 1
107 | fi
108 | read -rp "请输入您的域名(例如 example.com):" domain
109 | [[ -z $domain ]] && red "域名不能为空!" && exit 1
110 | cert_path="/etc/hysteria/cert.crt"
111 | key_path="/etc/hysteria/private.key"
112 | mkdir -p /etc/hysteria
113 | curl -fsSL https://get.acme.sh | sh
114 | ~/.acme.sh/acme.sh --issue -d "$domain" --standalone || {
115 | red "Acme 证书申请失败,请检查域名或网络"
116 | exit 1
117 | }
118 | ~/.acme.sh/acme.sh --install-cert -d "$domain" \
119 | --cert-file "$cert_path" \
120 | --key-file "$key_path" || {
121 | red "证书安装失败,请检查 acme.sh"
122 | exit 1
123 | }
124 | chmod 644 "$cert_path" "$key_path"
125 | hy_domain="$domain"
126 | }
127 |
128 | # 自定义证书路径
129 | inst_custom_cert() {
130 | green "将使用自定义证书路径"
131 | read -rp "请输入证书文件路径(例如 /path/to/cert.crt):" cert_path
132 | read -rp "请输入私钥文件路径(例如 /path/to/private.key):" key_path
133 | read -rp "请输入证书对应的域名(例如 example.com):" domain
134 | [[ -z $cert_path || -z $key_path || -z $domain ]] && red "证书路径、私钥路径或域名不能为空!" && exit 1
135 | [[ ! -f $cert_path || ! -f $key_path ]] && red "证书或私钥文件不存在!" && exit 1
136 | mkdir -p /etc/hysteria
137 | cp "$cert_path" /etc/hysteria/cert.crt
138 | cp "$key_path" /etc/hysteria/private.key
139 | chmod 644 /etc/hysteria/cert.crt /etc/hysteria/private.key
140 | hy_domain="$domain"
141 | }
142 |
143 | # 选择证书
144 | select_cert() {
145 | yellow "请选择证书类型:"
146 | echo -e " ${GREEN}1.${PLAIN} 必应自签证书 (默认)"
147 | echo -e " ${GREEN}2.${PLAIN} Acme 脚本自动申请"
148 | echo -e " ${GREEN}3.${PLAIN} 自定义证书路径"
149 | read -rp "请输入选项 [1-3]: " cert_choice
150 | case $cert_choice in
151 | 1|"") inst_cert ;;
152 | 2) inst_acme ;;
153 | 3) inst_custom_cert ;;
154 | *) red "无效选项,请选择 1-3!" && exit 1 ;;
155 | esac
156 | }
157 |
158 | # 设置单端口
159 | inst_port() {
160 | read -rp "设置 Hysteria 2 端口 [1-65535](回车则随机分配端口):" port
161 | [[ -z $port ]] && port=$(shuf -i 2000-65535 -n 1)
162 | until [[ -z $(ss -tunlp 2>/dev/null | grep -w udp | awk '{print $5}' | sed 's/.*://g' | grep -w "$port") ]]; do
163 | if [[ -n $(ss -tunlp 2>/dev/null | grep -w udp | awk '{print $5}' | sed 's/.*://g' | grep -w "$port") ]]; then
164 | red "$port 端口已经被其他程序占用,请更换端口重试!"
165 | read -rp "设置 Hysteria 2 端口 [1-65535](回车则随机分配端口):" port
166 | [[ -z $port ]] && port=$(shuf -i 2000-65535 -n 1)
167 | fi
168 | done
169 | yellow "将在 Hysteria 2 节点使用的端口是:$port"
170 | yellow "NAT VPS 用户:请确保端口 $port 已由 VPS 提供商映射到公网"
171 | yellow "若未映射,请联系提供商开放 UDP 端口 $port"
172 | }
173 |
174 | # 安装 Hysteria
175 | insthysteria() {
176 | realip
177 | select_cert
178 | inst_port
179 | green "NAT VPS 环境:跳过 iptables 防火墙配置"
180 | green "请确保端口 $port 已由 VPS 主机的防火墙或 NAT 规则开放"
181 | inst_pwd
182 | inst_site
183 |
184 | # 下载 Hysteria 2
185 | HYSTERIA_VERSION=$(curl -s https://api.github.com/repos/apernet/hysteria/releases/latest | grep tag_name | cut -d '"' -f 4)
186 | if [[ -z $HYSTERIA_VERSION ]]; then
187 | red "无法获取 Hysteria 最新版本,请检查网络连接"
188 | exit 1
189 | fi
190 | wget -O /usr/local/bin/hysteria https://github.com/apernet/hysteria/releases/download/${HYSTERIA_VERSION}/hysteria-linux-amd64
191 | chmod +x /usr/local/bin/hysteria
192 | if [[ ! -f "/usr/local/bin/hysteria" ]]; then
193 | red "Hysteria 2 安装失败,请检查网络或磁盘空间"
194 | exit 1
195 | fi
196 | green "Hysteria 2 安装成功!"
197 |
198 | # 设置服务端配置文件
199 | cat << EOF > /etc/hysteria/config.yaml
200 | listen: :$port
201 | tls:
202 | cert: $cert_path
203 | key: $key_path
204 | quic:
205 | initStreamReceiveWindow: 8388608
206 | maxStreamReceiveWindow: 8388608
207 | initConnReceiveWindow: 16777216
208 | maxConnReceiveWindow: 16777216
209 | auth:
210 | type: password
211 | password: $auth_pwd
212 | masquerade:
213 | type: proxy
214 | proxy:
215 | url: https://$proxysite
216 | rewriteHost: true
217 | EOF
218 |
219 | # 生成客户端配置文件
220 | if [[ -n $(echo $ip | grep ":") ]]; then
221 | last_ip="[$ip]"
222 | else
223 | last_ip=$ip
224 | fi
225 | mkdir -p /root/hy
226 | cat << EOF > /root/hy/hy-client.yaml
227 | server: $last_ip:$port
228 | auth: $auth_pwd
229 | tls:
230 | sni: $hy_domain
231 | insecure: true
232 | quic:
233 | initStreamReceiveWindow: 8388608
234 | maxStreamReceiveWindow: 8388608
235 | initConnReceiveWindow: 16777216
236 | maxConnReceiveWindow: 16777216
237 | fastOpen: true
238 | socks5:
239 | listen: 127.0.0.1:5080
240 | EOF
241 |
242 | url="hysteria2://$auth_pwd@$last_ip:$port/?insecure=1&sni=$hy_domain#Misaka-Hysteria2"
243 | echo $url > /root/hy/url.txt
244 |
245 | # 启动 Hysteria 2
246 | nohup /usr/local/bin/hysteria server --config /etc/hysteria/config.yaml > /var/log/hysteria.log 2>&1 &
247 | echo $! > /run/hysteria.pid
248 | sleep 2
249 | if ps | grep -q "[h]ysteria.*server"; then
250 | green "Hysteria 2 服务启动成功"
251 | else
252 | red "Hysteria 2 服务启动失败,请检查 /var/log/hysteria.log"
253 | exit 1
254 | fi
255 |
256 | red "======================================================================================"
257 | green "Hysteria 2 代理服务安装完成"
258 | yellow "Hysteria 2 客户端 YAML 配置文件 hy-client.yaml 内容如下,并保存到 /root/hy/hy-client.yaml"
259 | red "$(cat /root/hy/hy-client.yaml)"
260 | yellow "Hysteria 2 节点分享链接如下,并保存到 /root/hy/url.txt"
261 | red "$(cat /root/hy/url.txt)"
262 | yellow "NAT VPS 用户:请确保端口 $port 已映射到公网,否则客户端无法连接"
263 | }
264 |
265 | # 设置密码
266 | inst_pwd() {
267 | read -rp "设置 Hysteria 2 密码(回车跳过为随机字符):" auth_pwd
268 | [[ -z $auth_pwd ]] && auth_pwd=$(date +%s%N | md5sum | cut -c 1-8)
269 | yellow "使用在 Hysteria 2 节点的密码为:$auth_pwd"
270 | }
271 |
272 | # 设置伪装网站
273 | inst_site() {
274 | read -rp "请输入 Hysteria 2 的伪装网站地址 (去除https://) [回车默认 www.bing.com]:" proxysite
275 | [[ -z $proxysite ]] && proxysite="www.bing.com"
276 | yellow "使用在 Hysteria 2 节点的伪装网站为:$proxysite"
277 | }
278 |
279 | # 卸载 Hysteria
280 | unsthysteria() {
281 | kill $(cat /run/hysteria.pid) >/dev/null 2>&1
282 | rm -f /run/hysteria.pid
283 | rm -rf /usr/local/bin/hysteria /etc/hysteria /root/hy
284 | green "Hysteria 2 已彻底卸载完成!"
285 | yellow "NAT VPS 用户:若需清理主机的端口映射,请联系 VPS 提供商"
286 | }
287 |
288 | # 启动 Hysteria
289 | starthysteria() {
290 | nohup /usr/local/bin/hysteria server --config /etc/hysteria/config.yaml > /var/log/hysteria.log 2>&1 &
291 | echo $! > /run/hysteria.pid
292 | sleep 2
293 | if ps | grep -q "[h]ysteria.*server"; then
294 | green "Hysteria 2 服务已启动"
295 | else
296 | red "Hysteria 2 服务启动失败,请检查 /var/log/hysteria.log"
297 | fi
298 | }
299 |
300 | # 停止 Hysteria
301 | stophysteria() {
302 | kill $(cat /run/hysteria.pid) >/dev/null 2>&1
303 | rm -f /run/hysteria.pid
304 | green "Hysteria 2 服务已停止"
305 | }
306 |
307 | # 开关 Hysteria
308 | hysteriaswitch() {
309 | yellow "请选择你需要的操作:"
310 | echo -e " ${GREEN}1.${PLAIN} 启动 Hysteria 2"
311 | echo -e " ${GREEN}2.${PLAIN} 关闭 Hysteria 2"
312 | echo -e " ${GREEN}3.${PLAIN} 重启 Hysteria 2"
313 | read -rp "请输入选项 [0-3]: " switchInput
314 | drowsiness continues:
315 | case $switchInput in
316 | 1) starthysteria ;;
317 | 2) stophysteria ;;
318 | 3) stophysteria && starthysteria ;;
319 | *) exit 1 ;;
320 | esac
321 | }
322 |
323 | # 修改端口
324 | changeport() {
325 | old_port=$(cat /etc/hysteria/config.yaml 2>/dev/null | grep "listen:" | awk '{print $2}' | cut -d':' -f2)
326 | read -rp "设置 Hysteria 2 端口 [1-65535](回车则随机分配端口):" port
327 | [[ -z $port ]] && port=$(shuf -i 2000-65535 -n 1)
328 | until [[ -z $(ss -tunlp 2>/dev/null | grep -w udp | awk '{print $5}' | sed 's/.*://g' | grep -w "$port") ]]; do
329 | if [[ -n $(ss -tunlp 2>/dev/null | grep -w udp | awk '{print $5}' | sed 's/.*://g' | grep -w "$port") ]]; then
330 | red "$port 端口已经被其他程序占用,请更换端口重试!"
331 | read -rp "设置 Hysteria 2 端口 [1-65535](回车则随机分配端口):" port
332 | [[ -z $port ]] && port=$(shuf -i 2000-65535 -n 1)
333 | fi
334 | done
335 | sed -i "s/listen: :$old_port/listen: :$port/" /etc/hysteria/config.yaml
336 | sed -i "s/server: .*:$old_port/server: $last_ip:$port/" /root/hy/hy-client.yaml
337 | sed -i "s/:$old_port/:$port/" /root/hy/url.txt
338 | stophysteria && starthysteria
339 | green "Hysteria 2 端口已修改为:$port"
340 | yellow "NAT VPS 用户:请联系 VPS 提供商,将端口 $port 映射到公网"
341 | yellow "请手动更新客户端配置文件以使用节点"
342 | showconf
343 | }
344 |
345 | # 修改密码
346 | changepasswd() {
347 | oldpasswd=$(cat /etc/hysteria/config.yaml 2>/dev/null | grep password: | awk '{print $2}')
348 | read -rp "设置 Hysteria 2 密码(回车跳过为随机字符):" passwd
349 | [[ -z $passwd ]] && passwd=$(date +%s%N | md5sum | cut -c 1-8)
350 | sed -i "s/password: $oldpasswd/password: $passwd/g" /etc/hysteria/config.yaml
351 | sed -i "s/auth: $oldpasswd/auth: $passwd/g" /root/hy/hy-client.yaml
352 | sed -i "s/hysteria2:\/\/$oldpasswd@/hysteria2:\/\/$passwd@/" /root/hy/url.txt
353 | stophysteria && starthysteria
354 | green "Hysteria 2 节点密码已成功修改为:$passwd"
355 | yellow "请手动更新客户端配置文件以使用节点"
356 | showconf
357 | }
358 |
359 | # 修改配置
360 | changeconf() {
361 | green "Hysteria 2 配置变更选择如下:"
362 | echo -e " ${GREEN}1.${PLAIN} 修改端口"
363 | echo -e " ${GREEN}2.${PLAIN} 修改密码"
364 | read -rp "请选择操作 [1-2]:" confAnswer
365 | case $confAnswer in
366 | 1) changeport ;;
367 | 2) changepasswd ;;
368 | *) exit 1 ;;
369 | esac
370 | }
371 |
372 | # 显示配置
373 | showconf() {
374 | yellow "Hysteria 2 客户端 YAML 配置文件 hy-client.yaml 内容如下,并保存到 /root/hy/hy-client.yaml"
375 | red "$(cat /root/hy/hy-client.yaml)"
376 | yellow "Hysteria 2 节点分享链接如下,并保存到 /root/hy/url.txt"
377 | red "$(cat /root/hy/url.txt)"
378 | yellow "NAT VPS 用户:请确保端口 $port 已由 VPS 提供商映射到公网"
379 | }
380 |
381 | # 更新内核
382 | update_core() {
383 | HYSTERIA_VERSION=$(curl -s https://api.github.com/repos/apernet/hysteria/releases/latest | grep tag_name | cut -d '"' -f 4)
384 | if [[ -z $HYSTERIA_VERSION ]]; then
385 | red "无法获取 Hysteria 最新版本,请检查网络连接"
386 | exit 1
387 | fi
388 | wget -O /usr/local/bin/hysteria https://github.com/apernet/hysteria/releases/download/${HYSTERIA_VERSION}/hysteria-linux-amd64
389 | chmod +x /usr/local/bin/hysteria
390 | stophysteria && starthysteria
391 | green "Hysteria 2 内核已更新到最新版本"
392 | }
393 |
394 | # 主菜单
395 | menu() {
396 | clear
397 | echo "#############################################################"
398 | echo -e "# ${RED}Hysteria 2 一键安装脚本${PLAIN} #"
399 | echo -e "# ${GREEN}原作者${PLAIN}: MisakaNo の 小破站 #"
400 | echo -e "# ${GREEN}原博客${PLAIN}: https://blog.misaka.cyou #"
401 | echo -e "# ${GREEN}原GitHub 项目${PLAIN}: https://github.com/Misaka-blog #"
402 | echo -e "# ${GREEN}移植作者${PLAIN}: TheX #"
403 | echo -e "# ${GREEN}移植项目${PLAIN}: https://github.com/MEILOI/HYTWOALPINE #"
404 | echo -e "# ${GREEN}移植版本${PLAIN}: v1.0.5 #"
405 | echo "#############################################################"
406 | echo ""
407 | echo -e " ${GREEN}1.${PLAIN} 安装 Hysteria 2"
408 | echo -e " ${GREEN}2.${PLAIN} ${RED}卸载 Hysteria 2${PLAIN}"
409 | echo " -------------"
410 | echo -e " ${GREEN}3.${PLAIN} 关闭、开启、重启 Hysteria 2"
411 | echo -e " ${GREEN}4.${PLAIN} 修改 Hysteria 2 配置"
412 | echo -e " ${GREEN}5.${PLAIN} 显示 Hysteria 2 配置文件"
413 | echo -e " ${GREEN}6.${PLAIN} 更新 Hysteria 2 内核"
414 | echo -e " ${GREEN}0.${PLAIN} 退出脚本"
415 | read -rp "请输入选项 [0-6]: " menuInput
416 | case $menuInput in
417 | 1) insthysteria ;;
418 | 2) unsthysteria ;;
419 | 3) hysteriaswitch ;;
420 | 4) changeconf ;;
421 | 5) showconf ;;
422 | 6) update_core ;;
423 | *) exit 1 ;;
424 | esac
425 | }
426 |
427 | check_dependencies
428 | menu
--------------------------------------------------------------------------------