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))
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
86 child_hdd = static.File("/hdd")
88 if PASSWORDPROTECTION is False:
89 site = server.Site(Toplevel())
91 from twisted.cred.portal import Portal
92 from twisted.cred import checkers
93 from twisted.web2.auth import digest, basic, wrapper
94 from zope.interface import Interface, implements
95 from twisted.cred import portal
96 class IHTTPUser(Interface):
99 class HTTPUser(object):
100 implements(IHTTPUser)
102 class HTTPAuthRealm(object):
103 implements(portal.IRealm)
104 def requestAvatar(self, avatarId, mind, *interfaces):
105 if IHTTPUser in interfaces:
106 return IHTTPUser, HTTPUser()
107 raise NotImplementedError("Only IHTTPUser interface is supported")
109 portal = Portal(HTTPAuthRealm())
110 checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(root=PASSWORDPROTECTION_pwd)
111 portal.registerChecker(checker)
112 root = wrapper.HTTPAuthResource(Toplevel(),
113 (basic.BasicCredentialFactory('DM7025'),digest.DigestCredentialFactory(PASSWORDPROTECTION_mode,'DM7025')),
114 portal, (IHTTPUser,))
115 site = server.Site(root)
116 print "starting Webinterface on port",config.plugins.Webinterface.port.value
117 reactor.listenTCP(config.plugins.Webinterface.port.value, channel.HTTPFactory(site))
119 # start classes for PASSWORDPROTECTION
120 # end classes for PASSWORDPROTECTION
122 def autostart(reason, **kwargs):
123 if "session" in kwargs:
125 sessions.append(kwargs["session"])
131 in normal console output, twisted will print only the first Traceback.
132 is this a bug in twisted or a conflict with enigma2?
133 with this option enabled, twisted will print all TB to the logfile
134 use tail -f <file> to view this log
137 from twisted.python.log import startLogging
138 print "start twisted logfile, writing to %s" % DEBUGFILE
139 startLogging(open(DEBUGFILE,'w'))
141 if config.plugins.Webinterface.enable.value:
144 print "not starting Webinterface"
146 print "twisted not available, not starting web services"
148 def openconfig(session, **kwargs):
149 session.open(WebIfConfig.WebIfConfigScreen)
151 def Plugins(**kwargs):
152 return [PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart),
153 PluginDescriptor(name=_("Webinterface"), description=_("Configuration for the Webinterface"),where = [PluginDescriptor.WHERE_PLUGINMENU], fnc = openconfig)]