Przepraszan za drugi post, ale w jednym się nie zmieściło ograniczenie do 10000 znaków.
GGListener.cs
Kod:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Windows.Forms;
namespace PrzechwytywanieGG
{
class GGListener
{
private IPHeader ipHeader;
private TCPHeader tcpHeader;
private byte[] data;
private Socket s;
private string filePath;
//konstruktor klasy
public GGListener(string _fileParh)
{
filePath = _fileParh;
}
//metody
private bool IsGGPort(ushort port)
{
return (port == 8074 || port == 443);
}
public void StartToListen()
{
try
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
s.Bind(new IPEndPoint(MachineAddress(), 0));
byte[] optionInValue = new byte[4] { 1, 0, 0, 0 };
byte[] optionOutValue = new byte[4];
s.IOControl(IOControlCode.ReceiveAll, optionInValue, optionOutValue);
data = new byte[4096];
s.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(AsyncDataReceived), null);
}
catch (Exception ex)
{
//Błąd
MessageBox.Show("Wyjątek z StartToListen" + ex);
}
}
private IPAddress MachineAddress()
{
string hostName = Dns.GetHostName();
IPHostEntry ipHostEntry = Dns.GetHostByName(hostName);
return ipHostEntry.AddressList[0];
}
private void AsyncDataReceived(IAsyncResult result)
{
try
{
int nReceived = s.EndReceive(result);
ipHeader = new IPHeader(data, nReceived);
if (ipHeader.TypeOfProtocol == EProtocolType.TCP)
{
SaveInfo();
}
data = new byte[4096];
s.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(AsyncDataReceived), null);
}
catch (Exception ex)
{
MessageBox.Show("Wyjątek z AsynDataReceived" + ex);
}
}
//SaveInfo
private void SaveInfo()
{
try
{
tcpHeader = new TCPHeader(ipHeader.Data, ipHeader.MessageLength);
if (!(IsGGPort(tcpHeader.DestinationPort) || IsGGPort(tcpHeader.SourcePort)))
return;
if (BitConverter.ToUInt32(tcpHeader.TcpData, 0) == 0x0b || BitConverter.ToUInt32(tcpHeader.TcpData, 0) == 0x0a)
{
int nMsgLength = BitConverter.ToInt32(tcpHeader.TcpData,4);
int nStartingByte = 0;
if (!File.Exists(filePath))
using (File.Create(filePath)) ; //tu był średnik, gdy jest ostrzeżenie: Possible mistaken empty statement,bez znika
using (StreamWriter sw = File.AppendText(filePath))
{
string msgType = " ";
sw.Write("\r\n++++++++++++++++++++++++++++++++++++\r\n");
if (tcpHeader.DestinationPort == 8074 || tcpHeader.DestinationPort == 443)
{
sw.Write("Wiadomość wychodząca:\r\n");
msgType = "Numer odbiorcy: ";
nStartingByte = 20;
nMsgLength -= 12;
}
if (tcpHeader.SourcePort == 8074 || tcpHeader.SourcePort == 443)
{
sw.Write("Wiadomość przychodząca:\r\n");
msgType = "Numer nadawcy:";
nStartingByte = 24;
nMsgLength -= 16;
}
sw.Write("Port źródłowy " + tcpHeader.SourcePort + "\r\n");
sw.Write("Port docelowy " + tcpHeader.DestinationPort + "\r\n");
sw.Write(msgType + BitConverter.ToUInt32(tcpHeader.TcpData, 8) + "\r\n");
Encoding e = Encoding.GetEncoding("windows-1250");
sw.Write("Wiadomość: " + e.GetString(tcpHeader.TcpData, nStartingByte, nMsgLength));
}
}
}
catch (Exception ex)
{
MessageBox.Show("Wyjątek z SaveInfo" + ex);
}
}
}
}
Form.Cs
Kod:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PrzechwytywanieGG
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
GGListener ob = new GGListener(Application.UserAppDataPath + @"\plik.txt");
ob.StartToListen();
}
}
}