-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnest.py
More file actions
executable file
·178 lines (160 loc) · 7.12 KB
/
nest.py
File metadata and controls
executable file
·178 lines (160 loc) · 7.12 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
#!/usr/bin/env python3
# Start an alternate VNC-based desktop session on your local machine
# to which you and others can connect; or, start a "nested" desktop
# which runs in a window on your primary desktop.
#
# Why is this useful?
# * If your team's chat software (Teams, Slack) only offers the option
# to share your entire desktop, but your desktop is huge (2x4K monitors
# in my case) - gives you a smaller desktop where sharing it to others
# isn't considered a DoS attack.
# * If you're on a smaller monitor, do desktop sharing frequently, but
# would prefer everything you do weren't in view of the remote user(s) -
# gives you a "sandbox" which can be shared while your main desktop
# remains out of view.
#
# Example invocation:
#
# nest.sh --type=vnc --gemoetry=1920x1080 &
#
# Run `nest.sh --help` for slightly more documentation.
#
# Some apps (web browsers in particular) won't play nicely with multiple
# desktop sessions running on your machine at once - for example, you'll
# open documents/links in one desktop session and they'll pop up in a
# different session. I work around this for web browsers by using separate
# ephemeral profiles for each alternate desktop (for which see freshfox.sh,
# in this repository).
#
# Uses JWM as a window manager by default - it's simple, behaves in a
# civilized manner, and you can run multiple instances at once without harm.
#
# Clipboard sync for XNest/Xephyr based desktops relies on xclipsync,
# which is a git submodule of this repository - make sure you have this
# checked out and have tk installed (apt-get install tk), which it needs.
#
# Has several more dependencies (search the source for "apt-get"
# to list them all) - you won't need to install all of them.
import argparse, atexit, os, re, shlex, socket, subprocess, sys, time
import psutil # apt-get install python3-psutil
VNC_PORTS_BASE=5900
DEFAULT_SIZE=(1680,1050)
DEFAULT_WM='jwm' # apt-get install jwm
DEFAULT_DPI=96
DEFAULT_CMDS=['x-terminal-emulator']
DEFAULT_CMD=', '.join(DEFAULT_CMDS)
DEFAULT_SERVER='xephyr'
XCLIPSYNC_PATH=os.path.join(os.path.dirname(__file__), 'xclipsync', 'xclipsync')
def server_xtightvnc(size, dpi, title, display, local, cmds):
# apt-get install tightvncserver
print('>>>> starting %dx%d TightVnc server on port %d' % (*size, VNC_PORTS_BASE+display))
return ('Xtightvnc',
'-s', '10000',
'-dpi', str(dpi),
'-geometry', '%dx%d' % size,
'-desktop', title) + \
(('-interface', 'localhost') if local else ()) + \
(':%d' % display,)
def server_xtigervnc(size, dpi, title, display, local, cmds):
# apt-get install tigervnc-standalone-server
print('>>>> starting %dx%d TigerVnc server on port %d' % (*size, VNC_PORTS_BASE+display))
return ('Xtigervnc',
'-s', '10000',
'-dpi', str(dpi),
'-geometry', '%dx%d' % size,
'-desktop', title,
'-ZlibLevel', '3',
'-PasswordFile', '%s/.vnc/passwd' % os.environ.get('HOME')) + \
(('-interface', 'localhost') if local else ()) + \
(':%d' % display,)
def server_xephyr(size, dpi, title, display, local, cmds):
# apt-get install xserver-xephyr
print('>>>> starting %dx%d Xephyr server on :%d' % (*size, display))
cmds += [XCLIPSYNC_PATH]
return ('Xephyr',
'-retro', '-no-host-grab',
'-s', '10000',
'-dpi', str(dpi),
'-screen', '%dx%d' % size,
'-title', title,
':%d' % display)
def server_xnest(size, dpi, title, display, local, cmds):
# apt-get install xnest
print('>>>> starting %dx%d Xnest server on :%d' % (*size, display))
cmds += [XCLIPSYNC_PATH]
return ('Xnest',
'-retro', '-sss',
'-s', '10000',
'-dpi', str(dpi),
'-geometry', '%dx%d' % size,
'-name', title,
':%d' % display)
SERVERS = {
'vnc': server_xtigervnc,
'tightvnc': server_xtightvnc,
'tigervnc': server_xtigervnc,
'xephyr': server_xephyr,
'xnest': server_xnest
}
def validate_xserver(s):
if s not in SERVERS:
raise argparse.ArgumentTypeError
return s
SIZE_RX=re.compile(r'''(\d+)x(\d+)''')
def parse_size(size_str):
m = SIZE_RX.match(size_str)
if not m:
raise argparse.ArgumentTypeError
return (int(m.group(1)),int(m.group(2)))
parser = argparse.ArgumentParser(description='Start a nested desktop')
parser.add_argument('-s', '--size', '--geometry', type=parse_size, dest='size', metavar='WIDTHxHEIGHT', default=DEFAULT_SIZE,
help=('Desktop size in pixels (default: %dx%d)' % DEFAULT_SIZE))
parser.add_argument('-c', '--command', '--cmd', type=str, nargs='*', dest='commands', metavar='CMDLINE',
help='Command(s) to run in new desktop (default: %s)' % DEFAULT_CMD)
parser.add_argument('-x', '--xserver', '--server', '--type', type=validate_xserver, dest='xserver', metavar='TYPE', default=DEFAULT_SERVER,
help='X11 server to use (default: %s)' % DEFAULT_SERVER)
parser.add_argument('-w', '--window-manager', '--wm', type=str, dest='wm', metavar='WMGR', default=DEFAULT_WM,
help='Window manager to use on new desktop (default: %s)' % DEFAULT_WM)
parser.add_argument('-d', '--dpi', type=int, dest='dpi', metavar='DPI', default=DEFAULT_DPI,
help='Resolution in dots per inch (default: %s)' % DEFAULT_DPI)
parser.add_argument('-t', '--title', type=str, dest='title', metavar='TITLE', default=None,
help='Title of nested desktop')
parser.add_argument('-l', '--local', dest='local', action='store_true',
help='VNC should listen on loopback interface, not all interfaces')
args = parser.parse_args()
commands = list(args.commands if (args.commands is not None and len(args.commands) > 0) else DEFAULT_CMDS)
XN_RX = re.compile(r'^X(\d+)$')
display = min(set(range(0,100)) - set([int(XN_RX.match(x).group(1)) for x in os.listdir('/tmp/.X11-unix/') if XN_RX.match(x)]))
title = args.title or ('nested desktop %d: %s@%s' % (display, os.environ.get('USER'), os.uname().nodename))
xserver_args = SERVERS[args.xserver](args.size, args.dpi, title, display, args.local, commands)
with subprocess.Popen(xserver_args,
pass_fds=(sys.stdout.fileno(),sys.stderr.fileno())) as xserver:
children = [xserver]
def killall():
for child in children:
try:
child.kill()
except:
pass
atexit.register(killall)
X11_SOCK_RX = re.compile(r'^@?/tmp/\.X11-unix/X(\d+)$')
displays = []
polls = 0
while polls < 20 and len(displays) == 0:
time.sleep(0.25)
displays = [int(X11_SOCK_RX.match(x.laddr).group(1)) for x in psutil.Process(xserver.pid).connections('unix') if X11_SOCK_RX.match(x.laddr)]
polls += 1
if len(displays) == 0:
sys.stderr.print('unable to determine display number')
killall()
sys.exit(1)
display = min(displays)
extra_procs = [args.wm] + commands
extra_env = dict(os.environ)
extra_env['DISPLAY'] = ':%d' % display
for extra in extra_procs:
extra_args_list = shlex.split(extra)
children.append(subprocess.Popen(extra_args_list,
env=extra_env,
pass_fds=(sys.stdout.fileno(),sys.stderr.fileno())))
xserver.wait()