服务器E Eric chan5 分钟阅读393 阅读 Hysteria2 一键部署脚本(国内优化版,带加速代理)
摘要服务端脚本 #!/bin/bash # Hysteria2 一键部署脚本(国内优化版,带加速代理) # 支持系统:Ubuntu 18.04+ / Debian 9+ / CentOS 7+ # 颜色定义 RED='\033[0;3
服务端脚本
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
GITHUB_PROXY="https://ghfast.top/"
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}"
}
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}"
}
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)
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
客户端配置
server: 1.1.1.1:11111
auth: 2222
tls:
insecure: true
quic:
initStreamReceiveWindow: 8388608
maxStreamReceiveWindow: 8388608
initConnectionReceiveWindow: 25165824
maxConnectionReceiveWindow: 25165824
maxIdleTimeout: 30s
keepAlivePeriod: 10s
socks5:
listen: 127.0.0.1:61080
username: ""
password: ""
调用
curl -Proxy "socks5://127.0.0.1:61080" 4.ipw.cn
curl.exe --socks5 127.0.0.1:61080 4.ipw.cn