-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.sh
executable file
·67 lines (62 loc) · 2.44 KB
/
index.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
#!/usr/bin/env bash
USERNAME=$(curl -s -H "Authorization: Bearer $ACCESS_TOKEN" "https://graph.microsoft.com/v1.0/me" | jq -r ".displayName")
if [ "$USERNAME" == "null" ]
then
echo 'status: 401'
echo 'content-type: text/html'
echo 'cache-control: no-store'
echo ''
echo '<h3>WireGuard</h3>'
echo '<p>Not authorized</p>'
else
echo 'content-type: text/html'
echo 'cache-control: no-store'
echo ''
echo '<h3>WireGuard</h3>'
echo "<p>Hello $USERNAME</p>"
if [[ $(wg show wg0 peers | wc -l) -ne 0 ]]
then
echo '<table border="1" cellspacing="0" cellpadding="5">'
echo '<thead>'
echo '<tr>'
echo '<th>mail</th>'
echo '<th>public key</th>'
echo '<th>preshared key</th>'
echo '<th>endpoint</th>'
echo '<th>allowed ip</th>'
echo '<th>latest handshake</th>'
echo '<th>transfer rx</th>'
echo '<th>transfer tx</th>'
echo '<th>persistent keepalive</th>'
echo '<th></th>'
echo '</tr>'
echo '</thead>'
echo '<tbody>'
# wg show wg0 dump | sed 1d | awk '{ print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td>"$5"</td><td>"$6"</td><td>"$7"</td><td>"$8"</td><td><form style=\"margin:0\" action=/remove/"$1"><input type=submit value=× /></form></td></tr>" }'
while IFS= read -r line; do
peer=$(echo $line | awk '{ print $1 }')
mail=$(cat "/tmp/wg/$peer/mail")
echo '<tr>'
echo "<td>$mail</td>"
echo $line | awk -v access_token=$ACCESS_TOKEN '{ print "<td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td>"$5"</td><td>"$6"</td><td>"$7"</td><td>"$8"</td>" }'
echo "<td><button onclick=\"remove('$peer')\">×</button></td></td>"
echo '</tr>'
done <<< $(wg show wg0 dump | sed 1d)
echo '</tbody>'
echo '</table>'
echo '<script>
async function remove(public_key) {
const access_token = new URL(window.location).searchParams.get("access_token")
const response = await fetch("/remove", {
method: "POST",
body: JSON.stringify({ access_token, public_key })
})
const {message} = await response.json()
alert(message)
location.reload()
}
</script>'
else
echo '<p>At moment no peers configured</p>'
fi
fi