rewrite proxy to not use twisted

This commit is contained in:
Anthony Young 2010-12-22 23:41:07 -08:00
parent 4e9b4c9ce3
commit 4364a6e057
3 changed files with 65 additions and 64 deletions

View File

@ -12,101 +12,99 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')): if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)
import webob.dec
import webob.exc
from nova import exception from nova import exception
from nova import flags from nova import flags
from nova import utils from nova import utils
from nova import rpc from nova import rpc
from nova import wsgi
import exceptions import exceptions
import logging import logging
import urlparse import urlparse
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
from twisted.internet import reactor
from twisted.internet import task
from twisted.web import error, http
from twisted.web.proxy import Proxy, ProxyRequest
flags.DEFINE_integer('ajax_console_idle_timeout', 300, flags.DEFINE_integer('ajax_console_idle_timeout', 300,
'Seconds before idle connection destroyed') 'Seconds before idle connection destroyed')
class AjaxConsoleProxyRequest(ProxyRequest): import eventlet
def process(self): from eventlet import greenthread
if 'referer' in self.received_headers: from eventlet.green import urllib2
auth_uri = self.received_headers['referer']
else:
auth_uri = self.uri
class AjaxConsoleProxy(object):
tokens = {}
def __call__(self, environ, start_response):
try: try:
auth_params = urlparse.parse_qs(urlparse.urlparse(auth_uri).query) req_url = '%s://%s%s?%s' % (environ['wsgi.url_scheme'], environ['HTTP_HOST'], environ['PATH_INFO'], environ['QUERY_STRING'])
parsed_uri = urlparse.urlparse(self.uri) if 'HTTP_REFERER' in environ:
auth_url = environ['HTTP_REFERER']
else:
auth_url = req_url
auth_params = urlparse.parse_qs(urlparse.urlparse(auth_url).query)
parsed_url = urlparse.urlparse(req_url)
auth_info = auth_params['token'][0]
auth_info = AjaxConsoleProxy.tokens[auth_params['token'][0]] auth_info = AjaxConsoleProxy.tokens[auth_params['token'][0]]
args = auth_info['args'] args = auth_info['args']
auth_info['last_activity_at'] = time.time() auth_info['last_activity_at'] = time.time()
self.uri = ("http://%s:%s%s?token=%s"% ( remote_url = ("http://%s:%s%s?token=%s"% (
str(args['host']), str(args['host']),
str(args['port']), str(args['port']),
parsed_uri.path, parsed_url.path,
str(args['token']))) str(args['token'])))
ProxyRequest.process(self) opener = urllib2.urlopen(remote_url, environ['wsgi.input'].read())
body = opener.read()
info = opener.info()
start_response("200 OK", info.dict.items())
return body
except (exceptions.KeyError): except (exceptions.KeyError):
raise exception.NotAuthorized("Unauthorized Request") start_response("401 NOT AUTHORIZED",[])
return "Not Authorized"
except Exception:
start_response("500 ERROR",[])
return "Server Error"
class AjaxConsoleProxy(Proxy):
#tokens = {}
tokens = {'key': {'args':'','last_activity_at':time.time()}}
requestFactory = AjaxConsoleProxyRequest
def start(self): def register_listeners(self):
class Callback:
def __call__(self, data, message):
if data['method'] == 'authorize_ajax_console':
AjaxConsoleProxy.tokens[data['args']['token']] = \
{'args': data['args'], 'last_activity_at': time.time()}
conn = rpc.Connection.instance(new=True) conn = rpc.Connection.instance(new=True)
self.consumer = rpc.TopicConsumer( consumer = rpc.TopicConsumer(
connection=conn, connection=conn,
topic=FLAGS.ajax_console_proxy_topic) topic=FLAGS.ajax_console_proxy_topic)
self.consumer.register_callback(self) consumer.register_callback(Callback())
task.LoopingCall(self.age).start(1.0) def delete_expired_tokens():
task.LoopingCall(self.pollq).start(0.1)
factory = http.HTTPFactory()
factory.protocol = AjaxConsoleProxy
port = urlparse.urlparse(FLAGS.ajax_console_proxy_url).port
reactor.listenTCP(port, factory)
reactor.run()
def age(self):
now = time.time() now = time.time()
print now
to_delete = [] to_delete = []
for k, v in AjaxConsoleProxy.tokens.items(): for k, v in AjaxConsoleProxy.tokens.items():
if now - v['last_activity_at'] > FLAGS.ajax_console_idle_timeout: if now - v['last_activity_at'] > FLAGS.ajax_console_idle_timeout:
to_delete.append(k) to_delete.append(k)
for k in to_delete: for k in to_delete:
print "del"
del AjaxConsoleProxy.tokens[k] del AjaxConsoleProxy.tokens[k]
def pollq(self): utils.LoopingCall(consumer.fetch, auto_ack=True,
self.consumer.fetch(auto_ack=True, enable_callbacks=True) enable_callbacks=True).start(0.1)
utils.LoopingCall(delete_expired_tokens).start(1)
def __call__(self, data, message):
if data['method'] == 'authorize_ajax_console':
AjaxConsoleProxy.tokens[data['args']['token']] = {'args': data['args'], 'born_at': time.time()}
if __name__ == '__main__': if __name__ == '__main__':
utils.default_flagfile() utils.default_flagfile()
FLAGS(sys.argv) FLAGS(sys.argv)
server = wsgi.Server()
formatter = logging.Formatter('(%(name)s): %(levelname)s %(message)s') acp = AjaxConsoleProxy()
handler = logging.StreamHandler() acp.register_listeners()
handler.setFormatter(formatter) server.start(acp, FLAGS.ajax_console_proxy_port, host='0.0.0.0')
logging.getLogger().addHandler(handler) server.wait()
ajaxproxy = AjaxConsoleProxy()
ajaxproxy.start()

View File

@ -222,6 +222,9 @@ DEFINE_string('ajax_console_proxy_topic', 'ajax_proxy',
DEFINE_string('ajax_console_proxy_url', DEFINE_string('ajax_console_proxy_url',
'http://tonbuntu:8000', 'http://tonbuntu:8000',
'location of ajax console proxy, in the form "http://tonbuntu:8000"') 'location of ajax console proxy, in the form "http://tonbuntu:8000"')
DEFINE_string('ajax_console_proxy_port',
8000,
'port that ajax_console_proxy binds')
DEFINE_bool('verbose', False, 'show debug output') DEFINE_bool('verbose', False, 'show debug output')
DEFINE_boolean('fake_rabbit', False, 'use a fake rabbit') DEFINE_boolean('fake_rabbit', False, 'use a fake rabbit')
DEFINE_bool('fake_network', False, DEFINE_bool('fake_network', False,

View File

@ -253,7 +253,7 @@ else{ /* end IE initialization, try to deal with real browsers now ;-) */
oDoc.addEventListener("load", _sarissa_XMLDocument_onload, false); oDoc.addEventListener("load", _sarissa_XMLDocument_onload, false);
return oDoc; return oDoc;
}; };
if(window.XMLDocument){ if(false && window.XMLDocument){
/** /**
* <p>Emulate IE's onreadystatechange attribute</p> * <p>Emulate IE's onreadystatechange attribute</p>
*/ */