password-manager/scripts/proxy/main.py

134 lines
4.3 KiB
Python
Raw Permalink Normal View History

from twisted.internet import reactor
from twisted.web import proxy, server, http, resource, static
from posixpath import basename, dirname
2013-01-08 16:12:19 +01:00
import copy
import sys
import os
import uuid
2013-01-08 16:12:19 +01:00
import pprint
import codecs
import time
import subprocess
#! sys.path.append("/usr/local/lib/python2.7/site-packages")
# > export PYTHONPATH=/usr/local/lib/python2.7/site-packages
2013-01-08 16:12:19 +01:00
#--------------------------------------------------------------------
def scriptDir ():
return os.path.dirname(sys.argv[0])
def projectBaseDir ():
return os.path.abspath(scriptDir() + '/../..')
def projectTargetDir():
return projectBaseDir() + '/target/'
#--------------------------------------------------------------------
class ClipperzTestSite(server.Site):
def __init__(self, resource, logPath=None, timeout=60 * 60 * 12):
server.Site.__init__(self, resource, logPath, timeout)
def getResourceFor(self, request):
2013-08-30 17:56:53 +02:00
uri = request.uri
uri = uri.split("?", 1)[0]
uri = uri.split("#", 1)[0]
if uri == '/':
# This serves the message, but also throws an exception; can't understand why...
result = static.Data('<html>In production you would now be on https://clipperz.is/</html>', 'text/html')
elif uri.startswith('/json') or uri.startswith('/dump'):
resource.prepath = ['app']
2013-01-08 16:12:19 +01:00
result = resource.getChildForRequest(self.resource, request)
elif uri.startswith('/payment'):
resource.prepath = ['payment']
result = resource.getChildForRequest(self.resource, request)
elif uri == '/favicon.ico':
return
2013-01-08 16:12:19 +01:00
else:
2013-08-30 17:56:53 +02:00
pathParts = uri.split('/')
2013-01-08 16:12:19 +01:00
version = pathParts[1]
if pathParts[2].startswith('index.'):
print("-> index")
2013-01-08 16:12:19 +01:00
contentType = 'text/html'
absoluteFilePath = os.path.join(projectTargetDir(), 'dev', version, pathParts[2])
# print("INDEX.HTML absolute path " + str(absoluteFilePath))
2013-01-08 16:12:19 +01:00
result = static.File(absoluteFilePath, contentType)
2013-08-30 17:56:53 +02:00
elif pathParts[2].endswith('.webapp'):
print("-> webapp")
2013-08-30 17:56:53 +02:00
contentType = 'application/x-web-app-manifest+json'
absoluteFilePath = os.path.join(projectBaseDir(), 'frontend', version, 'properties', pathParts[2])
result = static.File(absoluteFilePath, contentType)
2014-08-05 18:22:20 +02:00
# elif pathParts[2].endswith('.appcache'):
elif pathParts[2].endswith('.appcache_disabled'):
print("-> appcache")
contentType = 'text/cache-manifest'
absoluteFilePath = os.path.join(projectBaseDir(), 'frontend', version, 'properties', pathParts[2])
fileContent = codecs.open(absoluteFilePath, 'r', 'utf-8').read()
# fileContent = fileContent.replace('@application.version@', str(uuid.uuid1()))
fileContent = fileContent.replace('@application.version@', str(round(time.time())))
result = static.Data(str(fileContent), contentType)
2013-01-08 16:12:19 +01:00
else:
# http://homer.local:8888/beta/css/clipperz/images/loginInfoBackground.png
# pathParts: ['', 'beta', 'css', 'clipperz', 'images', 'loginInfoBackground.png']
try:
imagePathIndex = pathParts.index('images')
resourceType = 'images'
for _ in range(2, imagePathIndex):
del pathParts[2]
except:
resourceType = pathParts[2]
basePath = projectBaseDir() + '/frontend'
if resourceType == 'images':
2013-08-30 17:56:53 +02:00
fileExtension = os.path.splitext(uri)[1]
2013-01-08 16:12:19 +01:00
if fileExtension == '.png':
# print("-> image - png")
2013-01-08 16:12:19 +01:00
contentType = 'image/png'
elif fileExtension == '.jpg':
# print("-> image - jpg")
2013-01-08 16:12:19 +01:00
contentType = 'image/jpeg'
elif fileExtension == '.gif':
# print("-> image - gif")
2013-01-08 16:12:19 +01:00
contentType = 'image/gif'
else:
print "ERROR - unknown image extension: " + fileExtension
absoluteFilePath = basePath + '/'.join(pathParts)
else:
resourceType = pathParts[2]
if resourceType == 'css':
# print("-> css")
2013-01-08 16:12:19 +01:00
contentType = 'text/css'
elif resourceType == 'js':
# print("-> js")
2013-01-08 16:12:19 +01:00
contentType = 'text/javascript'
else:
# print("-> text/html")
2013-01-08 16:12:19 +01:00
contentType = 'text/html'
2013-08-30 17:56:53 +02:00
absoluteFilePath = basePath + uri
2013-01-08 16:12:19 +01:00
result = static.File(absoluteFilePath, contentType)
# print("RESULT\n" + str(result))
2013-01-08 16:12:19 +01:00
return result
def main ():
2014-08-05 18:22:20 +02:00
# site = ClipperzTestSite(proxy.ReverseProxyResource('localhost', 8084, '/java-backend'))
site = ClipperzTestSite(proxy.ReverseProxyResource('localhost', 8084, '/app'))
# site = ClipperzTestSite(proxy.ReverseProxyResource('www.clipperz.com', 443, '/'))
2013-01-08 16:12:19 +01:00
reactor.listenTCP(8888, site)
2013-01-08 16:12:19 +01:00
reactor.run()
if __name__ == "__main__":
main()