Ejemplo n.º 1
0
    def __init__(self, name, port, handler, socktype, config):
        self.name = name
        self.port = port
        self.socktype = socktype
        self.server = None

        globalconf = Config()
        if socktype.upper() == 'UDP':
            self.server = SocketServer.ThreadingUDPServer(('', port), handler)
        elif socktype.upper() == 'TCP':
            self.server = SocketServer.ThreadingTCPServer(('', port), handler)
        elif socktype.upper() == 'SSL':
            if not get_data_file(globalconf.certfile) or not get_data_file(
                    globalconf.keyfile):
                log.warn("Cannot find certfile: %s or keyfile: %s for ssl",
                         globalconf.certfile, globalconf.keyfile)
            else:
                self.server = SocketServer.ThreadingTCPServer(('', port),
                                                              handler)
                self.server.socket = ssl.wrap_socket(
                    self.server.socket,
                    keyfile=get_data_file(globalconf.keyfile),
                    certfile=get_data_file(globalconf.certfile),
                    server_side=True)
        else:
            log.warn("Unsupported or invalid socket type: %s for config '%s'",
                     socktype, name)

        if self.server:
            self.server.cfg = ModuleConfig(config).cfg
Ejemplo n.º 2
0
    def __init__(self, name, port, handler, socktype, config):
        self.name = name
        self.port = port
        self.socktype = socktype
        self.server = None
        
        globalconf = Config()
        if socktype.upper() == 'UDP':
            self.server = SocketServer.ThreadingUDPServer(('', port), handler)
        elif socktype.upper() == 'TCP':
            self.server = SocketServer.ThreadingTCPServer(('', port), handler)
        elif socktype.upper() == 'SSL':
            if not get_data_file(globalconf.certfile) or not get_data_file(globalconf.keyfile):
                log.warn("Cannot find certfile: %s or keyfile: %s for ssl", 
                         globalconf.certfile, globalconf.keyfile)
            else:
                self.server = SocketServer.ThreadingTCPServer(('', port), handler)
                self.server.socket = ssl.wrap_socket(self.server.socket, 
                                                keyfile=get_data_file(globalconf.keyfile), 
                                                certfile=get_data_file(globalconf.certfile), 
                                                server_side=True)
        else:
            log.warn("Unsupported or invalid socket type: %s for config '%s'", 
                     socktype, name)

        if self.server:
            self.server.cfg = ModuleConfig(config).cfg
