Skip to content

Instantly share code, notes, and snippets.

@insom
Created November 8, 2012 11:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insom/4038278 to your computer and use it in GitHub Desktop.
Save insom/4038278 to your computer and use it in GitHub Desktop.
Libvirt NoVNC Proxy
#!/usr/bin/python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Based on work Copyright (c) 2012 Openstack, LLC.
import Cookie
import os
import socket
import sys
import websockify
class IwebWebSocketProxy(websockify.WebSocketProxy):
def __init__(self, *args, **kwargs):
websockify.WebSocketProxy.__init__(self, unix_target=None,
target_cfg=None,
ssl_target=None, *args, **kwargs)
def new_client(self):
cookie = Cookie.SimpleCookie()
cookie.load(self.headers.getheader('cookie'))
token = cookie['token'].value
from libvirt import openReadOnly
from xml.etree import ElementTree
c = openReadOnly(None)
d = c.lookupByName(token)
e = ElementTree.fromstring(d.XMLDesc(0))
host = 'localhost'
port = int(e.find('.//graphics').get('port'))
tsock = self.socket(host, port, connect=True)
if self.verbose and not self.daemon:
print(self.traffic_legend)
try:
self.do_proxy(tsock)
except Exception:
if tsock:
tsock.shutdown(socket.SHUT_RDWR)
tsock.close()
raise
if __name__ == '__main__':
server = IwebWebSocketProxy(listen_host='0.0.0.0', listen_port=5800,
source_is_ipv6=False, verbose=True, daemon=False, web='..',
target_host='ignore', target_port='ignore', wrap_mode='exit',
wrap_cmd=None)
server.start_server()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment