2013-10-01 59 views
1

我正在嘗試做一箇中繼器(作爲代理)來記錄SMTP客戶端和服務器之間的數據交換。我認爲這很容易: 傾聽客戶;SMTP直放站愛好者

  • 偵聽客戶端IP連接;
  • 隨時待命,連接到服務器;
  • 發回服務器消息;
  • 將客戶端消息發送到服務器並將服務器反饋返回給客戶端;

但是,正如我所看到的,一些服務器,MS Exchange發送多個反饋女巫打破handshaking.Like:

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

    下面是類

    進口System.Text 進口System.Net 進口的System.Net.Sockets 進口系統。線程

    公共類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 

那麼是否有人有任何解決方案提出?

謝謝大家!

弗蘭克

+0

我試圖根據Ashigore意見和服務器返回的消息以減去整個字符串相結合,改變我的做法,但SMTP客戶端似乎不理解它,它堅持像上次消息後返回: –

+0

服務器發送:220郵件。xxx.com Microsoft ESMTP MAIL服務已準備好2013年10月1日星期二11:31:30 -0400 客戶端發送:EHLO PC1112 服務器發送:250-mail.xxx.com您好[192.168.6.44] 服務器發送:250 -SIZE 41943040 服務器發送:250流水 服務器發送:250-DSN 服務器發送:250 ENHANCEDSTATUSCODES 服務器發送:250-STARTTLS 服務器發送:250-AUTH LOGIN 服務器發送:250-8BITMIME 服務器發送: 250-BINARYMIME 發送服務器:250 CHUNKING –

回答

0

您需要同時執行多個異步讀取/客戶端和服務器之間的寫作,使這項工作正常。 SMTP響應可以包含任意數量的行,並且客戶端或服務器可以在不需要命令的情況下返回消息(例如說再見)。解析數據對於實現基本日誌也是不必要的。

編輯:如果有幫助,如果3位數的消息號後面跟一個(減號)而不是一個(空格),這意味着還有另一條線。

+0

Haaa!這就是爲什麼減號,非常感謝信息! –