-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·142 lines (117 loc) · 3.47 KB
/
install.sh
File metadata and controls
executable file
·142 lines (117 loc) · 3.47 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
#!/bin/sh
# Clash installation script
# Usage: curl -fsSL https://clash.sh/install.sh | sh
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Configuration
REPO="clash-sh/clash"
BINARY_NAME="clash"
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
# Detect OS and architecture
detect_platform() {
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
case "$OS" in
darwin)
PLATFORM="apple-darwin"
;;
linux)
PLATFORM="unknown-linux-gnu"
;;
*)
echo "${RED}Unsupported operating system: $OS${NC}"
exit 1
;;
esac
case "$ARCH" in
x86_64)
ARCH="x86_64"
;;
arm64|aarch64)
ARCH="aarch64"
;;
*)
echo "${RED}Unsupported architecture: $ARCH${NC}"
exit 1
;;
esac
TARGET="${ARCH}-${PLATFORM}"
}
# Get the latest release version from GitHub
get_latest_version() {
VERSION="${VERSION:-latest}"
if [ "$VERSION" = "latest" ]; then
echo "Fetching latest version..."
VERSION=$(curl -sL "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ -z "$VERSION" ]; then
echo "${YELLOW}No releases found. Installing from main branch...${NC}"
install_from_source
exit 0
fi
fi
echo "Installing Clash ${VERSION}..."
}
# Download and install binary
install_binary() {
DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${VERSION}/clash-${TARGET}.tar.gz"
TEMP_DIR=$(mktemp -d)
echo "Downloading from: $DOWNLOAD_URL"
if ! curl -sL --fail "$DOWNLOAD_URL" -o "$TEMP_DIR/clash.tar.gz"; then
echo "${YELLOW}Binary not available for $TARGET. Falling back to source installation...${NC}"
rm -rf "$TEMP_DIR"
install_from_source
return
fi
echo "Extracting binary..."
tar -xzf "$TEMP_DIR/clash.tar.gz" -C "$TEMP_DIR"
echo "Installing to $INSTALL_DIR..."
if [ -w "$INSTALL_DIR" ]; then
mv "$TEMP_DIR/$BINARY_NAME" "$INSTALL_DIR/"
else
echo "${YELLOW}Need sudo access to install to $INSTALL_DIR${NC}"
sudo mv "$TEMP_DIR/$BINARY_NAME" "$INSTALL_DIR/"
fi
chmod +x "$INSTALL_DIR/$BINARY_NAME"
rm -rf "$TEMP_DIR"
}
# Install from source using cargo
install_from_source() {
if ! command -v cargo >/dev/null 2>&1; then
echo "${RED}Cargo not found. Please install Rust first:${NC}"
echo " curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
exit 1
fi
echo "Installing from source using cargo..."
cargo install --git "https://github.com/${REPO}.git" --branch main
}
# Verify installation
verify_installation() {
if command -v clash >/dev/null 2>&1; then
echo "${GREEN}✅ Clash installed successfully!${NC}"
echo ""
clash --version
echo ""
echo "Run 'clash --help' to get started"
echo "Visit https://github.com/clash-sh/clash for documentation"
else
echo "${RED}Installation failed. Please check the error messages above.${NC}"
exit 1
fi
}
# Main installation flow
main() {
echo "⚔️ Installing Clash..."
echo ""
detect_platform
echo "Platform: $OS ($ARCH)"
echo "Target: $TARGET"
echo ""
get_latest_version
install_binary
verify_installation
}
main "$@"