source: epoptes/trunk/fuentes/twisted/plugins/epoptesd.py @ 295

Last change on this file since 295 was 295, checked in by mabarracus, 3 years ago

copy trusty epoptes code

File size: 3.5 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4###########################################################################
5# Epoptesd.
6#
7# Copyright (C) 2010 Fotis Tsamis <ftsamis@gmail.com>
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FINESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22# On Debian GNU/Linux systems, the complete text of the GNU General
23# Public License can be found in `/usr/share/common-licenses/GPL".
24###########################################################################
25
26import os
27from zope.interface import implements
28
29from twisted.python import usage
30from twisted.plugin import IPlugin
31from twisted.application.service import IServiceMaker
32from twisted.application import internet, service
33
34from epoptes.daemon import bashplex, guiplex
35from epoptes.common import config
36import grp
37from OpenSSL import SSL
38
39
40class Options(usage.Options):
41    optParameters = [
42        ("clientport", "p", 789, "Client Port"),
43        ('pingInterval', 'i', 10),
44        ('pingTimeout', 't', 10),
45      ]
46
47
48class ServerContextFactory:
49    def getContext(self):
50        ctx = SSL.Context(SSL.SSLv23_METHOD)
51        ctx.use_certificate_file("/etc/epoptes/server.crt")
52        ctx.use_privatekey_file("/etc/epoptes/server.key")
53        return ctx
54
55
56class ServiceMaker(object):
57    implements(IServiceMaker, IPlugin)
58    tapname = "epoptes"
59    description = "Epoptes Daemon"
60    options = Options
61
62    def makeService(self, options):
63       
64        factory = bashplex.DelimitedBashReceiverFactory()
65        factory.pingInterval=int(options['pingInterval'])
66        factory.pingTimeout=int(options['pingTimeout'])
67        factory.startupCommands = self.filterBash('/usr/share/epoptes/client-functions')
68
69        if config.system['ENCRYPTION']:
70            clientService = internet.SSLServer(int(config.system['PORT']),
71                factory, ServerContextFactory())
72        else:
73            clientService = internet.TCPServer(int(config.system['PORT']),
74                factory)
75
76        gid = grp.getgrnam(config.system['SOCKET_GROUP'])[2]
77       
78        if not os.path.isdir(config.system['DIR']):
79            #TODO: for some reason this does 0750 instead
80            os.makedirs(config.system['DIR'], 02770)
81        os.chmod(config.system['DIR'], 02770)
82        os.chown(config.system['DIR'], -1, gid)
83        '''
84        guiService = internet.UNIXServer(
85            "%s/epoptes.socket" % config.system['DIR'],
86            guiplex.GUIFactory())
87        '''
88        guiService = internet.TCPServer( 
89            10000,
90            guiplex.GUIFactory())
91
92        topService = service.MultiService()
93        topService.addService(clientService)
94        topService.addService(guiService)
95
96        return topService
97   
98    def filterBash(self, script):
99        f = open(script)
100        functions = f.readlines()
101        f.close()
102       
103        result = ''
104       
105        for line in functions:
106            if line.strip() != '' and line.strip()[0] == '#':
107                continue
108            result += line
109        return result
110
111serviceMaker = ServiceMaker()
Note: See TracBrowser for help on using the repository browser.