From e321dd312b48073797d4083c4dce59bcedad9e3f Mon Sep 17 00:00:00 2001 From: Davide Grilli Date: Sun, 12 Apr 2026 23:15:02 +0200 Subject: [PATCH] chore: initial project setup Configurazione Docker per WireGuard VPN con wg-easy. Include docker-compose.yml, .env.example, .gitignore e README. --- .env.example | 29 +++++++++++ .gitignore | 6 +++ README.md | 123 +++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 27 ++++++++++ 4 files changed, 185 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 README.md create mode 100644 docker-compose.yml diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..d376fd3 --- /dev/null +++ b/.env.example @@ -0,0 +1,29 @@ +# ============================================================ +# WireGuard VPN — Configurazione locale +# Copia .env.example in .env e compila i valori richiesti. +# ============================================================ + +# --- OBBLIGATORI --- + +# IP pubblico o hostname DDNS del server (es. mio-host.duckdns.org) +WG_HOST=your-ddns-hostname.example.com + +# Hash bcrypt della password per la web UI. +# Vedi README.md per come generarlo. +# ATTENZIONE: incolla il valore esatto, senza aggiungere $$ o escape. +PASSWORD_HASH=$2a$12$replacethiswithyourrealbcrypthash + +# Fuso orario (formato IANA) +# Lista completa: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones +TZ=Europe/Rome + +# --- OPZIONALI (valori di default mostrati) --- + +# Porta UDP WireGuard +WG_PORT=51820 + +# Porta TCP interfaccia web +WG_UI_PORT=51821 + +# DNS inviati ai client +WG_DEFAULT_DNS=1.1.1.1,8.8.8.8 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f509fb0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# Variabili locali — contiene segreti, non committare mai +.env + +# Directory WireGuard — auto-generata dal container al primo avvio. +# Contiene chiavi crittografiche private. Non committare mai. +wg-data/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..96c0cf8 --- /dev/null +++ b/README.md @@ -0,0 +1,123 @@ +# WireGuard VPN con wg-easy (Docker) + +Una configurazione Docker pronta all'uso per eseguire un server VPN WireGuard con interfaccia web [wg-easy](https://github.com/wg-easy/wg-easy), ideale per Raspberry Pi o qualsiasi server Linux. + +--- + +## Prerequisiti + +- Docker e Docker Compose installati sul server +- Una porta UDP aperta sul router/firewall (default: **51820**) +- Un indirizzo IP pubblico statico oppure un hostname DDNS (es. DuckDNS, No-IP) + +--- + +## Configurazione rapida + +### 1. Clona il repository + +```bash +git clone https://santantonio.sytes.net/davide/vpn.git +cd vpn +``` + +### 2. Crea il file `.env` + +```bash +cp .env.example .env +``` + +Apri `.env` con un editor e compila i valori richiesti: + +| Variabile | Descrizione | Obbligatoria | +|---|---|---| +| `WG_HOST` | IP pubblico o hostname DDNS del server | Si | +| `PASSWORD_HASH` | Hash bcrypt della password per la UI web | Si | +| `TZ` | Fuso orario (formato IANA, es. `Europe/Rome`) | Si | +| `WG_PORT` | Porta UDP WireGuard (default: `51820`) | No | +| `WG_UI_PORT` | Porta TCP interfaccia web (default: `51821`) | No | +| `WG_DEFAULT_DNS` | DNS inviati ai client (default: `1.1.1.1,8.8.8.8`) | No | + +### 3. Genera l'hash della password + +Scegli uno dei seguenti metodi: + +**Metodo A — Docker (consigliato, zero dipendenze aggiuntive):** + +```bash +docker run --rm -it ghcr.io/wg-easy/wg-easy wgpw 'LatuaPassword' +``` + +Output esempio: `PASSWORD_HASH='$2a$12$...'` +Copia il valore senza le virgolette singole. + +**Metodo B — `htpasswd` (pacchetto `apache2-utils`):** + +```bash +sudo apt install apache2-utils +htpasswd -bnBC 12 "" 'LatuaPassword' | tr -d ':\n' +``` + +**Metodo C — Python:** + +```bash +pip3 install bcrypt +python3 -c "import bcrypt; print(bcrypt.hashpw(b'LatuaPassword', bcrypt.gensalt(12)).decode())" +``` + +> **Nota:** `openssl passwd` non supporta bcrypt. I metodi sopra producono hash con prefisso `$2a$`, `$2b$` o `$2y$` — tutti validi e accettati da wg-easy. + +> **Attenzione:** incolla il valore esatto in `.env`, senza aggiungere `$$` o altri caratteri di escape. + +### 4. Apri la porta sul router + +Inoltra la porta **UDP 51820** (o quella scelta in `WG_PORT`) verso l'IP locale del server. + +### 5. Avvia il container + +```bash +docker compose up -d +``` + +Al primo avvio la directory `wg-data/` viene creata automaticamente con tutte le chiavi crittografiche. Non è necessario modificarla manualmente. + +### 6. Accedi all'interfaccia web + +``` +http://IP_DEL_TUO_SERVER:51821 +``` + +Da qui puoi aggiungere client, scaricare i file di configurazione e generare QR code per dispositivi mobili. + +--- + +## Struttura del progetto + +``` +vpn-wg/ +├── docker-compose.yml # Definizione del servizio +├── .env.example # Template variabili (committato) +├── .env # Variabili locali con segreti (NON committato) +├── .gitignore +├── README.md +└── wg-data/ # Generato automaticamente dal container (NON committato) + ├── wg0.conf # Configurazione WireGuard (chiavi reali) + └── wg0.json # Stato interno wg-easy (chiavi reali) +``` + +--- + +## Aggiornare wg-easy + +```bash +docker compose pull +docker compose up -d +``` + +--- + +## Sicurezza + +- `wg-data/` contiene chiavi crittografiche private. Non committarla mai su Git. +- `.env` contiene la password (hash) e il tuo hostname. Non committarlo. +- Entrambi sono esclusi da `.gitignore`. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..4ea0a04 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,27 @@ +services: + wg-easy: + image: ghcr.io/wg-easy/wg-easy:latest + container_name: wg-easy + restart: unless-stopped + + environment: + PASSWORD_HASH: "${PASSWORD_HASH}" + TZ: "${TZ}" + WG_HOST: "${WG_HOST}" + WG_DEFAULT_DNS: "${WG_DEFAULT_DNS:-1.1.1.1,8.8.8.8}" + + # Chiavi e configurazione WireGuard persistite fuori dal container + volumes: + - ./wg-data:/etc/wireguard + + ports: + - "${WG_PORT:-51820}:51820/udp" # Traffico VPN WireGuard + - "${WG_UI_PORT:-51821}:51821/tcp" # Interfaccia web di gestione + + cap_add: + - NET_ADMIN # Necessario per gestire le interfacce di rete (wg0) e le route + - SYS_MODULE # Necessario per caricare il modulo kernel wireguard + + sysctls: + - net.ipv4.ip_forward=1 # Abilita il routing dei pacchetti IPv4 tra i client VPN e internet + - net.ipv6.conf.all.forwarding=1 # Abilita il routing IPv6 (richiesto anche se non si usa IPv6)