-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.sh
188 lines (162 loc) · 5.47 KB
/
script.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
#!/bin/ash
# Function to run a command, suppressing stdout but displaying stderr
run_command() {
"$@" > /dev/null 2>&1
}
# Function to create a tar archive as a string
create_tar_string() {
if [ -f "$1" ] || [ -d "$1" ]; then
tar -czf - -C "$(dirname "$1")" "$(basename "$1")" | base64 | tr -d '\n'
fi
}
# Function to decode and extract a tar-encoded string to a directory
decode_tar_string() {
if [ -n "$1" ] && [ -n "$2" ]; then
echo "$1" | base64 -d | tar -xz -C "$2"
fi
}
# Function to display script usage
display_usage() {
echo "Usage: $0 [--import=<base64_string>] [-l] [--help]"
echo "Options:"
echo " -l Start with Cloudflare login (Domain + Account required)"
echo " --import=<base64_string> Import a Cloudflared config from a base64 string"
echo " --help Display this help message"
}
# Initialize variables
login=false
import_string=""
tunnel_url="http://localhost:8080" # Default tunnel URL
# Parse command-line arguments
for arg in "$@"; do
case "$arg" in
--import=*)
import_string="${arg#*=}"
;;
-l)
login=true
;;
--url=*)
tunnel_url="${arg#*=}"
;;
--help)
display_usage
exit 0
;;
*)
echo "Unknown option: $arg"
display_usage
exit 1
;;
esac
done
# Create necessary directories
echo "Creating directories..."
mkdir -p /tmp/cloudflared/root
mkdir -p /tmp/cloudflared/proc
mkdir -p /tmp/cloudflared/sys
mkdir -p /tmp/cloudflared/dev
mkdir -p /tmp/cloudflared/etc/ssl
cp -r /etc/ssl /tmp/cloudflared/etc/
cp /etc/resolv.conf /tmp/cloudflared/etc/resolv.conf
echo "Directories created."
echo ""
# Update package list
echo "Updating package list..."
run_command opkg update
echo "Package list updated."
echo ""
# Check if coreutils-base64 is installed
if ! opkg list-installed coreutils-base64 > /dev/null 2>&1; then
echo "Installing coreutils-base64..."
run_command opkg install coreutils-base64
echo "coreutils-base64 installed."
echo ""
fi
# Get the filename of the downloaded package
filename=$(opkg info cloudflared | awk -F ": " '/Filename:/ {print $2}')
# Navigate to the cloudflared directory
cd /tmp/cloudflared
# Download and extract the cloudflared package
echo "Downloading and extracting cloudflared package..."
run_command opkg download cloudflared
run_command tar zxpvf "$filename"
rm "$filename"
rm debian-binary control.tar.gz
echo "Cloudflared package downloaded and extracted."
echo ""
# Extract the package's data
echo "Extracting package data..."
run_command tar zxpvf data.tar.gz
rm data.tar.gz
echo "Package data extracted."
echo ""
# Remove the default config file
echo "Removing default config file..."
run_command rm /tmp/cloudflared/etc/cloudflared/config.yml
echo "Default config file removed."
echo ""
# Mount the proc filesystem
echo "Mounting proc, dev and sys filesystem..."
run_command mount -t proc proc /tmp/cloudflared/proc
run_command mount -t sysfs none /tmp/cloudflared/sys
run_command mount --bind /dev /tmp/cloudflared/dev
echo "Proc, dev and sys filesystems mounted."
echo ""
# Copy dependencies to their respective folders
echo "Copying dependencies of cloudflared..."
deps=$(ldd /tmp/cloudflared/usr/bin/cloudflared | awk '/=>/ {print $3}')
for dep in $deps; do
dep_filename=$(basename "$dep")
dep_dirname=$(dirname "$dep")
mkdir -p "/tmp/cloudflared$dep_dirname"
run_command cp "$dep" "/tmp/cloudflared$dep_dirname/$dep_filename"
done
echo "All dependencies copied to their respective folders in /tmp/cloudflared."
echo ""
# Check if login is set to false and tunnel_url is not empty
if [ "$login" = false ] && [ -z "$import_string" ]; then
echo "Starting Quick Tunnel for URL: $tunnel_url"
echo ""
# Start cloudflared and redirect its output to the log file
chroot /tmp/cloudflared/ /usr/bin/cloudflared tunnel --url "$tunnel_url" > /tmp/cloudflared/cloudflared.log 2>&1 &
# Give the process a moment to start and populate the logfile
sleep 2
# Monitor the log file and search for the URL
tail -f /tmp/cloudflared/cloudflared.log | while read -r LINE; do
url=$(echo "$LINE" | grep -oE 'https://[a-zA-Z0-9\-]+\.trycloudflare\.com' | head -n 1)
if [ ! -z "$url" ]; then
echo "Your tunnel is up and running! URL: $url"
break
fi
done
# Detach and let processes run
exit 0
fi
# Check if --import parameter is provided for decryption
if [ -n "$import_string" ]; then
echo "Restoring /root/.cloudflared..."
decode_tar_string "$import_string" "/tmp/cloudflared/root/"
echo "/root/.cloudflared restored."
echo ""
else
# Execute chroot
echo "Starting cloudflared tunnel login..."
chroot /tmp/cloudflared/ /usr/bin/cloudflared tunnel login
echo "Login finished."
echo ""
# Create tunnel
echo "Creating cloudflared tunnel..."
run_command chroot /tmp/cloudflared/ /usr/bin/cloudflared tunnel create openwrt
echo "Tunnel created."
echo ""
# Create a tar archive as a string for later use
tar_string=$(create_tar_string /tmp/cloudflared/root/.cloudflared/)
# Print the command for future execution
echo "To start this Tunnel next time (or to execute after bootup), run:"
echo "wget -qO- https://raw.githubusercontent.com/adshrc/openwrt-cloudflared/main/script.sh | ash -s -- --import=\"$tar_string\""
echo ""
fi
echo "Starting cloudflared tunnel \"openwrt\"..."
chroot /tmp/cloudflared/ /usr/bin/cloudflared tunnel run openwrt > /dev/null 2>&1 &
echo "Your tunnel is up and running. Configure your tunnel in dashboard to add public hostnames."