SMTP Repeater loger

时间:2013-10-01 13:15:28

标签: c# vb.net smtp

我正在尝试使用转发器(作为代理)来记录SMTP客户端和服务器之间的数据交换。我认为这很简单: 倾听客户的意见;

  • 侦听客户端IP连接;
  • 通话时,连接服务器;
  • 发回服务器消息;
  • 将客户端消息发送到服务器并将服务器反馈返回给客户端;

但是,正如我所看到的,一些服务器作为MS交换发送了多个反馈,正在打破握手。喜欢:

  • 250-SIZE 41943040
  • 250-PIPELINING
  • 250-DSN
  • 250-ENHANCEDSTATUSCODES
  • 250-STARTTLS
  • 250-AUTH LOGIN
  • 250-8BITMIME
  • 250-BINARYMIME
  • 250 CHUNKING

    这是班级

    导入System.Text 进口System.Net 导入System.Net.Sockets 进口System.Threading

    Public Class SmtpProxy

    Private client As TcpClient
    Private cltstream As NetworkStream
    Private cltreader As System.IO.StreamReader
    Private cltwriter As System.IO.StreamWriter
    
    Private smtpserver As New TcpClient
    Private srvstream As NetworkStream
    Private srvreader As System.IO.StreamReader
    Private srvwriter As System.IO.StreamWriter
    
    
    Private Shared mHostSrv As String
    Public Shared Property HostSrv() As String
        Get
            Return mHostSrv
        End Get
        Set(ByVal value As String)
            mHostSrv = value
        End Set
    End Property
    
    Private Shared mServerPort As Integer = 25
    Public Shared Property ServerPort() As Integer
        Get
            Return mServerPort
        End Get
        Set(ByVal value As Integer
        )
            mServerPort = value
        End Set
    End Property
    
    Private Shared mUserNm As String
    Public Shared Property UserNm() As String
        Get
            Return mUserNm
        End Get
        Set(ByVal value As String)
            mUserNm = value
        End Set
    End Property
    
    Private Shared mPassword As String
    Public Shared Property Password() As String
        Get
            Return mPassword
        End Get
        Set(ByVal value As String)
            mPassword = value
        End Set
    End Property
    
    
    Public Sub New(ByVal client As TcpClient)
        Me.client = client
        cltstream = client.GetStream()
        cltreader = New System.IO.StreamReader(cltstream)
        cltwriter = New System.IO.StreamWriter(cltstream)
        cltwriter.NewLine = vbCr & vbLf
        cltwriter.AutoFlush = True
    
    End Sub
    
    
    Public Shared Sub Start() 'ByVal args As String())
        Dim listener As New TcpListener(IPAddress.Loopback, 2525)
        listener.Start()
        While True
            Dim handler As New SmtpProxy(listener.AcceptTcpClient())
            Dim thread As Thread = New System.Threading.Thread(New ThreadStart(AddressOf handler.Run))
            thread.Start()
        End While
    End Sub
    
    
    Public Sub Run()
    
        If mHostSrv Like "*.*.*.*" Then
            Dim IpS As IPAddress = Nothing
            IpS = IPAddress.Parse(mHostSrv)
            smtpserver.Connect(IpS, mServerPort)
        Else
            smtpserver.Connect(mHostSrv, mServerPort)
        End If
    
        srvstream = smtpserver.GetStream
        srvreader = New System.IO.StreamReader(srvstream)
        srvwriter = New System.IO.StreamWriter(srvstream)
        srvwriter.NewLine = vbCrLf
        srvwriter.AutoFlush = True
    
    
        Dim srvline As String = srvreader.ReadLine
        cltwriter.WriteLine(srvline)
        Debug.Print("Server sent: {0}", srvline & vbCrLf)
    
        Try
    
            Dim line As String = cltreader.ReadLine
            While line IsNot Nothing
                Debug.Print("Read line {0}", line)
                srvwriter.WriteLine(line)
                Application.DoEvents()
                srvline = srvreader.ReadLine()
                Debug.Print("Server sent: {0}", srvline)
                cltwriter.WriteLine(srvline.Replace("-", " "))
                Application.DoEvents()
                line = cltreader.ReadLine()
            End While
        Catch ex As Exception
            client.Close()
            smtpserver.Close()
        End Try
    
    End Sub
    

    结束班

我尝试一次读取多个服务器行,但同时客户端返回错误,如果我发送整个返回的服务器行...

Private Function ReadSrvLines() As String

    Dim strRet As String = ""
    Do
        If strRet Like "220*" OrElse srvreader.EndOfStream Then Exit Do
        Dim strTmp As String = srvreader.ReadLine
        If strTmp Is Nothing Then Exit Do
        strRet &= strTmp.Replace("-", " ") & vbCrLf
        cltwriter.WriteLine(strTmp.Replace("-", " "))
        Debug.Print("Server sent: {0}", strTmp.Replace("-", " ") & vbCrLf)
    Loop

    Return strRet
End Function

那么有人提出任何解决方案吗?

谢谢大家!!

1 个答案:

答案 0 :(得分:0)

您需要在客户端和服务器之间同时执行异步读/写操作才能使其正常工作。 SMTP响应可以包含任意数量的行,客户端或服务器可以返回消息而无需命令(例如再见)。解析数据对于实现基本日志也是不必要的。

编辑:如果有帮助,如果3位数的消息编号后跟一个(减号)而不是一个(空格),则表示还有另一条线要跟随。