]> git.llucax.com Git - software/pymin.git/blob - pymin/services/proxy/__init__.py
Give a nice error if a VPN key can't be readed (refs #22).
[software/pymin.git] / pymin / services / proxy / __init__.py
1 # vim: set encoding=utf-8 et sw=4 sts=4 :
2
3 from os import path
4 import logging ; log = logging.getLogger('pymin.services.proxy')
5
6 from pymin.seqtools import Sequence
7 from pymin.dispatcher import Handler, handler, HandlerError
8 from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
9                                 TransactionalHandler, ParametersHandler, \
10                                 DictSubHandler
11
12 import crypt
13
14 __all__ = ('ProxyHandler',)
15
16 class Host(Sequence):
17     def __init__(self,ip):
18         self.ip = ip
19     def as_tuple(self):
20         return (self.ip,)
21
22 # TODO convert to a SetSubHandler
23
24 class HostHandler(DictSubHandler):
25
26     handler_help = u"Manage proxy hosts"
27
28     _cont_subhandler_attr = 'hosts'
29     _cont_subhandler_class = Host
30
31 class User(Sequence):
32     def __init__(self, name, password):
33         self.name = name
34         self.password = crypt.crypt(password,'BA')
35     def as_tuple(self):
36         return (self.name, self.password)
37     def update(self, password=None):
38         if password is not None:
39             self.password = crypt.crypt(password,'BA')
40
41 class UserHandler(DictSubHandler):
42
43     handler_help = u"Manage proxy users"
44
45     _cont_subhandler_attr = 'users'
46     _cont_subhandler_class = User
47
48 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
49                    TransactionalHandler, ParametersHandler):
50
51     handler_help = u"Manage proxy service"
52
53     _initd_name = 'squid'
54
55     _persistent_attrs = ('params', 'hosts', 'users')
56
57     _restorable_defaults = dict(
58             hosts = dict(),
59             params  = dict(
60                 ip   = '192.168.0.1',
61                 port = '8080',
62             ),
63             users = dict(),
64     )
65
66     _config_writer_files = ('squid.conf','users.conf')
67     _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
68
69     def __init__(self, pickle_dir='.', config_dir='.'):
70         r"Initialize DhcpHandler object, see class documentation for details."
71         log.debug(u'ProxyHandler(%r, %r)', pickle_dir, config_dir)
72         self._persistent_dir = pickle_dir
73         self._config_writer_cfg_dir = config_dir
74         self._config_build_templates()
75         InitdHandler.__init__(self)
76         self.host = HostHandler(self)
77         self.user = UserHandler(self)
78
79     def _get_config_vars(self, config_file):
80         if config_file == 'squid.conf':
81             return dict(hosts=self.hosts.values(), **self.params)
82         return dict(users=self.users)
83
84
85 if __name__ == '__main__':
86
87     logging.basicConfig(
88         level   = logging.DEBUG,
89         format  = '%(asctime)s %(levelname)-8s %(message)s',
90         datefmt = '%H:%M:%S',
91     )
92
93     px = ProxyHandler()
94     px.set('ip','192.66.66.66')
95     px.set('port','666')
96     px.host.add('192.168.0.25.25')
97     px.host.add('192.168.0.25.26')
98     px.host.add('192.168.0.25.27')
99     px.host.delete('192.168.0.25.27')
100     px.user.add('lala','soronga')
101     px.user.add('culo','sarasa')
102     px.commit()