对于我的学校项目,我们必须在中间做一个男人。但是我们没有成功获取HTTP内容。我写了一点东西来重组tcp有效负载数据,但是我只得到HTTP标头,而不得到源代码。我看到Wireshark上的数据是未压缩的,但我不知道这是什么意思。
这是我的代码 TcpSession类
public class TcpSession : IDisposable
{
private MemoryStream _clientBuffer;
private MemoryStream _serverBuffer;
public TcpSession()
{
_clientBuffer = new MemoryStream();
_serverBuffer = new MemoryStream();
}
public void WritePacket(TcpPacket packet, bool toServer)
{
if (toServer)
{
_clientBuffer.Write(packet.PayloadData, 0, packet.PayloadData.Length);
}
else
{
_serverBuffer.Write(packet.PayloadData, 0, packet.PayloadData.Length);
}
}
public byte[] GetBuffer(bool toServer)
{
if (toServer)
{
return _clientBuffer.ToArray();
}
return _serverBuffer.ToArray();
}
public void Dispose()
{
_clientBuffer?.Dispose();
_serverBuffer?.Dispose();
}
}
PacketAssembler
public class PacketAssembler
{
public delegate void OnPacketReassembledDelegate(TcpSession session);
public OnPacketReassembledDelegate OnPacketReassembled;
private Dictionary<int, TcpSession> _tcpSessions;
public PacketAssembler()
{
_tcpSessions = new Dictionary<int, TcpSession>();
}
public void AddPacket(TcpPacket packet, bool toServer)
{
// Client port (unique) is used for the dictionary.
int port = toServer ? packet.SourcePort : packet.DestinationPort;
if (packet.Syn) // Connection established
{
if (!_tcpSessions.ContainsKey(port))
{
_tcpSessions.Add(port, new TcpSession());
}
}
else if (packet.Psh) // Push data
{
_tcpSessions[port].WritePacket(packet, toServer);
}
else if (packet.Rst) // reset the session
{
_tcpSessions.Remove(port);
}
else if (packet.Fin) // End the session
{
OnPacketReassembled(_tcpSessions[port]);
_tcpSessions.Remove(port);
}
}
}