from twisted.protocols import basic, policies
from twisted.plugin import IPlugin
from twisted.application.service import IServiceMaker
-from twisted.application import internet, strports
+from twisted.application import internet, strports, service
from twisted.web import static, server
import urlparse
import urllib
["privkey", "k", "server.pem", "SSL certificate to use for HTTPS."],
["certificate-chain", "C", None, "Chain SSL certificate"],
["staticpath", "s", "static", "Path to static content"],
+ ["flashPort", None, None, "Port to listen on for flash policy connections."],
]
optFlags = [["notracebacks", "n", "Display tracebacks in broken web pages. " +
raise usage.UsageError("SSL support not installed")
class FlashPolicyProtocol(protocol.Protocol, policies.TimeoutMixin):
- timeOut = 5
+ def connectionMade(self):
+ self.setTimeout(5)
def dataReceived(self, data):
if data == '<policy-file-request/>\0':
self.transport.write(self.factory.response_body)
self.transport.loseConnection()
return
- else:
+ elif self.factory.childProtocol:
self.setTimeout(None)
- p = self.factory.site.buildProtocol(self.transport.client)
+ p = self.factory.childProtocol.buildProtocol(self.transport.client)
p.transport = self.transport
self.transport.protocol = p
p.connectionMade()
p.dataReceived(data)
-
+ else:
+ self.transport.loseConnection()
class FlashPolicyFactory(protocol.ServerFactory):
protocol = FlashPolicyProtocol
- def __init__(self, site):
+ def __init__(self, childProtocol=None):
import config
base_url = urlparse.urlparse(config.BASE_URL)
port = base_url.port
else:
raise Exception("Unknown scheme: " + base_url.scheme)
- self.site = site
+ self.childProtocol = childProtocol
self.response_body = """<cross-domain-policy>
<allow-access-from domain="%s" to-ports="%d" />
</cross-domain-policy>""" % (urllib.quote(base_url.hostname), port) + '\0'
site = RootSite(config['staticpath'], logPath=config['logfile'])
else:
site = RootSite(config['staticpath'])
-
+
+ s = service.MultiService()
site.displayTracebacks = not config["notracebacks"]
if config['https']:
ssl_factory = get_ssl_factory_factory()
- i = internet.SSLServer(int(config['https']), FlashPolicyFactory(site), ssl_factory(config['privkey'], config['certificate'], certificateChainFile=config["certificate-chain"]), interface=config['ip'])
+ i = internet.SSLServer(int(config['https']), site, ssl_factory(config['privkey'], config['certificate'], certificateChainFile=config["certificate-chain"]), interface=config['ip'])
else:
i = internet.TCPServer(int(config['port']), FlashPolicyFactory(site), interface=config['ip'])
-
- return i
+
+ i.setServiceParent(s)
+ if config["flashPort"]:
+ f = internet.TCPServer(int(config['flashPort']), FlashPolicyFactory(), interface=config['ip'])
+ f.setServiceParent(s)
+
+ return s
def get_ssl_factory_factory():
from twisted.internet.ssl import DefaultOpenSSLContextFactory