Ejemplo n.º 3
0
 def handlepath(self, host, method, path):
     """Search config patterns to find an appropriate file/response to return.
     """
     for x in self.responses:
         m = re.match(x.pattern, host + path)
         if m:
             data = ""
             if x.file and x.file != "None":
                 if not os.path.exists(get_data_file(x.file)):
                     logging.warn(
                         "Cannot find referenced file: %s to return for http request",
                         x.file)
                 else:
                     with open(get_data_file(x.file), 'rb') as tmp:
                         data = tmp.read()
             self.wfile.write("HTTP/1.0 %s OK\r\n" % x.status)
             self.wfile.write(
                 "Date: %s\r\n" %
                 datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT"))
             self.wfile.write("Server: %s\r\n" % self.serverstring)
             self.wfile.write("Connection: close\r\n")
             if data:
                 self.wfile.write("Content-Length: %s\r\n" % len(data))
                 self.wfile.write("Content-Type: %s\r\n\r\n" %
                                  mimetypes.guess_type(x.file)[0])
                 self.wfile.write(data)
             else:
                 self.wfile.write("\r\n")
             return
Ejemplo n.º 4
0
 def handle(self):
     """Attempts SSL/TLS handshake on current connection
     and replaces the it with a wrapped socket to handle
     if successful.
     """
     self.connection = PeekableSSLSocket(self.connection,  
                                     keyfile=get_data_file(self.keyfile), 
                                     certfile=get_data_file(self.certfile), 
                                     server_side=True)
Ejemplo n.º 5
0
 def handle(self):
     """Attempts SSL/TLS handshake on current connection
     and replaces the it with a wrapped socket to handle
     if successful.
     """
     self.connection = PeekableSSLSocket(
         self.connection,
         keyfile=get_data_file(self.keyfile),
         certfile=get_data_file(self.certfile),
         server_side=True)
Ejemplo n.º 6
0
def test_ftp_upload():
    server = SocketServer.TCPServer(('', 0), ftp.FTPHandler)
    server.cfg = ModuleConfig('ftp.conf').cfg
    thread.start_new_thread(server.serve_forever, ())
    client = ftplib.FTP()
    client.connect('127.0.0.1', server.socket.getsockname()[1])
    client.login()
    with open(get_data_file("ftproot/README.txt")) as tmp:
        client.storbinary('STOR testing.txt', tmp)
    client.quit()
Ejemplo n.º 7
0
 def starttls(self):
     """Upgrade the transport used by this handler
     to ssl/tls.  As socket will already be connected
     this will trigger a handshake with the client endpoint.
     """
     globalconf = Config()
     self.connection = ssl.wrap_socket(
         self.connection,
         keyfile=get_data_file(globalconf.keyfile),
         certfile=get_data_file(globalconf.certfile),
         server_side=True)
     # flush and recreate the rfile/wfile handles
     iowrap = self.rfile
     iowrap.logread()
     iowrap.logwrite()
     self.rfile = self.connection.makefile('rb', self.rbufsize)
     self.wfile = self.connection.makefile('wb', self.wbufsize)
     iowrap = IOWrapper(self.client_address, self.server.server_address,
                        self.rfile, self.wfile)
     self.rfile = iowrap
     self.wfile = iowrap
Ejemplo n.º 8
0
 def starttls(self):
     """Upgrade the transport used by this handler
     to ssl/tls.  As socket will already be connected
     this will trigger a handshake with the client endpoint.
     """
     globalconf = Config()
     self.connection = ssl.wrap_socket(self.connection,
                             keyfile=get_data_file(globalconf.keyfile),
                             certfile=get_data_file(globalconf.certfile),
                             server_side=True)
     # flush and recreate the rfile/wfile handles
     iowrap = self.rfile
     iowrap.logread()
     iowrap.logwrite()
     self.rfile = self.connection.makefile('rb', self.rbufsize)
     self.wfile = self.connection.makefile('wb', self.wbufsize)
     iowrap = IOWrapper(self.client_address, 
                        self.server.server_address, 
                        self.rfile, 
                        self.wfile)
     self.rfile = iowrap
     self.wfile = iowrap
Ejemplo n.º 9
0
 def handlepath(self, host, method, path):
     """Search config patterns to find an appropriate file/response to return.
     """
     for x in self.responses:
         m = re.match(x.pattern, host + path)
         if m:
             data = ""
             if x.file and x.file != "None":
                 if not os.path.exists(get_data_file(x.file)):
                     logging.warn("Cannot find referenced file: %s to return for http request", x.file)
                 else:
                     with open(get_data_file(x.file), 'rb') as tmp:
                         data = tmp.read()
             self.wfile.write("HTTP/1.0 %s OK\r\n" % x.status)
             self.wfile.write("Date: %s\r\n" %  datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT"))
             self.wfile.write("Server: %s\r\n" % self.serverstring)
             self.wfile.write("Connection: close\r\n")
             if data:
                 self.wfile.write("Content-Length: %s\r\n" % len(data))
                 self.wfile.write("Content-Type: %s\r\n\r\n" % mimetypes.guess_type(x.file)[0])
                 self.wfile.write(data)
             else:
                 self.wfile.write("\r\n")
             return
Ejemplo n.º 10
0
 def handle(self):
     """Hands control off to pyftpd to process the client connection.
     """
     # server attributes/methods expected by pyftp handler
     self.server.backlog = 50
     self.server.ip_map = []
     self.server._accept_new_cons = lambda: True
     self.server._af = socket.AF_INET
     tmpdir = None
     try:
         # set up a temp dir as the ftp root for the user
         tmpdir = tempfile.mkdtemp(prefix='tmpftp')
         ftproot = os.path.join(tmpdir, self.dirseed).decode('utf-8')
         shutil.copytree(get_data_file(self.dirseed), ftproot)
         # hand off control to their handler with its own async ioloop
         handler = PyFTPHandler(self.request, self.server, ioloop=IOLoop())
         handler.authorizer = PermissiveAuthorizer(ftproot)
         handler.handle()
         handler.ioloop.loop(1)
     finally:
         if handler.ioloop:
             handler.ioloop.close()
         if tmpdir:
             shutil.rmtree(tmpdir)