简体   繁体   中英

Twisted - how to log FTP commands received by server

I'm using twisted as a FTP Server:

from twisted.protocols.ftp import FTPFactory, FTPRealm
from twisted.cred.portal import Portal
from twisted.cred.checkers import AllowAnonymousAccess, FilePasswordDB
from twisted.internet import reactor

p = Portal(FTPRealm('./'),
           [AllowAnonymousAccess(), FilePasswordDB("pass.dat")])

f = FTPFactory(p)

reactor.listenTCP(21, f)
reactor.run()

How can i log every received FTP command from client?

FTPRealm creates FTPAnonymousShell and FTPShell instances (avatars) to mediate access to the filesystem.

These classes both implement IFTPShell . One solution is to create a wrapper for FTPRealm which applies a logging wrapper to the avatars it creates.

from twisted.python.components import proxyForInterface

class WrappingRealm(proxyForInterface(IRealm)):
    wrap = staticmethod(logging_wrapper)

    def requestAvatar(self, *a, **kw):
        d = maybeDeferred(
            super(WrappingRealm, self).requestAvatar,
            *a, **kw
        )
        def got_avatar((iface, avatar, logout)):
            return (iface, self.wrap(avatar), logout)
       d.addCallback(got_avatar)
       return d

And implement logging_wrapper something like:

class _LoggingFTPShell(proxyForInterface(IFTPShell)):
    def makeDirectory(self, path):
        log(avatar=self.avatar, operation="makeDirectory", path=path)
        return super(_LoggingFTPShell, self).makeDirectory(path)

    # The same for the rest of the methods of IFTPShell
    # ...

def logging_wrapper(avatar):
    return _LoggingFTPShell(avatar)

This is a bit tedious since you have to add logging for each method on the interface. However, since Twisted's FTP protocol implementation doesn't natively provide facilities for performing the logging you want, it's hard to get around this. With some meta-programming you might save yourself some typing (but at the cost of some complexity).

Another approach would be to contribute a patch to Twisted adding the logging you're interested in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM