28
30
sys.path.insert(1, '@pythondir@')
44
def __init__(self, opts):
49
# select port, we also use this as the X display number
50
self.port = self._get_open_port()
51
# generate a random password to secure the Xpra session
52
self._generate_xpra_password()
55
# start watchdog if applicable
56
if self.OPTS.WATCHDOG:
57
self._loop_cleanup_watchdog()
59
def _get_open_port(self):
61
self.bindaddr = "[::]"
62
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
64
self.bindaddr = "0.0.0.0"
65
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
68
port = s.getsockname()[1]
72
def _generate_xpra_password(self):
73
word = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(64))
74
f = open(os.path.expanduser("~/.xpra/xts-pass"), "w")
78
def _start_xpra(self):
79
xpra_cmd = 'xpra start :' \
81
+ ' --password-file="' + os.path.expanduser("~/.xpra/xts-pass") + '"'
83
xpra_cmd = xpra_cmd + ' --bind-tcp="' + self.bindaddr + ':' + str(self.port) + '"'
87
def _loop_cleanup_watchdog(self):
88
print "starting watchdog, xpra-wrapper will remain in foreground"
90
stop_cmd = 'xpra stop :' \
92
+ ' --password-file="' + os.path.expanduser("~/.xpra/xts-pass") + '"'
93
info_cmd = "xpra info :" + str(self.port) \
94
+ ' --password-file="' + os.path.expanduser("~/.xpra/xts-pass") + '"' \
95
+ " | grep clients | head -n 1 | awk -F'=' '{print $2}'"
97
p = subprocess.Popen(info_cmd, shell=True, stdout=subprocess.PIPE)
98
out = int(p.communicate()[0])
101
print "client is connected"
104
print "client idle for", idle
105
if idle >= self.OPTS.TIMEOUT:
106
subprocess.Popen(stop_cmd, shell=True).wait()
107
print "reached timeout, Xpra stopped"
112
if __name__ == "__main__":
113
parser = optparse.OptionParser()
114
parser.add_option('-b', '--bind', help="Bind Xpra to a TCP port", default=False, action='store_true', dest='BIND')
115
parser.add_option('-6', '--ipv6', help="Enable IPv6", default=False, action='store_true', dest='IPV6')
116
parser.add_option('-w', '--watchdog', help="Enable cleanup watchdog", default=False, action='store_true', dest='WATCHDOG')
117
parser.add_option("-t", "--timeout", help="Number of seconds after which the watchdog will stop Xpra if no client is connected, default is 30", metavar="SECONDS", type="int", default=30, dest='TIMEOUT')
118
(opts, args) = parser.parse_args()
119
w = XpraWrapper(opts)
b'\\ No newline at end of file'