====== Настройка WireGuard + Xray-Gateway на хосте ====== Данное руководство описывает настройку сервера WireGuard, прозрачного прокси Xray-Gateway и управление ими. Все команды выполняются от **root**. ---- ===== 1. Установка WireGuard ===== ==== Установка пакетов ==== apt update apt install wireguard -y ==== Создание ключей ==== wg genkey | tee /etc/wireguard/server_private.key | wg pubkey > /etc/wireguard/server_public.key ==== Конфигурация сервера ==== Файл: **/etc/wireguard/wg0.conf** [Interface] Address = 10.10.10.1/24 ListenPort = 51820 PrivateKey = # Разрешить форвардинг PostUp = sysctl -w net.ipv4.ip_forward=1 PostUp = iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -o eth0 -j MASQUERADE PostDown = iptables -t nat -D POSTROUTING -s 10.10.10.0/24 -o eth0 -j MASQUERADE ==== Запуск WireGuard ==== systemctl enable wg-quick@wg0 systemctl start wg-quick@wg0 ==== Проверка ==== wg show ---- ===== 2. Установка Xray-Gateway (Docker) ===== ==== Создать директорию ==== mkdir -p /opt/xray-gateway cd /opt/xray-gateway ==== Конфигурация Docker Compose ==== Файл: **docker-compose.yml** services: xray: image: ghcr.io/xtls/xray-core:latest container_name: xray-gateway restart: unless-stopped command: ["run", "-c", "/etc/xray/config.json"] network_mode: host volumes: - ./config.json:/etc/xray/config.json:ro ==== Конфигурация Xray ==== Файл: **config.json** { "log": { "loglevel": "warning" }, "inbounds": [ { "tag": "socks", "port": 10808, "listen": "0.0.0.0", "protocol": "socks", "settings": { "auth": "noauth", "udp": true } }, { "tag": "transparent", "port": 12345, "listen": "0.0.0.0", "protocol": "dokodemo-door", "settings": { "network": "tcp,udp", "followRedirect": true }, "sniffing": { "enabled": true, "destOverride": ["http", "tls"] } } ], "outbounds": [ { "tag": "proxy", "protocol": "vless", "settings": { "vnext": [ { "address": "185.238.168.59", "port": 443, "users": [ { "id": "eac6da3c-e718-4661-80d5-d96838618122", "encryption": "none" } ] } ] }, "streamSettings": { "network": "tcp", "security": "reality", "realitySettings": { "fingerprint": "chrome", "serverName": "google.com", "publicKey": "Ks7lJ4awVwB_yxTXNadU0CWUdIP3Jie28tJv60omWFk", "shortId": "0f432ce5", "spiderX": "/" } } }, { "tag": "dns-proxy", "protocol": "dns", "settings": {} }, { "tag": "direct", "protocol": "freedom" } ], "dns": { "servers": [ { "tag": "dns-remote", "address": "8.8.8.8", "port": 53, "skipFallback": true } ] }, "routing": { "rules": [ { "type": "field", "port": 53, "outboundTag": "proxy" }, { "type": "field", "ip": ["10.10.10.0/24", "192.168.0.0/24", "geoip:private"], "outboundTag": "direct" } ] } } ---- ===== 3. IPTables для прозрачного прокси ===== ==== Применение правил ==== iptables -t nat -A PREROUTING -i wg0 -p tcp -j REDIRECT --to-ports 12345 iptables -t nat -A PREROUTING -i wg0 -p udp --dport 53 -j REDIRECT --to-ports 12345 ==== Исключение локальной сети ==== iptables -t nat -I PREROUTING -i wg0 -d 10.10.10.0/24 -j RETURN iptables -t nat -I PREROUTING -i wg0 -d 192.168.0.0/24 -j RETURN ==== Просмотр правил ==== iptables -t nat -L -n --line-numbers ==== Удаление правил ==== iptables -t nat -D PREROUTING <номер строки> ---- ===== 4. Запуск и управление Xray ===== ==== Запуск ==== cd /opt/xray-gateway docker compose up -d ==== Остановка ==== docker compose down ==== Перезапуск ==== docker restart xray-gateway ==== Просмотр логов ==== docker logs -f xray-gateway ---- ===== 5. Тестирование ===== ==== Проверка доступности порта Xray ==== ss -tulnp | grep 12345 ss -tulnp | grep 10808 ==== Проверка DNS через Xray ==== dig @127.0.0.1 -p 12345 google.com ==== Проверка трафика через прокси ==== curl --socks5 127.0.0.1:10808 https://api.ipify.org ==== Проверка с клиента WireGuard ==== curl https://api.ipify.org dig google.com ---- ===== 6. Полное удаление Xray ===== docker compose down rm -rf /opt/xray-gateway ---- ===== Готово ===== Сервер WireGuard + Xray-Gateway полностью работает с прозрачным проксированием и исключением локальных сетей.