-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudflared_setup.sh
More file actions
executable file
·173 lines (136 loc) · 5.91 KB
/
Copy pathcloudflared_setup.sh
File metadata and controls
executable file
·173 lines (136 loc) · 5.91 KB
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
#!/bin/sh
# Script to setup cloudflare's amazing SSH tunnel on both the remote-
# as well as any local machines
# After installation, the 'local' machine can create a ssh-connection
# to the 'remote' machine by using the default ssh command.
#
# see: man ssh
# see: https://developers.cloudflare.com/argo-tunnel/
VERSION='0.1'
CLOUDFLARE_LIST_FILE='/etc/apt/sources.list.d/cloudflare-main.list'
# ############################################################################ #
# usage #
# ############################################################################ #
if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]
then
echo "Cloudflare Daemon (cloudflared) setup v$VERSION" >&2
echo "Usage:" >&2
echo "$0 [local|remote] [port=22] HOSTNAME" >&2
exit 1
fi
# ############################################################################ #
# sanitize & check arguments #
# ############################################################################ #
if [ "$#" -eq 2 ]
then
env=$1
port=22
host=$2
else
env=$1
port=$2
host=$3
fi
if ! [ "$env" = "remote" ] && ! [ "$env" = "local" ]
then
echo "Unknown environment <$env> - expected one of [local,remote]" >&2
exit 2
fi
# ############################################################################ #
# check if we've got all programs we need #
# ############################################################################ #
num_missing=0
chkprg() {
local prg=$1
local pad=$2
printf "Checking availability of <%s> ...%s " $prg $pad
if ! command -v "$prg" > /dev/null
then
printf "not found!\n"
num_missing=$(expr $num_missing + 1)
else
printf "ok.\n"
fi
}
chkprg apt ............
chkprg apt-key ........
chkprg lsb_release ....
chkprg systemctl ......
[ $num_missing -gt 0 ] \
&& echo "$num_missing required program(s) not found. Aborting." \
&& exit 2
unset -f chkprg
unset -f num_missing
# ############################################################################ #
# script proper #
# ############################################################################ #
echo ""
echo "Cloudflared configuration:"
echo "debian_release: $(lsb_release -sc)"
echo "hostname: $host"
echo "ssh-port: $port"
echo "environment: $env"
echo ""
printf "Are the above settings correct? [yes/no] "
read -r answer
# exit if answer is anything but literal "yes"
[ "$answer" = "yes" ] || exit 0
unset -f answer
echo "Adding cloudflare debPkg to APT sources"
printf "deb http://pkg.cloudflare.com/ %s main" $(lsb_release -sc) \
| sudo tee "$CLOUDFLARE_LIST_FILE" 1>/dev/null \
|| exit 2
echo "Adding cloudflare GPG to APT"
curl -C - https://pkg.cloudflare.com/pubkey.gpg | sudo apt-key add - || exit 2
echo "Updating APT and installing cloudflared"
sudo apt update && sudo apt install cloudflared || exit 2
# ############################################################################ #
# cloudflared configuration #
# ############################################################################ #
if [ "$env" = "remote" ]
then
# ######################################################################## #
# remote configuration #
# ######################################################################## #
root_home=$(sudo printenv HOME)
cert_loc="$root_home/.cloudflared/cert.pem"
if sudo test -f $cert_loc
then
echo "Found existing certificate in $cert_loc"
printf "Replace and continue? [yes/no] "
read -r answer
[ "$answer" = "yes" ] || exit 0
unset -f answer
sudo rm $cert_loc
fi
unset -f cert_loc
echo "Authenticating cloudflared"
sudo cloudflared tunnel login || exit 2
echo "Connecting remote machine to cloudflare"
sudo mkdir -p /etc/cloudflared || exit 2
printf "hostname: %s\nurl: %s\nlogfile: %s\n" \
"$host" \
"ssh://localhost:$port" \
"/var/log/cloudflared.log" \
| sudo tee /etc/cloudflared/config.yml > /dev/null || exit 2
echo "Copying certificate"
sudo mv "$cert_loc" /etc/cloudflared/ || exit 2
sudo rm -rf "$root_home/.cloudflared" || exit 2
echo "Installing cloudflared systemd-service"
sudo cloudflared service install || exit 2
echo "Restarting cloudflared"
echo " this might take up to a few minutes."
sudo systemctl restart cloudflared || exit 2
unset -f root_home
else
# ######################################################################## #
# local configuration #
# ######################################################################## #
echo "Adding ssh-proxy for <$host> to ~/.ssh/config"
mkdir -p "$HOME/.ssh" || exit 2
printf "Host %s\n ProxyCommand %s access ssh --hostname %%h\n" \
$host \
$(command -v cloudflared) \
| tee -a "$HOME/.ssh/config" > /dev/null || exit 2
fi
echo "Done."