最后活跃于 1 month ago

nuno 修订了这个 Gist 1 month ago. 转到此修订

1 file changed, 151 insertions

gistfile1.txt(文件已创建)

@@ -0,0 +1,151 @@
1 + #!/usr/bin/env bash
2 +
3 + # ============================================
4 + # Lifecycle
5 + # ============================================
6 +
7 + function config::on_load() {
8 + config::validate
9 + }
10 +
11 + # ============================================
12 + # Server
13 + # ============================================
14 +
15 + WG_INTERFACE="wg0"
16 + WG_CONFIG="$(ctx::wg)/${WG_INTERFACE}.conf"
17 + WG_SERVER_PUBLIC_KEY_FILE="$(ctx::wg)/server_public.key"
18 + WG_SERVER_PRIVATE_KEY_FILE="$(ctx::wg)/server_private.key"
19 + WG_ENDPOINT="wg.krilio.net:51820"
20 + WG_DNS="10.0.0.103"
21 + WG_LISTEN_PORT="51820"
22 + WG_SUBNET="10.1.0.0/16"
23 + WG_LAN="10.0.0.0/24"
24 +
25 + # ============================================
26 + # Device Type → Subnet Mapping
27 + # ============================================
28 +
29 + declare -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
47 + WG_TUNNEL_SPLIT="${WG_SUBNET}, ${WG_LAN}"
48 + WG_TUNNEL_FULL="0.0.0.0/0, ::/0"
49 +
50 + # Default tunnel mode per device type
51 + declare -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 +
67 + function config::interface() { echo "$WG_INTERFACE"; }
68 + function config::config_file() { echo "$WG_CONFIG"; }
69 + function config::endpoint() { echo "$WG_ENDPOINT"; }
70 + function config::dns() { echo "$WG_DNS"; }
71 + function config::listen_port() { echo "$WG_LISTEN_PORT"; }
72 + function config::subnet() { echo "$WG_SUBNET"; }
73 + function config::lan() { echo "$WG_LAN"; }
74 + function config::tunnel_split() { echo "$WG_TUNNEL_SPLIT"; }
75 + function config::tunnel_full() { echo "$WG_TUNNEL_FULL"; }
76 +
77 + function config::server_public_key() {
78 + cat "$WG_SERVER_PUBLIC_KEY_FILE"
79 + }
80 +
81 + function config::device_types() {
82 + local types
83 + { set +u; types="${!DEVICE_SUBNETS[@]}"; set -u; }
84 + echo "$types"
85 + }
86 +
87 + function config::is_valid_type() {
88 + local type="$1"
89 + local subnet
90 + subnet=$(config::subnet_for "$type")
91 + [[ -n "$subnet" ]]
92 + }
93 +
94 + function config::is_guest_type() {
95 + local type="$1"
96 + [[ "$type" == "guest" || "$type" == guest-* ]]
97 + }
98 +
99 + function config::subnet_for() {
100 + local type="$1"
101 + local result
102 + { set +u; result="${DEVICE_SUBNETS[$type]:-}"; set -u; }
103 + echo "$result"
104 + }
105 +
106 + function 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 +
113 + function 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 +
136 + function 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 + }
上一页 下一页