1 from Plugins.Plugin import PluginDescriptor
3 from twisted.internet import reactor
4 from twisted.web2 import server, channel, static, resource, stream, http_headers, responsecode, http
5 from twisted.python import util
11 from Components.config import config, ConfigSubsection, ConfigInteger,ConfigYesNo
13 config.plugins.Webinterface = ConfigSubsection()
14 config.plugins.Webinterface.enable = ConfigYesNo(default = False)
15 config.plugins.Webinterface.port = ConfigInteger(80,limits = (1, 999))
16 config.plugins.Webinterface.includehdd = ConfigYesNo(default = False)
20 # set DEBUG to True, if twisted should write logoutput to a file.
22 DEBUGFILE= "/tmp/twisted.log"
24 # Passwordprotection Test
25 # set it only to True, if you have a patched wrapper.py
26 # see http://twistedmatrix.com/trac/ticket/2041
27 # in /usr/lib/python2.4/site-packages/twisted/web2/auth/wrapper.py
28 # The solution is to change this line
30 # return self.authenticate(req), seg[1:]
32 # return self.authenticate(req), seg
33 PASSWORDPROTECTION = False
34 PASSWORDPROTECTION_pwd = "root"
35 PASSWORDPROTECTION_mode = "sha";
36 # twisted supports more than sha ('md5','md5-sess','sha')
37 # but only sha works for me, but IE
38 # sha, Firefox=ok, Opera=ok, wget=ok, ie=not ok
39 # md5-sess, firefox=not ok, opera=not ok,wget=ok, ie=not ok
40 # md5 same as md5-sess
43 class ScreenPage(resource.Resource):
44 def __init__(self, path):
48 def render(self, req):
51 return http.Response(responsecode.OK, stream="please wait until enigma has booted")
53 class myProducerStream(stream.ProducerStream):
54 closed_callback = None
57 if self.closed_callback:
58 self.closed_callback()
59 stream.ProducerStream.close(self)
61 if os.path.isfile(self.path):
63 webif.renderPage(s, self.path, req, sessions[0]) # login?
64 return http.Response(responsecode.OK,stream=s)
66 return http.Response(responsecode.NOT_FOUND)
68 def locateChild(self, request, segments):
69 path = self.path+'/'+'/'.join(segments)
73 return ScreenPage(path), ()
75 class Toplevel(resource.Resource):
78 def render(self, req):
79 fp = open(util.sibpath(__file__, "web-data")+"/index.html")
82 return http.Response(responsecode.OK, {'Content-type': http_headers.MimeType('text', 'html')},stream=s)
84 child_web = ScreenPage(util.sibpath(__file__, "web")) # "/web/*"
85 child_webdata = static.File(util.sibpath(__file__, "web-data")) # FIXME: web-data appears as webdata
88 if config.plugins.Webinterface.includehdd.value:
89 toplevel.putChild("hdd",static.File("/hdd"))
91 if PASSWORDPROTECTION is False:
92 site = server.Site(toplevel)
94 from twisted.cred.portal import Portal
95 from twisted.cred import checkers
96 from twisted.web2.auth import digest, basic, wrapper
97 from zope.interface import Interface, implements
98 from twisted.cred import portal
99 class IHTTPUser(Interface):
102 class HTTPUser(object):
103 implements(IHTTPUser)
105 class HTTPAuthRealm(object):
106 implements(portal.IRealm)
107 def requestAvatar(self, avatarId, mind, *interfaces):
108 if IHTTPUser in interfaces:
109 return IHTTPUser, HTTPUser()
110 raise NotImplementedError("Only IHTTPUser interface is supported")
112 portal = Portal(HTTPAuthRealm())
113 checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(root=PASSWORDPROTECTION_pwd)
114 portal.registerChecker(checker)
115 root = wrapper.HTTPAuthResource(toplevel,
116 (basic.BasicCredentialFactory('DM7025'),digest.DigestCredentialFactory(PASSWORDPROTECTION_mode,'DM7025')),
117 portal, (IHTTPUser,))
118 site = server.Site(root)
119 print "starting Webinterface on port",config.plugins.Webinterface.port.value
120 reactor.listenTCP(config.plugins.Webinterface.port.value, channel.HTTPFactory(site))
123 def autostart(reason, **kwargs):
124 if "session" in kwargs:
126 sessions.append(kwargs["session"])
132 in normal console output, twisted will print only the first Traceback.
133 is this a bug in twisted or a conflict with enigma2?
134 with this option enabled, twisted will print all TB to the logfile
135 use tail -f <file> to view this log
138 from twisted.python.log import startLogging
139 print "start twisted logfile, writing to %s" % DEBUGFILE
140 startLogging(open(DEBUGFILE,'w'))
142 if config.plugins.Webinterface.enable.value:
145 print "not starting Webinterface"
147 print "twisted not available, not starting web services"
149 def openconfig(session, **kwargs):
150 session.open(WebIfConfig.WebIfConfigScreen)
152 def Plugins(**kwargs):
153 return [PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart),
154 PluginDescriptor(name=_("Webinterface"), description=_("Configuration for the Webinterface"),where = [PluginDescriptor.WHERE_PLUGINMENU], fnc = openconfig)]