-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
244 lines (208 loc) · 5.24 KB
/
install.sh
File metadata and controls
244 lines (208 loc) · 5.24 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
#!/usr/bin/env bash
set -euo pipefail
REPO="imrui/xray-pilot"
INSTALL_BIN="/usr/local/bin/xray-pilot"
CONFIG_DIR="/etc/xray-pilot"
CONFIG_FILE="${CONFIG_DIR}/config.yaml"
SSH_DIR="${CONFIG_DIR}/ssh"
STATE_DIR="/var/lib/xray-pilot"
KNOWN_HOSTS_FILE="${STATE_DIR}/known_hosts"
SERVICE_FILE="/etc/systemd/system/xray-pilot.service"
CREDS_FILE="${CONFIG_DIR}/install.env"
SERVICE_USER="xray-pilot"
SERVICE_GROUP="xray-pilot"
require_root() {
if [[ "${EUID}" -ne 0 ]]; then
echo "Please run this installer as root, for example: curl ... | sudo bash" >&2
exit 1
fi
}
require_cmd() {
local cmd="$1"
if ! command -v "${cmd}" >/dev/null 2>&1; then
echo "Missing required command: ${cmd}" >&2
exit 1
fi
}
detect_arch() {
local machine
machine="$(uname -m)"
case "${machine}" in
x86_64|amd64)
ARCH="amd64"
;;
aarch64|arm64)
ARCH="arm64"
;;
*)
echo "Unsupported architecture: ${machine}. Only amd64 and arm64 are supported." >&2
exit 1
;;
esac
}
detect_os() {
local system
system="$(uname -s)"
case "${system}" in
Linux)
OS="linux"
;;
*)
echo "Unsupported operating system: ${system}. This installer only supports Linux." >&2
exit 1
;;
esac
}
get_latest_tag() {
local api_url="https://api.github.com/repos/${REPO}/releases/latest"
TAG="$(curl -fsSL "${api_url}" | sed -n 's/.*"tag_name":[[:space:]]*"\([^"]*\)".*/\1/p' | head -n1)"
if [[ -z "${TAG}" ]]; then
echo "Unable to determine the latest release tag from ${api_url}" >&2
exit 1
fi
}
random_hex() {
local bytes="$1"
openssl rand -hex "${bytes}"
}
random_password() {
openssl rand -base64 24 | tr -d '=+/' | cut -c1-24
}
download_release() {
TMP_DIR="$(mktemp -d)"
ARCHIVE="xray-pilot_${TAG}_${OS}_${ARCH}.tar.gz"
ARCHIVE_URL="https://github.com/${REPO}/releases/download/${TAG}/${ARCHIVE}"
CHECKSUM_URL="https://github.com/${REPO}/releases/download/${TAG}/checksums.txt"
echo "Downloading ${ARCHIVE}..."
curl -fsSL -o "${TMP_DIR}/${ARCHIVE}" "${ARCHIVE_URL}"
curl -fsSL -o "${TMP_DIR}/checksums.txt" "${CHECKSUM_URL}"
}
verify_release() {
(
cd "${TMP_DIR}"
sha256sum -c checksums.txt --ignore-missing
)
}
install_binary() {
tar -xzf "${TMP_DIR}/${ARCHIVE}" -C "${TMP_DIR}"
install -m 0755 "${TMP_DIR}/xray-pilot" "${INSTALL_BIN}"
}
ensure_service_user() {
if ! getent group "${SERVICE_GROUP}" >/dev/null 2>&1; then
groupadd --system "${SERVICE_GROUP}"
fi
if ! id -u "${SERVICE_USER}" >/dev/null 2>&1; then
useradd \
--system \
--gid "${SERVICE_GROUP}" \
--home-dir "${STATE_DIR}" \
--shell /usr/sbin/nologin \
--comment "xray-pilot service user" \
"${SERVICE_USER}"
fi
}
write_config() {
mkdir -p "${CONFIG_DIR}" "${SSH_DIR}" "${STATE_DIR}"
chown "${SERVICE_USER}:${SERVICE_GROUP}" "${STATE_DIR}"
chmod 0750 "${STATE_DIR}"
chown root:"${SERVICE_GROUP}" "${CONFIG_DIR}"
chmod 0750 "${CONFIG_DIR}"
chown root:"${SERVICE_GROUP}" "${SSH_DIR}"
chmod 0750 "${SSH_DIR}"
touch "${KNOWN_HOSTS_FILE}"
chown "${SERVICE_USER}:${SERVICE_GROUP}" "${KNOWN_HOSTS_FILE}"
chmod 0600 "${KNOWN_HOSTS_FILE}"
if [[ ! -f "${CONFIG_FILE}" ]]; then
JWT_SECRET="$(random_hex 32)"
MASTER_KEY="$(random_hex 32)"
ADMIN_PASSWORD="$(random_password)"
cat >"${CONFIG_FILE}" <<EOF
server:
port: 2026
mode: release
database:
driver: sqlite
dsn: ${STATE_DIR}/xray-pilot.db
jwt:
secret: "${JWT_SECRET}"
expire: 24
crypto:
master_key: "${MASTER_KEY}"
ssh:
default_port: 22
default_user: "root"
default_key_path: "${SSH_DIR}/id_ed25519"
known_hosts_path: "${KNOWN_HOSTS_FILE}"
admins:
- username: admin
password: "${ADMIN_PASSWORD}"
EOF
cat >"${CREDS_FILE}" <<EOF
XRAY_PILOT_ADMIN_USERNAME=admin
XRAY_PILOT_ADMIN_PASSWORD=${ADMIN_PASSWORD}
XRAY_PILOT_VERSION=${TAG}
EOF
fi
chown root:"${SERVICE_GROUP}" "${CONFIG_FILE}"
chmod 0640 "${CONFIG_FILE}"
if [[ -f "${CREDS_FILE}" ]]; then
chown root:root "${CREDS_FILE}"
chmod 0600 "${CREDS_FILE}"
fi
}
write_service() {
cat >"${SERVICE_FILE}" <<EOF
[Unit]
Description=xray-pilot service
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=${SERVICE_USER}
Group=${SERVICE_GROUP}
WorkingDirectory=${CONFIG_DIR}
ExecStart=${INSTALL_BIN}
Restart=on-failure
RestartSec=5
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
EOF
}
enable_service() {
systemctl daemon-reload
systemctl enable --now xray-pilot.service
}
cleanup() {
if [[ -n "${TMP_DIR:-}" && -d "${TMP_DIR}" ]]; then
rm -rf "${TMP_DIR}"
fi
}
main() {
trap cleanup EXIT
require_root
require_cmd curl
require_cmd tar
require_cmd sha256sum
require_cmd openssl
require_cmd systemctl
detect_os
detect_arch
get_latest_tag
download_release
verify_release
install_binary
ensure_service_user
write_config
write_service
enable_service
echo
echo "xray-pilot ${TAG} installed successfully."
echo "Binary: ${INSTALL_BIN}"
echo "Config: ${CONFIG_FILE}"
echo "SSH directory: ${SSH_DIR}"
echo "Credentials: ${CREDS_FILE}"
echo "Service: systemctl status xray-pilot.service"
echo "If you use SSH key auth, place the private key at ${SSH_DIR}/id_ed25519 and set ownership so ${SERVICE_USER} can read it."
}
main "$@"