Add OpenWrt support to install-release.sh

- Implemented detection for OpenWrt systems, enhancing compatibility.
- Updated package management commands for OpenWrt using 'opkg'.
- Added installation and service management for Xray on OpenWrt, including init script creation.
- Improved error handling for unsupported operating systems and architectures.
- Enhanced logging setup for Xray service on OpenWrt.
pull/83/head
xiagw 2024-12-07 21:57:30 +08:00
parent 534ff8bd0a
commit 75c9580145
No known key found for this signature in database
GPG Key ID: 71003D6667FFFB53
1 changed files with 259 additions and 136 deletions

View File

@ -130,6 +130,15 @@ identify_the_operating_system_and_architecture() {
echo "error: This operating system is not supported." echo "error: This operating system is not supported."
return 1 return 1
fi fi
# Add OpenWrt detection
if grep -q -i 'openwrt' /etc/openwrt_release 2>/dev/null; then
echo "info: OpenWrt system detected."
OPENWRT=1
else
OPENWRT=0
fi
case "$(uname -m)" in case "$(uname -m)" in
'i386' | 'i686') 'i386' | 'i686')
MACHINE='32' MACHINE='32'
@ -181,6 +190,17 @@ identify_the_operating_system_and_architecture() {
return 1 return 1
;; ;;
esac esac
if [[ "$OPENWRT" -eq '1' ]]; then
if [[ ! -f '/etc/openwrt_release' ]]; then
echo "error: Not a supported OpenWrt system."
return 1
fi
PACKAGE_MANAGEMENT_INSTALL='opkg update; opkg install'
PACKAGE_MANAGEMENT_REMOVE='opkg remove'
package_provide_tput='ncurses-utils'
return 0
fi
if [[ ! -f '/etc/os-release' ]]; then if [[ ! -f '/etc/os-release' ]]; then
echo "error: Don't use outdated Linux distributions." echo "error: Don't use outdated Linux distributions."
return 1 return 1
@ -480,6 +500,27 @@ install_file() {
} }
install_xray() { install_xray() {
# Install Xray binary to /usr/bin/ and /etc/xray (OpenWrt)
if [[ "$OPENWRT" -eq '1' ]]; then
cp "${TMP_DIRECTORY}/xray" "/usr/bin/xray"
chmod 755 "/usr/bin/xray"
mkdir -p /etc/xray
if [[ ! -f /etc/xray/config.json ]]; then
echo "{}" >"/etc/xray/config.json"
CONFIG_NEW='1'
fi
mkdir -p "/usr/share/xray"
cp "${TMP_DIRECTORY}/geoip.dat" "/usr/share/xray/geoip.dat"
cp "${TMP_DIRECTORY}/geosite.dat" "/usr/share/xray/geosite.dat"
mkdir -p /var/log/xray
chown -R "$INSTALL_USER_UID:$INSTALL_USER_GID" /var/log/xray
if [[ ! -f /var/log/xray/access.log ]]; then
touch /var/log/xray/access.log
touch /var/log/xray/error.log
LOG=1
fi
return 0
fi
# Install Xray binary to /usr/local/bin/ and $DAT_PATH # Install Xray binary to /usr/local/bin/ and $DAT_PATH
install_file xray install_file xray
# If the file exists, geoip.dat and geosite.dat will not be installed or updated # If the file exists, geoip.dat and geosite.dat will not be installed or updated
@ -521,6 +562,74 @@ install_xray() {
} }
install_startup_service_file() { install_startup_service_file() {
if [[ "$OPENWRT" -eq '1' ]]; then
[[ -f /etc/init.d/xray ]] && return 0
# Create OpenWrt init script
cat >/etc/init.d/xray <<'EOF'
#!/bin/sh /etc/rc.common
USE_PROCD=1
START=99
CONF="xray"
PROG="/usr/bin/xray"
start_service() {
config_load "$CONF"
local enabled
config_get_bool enabled "enabled" "enabled" "0"
[ "$enabled" -eq "1" ] || return 1
local confdir
local conffiles
local datadir
local dialer
local format
config_get confdir "config" "confdir"
config_get conffiles "config" "conffiles"
config_get datadir "config" "datadir" "/usr/share/xray"
config_get dialer "config" "dialer"
config_get format "config" "format" "json"
procd_open_instance "$CONF"
procd_set_param command "$PROG" run
[ -n "$confdir" ] && procd_append_param command -confdir "$confdir"
[ -n "$conffiles" ] && {
for i in $conffiles
do
procd_append_param command -config "$i"
done
}
[ -n "$format" ] && procd_append_param command -format "$format"
[ -n "$dialer" ] && procd_set_param env XRAY_BROWSER_DIALER="$dialer"
procd_set_param env XRAY_LOCATION_ASSET="$datadir"
procd_set_param file $conffiles
procd_set_param limits core="unlimited"
procd_set_param limits nofile="1000000 1000000"
procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param respawn
procd_close_instance
}
reload_service() {
stop
start
}
service_triggers() {
procd_add_reload_trigger "$CONF"
}
EOF
chmod +x /etc/init.d/xray
/etc/init.d/xray enable
return 0
fi
mkdir -p '/etc/systemd/system/xray.service.d' mkdir -p '/etc/systemd/system/xray.service.d'
mkdir -p '/etc/systemd/system/xray@.service.d/' mkdir -p '/etc/systemd/system/xray@.service.d/'
local temp_CapabilityBoundingSet="CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE" local temp_CapabilityBoundingSet="CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE"
@ -613,6 +722,15 @@ ExecStart=/usr/local/bin/xray run -config ${JSON_PATH}/%i.json" > \
} }
start_xray() { start_xray() {
if [[ "$OPENWRT" -eq 1 ]]; then
if /etc/init.d/xray start; then
echo 'info: Start the Xray service.'
else
echo 'error: Failed to start Xray service.'
exit 1
fi
return 0
fi
if [[ -f '/etc/systemd/system/xray.service' ]]; then if [[ -f '/etc/systemd/system/xray.service' ]]; then
systemctl start "${XRAY_CUSTOMIZE:-xray}" systemctl start "${XRAY_CUSTOMIZE:-xray}"
sleep 1s sleep 1s
@ -626,6 +744,11 @@ start_xray() {
} }
stop_xray() { stop_xray() {
if [[ "$OPENWRT" -eq '1' ]]; then
/etc/init.d/xray stop
echo 'info: Stop the Xray service.'
return 0
fi
XRAY_CUSTOMIZE="$(systemctl list-units | grep 'xray@' | awk -F ' ' '{print $1}')" XRAY_CUSTOMIZE="$(systemctl list-units | grep 'xray@' | awk -F ' ' '{print $1}')"
if [[ -z "$XRAY_CUSTOMIZE" ]]; then if [[ -z "$XRAY_CUSTOMIZE" ]]; then
local xray_daemon_to_stop='xray.service' local xray_daemon_to_stop='xray.service'