Skip to content

Commit 020b705

Browse files
init upload
packed with vnc managed - startvnc stable - stopvnc unstable
1 parent 58d8608 commit 020b705

File tree

7 files changed

+203
-0
lines changed

7 files changed

+203
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

install.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/bash
2+
3+
_c_magneta="\e[95m"
4+
_c_green="\e[32m"
5+
_c_red="\e[31m"
6+
_c_blue="\e[34m"
7+
RST="\e[0m"
8+
9+
die() { echo -e "${_c_red}[E] ${*}${RST}";exit 1;:;}
10+
warn() { echo -e "${_c_red}[W] ${*}${RST}";:;}
11+
shout() { echo -e "${_c_blue}[-] ${*}${RST}";:;}
12+
lshout() { echo -e "${_c_blue}-> ${*}${RST}";:;}
13+
imsg() { if [ -n "$UDROID_VERBOSE" ]; then echo -e ": ${*} \e[0m" >&2;fi;:;}
14+
msg() { echo -e "${*} \e[0m" >&2;:;}
15+
16+
if ! command -v python3 >/dev/null 2>&1; then
17+
die "Python3 is not installed"
18+
fi
19+
20+
if (( UID != 0 )); then
21+
die "You need to be root to run this script"
22+
fi
23+
24+
shout "Installing udroidmgr"
25+
mkdir -pv /usr/share/udroid || {
26+
die "Failed to create /usr/share/udroid"
27+
}
28+
29+
if [ -d utils ]; then
30+
cp -rv utils /usr/share/udroid/ || {
31+
die "Failed to copy utils"
32+
}
33+
fi
34+
35+
if [ -f main.py ]; then
36+
cp -v main.py /usr/share/udroid/ || {
37+
die "Failed to copy main.py"
38+
}
39+
fi
40+
41+
if [ -f main.sh ]; then
42+
cp -v main.sh /usr/share/udroid/ || {
43+
die "Failed to copy main.sh"
44+
}
45+
fi
46+
47+
48+
if [[ -f /usr/bin/startvnc ]]; then
49+
rm -rvf /usr/bin/startvnc
50+
fi
51+
52+
if [[ -f /usr/bin/stopvnc ]]; then
53+
rm -rvf /usr/bin/stopvnc
54+
fi
55+
56+
ln -sv /usr/share/udroid/main.sh /usr/bin/startvnc || {
57+
die "Failed to create symlink"
58+
}
59+
ln -sv /usr/share/udroid/main.sh /usr/bin/stopvnc || {
60+
die "Failed to create symlink"
61+
}

main.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
import sys
3+
import optparse
4+
from utils.info import *
5+
from utils.funs import *
6+
7+
def vnc_mode(port=1,mode="start",xstartup=os.getenv('HOME')+"/.vnc/xstartup"):
8+
import utils.vnc as vnc
9+
10+
if os.path.exists(xstartup):
11+
if mode == "start":
12+
if vnc.startvnc(port):
13+
print(vncokdialog(port))
14+
else:
15+
print(vncrunningdialog(port))
16+
elif mode == "stop":
17+
if vnc.stopvnc(port):
18+
print(vnckilldialog(port))
19+
else:
20+
print("No VNC server running at "+str(port))
21+
else:
22+
print("xstartup file not found")
23+
24+
if __name__ == '__main__':
25+
parser = optparse.OptionParser()
26+
parser.add_option("--startvnc",action='store_true', default=False, help="Start VNC on port")
27+
parser.add_option("--stopvnc" ,action='store_true', default=False, help="Stop VNC on port")
28+
parser.add_option("-p", "--port", help="VNC port", default=1)
29+
30+
(options, args) = parser.parse_args()
31+
32+
if options.startvnc:
33+
vnc_mode(port=options.port,mode="start")
34+
sys.exit(0)
35+
if options.stopvnc:
36+
vnc_mode(port=options.port,mode="stop")
37+
sys.exit(0)
38+

main.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
4+
case $(echo "$0" | cut -d "/" -f 4) in
5+
"startvnc")
6+
python3 /usr/share/udroid/main.py --startvnc $*
7+
;;
8+
"stopvnc")
9+
python3 /usr/share/udroid/main.py --stopvnc $*
10+
;;
11+
esac

utils/funs.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def lwarn(msg):
2+
print("[W] " + msg)
3+
4+
def nmsg(msg):
5+
print("* " + msg)
6+
7+
def die(msg):
8+
print("[E] " + msg)
9+
exit(1)
10+
11+
## COLORS
12+
def red(msg):
13+
return "\033[1;31m" + msg + "\033[0m"
14+
15+
def blue(msg):
16+
return "\033[1;34m" + msg + "\033[0m"
17+
18+
def green(msg):
19+
return "\033[1;32m" + msg + "\033[0m"
20+
21+
def magneta(msg):
22+
return "\033[1;35m" + msg + "\033[0m"

utils/info.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
from utils.funs import *
3+
4+
######## VNC
5+
HOSTPUBIP=os.system("hostname -I | cut -d ' ' -f 2")
6+
LOCALIP="127.0.0.1"
7+
8+
def vncokdialog(port):
9+
return "vncserver started on "\
10+
+"\n"+magneta(str(LOCALIP)+":"+str(port))\
11+
+"\n"+magneta(str(LOCALIP)+":"+str(port))\
12+
+"\n"+"To stop it, run:\n"\
13+
+green("vncserver -kill :"+str(port))\
14+
+" or "+blue("stopvnc")
15+
16+
def vncrunningdialog(port):
17+
return "vncserver already started on "\
18+
+"\n"+magneta(str(LOCALIP)+":"+str(port))\
19+
+"\n"+magneta(str(LOCALIP)+":"+str(port))\
20+
+"\n"+"To stop it, run:\n"\
21+
+green("vncserver -kill :"+str(port))\
22+
+" or "+blue("stopvnc")
23+
24+
def vnckilldialog(port):
25+
return "vncserver stopped on "+str(LOCALIP)+" port "+str(port)
26+

utils/vnc.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
3+
DISPLAY=os.getenv('DISPLAY')
4+
VNC_LOCK_TYPE="/tmp/X11-unix/X"
5+
6+
def isvncstarted(port:int) -> bool:
7+
"""
8+
Check if VNC is started
9+
"""
10+
if os.path.exists(VNC_LOCK_TYPE+str(port)) \
11+
or os.path.exists("/tmp/X"+str(port)+"-lock"):
12+
return True
13+
else:
14+
return False
15+
16+
def startvnc(port:int) -> bool:
17+
"""
18+
Start VNC
19+
"""
20+
if os.getenv('DEFAULT_VNC_PORT') is not None and os.getenv('DEFAULT_VNC_PORT').isnumeric:
21+
port=int(os.getenv('DEFAULT_VNC_PORT'))
22+
23+
if isvncstarted(port):
24+
return False
25+
else:
26+
if os.WEXITSTATUS(os.system("vncserver :"+str(port))) == 0:
27+
return True
28+
else:
29+
return False
30+
31+
def stopvnc(port:int) -> bool:
32+
"""
33+
Stop VNC
34+
"""
35+
if os.getenv('DEFAULT_VNC_PORT') is not None and os.getenv('DEFAULT_VNC_PORT').isnumeric:
36+
port = int(os.getenv('DEFAULT_VNC_PORT'))
37+
38+
if isvncstarted(port):
39+
if os.WEXITSTATUS(os.system("vncserver -kill :"+str(port))) == 0:
40+
return True
41+
else:
42+
return False
43+
else:
44+
return False

0 commit comments

Comments
 (0)