Revision b129b0c0 vncauthproxy/proxy.py
b/vncauthproxy/proxy.py | ||
---|---|---|
20 | 20 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
21 | 21 |
# 02110-1301, USA. |
22 | 22 |
|
23 |
DEFAULT_CTRL_SOCKET = "/var/run/vncauthproxy/ctrl.sock" |
|
23 |
DEFAULT_BIND_ADDRESS = None |
|
24 |
DEFAULT_LPORT = 24999 |
|
24 | 25 |
DEFAULT_LOG_FILE = "/var/log/vncauthproxy/vncauthproxy.log" |
25 | 26 |
DEFAULT_PID_FILE = "/var/run/vncauthproxy/vncauthproxy.pid" |
26 | 27 |
DEFAULT_CONNECT_TIMEOUT = 30 |
... | ... | |
453 | 454 |
from optparse import OptionParser |
454 | 455 |
|
455 | 456 |
parser = OptionParser() |
456 |
parser.add_option("-s", "--socket", dest="ctrl_socket", |
|
457 |
default=DEFAULT_CTRL_SOCKET, |
|
458 |
metavar="PATH", |
|
459 |
help=("UNIX socket for control connections (default: " |
|
460 |
"%s" % DEFAULT_CTRL_SOCKET)) |
|
457 |
parser.add_option("--bind", dest="bind_address", |
|
458 |
default=DEFAULT_BIND_ADDRESS, |
|
459 |
metavar="ADDRESS", |
|
460 |
help=("Address to listen for control connections")) |
|
461 |
parser.add_option( "--lport", dest="lport", |
|
462 |
default=DEFAULT_LPORT, |
|
463 |
metavar="LPORT", |
|
464 |
help=("Port to listen for control connections")) |
|
461 | 465 |
parser.add_option("-d", "--debug", action="store_true", dest="debug", |
462 | 466 |
help="Enable debugging information") |
463 | 467 |
parser.add_option("-l", "--log", dest="log_file", |
... | ... | |
648 | 652 |
# we *must* reinit gevent |
649 | 653 |
gevent.reinit() |
650 | 654 |
|
651 |
if os.path.exists(opts.ctrl_socket): |
|
652 |
logger.critical("Socket '%s' already exists", opts.ctrl_socket) |
|
653 |
sys.exit(1) |
|
654 |
|
|
655 |
# TODO: make this tunable? chgrp as well? |
|
656 |
old_umask = os.umask(0007) |
|
657 |
|
|
658 |
ctrl = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
|
659 |
ctrl.bind(opts.ctrl_socket) |
|
660 |
|
|
661 |
os.umask(old_umask) |
|
655 |
sockets = [] |
|
656 |
for res in socket.getaddrinfo(opts.bind_address, opts.lport, |
|
657 |
socket.AF_UNSPEC, socket.SOCK_STREAM, 0, |
|
658 |
socket.AI_PASSIVE): |
|
659 |
af, socktype, proto, canonname, sa = res |
|
660 |
try: |
|
661 |
s = None |
|
662 |
s = socket.socket(af, socktype, proto) |
|
663 |
if af == socket.AF_INET6: |
|
664 |
# Bind v6 only when AF_INET6, otherwise either v4 or v6 bind |
|
665 |
# will fail. |
|
666 |
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1) |
|
667 |
s.bind(sa) |
|
668 |
s.listen(opts.backlog) |
|
669 |
sockets.append(s) |
|
670 |
logger.info("Control socket listening on %s:%d", *sa[:2]) |
|
671 |
except socket.error, msg: |
|
672 |
logger.critical("Error binding control socket to %s:%d: %s", |
|
673 |
sa[0], sa[1], msg[1]) |
|
674 |
if s: |
|
675 |
s.close() |
|
676 |
while sockets: |
|
677 |
sockets.pop.close() |
|
662 | 678 |
|
663 |
ctrl.listen(opts.backlog) |
|
664 |
logger.info("Initialized, waiting for control connections at %s", |
|
665 |
opts.ctrl_socket) |
|
679 |
sys.exit(1) |
|
666 | 680 |
|
667 | 681 |
# Catch signals to ensure graceful shutdown, |
668 | 682 |
# e.g., to make sure the control socket gets unlink()ed. |
... | ... | |
677 | 691 |
|
678 | 692 |
while True: |
679 | 693 |
try: |
680 |
client, addr = ctrl.accept() |
|
681 |
logger.info("New control connection") |
|
694 |
rlist, _, _ = select(sockets, [], []) |
|
695 |
for ctrl in rlist: |
|
696 |
client, addr = ctrl.accept() |
|
697 |
logger.info("New control connection") |
|
682 | 698 |
|
683 |
gevent.Greenlet.spawn(establish_connection, client, addr, |
|
684 |
ports, opts) |
|
699 |
gevent.Greenlet.spawn(establish_connection, client, addr,
|
|
700 |
ports, opts)
|
|
685 | 701 |
except Exception, e: |
686 | 702 |
logger.exception(e) |
687 | 703 |
continue |
688 | 704 |
except SystemExit: |
689 | 705 |
break |
690 | 706 |
|
691 |
logger.info("Unlinking control socket at %s", opts.ctrl_socket) |
|
692 |
os.unlink(opts.ctrl_socket) |
|
707 |
logger.info("Closing control sockets") |
|
708 |
while sockets: |
|
709 |
sockets.pop.close() |
|
693 | 710 |
daemon_context.close() |
694 | 711 |
sys.exit(0) |
Also available in: Unified diff