Repository navigation
Expand file tree
/
Copy pathlinux_install.sh
More file actions
executable file
·258 lines (224 loc) · 6.95 KB
/
Copy pathlinux_install.sh
File metadata and controls
executable file
·258 lines (224 loc) · 6.95 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
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOUSAGE'
Usage:
./linux_install.sh [--install]
./linux_install.sh --uninstall
Options:
--install Install oppo-multiplexer (default action)
--uninstall Uninstall oppo-multiplexer
-h, --help Show this help
Notes:
- sudo/root is required to install and uninstall.
- Binary is installed to /opt/oppo-multiplexer/.
- Config is installed to /etc/oppo-multiplexer/.
- For privileged listen ports (<1024) the service runs as root.
- For non-privileged listen ports the service runs as the invoking user.
EOUSAGE
}
ACTION="install"
for arg in "$@"; do
case "$arg" in
--install)
ACTION="install"
;;
--uninstall)
ACTION="uninstall"
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $arg"
usage
exit 1
;;
esac
done
if ! command -v systemctl >/dev/null 2>&1; then
echo "systemctl is required but not installed."
exit 1
fi
if [[ "${EUID}" -ne 0 ]]; then
echo "This script must run with sudo/root."
exec sudo "$0" "$@"
fi
BIN_DIR="/opt/oppo-multiplexer"
BIN_PATH="${BIN_DIR}/oppo-multiplexer"
ENV_DIR="/etc/oppo-multiplexer"
ENV_FILE="${ENV_DIR}/env"
UNIT_FILE="/etc/systemd/system/oppo-multiplexer.service"
if [[ "${ACTION}" == "uninstall" ]]; then
systemctl disable --now oppo-multiplexer >/dev/null 2>&1 || true
rm -f "${UNIT_FILE}" "${ENV_FILE}" "${BIN_PATH}"
rmdir "${ENV_DIR}" 2>/dev/null || true
rmdir "${BIN_DIR}" 2>/dev/null || true
systemctl daemon-reload
systemctl reset-failed >/dev/null 2>&1 || true
echo "Uninstall complete."
exit 0
fi
if ! command -v curl >/dev/null 2>&1; then
echo "curl is required but not installed."
exit 1
fi
if ! command -v tar >/dev/null 2>&1; then
echo "tar is required but not installed."
exit 1
fi
# Matches .github/workflows/release.yml naming:
# oppo-multiplexer-linux-amd64.tar.gz
# oppo-multiplexer-linux-arm64.tar.gz
case "$(uname -m)" in
x86_64|amd64) ARCH="amd64" ;;
aarch64|arm64) ARCH="arm64" ;;
*)
echo "Unsupported architecture: $(uname -m)"
exit 1
;;
esac
read -r -p "Release tag to install (default latest): " RELEASE_TAG
RELEASE_TAG="${RELEASE_TAG:-latest}"
read -r -p "Player IP/host (example 192.168.1.50): " OPPO_HOST
if [[ -z "${OPPO_HOST}" ]]; then
echo "Player IP/host cannot be empty."
exit 1
fi
read -r -p "Protocol (1=udp20x, 2=magnetar, default 1): " PROTOCOL_CHOICE
PROTOCOL_CHOICE="${PROTOCOL_CHOICE:-1}"
case "${PROTOCOL_CHOICE}" in
1) PROTOCOL="udp20x" ;;
2) PROTOCOL="magnetar" ;;
*)
echo "Invalid protocol choice: ${PROTOCOL_CHOICE} (expected 1 or 2)"
exit 1
;;
esac
# UDP-20X players listen on port 23; Magnetar players on 8102.
if [[ "${PROTOCOL}" == "magnetar" ]]; then
DEFAULT_OPPO_PORT=8102
else
DEFAULT_OPPO_PORT=23
fi
read -r -p "Player port (default ${DEFAULT_OPPO_PORT}): " OPPO_PORT
OPPO_PORT="${OPPO_PORT:-${DEFAULT_OPPO_PORT}}"
read -r -p "Listen port for oppo-multiplexer: " LISTEN_PORT
read -r -p "Timeout seconds (default 10): " TIMEOUT_SECONDS
TIMEOUT_SECONDS="${TIMEOUT_SECONDS:-10}"
read -r -p "Max consecutive timed-out requests before reconnect (default 3): " MAX_CONSECUTIVE_TIMEOUTS
MAX_CONSECUTIVE_TIMEOUTS="${MAX_CONSECUTIVE_TIMEOUTS:-3}"
read -r -p "Log level (error/warn/info/debug/trace, default info): " LOG_LEVEL
LOG_LEVEL="${LOG_LEVEL:-info}"
if ! [[ "${OPPO_PORT}" =~ ^[0-9]+$ ]] || (( OPPO_PORT < 1 || OPPO_PORT > 65535 )); then
echo "Invalid Player port: ${OPPO_PORT}"
exit 1
fi
if ! [[ "${LISTEN_PORT}" =~ ^[0-9]+$ ]] || (( LISTEN_PORT < 1 || LISTEN_PORT > 65535 )); then
echo "Invalid listen port: ${LISTEN_PORT}"
exit 1
fi
if ! [[ "${TIMEOUT_SECONDS}" =~ ^[0-9]+$ ]]; then
echo "Invalid timeout seconds: ${TIMEOUT_SECONDS}, defaulting to 10."
TIMEOUT_SECONDS="10"
fi
if ! [[ "${MAX_CONSECUTIVE_TIMEOUTS}" =~ ^[0-9]+$ ]] || (( MAX_CONSECUTIVE_TIMEOUTS < 1 || MAX_CONSECUTIVE_TIMEOUTS > 100 )); then
echo "Invalid max consecutive timeouts: ${MAX_CONSECUTIVE_TIMEOUTS}, defaulting to 3."
MAX_CONSECUTIVE_TIMEOUTS="3"
fi
case "${LOG_LEVEL}" in
error|warn|info|debug|trace) ;;
*)
echo "Invalid log level: ${LOG_LEVEL}, defaulting to info."
LOG_LEVEL="info"
;;
esac
# Privileged ports (<1024): service runs as root.
# Non-privileged ports: service runs as the invoking user.
SERVICE_USER=""
SERVICE_GROUP=""
if (( LISTEN_PORT < 1024 )); then
echo "Privileged listen port ${LISTEN_PORT}: service will run as root."
else
if [[ -z "${SUDO_USER:-}" || "${SUDO_USER}" == "root" ]]; then
echo "For non-privileged listen ports, re-run as: sudo ./linux_install.sh (from your normal user account)."
exit 1
fi
SERVICE_USER="${SUDO_USER}"
SERVICE_GROUP="$(id -gn "${SUDO_USER}")"
echo "Non-privileged listen port ${LISTEN_PORT}: service will run as ${SERVICE_USER}:${SERVICE_GROUP}."
fi
ASSET="oppo-multiplexer-linux-${ARCH}.tar.gz"
if [[ "${RELEASE_TAG}" == "latest" ]]; then
URL="https://github.com/henrikwidlund/oppo-multiplexer/releases/latest/download/${ASSET}"
else
URL="https://github.com/henrikwidlund/oppo-multiplexer/releases/download/${RELEASE_TAG}/${ASSET}"
fi
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "${TMP_DIR}"' EXIT
echo "Downloading ${URL}"
curl --proto "=https" --tlsv1.2 -sSfL "${URL}" -o "${TMP_DIR}/${ASSET}"
tar -xzf "${TMP_DIR}/${ASSET}" -C "${TMP_DIR}"
if [[ ! -f "${TMP_DIR}/oppo-multiplexer" ]]; then
echo "Archive did not contain expected binary: oppo-multiplexer"
exit 1
fi
mkdir -p "${BIN_DIR}" "${ENV_DIR}"
install -m 755 "${TMP_DIR}/oppo-multiplexer" "${BIN_PATH}"
# Config readable by root and the service user only.
if [[ -n "${SERVICE_USER}" ]]; then
ENV_GROUP="${SERVICE_GROUP}"
else
ENV_GROUP="root"
fi
cat > "${ENV_FILE}" <<EOCFG
OPPO_HOST=${OPPO_HOST}
OPPO_PORT=${OPPO_PORT}
LISTEN_PORT=${LISTEN_PORT}
TIMEOUT_SECONDS=${TIMEOUT_SECONDS}
MAX_CONSECUTIVE_TIMEOUTS=${MAX_CONSECUTIVE_TIMEOUTS}
PROTOCOL=${PROTOCOL}
RUST_LOG=${LOG_LEVEL}
EOCFG
chown "root:${ENV_GROUP}" "${ENV_FILE}"
chmod 640 "${ENV_FILE}"
# Build the unit, inserting User/Group only for non-privileged installs.
cat > "${UNIT_FILE}" <<EOUNIT
[Unit]
Description=OPPO Multiplexer
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
EnvironmentFile=${ENV_FILE}
ExecStart=${BIN_PATH} \${LISTEN_PORT} \${OPPO_HOST}:\${OPPO_PORT} \${TIMEOUT_SECONDS} \${MAX_CONSECUTIVE_TIMEOUTS} --protocol \${PROTOCOL}
Restart=always
RestartSec=2
TimeoutStopSec=5
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=full
ProtectHome=true
EOUNIT
if [[ -n "${SERVICE_USER}" ]]; then
{
echo "User=${SERVICE_USER}"
echo "Group=${SERVICE_GROUP}"
} >> "${UNIT_FILE}"
fi
cat >> "${UNIT_FILE}" <<'EOUNIT'
[Install]
WantedBy=multi-user.target
EOUNIT
systemctl daemon-reload
systemctl enable oppo-multiplexer
systemctl restart oppo-multiplexer
echo
echo "Install complete."
systemctl status oppo-multiplexer --no-pager || true
echo
echo "Binary: ${BIN_PATH}"
echo "Config: ${ENV_FILE}"
echo "Logs: journalctl -u oppo-multiplexer -f"
echo " journalctl -b -t oppo-multiplexer"