Revision dd62f34b vncauthproxy/proxy.py
b/vncauthproxy/proxy.py | ||
---|---|---|
208 | 208 |
""" |
209 | 209 |
server = None |
210 | 210 |
|
211 |
tries = self.retries
|
|
211 |
tries = VncAuthProxy.connect_retries
|
|
212 | 212 |
while tries: |
213 | 213 |
tries -= 1 |
214 | 214 |
|
215 | 215 |
# Initiate server connection |
216 |
for res in socket.getaddrinfo(self.addr, self.dport, |
|
216 |
for res in socket.getaddrinfo(self.daddr, self.dport,
|
|
217 | 217 |
socket.AF_UNSPEC, |
218 | 218 |
socket.SOCK_STREAM, 0, |
219 | 219 |
socket.AI_PASSIVE): |
... | ... | |
225 | 225 |
continue |
226 | 226 |
|
227 | 227 |
# Set socket timeout for the initial handshake |
228 |
server.settimeout(self.server_timeout)
|
|
228 |
server.settimeout(VncAuthProxy.server_timeout)
|
|
229 | 229 |
|
230 | 230 |
try: |
231 |
logger.debug("Connecting to %s:%s", *sa[:2])
|
|
231 |
self.debug("Connecting to %s:%s", *sa[:2])
|
|
232 | 232 |
server.connect(sa) |
233 |
logger.debug("Connection to %s:%s successful", *sa[:2])
|
|
233 |
self.debug("Connection to %s:%s successful", *sa[:2])
|
|
234 | 234 |
except socket.error: |
235 | 235 |
server.close() |
236 | 236 |
server = None |
... | ... | |
241 | 241 |
break |
242 | 242 |
|
243 | 243 |
# Wait and retry |
244 |
gevent.sleep(self.retry_wait)
|
|
244 |
gevent.sleep(VncAuthProxy.retry_wait)
|
|
245 | 245 |
|
246 | 246 |
if server is None: |
247 | 247 |
raise Exception("Failed to connect to server") |
... | ... | |
258 | 258 |
raise Exception("Error handshaking with the server") |
259 | 259 |
|
260 | 260 |
else: |
261 |
logger.debug("Supported authentication types: %s",
|
|
261 |
self.debug("Supported authentication types: %s",
|
|
262 | 262 |
" ".join([str(x) for x in types])) |
263 | 263 |
|
264 | 264 |
if rfb.RFB_AUTHTYPE_NONE not in types: |
... | ... | |
318 | 318 |
req = json.loads(buf) |
319 | 319 |
|
320 | 320 |
sport_orig = int(req['source_port']) |
321 |
daddr = req['destination_address'] |
|
322 |
dport = int(req['destination_port']) |
|
323 |
password = req['password'] |
|
321 |
self.daddr = req['destination_address']
|
|
322 |
self.dport = int(req['destination_port'])
|
|
323 |
self.password = req['password']
|
|
324 | 324 |
except Exception, e: |
325 |
logger.warn("Malformed request: %s", buf)
|
|
325 |
self.warn("Malformed request: %s", buf)
|
|
326 | 326 |
client.send(json.dumps(response)) |
327 | 327 |
client.close() |
328 | 328 |
raise gevent.GreenletExit |
... | ... | |
338 | 338 |
ports.remove(sport) |
339 | 339 |
break |
340 | 340 |
except ValueError: |
341 |
logger.debug("Port %d already taken", sport)
|
|
341 |
self.debug("Port %d already taken", sport)
|
|
342 | 342 |
|
343 |
logger.debug("Got port %d from pool, %d remaining",
|
|
343 |
self.debug("Got port %d from pool, %d remaining",
|
|
344 | 344 |
sport, len(ports)) |
345 | 345 |
pool = ports |
346 | 346 |
else: |
... | ... | |
350 | 350 |
self.sport = sport |
351 | 351 |
self.pool = pool |
352 | 352 |
|
353 |
self.listeners = get_listening_sockets(sport) |
|
354 |
perform_server_handshake() |
|
353 |
self.listeners = get_listening_sockets(self, sport)
|
|
354 |
self._perform_server_handshake()
|
|
355 | 355 |
|
356 |
logger.info("New forwarding: %d (client req'd: %d) -> %s:%d",
|
|
356 |
self.info("New forwarding: %d (client req'd: %d) -> %s:%d",
|
|
357 | 357 |
sport, sport_orig, self.daddr, self.dport) |
358 | 358 |
response = {"source_port": sport, |
359 | 359 |
"status": "OK"} |
360 | 360 |
except IndexError: |
361 |
logger.error(("FAILED forwarding, out of ports for [req'd by "
|
|
361 |
self.error(("FAILED forwarding, out of ports for [req'd by "
|
|
362 | 362 |
"client: %d -> %s:%d]"), |
363 | 363 |
sport_orig, self.daddr, self.dport) |
364 | 364 |
raise gevent.GreenletExit |
365 | 365 |
except Exception, msg: |
366 |
logger.error(msg)
|
|
367 |
logger.error(("FAILED forwarding: %d (client req'd: %d) -> "
|
|
366 |
self.error(msg)
|
|
367 |
self.error(("FAILED forwarding: %d (client req'd: %d) -> "
|
|
368 | 368 |
"%s:%d"), sport, sport_orig, self.daddr, self.dport) |
369 | 369 |
if not pool is None: |
370 | 370 |
pool.append(sport) |
371 |
logger.debug("Returned port %d to pool, %d remanining",
|
|
371 |
self.debug("Returned port %d to pool, %d remanining",
|
|
372 | 372 |
sport, len(pool)) |
373 | 373 |
if not server is None: |
374 | 374 |
server.close() |
... | ... | |
443 | 443 |
self.info("Waiting for a client to connect at %s", |
444 | 444 |
", ".join(["%s:%d" % s.getsockname()[:2] |
445 | 445 |
for s in self.listeners])) |
446 |
rlist, _, _ = select(self.listeners, [], [], timeout=self.timeout)
|
|
447 |
|
|
446 |
rlist, _, _ = select(self.listeners, [], [], |
|
447 |
timeout=VncAuthProxy.connect_timeout) |
|
448 | 448 |
if not rlist: |
449 | 449 |
self.info("Timed out, no connection after %d sec", |
450 |
self.timeout)
|
|
450 |
VncAuthProxy.connect_timeout)
|
|
451 | 451 |
raise gevent.GreenletExit |
452 | 452 |
|
453 | 453 |
for sock in rlist: |
... | ... | |
501 | 501 |
self._cleanup() |
502 | 502 |
|
503 | 503 |
def _run(self): |
504 |
_establish_connection() |
|
505 |
_proxy() |
|
504 |
self._establish_connection()
|
|
505 |
self._proxy()
|
|
506 | 506 |
|
507 | 507 |
# Logging support inside VncAuthproxy |
508 | 508 |
# Wrap all common logging functions in logging-specific methods |
... | ... | |
522 | 522 |
raise SystemExit |
523 | 523 |
|
524 | 524 |
|
525 |
def get_listening_sockets(sport, saddr=None): |
|
525 |
def get_listening_sockets(logger, sport, saddr=None):
|
|
526 | 526 |
sockets = [] |
527 | 527 |
|
528 | 528 |
# Use two sockets, one for IPv4, one for IPv6. IPv4-to-IPv6 mapped |
... | ... | |
581 | 581 |
metavar="LISTEN_PORT", |
582 | 582 |
help=("Port to listen for control connections" |
583 | 583 |
"(default: %d)" % DEFAULT_LISTEN_PORT)) |
584 |
parser.add_option("-b", "--backlog", dest="backlog", |
|
585 |
default=DEFAULT_BACKLOG, type="int", metavar="N", |
|
586 |
help=("Size of the backlog for the control connection " |
|
587 |
"socket (default: %s)" % DEFAULT_BACKLOG)) |
|
588 | 584 |
parser.add_option("--server-timeout", dest="server_timeout", |
589 | 585 |
default=DEFAULT_SERVER_TIMEOUT, type="float", |
590 | 586 |
metavar="N", |
... | ... | |
625 | 621 |
parser.print_help() |
626 | 622 |
sys.exit(1) |
627 | 623 |
|
624 |
return opts |
|
625 |
|
|
628 | 626 |
|
629 | 627 |
def main(): |
630 | 628 |
"""Run the daemon from the command line""" |
631 | 629 |
|
632 |
(opts, ) = parse_arguments(sys.argv[1:])
|
|
630 |
opts = parse_arguments(sys.argv[1:])
|
|
633 | 631 |
|
634 | 632 |
# Create pidfile |
635 | 633 |
pidf = pidlockfile.TimeoutPIDLockFile(opts.pid_file, 10) |
... | ... | |
690 | 688 |
VncAuthProxy.connect_retries = opts.connect_retries |
691 | 689 |
VncAuthProxy.retry_wait = opts.retry_wait |
692 | 690 |
VncAuthProxy.connect_timeout = opts.connect_timeout |
691 |
VncAuthProxy.ports = ports |
|
693 | 692 |
|
694 | 693 |
try: |
695 |
sockets = get_listening_sockets(opts.listen_address,
|
|
696 |
opts.listen_port)
|
|
694 |
sockets = get_listening_sockets(logger, opts.listen_port,
|
|
695 |
opts.listen_address)
|
|
697 | 696 |
except socket.error: |
698 | 697 |
logger.critical("Error binding control socket") |
699 | 698 |
sys.exit(1) |
... | ... | |
714 | 713 |
|
715 | 714 |
logger.info("Closing control sockets") |
716 | 715 |
while sockets: |
717 |
sockets.pop.close() |
|
716 |
sockets.pop().close()
|
|
718 | 717 |
daemon_context.close() |
719 | 718 |
sys.exit(0) |
Also available in: Unified diff