angristan / angristan/wireguard-install
支持了Amazon Linux,亲测可用
- Langage dominant
- Shell
- Étoiles
- 11.3k
- Forks
- 1.7k
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
改一下一下配置,支持了AWS EC2实例的: Amazon Linux, 有些注释顺便翻译了一下
```shell
[ec2-user@ip-172-31-45-202 ~]$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2023"
ID="amzn"
ID_LIKE="fedora"
VERSION_ID="2023"
PLATFORM_ID="platform:al2023"
PRETTY_NAME="Amazon Linux 2023.10.20260105"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2023"
HOME_URL="https://aws.amazon.com/linux/amazon-linux-2023/"
DOCUMENTATION_URL="https://docs.aws.amazon.com/linux/"
SUPPORT_URL="https://aws.amazon.com/premiumsupport/"
BUG_REPORT_URL="https://github.com/amazonlinux/amazon-linux-2023"
VENDOR_NAME="AWS"
VENDOR_URL="https://aws.amazon.com/"
SUPPORT_END="2029-06-30"
```
具体sh如下,直接复制替换即可
```shell
#!/bin/bash
# Secure WireGuard server installer
# https://github.com/angristan/wireguard-install
RED='\033[0;31m'
ORANGE='\033[0;33m'
GREEN='\033[0;32m'
NC='\033[0m'
# 中文翻译函数
function echo_with_translation() {
echo -e "$1"
echo -e "$2"
}
function installPackages() {
if ! "$@"; then
echo_with_translation "${RED}Failed to install packages.${NC}" "${RED}安装软件包失败。${NC}"
echo_with_translation "Please check your internet connection and package sources." "请检查您的互联网连接和软件源。"
exit 1
fi
}
function isRoot() {
if [ "${EUID}" -ne 0 ]; then
echo_with_translation "You need to run this script as root" "您需要以 root 身份运行此脚本"
exit 1
fi
}
function checkVirt() {
if command -v virt-what &>/dev/null; then
VIRT=$(virt-what)
else
VIRT=$(systemd-detect-virt)
fi
if [[ ${VIRT} == "openvz" ]]; then
echo_with_translation "OpenVZ is not supported" "OpenVZ 不受支持"
exit 1
fi
if [[ ${VIRT} == "lxc" ]]; then
echo_with_translation "LXC is not supported (yet)." "LXC 暂不支持。"
echo_with_translation "WireGuard can technically run in an LXC container," "WireGuard 理论上可以在 LXC 容器中运行,"
echo_with_translation "but the kernel module has to be installed on the host," "但内核模块必须安装在宿主机上,"
echo_with_translation "the container has to be run with some specific parameters" "容器必须使用特定参数运行"
echo_with_translation "and only the tools need to be installed in the container." "且只需要在容器中安装工具。"
exit 1
fi
}
function checkOS() {
source /etc/os-release
OS="${ID}"
if [[ ${OS} == "debian" || ${OS} == "raspbian" ]]; then
if [[ ${VERSION_ID} -lt 10 ]]; then
echo_with_translation "Your version of Debian (${VERSION_ID}) is not supported. Please use Debian 10 Buster or later" "您的 Debian 版本 (${VERSION_ID}) 不受支持。请使用 Debian 10 Buster 或更高版本"
exit 1
fi
OS=debian # overwrite if raspbian
elif [[ ${OS} == "ubuntu" ]]; then
RELEASE_YEAR=$(echo "${VERSION_ID}" | cut -d'.' -f1)
if [[ ${RELEASE_YEAR} -lt 18 ]]; then
echo_with_translation "Your version of Ubuntu (${VERSION_ID}) is not supported. Please use Ubuntu 18.04 or later" "您的 Ubuntu 版本 (${VERSION_ID}) 不受支持。请使用 Ubuntu 18.04 或更高版本"
exit 1
fi
elif [[ ${OS} == "fedora" ]]; then
if [[ ${VERSION_ID} -lt 32 ]]; then
echo_with_translation "Your version of Fedora (${VERSION_ID}) is not supported. Please use Fedora 32 or later" "您的 Fedora 版本 (${VERSION_ID}) 不受支持。请使用 Fedora 32 或更高版本"
exit 1
fi
elif [[ ${OS} == 'centos' ]] || [[ ${OS} == 'almalinux' ]] || [[ ${OS} == 'rocky' ]]; then
if [[ ${VERSION_ID} == 7* ]]; then
echo_with_translation "Your version of CentOS (${VERSION_ID}) is not supported. Please use CentOS 8 or later" "您的 CentOS 版本 (${VERSION_ID}) 不受支持。请使用 CentOS 8 或更高版本"
exit 1
fi
elif [[ -e /etc/oracle-release ]]; then
source /etc/os-release
OS=oracle
elif [[ -e /etc/arch-release ]]; then
OS=arch
elif [[ -e /etc/alpine-release ]]; then
OS=alpine
if ! command -v virt-what &>/dev/null; then
if ! (apk update && apk add virt-what); then
echo_with_translation "${RED}Failed to install virt-what. Continuing without virtualization check.${NC}" "${RED}安装 virt-what 失败。继续执行虚拟化检查。${NC}"
fi
fi
elif [[ ${OS} == "amzn" ]]; then
# Amazon Linux 2023 support
if [[ ${VERSION_ID} -lt 2023 ]]; then
echo_with_translation "Your version of Amazon Linux (${VERSION_ID}) is not supported. Please use Amazon Linux 2023 or later" "您的 Amazon Linux 版本 (${VERSION_ID}) 不受支持。请使用 Amazon Linux 2023 或更高版本"
exit 1
fi
OS=amzn
else
echo_with_translation "Looks like you aren't running this installer on a Debian, Ubuntu, Fedora, CentOS, AlmaLinux, Oracle, Amazon Linux or Arch Linux system" "看起来您没有在 Debian、Ubuntu、Fedora、CentOS、AlmaLinux、Oracle、Amazon Linux 或 Arch Linux 系统上运行此安装程序"
exit 1
fi
}
function getHomeDirForClient() {
local CLIENT_NAME=$1
if [ -z "${CLIENT_NAME}" ]; then
echo_with_translation "Error: getHomeDirForClient() requires a client name as argument" "错误:getHomeDirForClient() 需要一个客户端名称作为参数"
exit 1
fi
# Home directory of the user, where the client configuration will be written
if [ -e "/home/${CLIENT_NAME}" ]; then
# if $1 is a user name
HOME_DIR="/home/${CLIENT_NAME}"
elif [ "${SUDO_USER}" ]; then
# if not, use SUDO_USER
if [ "${SUDO_USER}" == "root" ]; then
# If running sudo as root
HOME_DIR="/root"
else
HOME_DIR="/home/${SUDO_USER}"
fi
else
# if not SUDO_USER, use /root
HOME_DIR="/root"
fi
echo "$HOME_DIR"
}
function initialCheck() {
isRoot
checkOS
checkVirt
}
function installQuestions() {
echo_with_translation "Welcome to the WireGuard installer!" "欢迎使用 WireGuard 安装程序!"
echo_with_translation "The git repository is available at: https://github.com/angristan/wireguard-install" "Git 仓库位于:https://github.com/angristan/wireguard-install"
echo ""
echo_with_translation "I need to ask you a few questions before starting the setup." "在开始设置之前,我需要问您几个问题。"
echo_with_translation "You can keep the default options and just press enter if you are ok with them." "如果您对默认选项满意,可以保留默认值并直接按回车键。"
echo ""
# Detect public IPv4 or IPv6 address and pre-fill for the user
SERVER_PUB_IP=$(ip -4 addr | sed -ne 's|^.* inet \([^/]*\)/.* scope global.*$|\1|p' | awk '{print $1}' | head -1)
if [[ -z ${SERVER_PUB_IP} ]]; then
# Detect public IPv6 address
SERVER_PUB_IP=$(ip -6 addr | sed -ne 's|^.* inet6 \([^/]*\)/.* scope global.*$|\1|p' | head -1)
fi
read -rp "IPv4 or IPv6 public address: " -e -i "${SERVER_PUB_IP}" SERVER_PUB_IP
# Detect public interface and pre-fill for the user
SERVER_NIC="$(ip -4 route ls | grep default | awk '/dev/ {for (i=1; i<=NF; i++) if ($i == "dev") print $(i+1)}' | head -1)"
until [[ ${SERVER_PUB_NIC} =~ ^[a-zA-Z0-9_]+$ ]]; do
read -rp "Public interface: " -e -i "${SERVER_NIC}" SERVER_PUB_NIC
done
until [[ ${SERVER_WG_NIC} =~ ^[a-zA-Z0-9_]+$ && ${#SERVER_WG_NIC} -lt 16 ]]; do
read -rp "WireGuard interface name: " -e -i wg0 SERVER_WG_NIC
done
until [[ ${SERVER_WG_IPV4} =~ ^([0-9]{1,3}\.){3} ]]; do
read -rp "Server WireGuard IPv4: " -e -i 10.66.66.1 SERVER_WG_IPV4
done
until [[ ${SERVER_WG_IPV6} =~ ^([a-f0-9]{1,4}:){3,4}: ]]; do
read -rp "Server WireGuard IPv6: " -e -i fd42:42:42::1 SERVER_WG_IPV6
done
# Generate random number within private ports range
RANDOM_PORT=$(shuf -i49152-65535 -n1)
until [[ ${SERVER_PORT} =~ ^[0-9]+$ ]] && [ "${SERVER_PORT}" -ge 1 ] && [ "${SERVER_PORT}" -le 65535 ]; do
read -rp "Server WireGuard port [1-65535]: " -e -i "${RANDOM_PORT}" SERVER_PORT
done
# Cloudflare DNS by default
until [[ ${CLIENT_DNS_1} =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]; do
read -rp "First DNS resolver to use for the clients: " -e -i 1.1.1.1 CLIENT_DNS_1
done
until [[ ${CLIENT_DNS_2} =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]; do
read -rp "Second DNS resolver to use for the clients (optional): " -e -i 1.0.0.1 CLIENT_DNS_2
if [[ ${CLIENT_DNS_2} == "" ]]; then
CLIENT_DNS_2="${CLIENT_DNS_1}"
fi
done
until [[ ${ALLOWED_IPS} =~ ^.+$ ]]; do
echo_with_translation -e "\nWireGuard uses a parameter called AllowedIPs to determine what is routed over the VPN." "\nWireGuard 使用一个名为 AllowedIPs 的参数来确定通过 VPN 路由的内容。"
read -rp "Allowed IPs list for generated clients (leave default to route everything): " -e -i '0.0.0.0/0,::/0' ALLOWED_IPS
if [[ ${ALLOWED_IPS} == "" ]]; then
ALLOWED_IPS="0.0.0.0/0,::/0"
fi
done
echo ""
echo_with_translation "Okay, that was all I needed. We are ready to setup your WireGuard server now." "好的,这就是我所需要的全部信息。我们现在准备设置您的 WireGuard 服务器。"
echo_with_translation "You will be able to generate a client at the end of the installation." "安装结束时,您将能够生成客户端。"
read -n1 -r -p "Press any key to continue..."
}
function installWireGuard() {
# Run setup questions first
installQuestions
# Install WireGuard tools and module
if [[ ${OS} == 'ubuntu' ]] || [[ ${OS} == 'debian' && ${VERSION_ID} -gt 10 ]]; then
apt-get update
installPackages apt-get install -y wireguard iptables resolvconf qrencode
elif [[ ${OS} == 'debian' ]]; then
if ! grep -rqs "^deb .* buster-backports" /etc/apt/; then
echo "deb http://deb.debian.org/debian buster-backports main" >/etc/apt/sources.list.d/backports.list
apt-get update
fi
apt-get update
installPackages apt-get install -y iptables resolvconf qrencode
installPackages apt-get install -y -t buster-backports wireguard
elif [[ ${OS} == 'fedora' ]]; then
if [[ ${VERSION_ID} -lt 32 ]]; then
installPackages dnf install -y dnf-plugins-core
dnf copr enable -y jdoss/wireguard
installPackages dnf install -y wireguard-dkms
fi
installPackages dnf install -y wireguard-tools iptables qrencode
elif [[ ${OS} == 'centos' ]] || [[ ${OS} == 'almalinux' ]] || [[ ${OS} == 'rocky' ]]; then
if [[ ${VERSION_ID} == 8* ]]; then
installPackages yum install -y epel-release elrepo-release
installPackages yum install -y kmod-wireguard
yum install -y qrencode || true # not available on release 9
fi
installPackages yum install -y wireguard-tools iptables
elif [[ ${OS} == 'oracle' ]]; then
installPackages dnf install -y oraclelinux-developer-release-el8
dnf config-manager --disable -y ol8_developer
dnf config-manager --enable -y ol8_developer_UEKR6
dnf config-manager --save -y --setopt=ol8_developer_UEKR6.includepkgs='wireguard-tools*'
installPackages dnf install -y wireguard-tools qrencode iptables
elif [[ ${OS} == 'arch' ]]; then
installPackages pacman -S --needed --noconfirm wireguard-tools qrencode
elif [[ ${OS} == 'alpine' ]]; then
apk update
installPackages apk add wireguard-tools iptables libqrencode-tools
elif [[ ${OS} == 'amzn' ]]; then
# Amazon Linux 2023 uses dnf as package manager
installPackages dnf install -y wireguard-tools iptables qrencode
fi
# Verify WireGuard installation
if ! command -v wg &>/dev/null; then
echo_with_translation "${RED}WireGuard installation failed. The 'wg' command was not found.${NC}" "${RED}WireGuard 安装失败。未找到 'wg' 命令。${NC}"
echo_with_translation "Please check the installation output above for errors." "请检查上面的安装输出以查找错误。"
exit 1
fi
# Make sure the directory exists (this does not seem the be the case on fedora)
mkdir /etc/wireguard >/dev/null 2>&1
chmod 600 -R /etc/wireguard/
SERVER_PRIV_KEY=$(wg genkey)
SERVER_PUB_KEY=$(echo "${SERVER_PRIV_KEY}" | wg pubkey)
# Save WireGuard settings
echo "SERVER_PUB_IP=${SERVER_PUB_IP}
SERVER_PUB_NIC=${SERVER_PUB_NIC}
SERVER_WG_NIC=${SERVER_WG_NIC}
SERVER_WG_IPV4=${SERVER_WG_IPV4}
SERVER_WG_IPV6=${SERVER_WG_IPV6}
SERVER_PORT=${SERVER_PORT}
SERVER_PRIV_KEY=${SERVER_PRIV_KEY}
SERVER_PUB_KEY=${SERVER_PUB_KEY}
CLIENT_DNS_1=${CLIENT_DNS_1}
CLIENT_DNS_2=${CLIENT_DNS_2}
ALLOWED_IPS=${ALLOWED_IPS}" >/etc/wireguard/params
# Add server interface
echo "[Interface]
Address = ${SERVER_WG_IPV4}/24,${SERVER_WG_IPV6}/64
ListenPort = ${SERVER_PORT}
PrivateKey = ${SERVER_PRIV_KEY}" >"/etc/wireguard/${SERVER_WG_NIC}.conf"
if pgrep firewalld; then
FIREWALLD_IPV4_ADDRESS=$(echo "${SERVER_WG_IPV4}" | cut -d"." -f1-3)".0"
FIREWALLD_IPV6_ADDRESS=$(echo "${SERVER_WG_IPV6}" | sed 's/:[^:]*$/:0/')
echo "PostUp = firewall-cmd --zone=public --add-interface=${SERVER_WG_NIC} && firewall-cmd --add-port ${SERVER_PORT}/udp && firewall-cmd --add-rich-rule='rule family=ipv4 source address=${FIREWALLD_IPV4_ADDRESS}/24 masquerade' && firewall-cmd --add-rich-rule='rule family=ipv6 source address=${FIREWALLD_IPV6_ADDRESS}/24 masquerade'
PostDown = firewall-cmd --zone=public --add-interface=${SERVER_WG_NIC} && firewall-cmd --remove-port ${SERVER_PORT}/udp && firewall-cmd --remove-rich-rule='rule family=ipv4 source address=${FIREWALLD_IPV4_ADDRESS}/24 masquerade' && firewall-cmd --remove-rich-rule='rule family=ipv6 source address=${FIREWALLD_IPV6_ADDRESS}/24 masquerade'" >>"/etc/wireguard/${SERVER_WG_NIC}.conf"
else
echo "PostUp = iptables -I INPUT -p udp --dport ${SERVER_PORT} -j ACCEPT
PostUp = iptables -I FORWARD -i ${SERVER_PUB_NIC} -o ${SERVER_WG_NIC} -j ACCEPT
PostUp = iptables -I FORWARD -i ${SERVER_WG_NIC} -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE
PostUp = ip6tables -I FORWARD -i ${SERVER_WG_NIC} -j ACCEPT
PostUp = ip6tables -t nat -A POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE
PostDown = iptables -D INPUT -p udp --dport ${SERVER_PORT} -j ACCEPT
PostDown = iptables -D FORWARD -i ${SERVER_PUB_NIC} -o ${SERVER_WG_NIC} -j ACCEPT
PostDown = iptables -D FORWARD -i ${SERVER_WG_NIC} -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE
PostDown = ip6tables -D FORWARD -i ${SERVER_WG_NIC} -j ACCEPT
PostDown = ip6tables -t nat -D POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE" >>"/etc/wireguard/${SERVER_WG_NIC}.conf"
fi
# Enable routing on the server
echo "net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1" >/etc/sysctl.d/wg.conf
if [[ ${OS} == 'fedora' ]]; then
chmod -v 700 /etc/wireguard
chmod -v 600 /etc/wireguard/*
fi
if [[ ${OS} == 'alpine' ]]; then
sysctl -p /etc/sysctl.d/wg.conf
rc-update add sysctl
ln -s /etc/init.d/wg-quick "/etc/init.d/wg-quick.${SERVER_WG_NIC}"
rc-service "wg-quick.${SERVER_WG_NIC}" start
rc-update add "wg-quick.${SERVER_WG_NIC}"
else
sysctl --system
systemctl start "wg-quick@${SERVER_WG_NIC}"
systemctl enable "wg-quick@${SERVER_WG_NIC}"
fi
newClient
echo_with_translation "${GREEN}If you want to add more clients, you simply need to run this script another time!${NC}" "${GREEN}如果要添加更多客户端,只需再次运行此脚本即可!${NC}"
# Check if WireGuard is running
if [[ ${OS} == 'alpine' ]]; then
rc-service --quiet "wg-quick.${SERVER_WG_NIC}" status
else
systemctl is-active --quiet "wg-quick@${SERVER_WG_NIC}"
fi
WG_RUNNING=$?
# WireGuard might not work if we updated the kernel. Tell the user to reboot
if [[ ${WG_RUNNING} -ne 0 ]]; then
echo_with_translation "\n${RED}WARNING: WireGuard does not seem to be running.${NC}" "\n${RED}警告:WireGuard 似乎没有运行。${NC}"
if [[ ${OS} == 'alpine' ]]; then
echo_with_translation "${ORANGE}You can check if WireGuard is running with: rc-service wg-quick.${SERVER_WG_NIC} status${NC}" "${ORANGE}您可以使用以下命令检查 WireGuard 是否正在运行:rc-service wg-quick.${SERVER_WG_NIC} status${NC}"
else
echo_with_translation "${ORANGE}You can check if WireGuard is running with: systemctl status wg-quick@${SERVER_WG_NIC}${NC}" "${ORANGE}您可以使用以下命令检查 WireGuard 是否正在运行:systemctl status wg-quick@${SERVER_WG_NIC}${NC}"
fi
echo_with_translation "${ORANGE}If you get something like \"Cannot find device ${SERVER_WG_NIC}\", please reboot!${NC}" "${ORANGE}如果出现类似\"找不到设备 ${SERVER_WG_NIC}\"的消息,请重启!${NC}"
else # WireGuard is running
echo_with_translation "\n${GREEN}WireGuard is running.${NC}" "\n${GREEN}WireGuard 正在运行。${NC}"
if [[ ${OS} == 'alpine' ]]; then
echo_with_translation "${GREEN}You can check the status of WireGuard with: rc-service wg-quick.${SERVER_WG_NIC} status\n\n${NC}" "${GREEN}您可以使用以下命令检查 WireGuard 的状态:rc-service wg-quick.${SERVER_WG_NIC} status\n\n${NC}"
else
echo_with_translation "${GREEN}You can check the status of WireGuard with: systemctl status wg-quick@${SERVER_WG_NIC}\n\n${NC}" "${GREEN}您可以使用以下命令检查 WireGuard 的状态:systemctl status wg-quick@${SERVER_WG_NIC}\n\n${NC}"
fi
echo_with_translation "${ORANGE}If you don't have internet connectivity from your client, try to reboot the server.${NC}" "${ORANGE}如果您的客户端没有互联网连接,请尝试重启服务器。${NC}"
fi
}
function newClient() {
# If SERVER_PUB_IP is IPv6, add brackets if missing
if [[ ${SERVER_PUB_IP} =~ .*:.* ]]; then
if [[ ${SERVER_PUB_IP} != *"["* ]] || [[ ${SERVER_PUB_IP} != *"]"* ]]; then
SERVER_PUB_IP="[${SERVER_PUB_IP}]"
fi
fi
ENDPOINT="${SERVER_PUB_IP}:${SERVER_PORT}"
echo ""
echo_with_translation "Client configuration" "客户端配置"
echo ""
echo_with_translation "The client name must consist of alphanumeric character(s). It may also include underscores or dashes and can't exceed 15 chars." "客户端名称必须包含字母数字字符。也可以包含下划线或破折号,但不能超过15个字符。"
until [[ ${CLIENT_NAME} =~ ^[a-zA-Z0-9_-]+$ && ${CLIENT_EXISTS} == '0' && ${#CLIENT_NAME} -lt 16 ]]; do
read -rp "Client name: " -e CLIENT_NAME
CLIENT_EXISTS=$(grep -c -E "^### Client ${CLIENT_NAME}\$" "/etc/wireguard/${SERVER_WG_NIC}.conf")
if [[ ${CLIENT_EXISTS} != 0 ]]; then
echo ""
echo_with_translation "${ORANGE}A client with the specified name was already created, please choose another name.${NC}" "${ORANGE}已创建具有指定名称的客户端,请选择另一个名称。${NC}"
echo ""
fi
done
for DOT_IP in {2..254}; do
DOT_EXISTS=$(grep -c "${SERVER_WG_IPV4::-1}${DOT_IP}" "/etc/wireguard/${SERVER_WG_NIC}.conf")
if [[ ${DOT_EXISTS} == '0' ]]; then
break
fi
done
if [[ ${DOT_EXISTS} == '1' ]]; then
echo ""
echo_with_translation "The subnet configured supports only 253 clients." "配置的子网仅支持253个客户端。"
exit 1
fi
BASE_IP=$(echo "$SERVER_WG_IPV4" | awk -F '.' '{ print $1"."$2"."$3 }')
until [[ ${IPV4_EXISTS} == '0' ]]; do
read -rp "Client WireGuard IPv4: ${BASE_IP}." -e -i "${DOT_IP}" DOT_IP
CLIENT_WG_IPV4="${BASE_IP}.${DOT_IP}"
IPV4_EXISTS=$(grep -c "$CLIENT_WG_IPV4/32" "/etc/wireguard/${SERVER_WG_NIC}.conf")
if [[ ${IPV4_EXISTS} != 0 ]]; then
echo ""
echo_with_translation "${ORANGE}A client with the specified IPv4 was already created, please choose another IPv4.${NC}" "${ORANGE}已创建具有指定IPv4的客户端,请选择另一个IPv4。${NC}"
echo ""
fi
done
BASE_IP=$(echo "$SERVER_WG_IPV6" | awk -F '::' '{ print $1 }')
until [[ ${IPV6_EXISTS} == '0' ]]; do
read -rp "Client WireGuard IPv6: ${BASE_IP}::" -e -i "${DOT_IP}" DOT_IP
CLIENT_WG_IPV6="${BASE_IP}::${DOT_IP}"
IPV6_EXISTS=$(grep -c "${CLIENT_WG_IPV6}/128" "/etc/wireguard/${SERVER_WG_NIC}.conf")
if [[ ${IPV6_EXISTS} != 0 ]]; then
echo ""
echo_with_translation "${ORANGE}A client with the specified IPv6 was already created, please choose another IPv6.${NC}" "${ORANGE}已创建具有指定IPv6的客户端,请选择另一个IPv6。${NC}"
echo ""
fi
done
# Generate key pair for the client
CLIENT_PRIV_KEY=$(wg genkey)
CLIENT_PUB_KEY=$(echo "${CLIENT_PRIV_KEY}" | wg pubkey)
CLIENT_PRE_SHARED_KEY=$(wg genpsk)
HOME_DIR=$(getHomeDirForClient "${CLIENT_NAME}")
# Create client file and add the server as a peer
echo "[Interface]
PrivateKey = ${CLIENT_PRIV_KEY}
Address = ${CLIENT_WG_IPV4}/32,${CLIENT_WG_IPV6}/128
DNS = ${CLIENT_DNS_1},${CLIENT_DNS_2}
# Uncomment the next line to set a custom MTU
# This might impact performance, so use it only if you know what you are doing
# See https://github.com/nitred/nr-wg-mtu-finder to find your optimal MTU
# MTU = 1420
[Peer]
PublicKey = ${SERVER_PUB_KEY}
PresharedKey = ${CLIENT_PRE_SHARED_KEY}
Endpoint = ${ENDPOINT}
AllowedIPs = ${ALLOWED_IPS}" >"${HOME_DIR}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf"
# Add the client as a peer to the server
echo -e "\n### Client ${CLIENT_NAME}
[Peer]
PublicKey = ${CLIENT_PUB_KEY}
PresharedKey = ${CLIENT_PRE_SHARED_KEY}
AllowedIPs = ${CLIENT_WG_IPV4}/32,${CLIENT_WG_IPV6}/128" >>"/etc/wireguard/${SERVER_WG_NIC}.conf"
wg syncconf "${SERVER_WG_NIC}" <(wg-quick strip "${SERVER_WG_NIC}")
# Log the creation of the new client
echo "$(date '+%Y-%m-%d %H:%M:%S') - Created client: ${CLIENT_NAME}" >> /var/log/wireguard-management.log 2>/dev/null || echo "$(date '+%Y-%m-%d %H:%M:%S') - Created client: ${CLIENT_NAME}" > /tmp/wireguard-management.log
# Generate QR code if qrencode is installed
if command -v qrencode &>/dev/null; then
echo_with_translation "${GREEN}\nHere is your client config file as a QR Code:\n${NC}" "${GREEN}\n这是您的客户端配置文件的二维码:\n${NC}"
qrencode -t ansiutf8 -l L <"${HOME_DIR}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf"
echo ""
fi
echo_with_translation "${GREEN}Your client config file is in ${HOME_DIR}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf${NC}" "${GREEN}您的客户端配置文件位于 ${HOME_DIR}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf${NC}"
}
function listClients() {
NUMBER_OF_CLIENTS=$(grep -c -E "^### Client" "/etc/wireguard/${SERVER_WG_NIC}.conf")
if [[ ${NUMBER_OF_CLIENTS} -eq 0 ]]; then
echo ""
echo_with_translation "You have no existing clients!" "您没有任何现有客户端!"
exit 1
fi
grep -E "^### Client" "/etc/wireguard/${SERVER_WG_NIC}.conf" | cut -d ' ' -f 3 | nl -s ') '
}
function showConnectedClients() {
if ! command -v wg &>/dev/null; then
echo_with_translation "WireGuard is not installed or 'wg' command not found." "WireGuard 未安装或未找到 'wg' 命令。"
return 1
fi
echo_with_translation "Currently connected WireGuard clients:" "当前连接的 WireGuard 客户端:"
wg show "${SERVER_WG_NIC}" latest-handshakes
}
function revokeClient() {
NUMBER_OF_CLIENTS=$(grep -c -E "^### Client" "/etc/wireguard/${SERVER_WG_NIC}.conf")
if [[ ${NUMBER_OF_CLIENTS} == '0' ]]; then
echo ""
echo_with_translation "You have no existing clients!" "您没有任何现有客户端!"
exit 1
fi
echo ""
echo_with_translation "Select the existing client you want to revoke" "选择您要撤销的现有客户端"
grep -E "^### Client" "/etc/wireguard/${SERVER_WG_NIC}.conf" | cut -d ' ' -f 3 | nl -s ') '
until [[ ${CLIENT_NUMBER} -ge 1 && ${CLIENT_NUMBER} -le ${NUMBER_OF_CLIENTS} ]]; do
if [[ ${CLIENT_NUMBER} == '1' ]]; then
read -rp "Select one client [1]: " CLIENT_NUMBER
else
read -rp "Select one client [1-${NUMBER_OF_CLIENTS}]: " CLIENT_NUMBER
fi
done
# match the selected number to a client name
CLIENT_NAME=$(grep -E "^### Client" "/etc/wireguard/${SERVER_WG_NIC}.conf" | cut -d ' ' -f 3 | sed -n "${CLIENT_NUMBER}"p)
# remove [Peer] block matching $CLIENT_NAME
sed -i "/^### Client ${CLIENT_NAME}\$/,/^$/d" "/etc/wireguard/${SERVER_WG_NIC}.conf"
# remove generated client file
HOME_DIR=$(getHomeDirForClient "${CLIENT_NAME}")
rm -f "${HOME_DIR}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf"
# restart wireguard to apply changes
wg syncconf "${SERVER_WG_NIC}" <(wg-quick strip "${SERVER_WG_NIC}")
# Log the revocation of the client
echo "$(date '+%Y-%m-%d %H:%M:%S') - Revoked client: ${CLIENT_NAME}" >> /var/log/wireguard-management.log 2>/dev/null || echo "$(date '+%Y-%m-%d %H:%M:%S') - Revoked client: ${CLIENT_NAME}" > /tmp/wireguard-management.log
}
function uninstallWg() {
echo ""
echo_with_translation "\n${RED}WARNING: This will uninstall WireGuard and remove all the configuration files!${NC}" "\n${RED}警告:这将卸载 WireGuard 并删除所有配置文件!${NC}"
echo_with_translation "${ORANGE}Please backup the /etc/wireguard directory if you want to keep your configuration files.\n${NC}" "${ORANGE}如果要保留配置文件,请备份 /etc/wireguard 目录。\n${NC}"
read -rp "Do you really want to remove WireGuard? [y/n]: " -e REMOVE
REMOVE=${REMOVE:-n}
if [[ $REMOVE == 'y' ]]; then
checkOS
if [[ ${OS} == 'alpine' ]]; then
rc-service "wg-quick.${SERVER_WG_NIC}" stop
rc-update del "wg-quick.${SERVER_WG_NIC}"
unlink "/etc/init.d/wg-quick.${SERVER_WG_NIC}"
rc-update del sysctl
else
systemctl stop "wg-quick@${SERVER_WG_NIC}"
systemctl disable "wg-quick@${SERVER_WG_NIC}"
fi
if [[ ${OS} == 'ubuntu' ]] || [[ ${OS} == 'debian' ]]; then
apt-get remove -y wireguard wireguard-tools qrencode
elif [[ ${OS} == 'fedora' ]]; then
dnf remove -y --noautoremove wireguard-tools qrencode
if [[ ${VERSION_ID} -lt 32 ]]; then
dnf remove -y --noautoremove wireguard-dkms
dnf copr disable -y jdoss/wireguard
fi
elif [[ ${OS} == 'centos' ]] || [[ ${OS} == 'almalinux' ]] || [[ ${OS} == 'rocky' ]]; then
yum remove -y --noautoremove wireguard-tools
if [[ ${VERSION_ID} == 8* ]]; then
yum remove --noautoremove kmod-wireguard qrencode
fi
elif [[ ${OS} == 'oracle' ]]; then
yum remove --noautoremove wireguard-tools qrencode
elif [[ ${OS} == 'arch' ]]; then
pacman -Rs --noconfirm wireguard-tools qrencode
elif [[ ${OS} == 'alpine' ]]; then
(cd qrencode-4.1.1 || exit && make uninstall)
rm -rf qrencode-* || exit
apk del wireguard-tools libqrencode libqrencode-tools
elif [[ ${OS} == 'amzn' ]]; then
dnf remove -y --noautoremove wireguard-tools qrencode
fi
rm -rf /etc/wireguard
rm -f /etc/sysctl.d/wg.conf
if [[ ${OS} == 'alpine' ]]; then
rc-service --quiet "wg-quick.${SERVER_WG_NIC}" status &>/dev/null
else
# Reload sysctl
sysctl --system
# Check if WireGuard is running
systemctl is-active --quiet "wg-quick@${SERVER_WG_NIC}"
fi
WG_RUNNING=$?
if [[ ${WG_RUNNING} -eq 0 ]]; then
echo_with_translation "WireGuard failed to uninstall properly." "WireGuard 卸载失败。"
exit 1
else
echo_with_translation "WireGuard uninstalled successfully." "WireGuard 卸载成功。"
exit 0
fi
else
echo ""
echo_with_translation "Removal aborted!" "移除已取消!"
fi
}
function manageMenu() {
echo_with_translation "Welcome to WireGuard-install!" "欢迎使用 WireGuard 安装程序!"
echo_with_translation "The git repository is available at: https://github.com/angristan/wireguard-install" "Git 仓库位于:https://github.com/angristan/wireguard-install"
echo ""
echo_with_translation "It looks like WireGuard is already installed." "看起来 WireGuard 已经安装。"
echo ""
echo_with_translation "What do you want to do?" "您想做什么?"
echo_with_translation " 1) Add a new user" " 1) 添加新用户"
echo_with_translation " 2) List all users" " 2) 列出所有用户"
echo_with_translation " 3) Revoke existing user" " 3) 撤销现有用户"
echo_with_translation " 4) Show connected clients" " 4) 显示已连接的客户端"
echo_with_translation " 5) Uninstall WireGuard" " 5) 卸载 WireGuard"
echo_with_translation " 6) Exit" " 6) 退出"
until [[ ${MENU_OPTION} =~ ^[1-6]$ ]]; do
read -rp "Select an option [1-6]: " MENU_OPTION
done
case "${MENU_OPTION}" in
1)
newClient
;;
2)
listClients
;;
3)
revokeClient
;;
4)
showConnectedClients
;;
5)
uninstallWg
;;
6)
exit 0
;;
esac
}
# Check for root, virt, OS...
initialCheck
# Check if WireGuard is already installed and load params
if [[ -e /etc/wireguard/params ]]; then
source /etc/wireguard/params
manageMenu
else
installWireGuard
fi
```
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Évaluation
Cette issue n'a pas encore été évaluée.