File x11vnc of Package tigervnc.8914

#!/usr/bin/env python

# This is wrapper for x0vncserver that translate most common x11vnc arguments
# to x0vncserver's arguments

import argparse
import socket
import os
import sys

def is_port_free(port):
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind(('', port))
        sock.close()
        return True
    except socket.error as e:
        return False

def find_free_port(starting_port):
    for port in range(starting_port, 6000):
        if is_port_free(port):
            return port
    return None

parser = argparse.ArgumentParser(add_help=False)

parser.add_argument('-help', '-h', action='help')
parser.add_argument('--version', '-V', action='store_true')

parser.add_argument('-storepasswd', nargs=2)

parser.add_argument('-display')
parser.add_argument('-auth')
parser.add_argument('-N', action='store_true')
parser.add_argument('-rfbport', type=int)
parser.add_argument('-autoport', type=int, default=5900)
parser.add_argument('-6', dest='yes6', action='store_true')
parser.add_argument('-no6', action='store_true')

#parser.add_argument('-once', action='store_true') # TODO: Add support to x0vncserver
parser.add_argument('-forever', '-many', action='store_true')
parser.add_argument('-viewonly', action='store_true')

#parser.add_argument('-shared', action='store_true') # TODO?
parser.add_argument('-alwaysshared', action='store_true')
parser.add_argument('-nevershared', action='store_true')
parser.add_argument('-dontdisconnect', action='store_true')

#parser.add_argument('-timeout', nargs=1, type=int) # TODO?

parser.add_argument('-clip')
parser.add_argument('-deferupdate', type=int)
parser.add_argument('-noshm', action='store_true')

#parser.add_argument('-allow', nargs=1) # TODO?
#parser.add_argument('-localhost') # TODO?

parser.add_argument('-rfbauth')
parser.add_argument('-nopw', action='store_true')
parser.add_argument('-unixpw')

# Accepted, but ignored arguments
ignored_arguments = ['-v', '-verbose', '-q', '-quiet']
parser.add_argument(*ignored_arguments, dest='ignored_argument', action='store_true')


print('Warning: x11vnc is deprecated in favor of x0vncserver.')
print('         This is a wrapper that maps the most common set of x11vnc')
print('         arguments to x0vncserver arguments.')
print()
print('         Use x0vncserver directly if you want encrypted connection.')
print()

args = parser.parse_args()


# Warnings
if args.ignored_argument:
    print('Warning: x11vnc wrapper accepts but ignores following arguments:')
    print(', '.join(ignored_arguments))


# vncpasswd
if args.storepasswd:
    (password, passwdfile) = args.storepasswd

    os.execlp('vncpasswd.arg', 'vncpasswd.arg', passwdfile, password)


# x0vncserver
new_args = ['x0vncserver']

default_security_type = 'None'
security_type = default_security_type

if args.version:
    new_args.append('-version')

if args.display:
    new_args.append('-display')
    new_args.append(args.display)

if args.auth:
    os.environ['XAUTHORITY'] = args.auth

if args.N:
    display = args.display or os.environ['DISPLAY']
    if not display:
        print('No display set')
        sys.exit(1)
    port = int(display.split(':')[-1]) + 5900
    if is_port_free(port):
        new_args.append('-rfbport')
        new_args.append(str(port))
    else:
        print('Port %d is already used'%port)
        sys.exit(1)

if args.rfbport:
    new_args.append('-rfbport')
    new_args.append(str(args.rfbport))
else:
    port = find_free_port(args.autoport)
    new_args.append('-rfbport')
    new_args.append(str(port))

if args.yes6:
    new_args.append('-UseIPv6')
if args.no6:
    new_args.append('-UseIPv6=0')

if args.forever:
    # This is default in x0vncserver
    pass

if args.viewonly:
    new_args.append('-AcceptKeyEvents=0')
    new_args.append('-AcceptPointerEvents=0')
    new_args.append('-AcceptCutText=0')
    new_args.append('-AcceptSetDesktopSize=0')

if args.alwaysshared:
    new_args.append('-AlwaysShared')
if args.nevershared:
    new_args.append('-NeverShared')
if args.dontdisconnect:
    new_args.append('-DisconnectClients=0')
else:
    new_args.append('-DisconnectClients')

if args.clip:
    new_args.append('-Geometry')
    new_args.append(args.clip)

if args.deferupdate:
    new_args.append('-DeferUpdate')
    new_args.append(str(args.deferupdate))

if args.noshm:
    new_args.append('-UseSHM=0')

if args.rfbauth:
    security_type = 'VncAuth'
    new_args.append('-PasswordFile')
    new_args.append(args.rfbauth)

if args.unixpw:
    security_type = 'Plain'
    new_args.append('-PlainUsers')
    new_args.append(args.unixpw)

new_args.append('-SecurityTypes')
new_args.append(security_type)

if not args.nopw:
    # Note: This is the same warning as the original x11vnc gives
    if security_type == default_security_type:
        print("""
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
#@                                                           @#
#@  **  WARNING  **  WARNING  **  WARNING  **  WARNING  **   @#
#@                                                           @#
#@        YOU ARE RUNNING X11VNC WITHOUT A PASSWORD!!        @#
#@                                                           @#
#@  This means anyone with network access to this computer   @#
#@  may be able to view and control your desktop.            @#
#@                                                           @#
#@ >>> If you did not mean to do this Press CTRL-C now!! <<< @#
#@                                                           @#
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
""")

os.execvp('x0vncserver', new_args)