共计 5045 个字符,预计需要花费 13 分钟才能阅读完成。
服务端脚本
#!/bin/bash
# Hysteria2 一键部署脚本(国内优化版,带加速代理)
# 支持系统:Ubuntu 18.04+ / Debian 9+ / CentOS 7+
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # 无颜色
# 国内GitHub加速代理
GITHUB_PROXY="https://ghfast.top/"
# 检查是否为root用户
check_root() {
if [ "$(id -u)" -ne 0 ]; then
echo -e "${RED}错误:请使用root用户运行此脚本${NC}" >&2
exit 1
fi
}
# 检查系统并安装必要依赖
check_dependencies() {
echo -e "${BLUE}正在检查系统依赖...${NC}"
# 检测操作系统
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$NAME
VERSION=$VERSION_ID
else
echo -e "${RED}无法检测操作系统,请手动安装依赖${NC}"
exit 1
fi
# 安装必要工具
if [[ $OS == *"Ubuntu"* || $OS == *"Debian"* ]]; then
apt-get update -y > /dev/null
apt-get install -y wget curl unzip tar firewalld openssl > /dev/null
elif [[ $OS == *"CentOS"* || $OS == *"Red Hat"* ]]; then
yum install -y wget curl unzip tar firewalld openssl > /dev/null
systemctl start firewalld
systemctl enable firewalld
else
echo -e "${RED}不支持的操作系统${NC}"
exit 1
fi
echo -e "${GREEN}系统依赖检查完成${NC}"
}
# 获取最新版本Hysteria2
get_latest_version() {
echo -e "${BLUE}正在获取最新版本的Hysteria2...${NC}"
# 使用带代理的源获取版本信息
LATEST_VERSION=$(curl -s "${GITHUB_PROXY}https://api.github.com/repos/apernet/hysteria/releases/latest" | grep -oP '"tag_name": "\K(.*)(?=")')
if [ -z "$LATEST_VERSION" ]; then
echo -e "${YELLOW}无法获取最新版本,使用默认版本v2.6.5${NC}"
LATEST_VERSION="app/v2.6.5"
fi
echo -e "${GREEN}将安装 Hysteria2 $LATEST_VERSION${NC}"
}
# 下载并安装Hysteria2
install_hysteria() {
echo -e "${BLUE}正在安装Hysteria2...${NC}"
# 检测系统架构
ARCH=$(uname -m)
case $ARCH in
x86_64)
ARCH="amd64"
;;
aarch64)
ARCH="arm64"
;;
*)
echo -e "${RED}不支持的架构: $ARCH${NC}"
exit 1
;;
esac
# 带代理的下载地址
DOWNLOAD_URL="${GITHUB_PROXY}https://github.com/apernet/hysteria/releases/download/$LATEST_VERSION/hysteria-linux-$ARCH"
# 创建目录
mkdir -p /etc/hysteria
mkdir -p /var/log/hysteria
# 下载二进制文件
if ! wget -q -O /usr/local/bin/hysteria "$DOWNLOAD_URL"; then
echo -e "${YELLOW}主代理地址下载失败,尝试备用代理...${NC}"
# 备用代理地址
DOWNLOAD_URL="https://github.moeyy.xyz/https://github.com/apernet/hysteria/releases/download/$LATEST_VERSION/hysteria-linux-$ARCH"
if ! wget -q -O /usr/local/bin/hysteria "$DOWNLOAD_URL"; then
echo -e "${RED}所有代理地址下载均失败,请检查网络连接${NC}"
exit 1
fi
fi
# 设置权限
chmod +x /usr/local/bin/hysteria
echo -e "${GREEN}Hysteria2 安装完成${NC}"
}
# 生成配置文件
generate_config() {
echo -e "${BLUE}正在生成配置文件...${NC}"
# 生成随机端口
PORT=$((RANDOM % 5000 + 10000))
# 生成随机密码
PASSWORD=$(head -c 16 /dev/urandom | base64)
# 生成TLS证书
openssl req -x509 -newkey rsa:4096 -nodes -keyout /etc/hysteria/server.key -out /etc/hysteria/server.crt -days 3650 -subj "/CN=hysteria.example.com" > /dev/null 2>&1
# 创建服务器配置文件
cat > /etc/hysteria/config.yaml << EOF
listen: :$PORT
tls:
cert: /etc/hysteria/server.crt
key: /etc/hysteria/server.key
auth:
type: password
password: $PASSWORD
masquerade:
type: file
file:
dir: /var/www/html # 这里指定目录(必须存在)
path: /
quic:
initStreamReceiveWindow: 8388608
maxStreamReceiveWindow: 8388608
initConnectionReceiveWindow: 25165824
maxConnectionReceiveWindow: 25165824
maxIdleTimeout: 30s
keepAlivePeriod: 10s
EOF
# 创建伪装页面目录
mkdir -p /var/www/html
echo "<h1>Welcome</h1>" > /var/www/html/index.html
echo -e "${GREEN}配置文件生成完成${NC}"
}
# 创建系统服务
create_service() {
echo -e "${BLUE}正在创建系统服务...${NC}"
cat > /etc/systemd/system/hysteria.service << EOF
[Unit]
Description=Hysteria2 Server
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/hysteria server -c /etc/hysteria/config.yaml
Restart=always
RestartSec=5
User=root
[Install]
WantedBy=multi-user.target
EOF
# 重新加载系统服务
systemctl daemon-reload
systemctl enable hysteria
echo -e "${GREEN}系统服务创建完成${NC}"
}
# 配置防火墙
configure_firewall() {
echo -e "${BLUE}正在配置防火墙...${NC}"
# 开放端口
firewall-cmd --zone=public --add-port=$PORT/udp --permanent
firewall-cmd --reload
echo -e "${GREEN}防火墙配置完成${NC}"
}
# 启动服务并显示信息
start_service() {
echo -e "${BLUE}正在启动Hysteria2服务...${NC}"
systemctl start hysteria
# 检查服务状态
if systemctl is-active --quiet hysteria; then
echo -e "${GREEN}Hysteria2 服务启动成功!${NC}"
echo -e "\n${YELLOW}配置信息:${NC}"
echo -e "地址: $(curl -s icanhazip.com)"
echo -e "端口: $PORT"
echo -e "密码: $PASSWORD"
echo -e "证书: 自签名证书"
echo -e "\n${YELLOW}客户端配置示例:${NC}"
echo -e "server: $(curl -s icanhazip.com):$PORT"
echo -e "auth:"
echo -e " password: $PASSWORD"
echo -e "tls:"
echo -e " insecure: true"
echo -e "\n${YELLOW}管理命令:${NC}"
echo -e "启动: systemctl start hysteria"
echo -e "停止: systemctl stop hysteria"
echo -e "重启: systemctl restart hysteria"
echo -e "状态: systemctl status hysteria"
echo -e "日志: journalctl -u hysteria -f"
else
echo -e "${RED}Hysteria2 服务启动失败,请检查日志${NC}"
exit 1
fi
}
# 主函数
main() {
clear
echo -e "${BLUE}=====================================${NC}"
echo -e "${BLUE} Hysteria2 一键部署脚本 ${NC}"
echo -e "${BLUE} (国内环境优化版) ${NC}"
echo -e "${BLUE}=====================================${NC}\n"
check_root
check_dependencies
get_latest_version
install_hysteria
generate_config
create_service
configure_firewall
start_service
echo -e "\n${GREEN}部署完成!${NC}"
}
# 运行主函数
main
客户端配置
# 服务器地址(替换为你的服务器IP/域名和端口)
server: 1.1.1.1:11111
# 认证信息(与服务器端的密码一致)
auth: 2222
# TLS 配置(服务器使用自签名证书时需要)
tls:
# 忽略证书验证(自签名证书必须开启)
insecure: true
# 可选:如果使用了自定义证书,可以指定证书路径
# ca: /path/to/ca.crt
# QUIC 协议配置(可选,根据需要调整)
quic:
initStreamReceiveWindow: 8388608
maxStreamReceiveWindow: 8388608
initConnectionReceiveWindow: 25165824
maxConnectionReceiveWindow: 25165824
maxIdleTimeout: 30s
keepAlivePeriod: 10s
# 出站代理配置(可选,根据客户端需求设置)
socks5:
listen: 127.0.0.1:61080 # SOCKS5 代理监听地址
username: "" # 可选:代理认证用户名
password: "" # 可选:代理认证密码
#http:
# listen: 127.0.0.1:8080 # HTTP 代理监听地址
# 可选:设置上游HTTP代理
# proxy: http://1.2.3.4:8080
调用
# powershell
curl -Proxy "socks5://127.0.0.1:61080" 4.ipw.cn
# Linux
curl.exe --socks5 127.0.0.1:61080 4.ipw.cn
正文完