-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.sh
executable file
·241 lines (201 loc) · 7.19 KB
/
client.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env bash
if [ -z $(which kubectl) ]
then
echo "kubectl not found: brew install kubectl"
exit 1
fi
if [ -z $(which wg-quick) ]
then
echo "wireguard tools not found: brew install wireguard-tools"
exit 1
fi
if [ -z $(which az) ]
then
echo "azure tools not found: brew install azure-cli"
exit 1
fi
case $1 in
up|start|connect)
if [[ $(sudo wg | wc -l) -ne 0 ]]
then
echo "you already connected"
exit 1
fi
echo -n "checking if kubectl is connected: "
if ! kubectl get ns default >/dev/null 2>&1
then
echo "kubectl is not connected"
exit 1
else
echo "ok"
fi
echo -n "checking az cli: "
if ! az account show >/dev/null 2>&1
then
echo "az cli not connected, run: az login"
exit 1
else
echo "ok"
fi
echo -n "retrieving server public ip: "
SERVER_PUBLIC_IP=$(kubectl -n vpn get svc vpn -o jsonpath="{.status.loadBalancer.ingress[0].ip}")
if [ -z "$SERVER_PUBLIC_IP" ]
then
echo "can not find vpn service public ip, make sure that kubernetes your are pointing right now has vpn being deployed and running"
exit 1
else
echo "ok"
fi
echo -n "retrieving access token: "
ACCESS_TOKEN=$(az account get-access-token --resource-type ms-graph | jq -r ".accessToken")
if [ -z "$ACCESS_TOKEN" ]
then
echo "can not get access token for your account"
echo "try to run the following command to see why it is not working:"
echo "az account get-access-token --resource-type ms-graph"
exit 1
else
echo "ok"
fi
echo -n "generating client keys: "
CLIENT_PRIVATE_KEY=$(wg genkey)
CLIENT_PUBLIC_KEY=$(echo -n $CLIENT_PRIVATE_KEY | wg pubkey)
echo "ok"
echo -n "sending request: "
RESPONSE=$(curl -s -X POST "http://$SERVER_PUBLIC_IP/add" -d "{\"access_token\":\"$ACCESS_TOKEN\",\"public_key\":\"$CLIENT_PUBLIC_KEY\"}")
CLIENT_PRIVATE_IP=$(echo $RESPONSE | jq -r ".client_private_ip")
SERVER_PUBLIC_KEY=$(echo $RESPONSE | jq -r ".server_public_key")
if [ "$CLIENT_PRIVATE_IP" == "null" ]
then
echo "was not able to retrieve an client private ip"
echo "the response was:"
echo $RESPONSE
exit 1
fi
if [ "$SERVER_PUBLIC_KEY" == "null" ]
then
echo "was not able to retrieve an server public key"
echo "the response was:"
echo $RESPONSE
exit 1
fi
echo "ok"
echo -n "retrieving dns addr: "
DNS=$(kubectl -n kube-system get svc kube-dns -o jsonpath="{.spec.clusterIP}")
if [ -z "$DNS" ]
then
echo "was not able to retireve dns ip address"
echo "i did tried to run following command to get it:"
echo "kubectl -n kube-system get svc kube-dns"
exit 1
else
echo "ok"
fi
echo -n "preparing searchdomains: "
for NS in $(kubectl get ns -o jsonpath="{.items[*].metadata.name}")
do
DNS="$DNS, $NS.svc.cluster.local"
done
DNS="$DNS, svc.cluster.local, cluster.local"
echo "ok"
echo -n "preparing allowed ips: "
ALLOWEDIPS="10.0.0.0/8"
for IP in $(kubectl get ing -A -o jsonpath="{.items[*].status.loadBalancer.ingress[0].ip}" | tr ' ' '\n' | uniq)
do
ALLOWEDIPS="$ALLOWEDIPS, $IP/32"
done
echo "ok"
echo -n "preparing confing: "
mkdir -p ~/.config
echo "[Interface]" > ~/.config/vpn.conf
echo "Address = $CLIENT_PRIVATE_IP" >> ~/.config/vpn.conf
echo "PrivateKey = $CLIENT_PRIVATE_KEY" >> ~/.config/vpn.conf
echo "DNS = $DNS" >> ~/.config/vpn.conf
echo "" >> ~/.config/vpn.conf
echo "[Peer]" >> ~/.config/vpn.conf
echo "PublicKey = $SERVER_PUBLIC_KEY" >> ~/.config/vpn.conf
echo "Endpoint = $SERVER_PUBLIC_IP:51820" >> ~/.config/vpn.conf
echo "AllowedIPs = $ALLOWEDIPS" >> ~/.config/vpn.conf
echo "PersistentKeepalive = 25" >> ~/.config/vpn.conf
echo "" >> ~/.config/vpn.conf
chmod 600 ~/.config/vpn.conf
echo "ok"
echo "starting wireguard"
wg-quick up ~/.config/vpn.conf
;;
status|check)
if [[ $(sudo wg | wc -l) -ne 0 ]]
then
sudo wg
echo ''
echo -n "retrieving client public key: "
CLIENT_PUBLIC_KEY=$(sudo wg | grep ' public key: ' | awk '{ print $3 }')
echo "ok"
echo -n "retrieving access token: "
ACCESS_TOKEN=$(az account get-access-token --resource-type ms-graph | jq -r ".accessToken")
echo "ok"
echo -n "retrieving server public ip: "
SERVER_PUBLIC_IP=$(kubectl -n vpn get svc vpn -o jsonpath="{.status.loadBalancer.ingress[0].ip}")
echo "ok"
echo -n "sending check request: "
curl -s -X POST "http://$SERVER_PUBLIC_IP/check" -d "{\"access_token\":\"$ACCESS_TOKEN\",\"public_key\":\"$CLIENT_PUBLIC_KEY\"}"
else
echo "disconnected"
fi
;;
down|stop|disconnect)
wg-quick down ~/.config/vpn.conf
echo '[#] rm -f ~/.config/vpn.conf'
rm -f ~/.config/vpn.conf || true
;;
ui|portal|manage)
echo -n "checking if kubectl is connected: "
if ! kubectl get ns default >/dev/null 2>&1
then
echo "kubectl is not connected"
exit 1
else
echo "ok"
fi
echo -n "checking az cli: "
if ! az account show >/dev/null 2>&1
then
echo "az cli not connected, run: az login"
exit 1
else
echo "ok"
fi
echo -n "retrieving server public ip: "
SERVER_PUBLIC_IP=$(kubectl -n vpn get svc vpn -o jsonpath="{.status.loadBalancer.ingress[0].ip}")
if [ -z "$SERVER_PUBLIC_IP" ]
then
echo "can not find vpn service public ip, make sure that kubernetes your are pointing right now has vpn being deployed and running"
exit 1
else
echo "ok"
fi
echo -n "retrieving access token: "
ACCESS_TOKEN=$(az account get-access-token --resource-type ms-graph | jq -r ".accessToken")
if [ -z "$ACCESS_TOKEN" ]
then
echo "can not get access token for your account"
echo "try to run the following command to see why it is not working:"
echo "az account get-access-token --resource-type ms-graph"
exit 1
else
echo "ok"
fi
open "http://$SERVER_PUBLIC_IP/?access_token=$ACCESS_TOKEN"
;;
*)
echo "WireGuard VPN Client"
echo ""
echo "Commands:"
echo " up start vpn connection"
echo " down stop vpn connection"
echo " status current status"
echo ""
echo "Config is saved here: ~/.config/vpn.conf"
echo ""
;;
esac