provides a Web Proxy to use with Enigma2. At the moment only standard HTTP is support...
[enigma2-plugins.git] / httpproxy / src / plugin.py
1 # by 3c5x9@2008
2 from enigma import eTimer
3
4 from Screens.Screen import Screen
5
6 from Components.config import config, getConfigListEntry
7 from Components.ConfigList import ConfigListScreen
8 from Components.Label import Label
9 from Components.ActionMap import ActionMap
10 from Components.config import config, ConfigSubsection, ConfigInteger,ConfigYesNo
11 from Components.Network import Network
12 from Plugins.Plugin import PluginDescriptor
13
14 from twisted.web import proxy, http
15 from twisted.internet import reactor
16
17
18 ###############################################################################
19 config.plugins.httpproxy = ConfigSubsection()
20 config.plugins.httpproxy.enable = ConfigYesNo(default = True)
21 config.plugins.httpproxy.port = ConfigInteger(8080,limits = (1, 65536))
22
23 ###############################################################################
24 class HTTPProxyConfigScreen(ConfigListScreen,Screen):
25     skin = """
26         <screen position="100,100" size="550,400" title="HTTP Proxy Setup" >
27         <widget name="config" position="0,0" size="550,360" scrollbarMode="showOnDemand" />
28         <widget name="buttonred" position="10,360" size="100,40" backgroundColor="red" valign="center" halign="center" zPosition="2"  foregroundColor="white" font="Regular;18"/>
29         <widget name="buttongreen" position="120,360" size="100,40" backgroundColor="green" valign="center" halign="center" zPosition="2"  foregroundColor="white" font="Regular;18"/>
30         </screen>"""
31
32     def __init__(self, session, args = 0):
33         self.session = session
34         Screen.__init__(self, session)
35         self.list = []
36         self.list.append(getConfigListEntry(_("start HTTP Proxy"), config.plugins.httpproxy.enable))
37         self.list.append(getConfigListEntry(_("use Port"), config.plugins.httpproxy.port))
38
39         ConfigListScreen.__init__(self, self.list)
40         self["buttonred"] = Label(_("cancel"))
41         self["buttongreen"] = Label(_("ok"))
42         self["setupActions"] = ActionMap(["SetupActions"],
43         {
44             "green": self.save,
45             "red": self.cancel,
46             "save": self.save,
47             "cancel": self.cancel,
48             "ok": self.save,
49         }, -2)
50
51     def save(self):
52         print "saving"
53         for x in self["config"].list:
54             x[1].save()
55         self.close(True,self.session)
56
57     def cancel(self):
58         print "cancel"
59         for x in self["config"].list:
60             x[1].cancel()
61         self.close(False,self.session)
62
63 ###############################################################################
64 class ProxyFactory(http.HTTPFactory):
65         protocol = proxy.Proxy
66
67 ###############################################################################
68 def main(session, **kwargs):
69     """ open config screen """
70     session.open(HTTPProxyConfigScreen)
71
72 def autostart(**kwargs):
73     """ start proxy in background """
74     if config.plugins.httpproxy.enable.value:
75         try:
76             nw = Network()
77             for adaptername in nw.ifaces:
78                 extip = nw.ifaces[adaptername]['ip']
79                 if nw.ifaces[adaptername]['up'] is True:
80                     extip = "%i.%i.%i.%i"%(extip[0],extip[1],extip[2],extip[3])
81                     print "starting proxy on ",extip,":", config.plugins.httpproxy.port.value
82                     reactor.listenTCP(int(config.plugins.httpproxy.port.value), ProxyFactory(),interface=extip)
83         except Exception,e:
84             print "starting the http proxy failed!"
85             print e
86
87 def Plugins(**kwargs):
88   return [
89           PluginDescriptor(name="HTTP Proxy",description="use your Dreambox as Web Proxy",where = PluginDescriptor.WHERE_PLUGINMENU,fnc = main),
90           PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart)
91           ]
92