0

ここではPythonの初心者です。Twisted と twisted.mail.smtp を使って SMTP サーバーを書いています。着信接続をログに記録し、同時接続が多すぎる場合はそれらをダンプしたいと思います。基本的に、新しい接続が確立されたときに、次のように ConsoleMessageDelivery.connectionMade() メソッドが呼び出されるようにします。

class ConsoleMessageDelivery:
    implements(smtp.IMessageDelivery)

    def connectionMade(self):
        # This never gets called

    def receivedHeader(self, helo, origin, recipients):
        myHostname, clientIP = helo
        headerValue = "by %s from %s with ESMTP ; %s" % (myHostname, clientIP, smtp.rfc822date())
        # email.Header.Header used for automatic wrapping of long lines
        return "Received: %s" % Header(headerValue)

    def validateFrom(self, helo, origin):
        # All addresses are accepted
        return origin

    def validateTo(self, user):
        if user.dest.local == "console":
            return lambda: ConsoleMessage()
        raise smtp.SMTPBadRcpt(user)

class ConsoleMessage:
    implements(smtp.IMessage)

    def __init__(self):
        self.lines = []

    def lineReceived(self, line):
        self.lines.append(line)

    def eomReceived(self):
        return defer.succeed(None)

    def connectionLost(self):
        # There was an error, throw away the stored lines
        self.lines = None

class ConsoleSMTPFactory(smtp.SMTPFactory):
    protocol = smtp.ESMTP

    def __init__(self, *a, **kw):
        smtp.SMTPFactory.__init__(self, *a, **kw)
        self.delivery = ConsoleMessageDelivery()

    def buildProtocol(self, addr):
        p = smtp.SMTPFactory.buildProtocol(self, addr)
        p.delivery = self.delivery
        return p
4

1 に答える 1

4

connectionMadeの一部でありtwisted.internet.interfaces.IProtocol、の一部ではありませんtwisted.mail.smtp.IMessageDeliveryconnectionMadeメッセージ配信実装のメソッドを気にするコードは、メール サーバー実装のどこにもありません。

接続ごとのロジックを配置するのに適した場所は、工場です。特に、これにアプローチする良い方法は、ファクトリラッパーを使用して、SMTP 接続のサービスに関するロジックから接続制限とログ記録に関するロジックを分離することです。

Twisted にはいくつかのファクトリー ラッパーが付属しています。あなたにとって特に興味深いカップルはtwisted.protocols.policies.LimitConnectionsByPeertwisted.protocols.policies.LimitTotalConnectionsFactoryです。

残念ながら、私は説明しているドキュメントを知りませんtwisted.protocols.policies。幸いなことに、それほど複雑ではありません。モジュール内のファクトリのほとんどは、別の任意のファクトリをラップして、いくつかの動作を追加します。たとえば、 を使用するLimitConnectionsByPeerには、次のようにします。

from twisted.protocols.policies import LimitConnectionsByPeer
...
factory = ConsoleSMTPFactory()
wrapper = LimitConnectionsByPeer(ConsoleSMTPFactory(...))
reactor.listenTCP(465, wrapper)

これは、LimitConnectionsByPeerその仕事をするために必要なすべてです。

独自のラッパーを作成する場合は、少しだけ複雑になります。まず、サブクラスWrappingFactory。次に、カスタマイズしたいメソッドを実装します。あなたの場合、特定の IP からの接続を拒否したい場合、それはオーバーライドを意味しますbuildProtocol。次に、構築されたプロトコルをカスタマイズしたい場合 (この場合はカスタマイズしません) を除き、基本実装を呼び出してその結果を返します。例えば:

from twisted.protocols.policies import WrappingFactory

class DenyFactory(WrappingFactory):
    def buildProtocol(self, clientAddress):
        if clientAddress.host == '1.3.3.7':
            # Reject it
            return None
         # Accept everything else
         return WrappingFactory.buildProtocol(self, clientAddress)

これらのラッパーはスタックされるため、それらを組み合わせることもできます。

from twisted.protocols.policies import LimitConnectionsByPeer
...
factory = ConsoleSMTPFactory()
wrapper = LimitConnectionsByPeer(DenyFactory(ConsoleSMTPFactory(...)))
reactor.listenTCP(465, wrapper)
于 2012-08-28T18:31:14.063 に答える