Skip to content

Commit

Permalink
vicharak-config: Refactor support for vncserver
Browse files Browse the repository at this point in the history
Now it will propely install, configure and start vncserver

Change-Id: Ib223e170a43dd9619996ccaa42e1e8b3716b2295
Signed-off-by: UtsavBalar1231 <utsavbalar1231@gmail.com>
  • Loading branch information
UtsavBalar1231 committed Nov 28, 2023
1 parent cdc739c commit 48872db
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 74 deletions.
62 changes: 54 additions & 8 deletions src/usr/lib/vicharak-config/cli/vncserver.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,77 @@
# shellcheck shell=bash

shopt -s nullglob

install_vncserver() {
__parameter_count_check 0 "$@"
apt-get install x11vnc
apt-get update -y
apt-get install xfonts-base tightvncserver autocutsel -y
}

uninstall_vncserver() {
__parameter_count_check 0 "$@"
apt-get remove x11vnc

disable_vncserver "$@"
apt-get purge tightvncserver -y
}

enable_vncserver() {
__parameter_count_check 0 "$@"

# check if systemd service is enabled
if systemctl is-enabled vncserver.service; then
systemctl start vncserver.service >/dev/null 2>&1
else
systemctl enable --now vncserver.service >/dev/null 2>&1
if systemctl is-enabled vncserver.service >/dev/null 2>&1; then
# check if vncserver is running
if systemctl is-active vncserver.service >/dev/null 2>&1; then
systemctl stop vncserver.service >/dev/null 2>&1
else
systemctl disable --now vncserver.service >/dev/null 2>&1
fi
fi

if [ -e "/tmp/.X[0-9]*-lock" ]; then
rm -f "/tmp/.X[0-9]*-lock"
fi

if [ -d "/tmp/.X11-unix" ]; then
rm -rf "/tmp/.X11-unix"
fi

local user
user=$(logname)

local files
files=("/home/${user}/.vnc/${user}:"[0-9]*)

if [ ${#files[@]} -gt 0 ]; then
rm -rf "${files[@]}"
fi

systemctl enable --now vncserver.service >/dev/null 2>&1
systemctl start vncserver.service >/dev/null 2>&1
}

disable_vncserver() {
__parameter_count_check 0 "$@"

if [ -e "/tmp/.X[0-9]*-lock" ]; then
rm -f "/tmp/.X[0-9]*-lock"
fi

if [ -d "/tmp/.X11-unix" ]; then
rm -rf "/tmp/.X11-unix"
fi

local user
user=$(logname)

local files
files=("/home/${user}/.vnc/${user}:"[0-9]*)

if [ ${#files[@]} -gt 0 ]; then
rm -rf "${files[@]}"
fi

# check if systemd service is enabled
if systemctl is-enabled vncserver.service; then
if systemctl is-enabled vncserver.service >/dev/null 2>&1; then
systemctl stop vncserver.service >/dev/null 2>&1
systemctl disable --now vncserver.service >/dev/null 2>&1
fi
Expand Down
3 changes: 3 additions & 0 deletions src/usr/lib/vicharak-config/tui/advanced/advanced.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ source "/usr/lib/vicharak-config/tui/advanced/gpu/gpu.sh"
source "/usr/lib/vicharak-config/tui/advanced/ssh/ssh.sh"
# shellcheck source=src/usr/lib/vicharak-config/tui/advanced/display/display.sh
source "/usr/lib/vicharak-config/tui/advanced/display/display.sh"
# shellcheck source=src/usr/lib/vicharak-config/tui/advanced/vnc/vncserver.sh
source "/usr/lib/vicharak-config/tui/advanced/vnc/vncserver.sh"

__advanced() {
menu_init
#menu_add __advanced_docker "Docker"
menu_add __advanced_gpu "Mali GPU"
menu_add __advanced_display "Display Options"
menu_add __advanced_vncserver "VNC Server"
#menu_add __advanced_ssh "SSH"
menu_show "Please select an option below:"
}
68 changes: 2 additions & 66 deletions src/usr/lib/vicharak-config/tui/advanced/display/display.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# shellcheck shell=bash
# shellcheck disable=SC2086,SC2119

# shellcheck source=src/usr/lib/vicharak-config/cli/vncserver.sh
source "/usr/lib/vicharak-config/cli/vncserver.sh"
# shellcheck disable=SC2086

__select_resolution_options() {
local selected_resolution_option=""
Expand Down Expand Up @@ -176,71 +173,10 @@ Please check if the screen is connected and powered on."
fi
}

__advanced_enable_vncserver() {
if yesno "Are you sure to enable X11VNC server?"; then
if enable_vncserver; then
msgbox "Successfully enabled X11 VNC server.\nStart the server with 'x11vnc' command."
else
msgbox "Failed to enable X11VNC server."
return
fi
fi
}

__advanced_install_vncserver() {
if yesno "Are you sure to install server?"; then
if ! install_vncserver; then
msgbox "Unable to install X11VNC server."
return
else
msgbox "Successfully installed X11VNC server."
fi
fi
}

__advanced_disable_vncserver() {
if yesno "Are you sure to disable X11VNC server?"; then
if disable_vncserver; then
msgbox "Successfully disabled X11 VNC server."
else
msgbox "Failed to disable X11VNC server."
return
fi
fi
}

__advanced_remove_vncserver() {
if yesno "Are you sure to uninstall X11VNC server?"; then
if ! uninstall_vncserver; then
msgbox "Unable to uninstall X11VNC server."
return
else
msgbox "Successfully uninstalled X11VNC server."
fi
fi
}

__advanced_vncserver() {
menu_init
if __is_installed x11vnc; then
menu_add __advanced_remove_vncserver "Uninstall X11 VNC server"
else
menu_add __advanced_install_vncserver "Install X11 VNC server"
fi

if [[ "$(systemctl is-enabled vncserver.service)" == "enabled" ]]; then
menu_add __advanced_disable_vncserver "Disable X11 VNC server"
else
menu_add __advanced_enable_vncserver "Enable X11 VNC server"
fi

menu_show "Please select an option below:"
}

__advanced_display() {
menu_init
menu_add __advanced_set_display_resolution "Set display resolution"
menu_add __advanced_vncserver "X11VNC Server"
menu_add __advanced_vncserver "VNC Server"

menu_show "Advanced Display"
}
140 changes: 140 additions & 0 deletions src/usr/lib/vicharak-config/tui/advanced/vnc/vncserver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# shellcheck shell=bash
# shellcheck disable=SC2119

# shellcheck source=src/usr/lib/vicharak-config/cli/vncserver.sh
source "/usr/lib/vicharak-config/cli/vncserver.sh"

__advanced_enable_vncserver() {
if yesno "Are you sure to enable VNC server?"; then
if enable_vncserver; then
local user
local available_ports
user=$(logname)
available_ports=$(ls "/home/${user}/.vnc/${user}:"[0-9]*.log)
for port in ${available_ports}; do
msgbox "Successfully enabled VNC server at localhost:$(grep -oP "TCP port \K\d+" "${port}")."
done
else
msgbox "Failed to enable VNC server."
fi
fi
}

__advanced_install_vncserver() {
if yesno "Are you sure to install server?"; then
if ! install_vncserver; then
msgbox "Unable to install VNC server."
return
else
local user
user=$(logname)
# Check for old password
if [[ -f "/home/${user}/.vnc/passwd" ]]; then
msgbox "Found old password. Remove it before configuring VNC server."
rm -f "/home/${user}/.vnc/passwd"
fi
if [[ -f "/root/.vnc/passwd" ]]; then
msgbox "Found old password in root. Remove it before configuring VNC server."
rm -f "/root/.vnc/passwd"
fi

vncpasswd

if [ ! -d "/home/${user}/.vnc" ]; then
mkdir -p "/home/${user}/.vnc"
fi
chown "${user}:${user}" "/home/${user}/.vnc"

sudo cp -pdR "/root/.vnc/passwd" "/home/${user}/.vnc/passwd"
chown "${user}:${user}" "/home/${user}/.vnc/passwd"

if [[ -f "/home/${user}/.vnc/xstartup" ]]; then
rm -f "/home/${user}/.vnc/xstartup"
fi
cat <<EOF >"/home/${user}/.vnc/xstartup"
#!/bin/bash
# Change "gnome" to "xfce" for a xfce desktop, or "lxde" for lxde desktop
# or "" for a generic desktop
MODE="lxde"
export XKL_XMODMAP_DISABLE=1
export LD_LIBRARY_PATH="/usr/lib/aarch64-linux-gnu:\${LD_LIBRARY_PATH}"
if [ -e "\$HOME/.Xresources" ]; then
xrdb "\$HOME/.Xresources"
fi
autocutsel -fork
if [ "\$MODE" = "lxde" ]; then
if command -v startlxde >/dev/null 2>&1; then
startlxde &
else
MODE=""
fi
elif [ "\$MODE" = "xfce" ]; then
if command -v startxfce4 >/dev/null 2>&1; then
startxfce4 &
else
MODE=""
fi
elif [ "\$MODE" = "gnome" ]; then
if command -v gnome-session >/dev/null 2>&1; then
gnome-session &
else
MODE="xfce"
fi
else
MODE=""
fi
if [ -z "\$MODE" ]; then
xsetroot -solid grey
x-terminal-emulator -geometry 80x24+10+10 -ls -title "\$VNCDESKTOP Desktop" &
x-window-manager &
fi
EOF

chown "${user}:${user}" "/home/${user}/.vnc/xstartup"
chmod +x "/home/${user}/.vnc/xstartup"

msgbox "Successfully installed VNC server.\nYou can enable it from menu now."
fi
fi
}

__advanced_disable_vncserver() {
if yesno "Are you sure to disable VNC server?"; then
if disable_vncserver; then
msgbox "Successfully disabled VNC server."
else
msgbox "Failed to disable VNC server."
fi
fi
}

__advanced_remove_vncserver() {
if yesno "Are you sure to uninstall VNC server?"; then
if ! uninstall_vncserver; then
msgbox "Unable to uninstall VNC server."
else
msgbox "Successfully uninstalled VNC server."
fi
fi
}

__advanced_vncserver() {
menu_init
if __is_installed tightvncserver; then
menu_add __advanced_remove_vncserver "Uninstall VNC server"

if [[ "$(systemctl is-enabled vncserver.service)" == "enabled" ]]; then
menu_add __advanced_disable_vncserver "Disable VNC server"
else
menu_add __advanced_enable_vncserver "Enable VNC server"
fi
else
menu_add __advanced_install_vncserver "Install VNC server"
fi

menu_show "Please select an option below:"
}

0 comments on commit 48872db

Please sign in to comment.