-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·447 lines (367 loc) · 9.43 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·447 lines (367 loc) · 9.43 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/usr/bin/env bash
#
# install.sh - Install RequestBite Proxy from GitHub releases
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/requestbite/proxy-go/main/install.sh | bash
# curl -fsSL https://raw.githubusercontent.com/requestbite/proxy-go/main/install.sh | bash -s -- --prefix=$HOME/bin
# curl -fsSL https://raw.githubusercontent.com/requestbite/proxy-go/main/install.sh | bash -s -- --version 0.3.0
#
set -euo pipefail
# Configuration
BINARY_NAME="rbite-proxy"
GITHUB_REPO="requestbite/proxy"
DEFAULT_INSTALL_DIR="$HOME/.local/bin"
# Parse command line arguments
VERSION=""
PREFIX=""
while [[ $# -gt 0 ]]; do
case $1 in
--version)
VERSION="$2"
shift 2
;;
--prefix)
PREFIX="$2"
shift 2
;;
--help)
cat <<EOF
RequestBite Proxy - Installation Script
Usage:
install.sh [options]
Options:
--version VERSION Install specific version (e.g., 0.3.0)
--prefix PATH Install to PATH/bin (default: ~/.local/bin)
--help Show this help message
Examples:
# Install latest version to default location
./install.sh
# Install specific version
./install.sh --version 0.3.0
# Install to custom location
./install.sh --prefix \$HOME
# One-line install from GitHub
curl -fsSL https://raw.githubusercontent.com/requestbite/proxy-go/main/install.sh | bash
EOF
exit 0
;;
*)
echo "Unknown option: $1"
echo "Run with --help for usage information"
exit 1
;;
esac
done
# Colors (only if terminal supports it)
if [ -t 1 ]; then
COLOR_RESET='\033[0m'
COLOR_BOLD='\033[1m'
COLOR_GREEN='\033[32m'
COLOR_BLUE='\033[34m'
COLOR_RED='\033[31m'
COLOR_YELLOW='\033[33m'
else
COLOR_RESET=''
COLOR_BOLD=''
COLOR_GREEN=''
COLOR_BLUE=''
COLOR_RED=''
COLOR_YELLOW=''
fi
# Utility functions
info() {
echo -e "${COLOR_BOLD}${COLOR_BLUE}==>${COLOR_RESET} ${COLOR_BOLD}$*${COLOR_RESET}"
}
success() {
echo -e "${COLOR_GREEN}✓${COLOR_RESET} $*"
}
error() {
echo -e "${COLOR_RED}✗ Error:${COLOR_RESET} $*" >&2
}
warning() {
echo -e "${COLOR_YELLOW}⚠${COLOR_RESET} $*"
}
die() {
error "$*"
exit 1
}
# Check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Cleanup function
TEMP_DIR=""
cleanup() {
if [ -n "$TEMP_DIR" ] && [ -d "$TEMP_DIR" ]; then
rm -rf "$TEMP_DIR"
fi
}
trap cleanup EXIT INT TERM
# Check prerequisites
check_prerequisites() {
local missing=()
if ! command_exists curl; then
missing+=("curl")
fi
if ! command_exists tar; then
missing+=("tar")
fi
if ! command_exists shasum && ! command_exists sha256sum; then
missing+=("shasum or sha256sum")
fi
if [ ${#missing[@]} -gt 0 ]; then
error "Missing required tools:"
for tool in "${missing[@]}"; do
echo " - $tool"
done
exit 1
fi
}
# Detect operating system
detect_os() {
local os
os="$(uname -s)"
case "$os" in
Linux*)
echo "linux"
;;
Darwin*)
echo "darwin"
;;
*)
die "Unsupported operating system: $os (supported: Linux, macOS)"
;;
esac
}
# Detect architecture
detect_arch() {
local arch
arch="$(uname -m)"
case "$arch" in
x86_64)
echo "amd64"
;;
arm64 | aarch64)
echo "arm64"
;;
*)
die "Unsupported architecture: $arch (supported: x86_64, arm64)"
;;
esac
}
# Get latest version from GitHub
get_latest_version() {
info "Fetching latest version from GitHub..." >&2
local version
version=$(curl -fsSL "https://api.github.com/repos/$GITHUB_REPO/releases/latest" |
grep '"tag_name"' |
sed -E 's/.*"tag_name": *"v?([^"]+)".*/\1/' || echo "")
if [ -z "$version" ]; then
die "Failed to fetch latest version from GitHub API"
fi
echo "$version"
}
# Determine installation directory
determine_install_dir() {
local install_dir
if [ -n "$PREFIX" ]; then
install_dir="$PREFIX/bin"
elif [ -d "$DEFAULT_INSTALL_DIR" ] || mkdir -p "$DEFAULT_INSTALL_DIR" 2>/dev/null; then
install_dir="$DEFAULT_INSTALL_DIR"
else
install_dir="/usr/local/bin"
warning "Cannot create $DEFAULT_INSTALL_DIR, will try system-wide installation"
warning "This may require sudo privileges"
fi
echo "$install_dir"
}
# Check if directory is writable
is_writable() {
local dir="$1"
if [ -d "$dir" ]; then
[ -w "$dir" ]
else
# Check parent directory
local parent
parent="$(dirname "$dir")"
[ -w "$parent" ]
fi
}
# Download and verify archive
download_and_verify() {
local url="$1"
local archive_name="$2"
local checksum_url="$3"
info "Downloading from GitHub releases..."
if ! curl -fsSL --progress-bar "$url" -o "$archive_name"; then
die "Failed to download $url"
fi
success "Downloaded $archive_name"
# Download checksums
info "Verifying checksum..."
if ! curl -fsSL "$checksum_url" -o SHA256SUMS; then
warning "Could not download checksums file, skipping verification"
return 0
fi
# Verify checksum
local expected_checksum
expected_checksum=$(grep "$archive_name" SHA256SUMS | awk '{print $1}')
if [ -z "$expected_checksum" ]; then
warning "Checksum not found in SHA256SUMS, skipping verification"
return 0
fi
local actual_checksum
if command_exists shasum; then
actual_checksum=$(shasum -a 256 "$archive_name" | awk '{print $1}')
else
actual_checksum=$(sha256sum "$archive_name" | awk '{print $1}')
fi
if [ "$expected_checksum" != "$actual_checksum" ]; then
die "Checksum verification failed!
Expected: $expected_checksum
Actual: $actual_checksum"
fi
success "Checksum verified"
}
# Extract binary from archive
extract_binary() {
local archive_name="$1"
info "Extracting binary..."
if [[ "$archive_name" == *.tar.gz ]]; then
tar -xzf "$archive_name"
if [ ! -f "$BINARY_NAME/$BINARY_NAME" ]; then
die "Binary not found in archive"
fi
mv "$BINARY_NAME/$BINARY_NAME" "${BINARY_NAME}.tmp"
rm -rf "$BINARY_NAME"
mv "${BINARY_NAME}.tmp" "$BINARY_NAME"
else
die "Unsupported archive format: $archive_name"
fi
success "Extracted binary"
}
# Install binary
install_binary() {
local install_dir="$1"
local use_sudo=false
# Create install directory if needed
if [ ! -d "$install_dir" ]; then
if ! mkdir -p "$install_dir" 2>/dev/null; then
use_sudo=true
fi
fi
# Check if we need sudo
if ! is_writable "$install_dir"; then
use_sudo=true
fi
info "Installing to $install_dir..."
if [ "$use_sudo" = true ]; then
warning "Installation requires elevated privileges"
if ! command_exists sudo; then
die "sudo is required but not available"
fi
sudo mkdir -p "$install_dir"
sudo cp "$BINARY_NAME" "$install_dir/"
sudo chmod +x "$install_dir/$BINARY_NAME"
else
mkdir -p "$install_dir"
cp "$BINARY_NAME" "$install_dir/"
chmod +x "$install_dir/$BINARY_NAME"
fi
success "Installed $BINARY_NAME to $install_dir"
}
# Verify installation
verify_installation() {
local install_dir="$1"
local binary_path="$install_dir/$BINARY_NAME"
if [ ! -f "$binary_path" ]; then
die "Installation verification failed: $binary_path not found"
fi
if [ ! -x "$binary_path" ]; then
die "Installation verification failed: $binary_path is not executable"
fi
# Try to run --version
if "$binary_path" --version >/dev/null 2>&1; then
success "Installation verified"
else
warning "Binary exists but --version check failed"
fi
}
# Check if install directory is in PATH
check_path() {
local install_dir="$1"
if ! echo "$PATH" | grep -q "$install_dir"; then
warning "Installation directory is not in your PATH"
echo ""
echo "Add to PATH by adding this line to your shell configuration:"
echo " export PATH=\"$install_dir:\$PATH\""
echo ""
# Detect shell and config file
local shell_config=""
if [ -n "${BASH_VERSION:-}" ]; then
shell_config="~/.bashrc or ~/.bash_profile"
elif [ -n "${ZSH_VERSION:-}" ]; then
shell_config="~/.zshrc"
else
shell_config="your shell configuration file"
fi
echo "Example for $shell_config:"
echo " echo 'export PATH=\"$install_dir:\$PATH\"' >> $shell_config"
echo " source $shell_config"
echo ""
fi
}
# Main installation function
main() {
echo ""
info "RequestBite Proxy - Installation Script"
echo ""
# Check prerequisites
check_prerequisites
# Detect platform
local os
local arch
os=$(detect_os)
arch=$(detect_arch)
echo "Detected platform: $os/$arch"
# Get version
if [ -z "$VERSION" ]; then
VERSION=$(get_latest_version)
fi
echo "Version: $VERSION"
# Determine installation directory
local install_dir
install_dir=$(determine_install_dir)
echo "Install directory: $install_dir"
echo ""
# Construct download URL
local archive_name="${BINARY_NAME}-${VERSION}-${os}-${arch}.tar.gz"
local base_url="https://github.com/${GITHUB_REPO}/releases/download/${VERSION}"
local download_url="${base_url}/${archive_name}"
local checksum_url="${base_url}/SHA256SUMS"
# Create temporary directory
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR"
# Download and verify
download_and_verify "$download_url" "$archive_name" "$checksum_url"
# Extract
extract_binary "$archive_name"
# Install
install_binary "$install_dir"
# Verify
verify_installation "$install_dir"
echo ""
success "Installation complete!"
echo ""
# Check PATH
check_path "$install_dir"
# Show usage
echo "Usage:"
echo " $BINARY_NAME --help"
echo " $BINARY_NAME --version"
echo " $BINARY_NAME --port 8080"
echo ""
}
# Run main function
main