Esempio n. 1
0
 def __init__(self,reactor=reactor):
     Resource.__init__(self)
     self.rp = RPConfig(CONFPATH,domain=DOMAIN)
     self.cache = GLOBALCACHE
     self.reactor = reactor
     self.scheme = ''
     self.host = ''
     self.port = 80
     self.realhost = ''
Esempio n. 2
0
class MyReverseProxyResource(Resource): 
    proxyClientFactoryClass = MyProxyClientFactory
    def __init__(self,reactor=reactor):
        Resource.__init__(self)
        self.rp = RPConfig(CONFPATH,domain=DOMAIN)
        self.cache = GLOBALCACHE
        self.reactor = reactor
        self.scheme = ''
        self.host = ''
        self.port = 80
        self.realhost = ''

    def getChild(self, path, request):
        """
        Create and return a proxy resource with the same proxy configuration
        as this one, except that its path also contains the segment given by
        C{path} at the end.
        """
        return MyReverseProxyResource(self.reactor)
    
    def render(self, request):
        """
        Render a request by forwarding it to the proxied server.
        """
        #robots
        if request.uri == "/robots.txt":
            return open(PATH+"/robots.txt").read()

        self.host = request.received_headers['host']
        if self.host == DOMAIN or \
            self.host == "www."+DOMAIN:
            return self.confpage(request)
        
        self.realhost,self.port,self.scheme = self.rp.get_realhost(self.host)
        # if no alias is found, confpage again
        if self.realhost == self.host:
            return self.confpage(request)

        if request.getHeader("x-forwarded-proto")=="https":
            if not self.scheme:
                self.port = 443
                self.scheme = 'https'

        for k,v in request.getAllHeaders().items():
            request.received_headers[k] = self.rp.get_realheader(self.host,v)

        request.received_headers['host'] = self.realhost
        request.content.seek(0, 0)
        clientFactory = self.proxyClientFactoryClass(
            request.method, request.uri, request.clientproto,
            request.getAllHeaders(), request.content.read(), request)
        if DEBUG:
            print self.realhost,self.port,self.scheme,request.method,request.uri
            #print request.clientproto
            #print request.getAllHeaders()
            #print request.content.read()
        clientFactory.host = self.host
        clientFactory.realhost = self.realhost
        clientFactory.rp = self.rp
        clientFactory.cache = self.cache

        #cached?
        key = md5(self.host+request.uri).hexdigest()
        if request.method == "GET":
            cacheddata = self.cache.get(key)
            if cacheddata:
                headers,data = cacheddata
                #request.setResponseCode(304,'(Not Modified)')
                request.setResponseCode(200,'OK')
                for k,v in headers.items():
                    request.setHeader(k,v)
                return data
        clientFactory.key = key

        if self.scheme == 'https':
            self.reactor.connectSSL(self.realhost,self.port,clientFactory,ssl.ClientContextFactory())
        else:
            self.reactor.connectTCP(self.realhost,self.port,clientFactory)
        return NOT_DONE_YET

    def page_index(self):
            html = Template(open(PATH+"/templates/index.html").read()).render(cfgs=self.rp.cfgs,domain=DOMAIN)
            return html

    def confpage(self,request):
        if not WEBCONFIG:
            return "web config is disabled."
        if request.uri == "/":
            return self.page_index()
        elif request.uri.startswith("/del"):
            alias = request.uri.split("=")[-1]
            self.rp.del_alias(alias)
            return self.page_index()
        elif request.uri.startswith("/add"):
            cfg = ['','','Y','Y','Y','Y','N','N']
            html = Template(open(PATH+"/templates/edit.html").read()).render(cfg=cfg)
            return html
        elif request.uri.startswith("/edit"):
            if request.method == "GET":
                alias = request.uri.split("=")[-1]
                cfg = self.rp.get_config(alias)
                html = Template(open(PATH+"/templates/edit.html").read()).render(cfg=cfg)
                return html
            elif request.method == "POST":
                args = request.args
                try:
                    target = args['target'][0]
                    alias = args['alias'][0]
                    html = args['html'][0].upper()
                    css = args['css'][0].upper()
                    js = args['js'][0].upper()
                    flash = args['flash'][0].upper()
                    extern = args['global'][0].upper()
                    sslonhttp = args['sslonhttp'][0].upper()
                    if alias:
                        self.rp.add_alias(target,alias,html,css,js,flash,extern,sslonhttp)
                except:
                    pass
                return self.page_index() 
        else:
            if os.path.exists(PATH+request.uri):
                if request.uri.endswith('css'):
                    request.setHeader('content-type', 'text/css')
                elif request.uri.endswith('jpg'):
                    request.setHeader('content-type', 'image/jpeg')
                else:
                    request.setHeader('content-type', 'text/plain')
                return open(PATH+request.uri).read()
            else:
                return "Not Found"