Última atividade 1 month ago

gistfile1.txt Bruto
1#!/usr/bin/env bash
2
3# ============================================
4# Lifecycle
5# ============================================
6
7function config::on_load() {
8 config::validate
9}
10
11# ============================================
12# Server
13# ============================================
14
15WG_INTERFACE="wg0"
16WG_CONFIG="$(ctx::wg)/${WG_INTERFACE}.conf"
17WG_SERVER_PUBLIC_KEY_FILE="$(ctx::wg)/server_public.key"
18WG_SERVER_PRIVATE_KEY_FILE="$(ctx::wg)/server_private.key"
19WG_ENDPOINT="wg.krilio.net:51820"
20WG_DNS="10.0.0.103"
21WG_LISTEN_PORT="51820"
22WG_SUBNET="10.1.0.0/16"
23WG_LAN="10.0.0.0/24"
24
25# ============================================
26# Device Type → Subnet Mapping
27# ============================================
28
29declare -gA DEVICE_SUBNETS=(
30 [desktop]="10.1.1"
31 [laptop]="10.1.2"
32 [phone]="10.1.3"
33 [tablet]="10.1.4"
34 [guest]="10.1.100"
35 [guest-desktop]="10.1.101"
36 [guest-laptop]="10.1.102"
37 [guest-phone]="10.1.103"
38 [guest-tablet]="10.1.104"
39)
40
41# ============================================
42# Tunnel Modes
43# ============================================
44
45# split — route only VPN subnet + LAN through WireGuard
46# full — route all traffic through WireGuard
47WG_TUNNEL_SPLIT="${WG_SUBNET}, ${WG_LAN}"
48WG_TUNNEL_FULL="0.0.0.0/0, ::/0"
49
50# Default tunnel mode per device type
51declare -gA DEVICE_TUNNEL_MODE=(
52 [desktop]="split"
53 [laptop]="split"
54 [phone]="split"
55 [tablet]="split"
56 [guest]="split"
57 [guest-desktop]="split"
58 [guest-laptop]="split"
59 [guest-phone]="split"
60 [guest-tablet]="split"
61)
62
63# ============================================
64# Accessors
65# ============================================
66
67function config::interface() { echo "$WG_INTERFACE"; }
68function config::config_file() { echo "$WG_CONFIG"; }
69function config::endpoint() { echo "$WG_ENDPOINT"; }
70function config::dns() { echo "$WG_DNS"; }
71function config::listen_port() { echo "$WG_LISTEN_PORT"; }
72function config::subnet() { echo "$WG_SUBNET"; }
73function config::lan() { echo "$WG_LAN"; }
74function config::tunnel_split() { echo "$WG_TUNNEL_SPLIT"; }
75function config::tunnel_full() { echo "$WG_TUNNEL_FULL"; }
76
77function config::server_public_key() {
78 cat "$WG_SERVER_PUBLIC_KEY_FILE"
79}
80
81function config::device_types() {
82 local types
83 { set +u; types="${!DEVICE_SUBNETS[@]}"; set -u; }
84 echo "$types"
85}
86
87function config::is_valid_type() {
88 local type="$1"
89 local subnet
90 subnet=$(config::subnet_for "$type")
91 [[ -n "$subnet" ]]
92}
93
94function config::is_guest_type() {
95 local type="$1"
96 [[ "$type" == "guest" || "$type" == guest-* ]]
97}
98
99function config::subnet_for() {
100 local type="$1"
101 local result
102 { set +u; result="${DEVICE_SUBNETS[$type]:-}"; set -u; }
103 echo "$result"
104}
105
106function config::default_tunnel_for() {
107 local type="$1"
108 local result
109 { set +u; result="${DEVICE_TUNNEL_MODE[$type]:-split}"; set -u; }
110 echo "$result"
111}
112
113function config::allowed_ips_for() {
114 local type="$1"
115 local tunnel="${2:-}"
116
117 # If tunnel mode not specified, use device default
118 if [[ -z "$tunnel" ]]; then
119 tunnel=$(config::default_tunnel_for "$type")
120 fi
121
122 case "$tunnel" in
123 full) echo "$WG_TUNNEL_FULL" ;;
124 split) echo "$WG_TUNNEL_SPLIT" ;;
125 *)
126 log::error "Unknown tunnel mode: ${tunnel} (use 'split' or 'full')"
127 return 1
128 ;;
129 esac
130}
131
132# ============================================
133# Validation
134# ============================================
135
136function config::validate() {
137 if [[ ! -f "$WG_SERVER_PUBLIC_KEY_FILE" ]]; then
138 log::error "Server public key not found: ${WG_SERVER_PUBLIC_KEY_FILE}"
139 exit 1
140 fi
141
142 if [[ ! -f "$WG_SERVER_PRIVATE_KEY_FILE" ]]; then
143 log::error "Server private key not found: ${WG_SERVER_PRIVATE_KEY_FILE}"
144 exit 1
145 fi
146
147 if [[ ! -f "$WG_CONFIG" ]]; then
148 log::error "WireGuard config not found: ${WG_CONFIG}"
149 exit 1
150 fi
151}