namespace XMeGo.Network.Sockets
{
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using XMeGo;
public class ServerSocket
{
private ConcurrentDictionary BruteforceProtection;
private Socket Connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private bool enabled;
private string ipString;
private ushort port;
public bool PrintoutIPs;
private object SyncRoot = new object();
private Thread thread;
private const int TimeLimit = 0x3a98;
public event Action OnClientConnect;
public event Action OnClientDisconnect;
public event Action OnClientReceive;
public ServerSocket()
{
this.thread = new Thread(new ThreadStart(this.doSyncAccept));
this.thread.Start();
}
public void Disable()
{
this.enabled = false;
this.Connection.Close(1);
}
private void doSyncAccept()
{
while (true)
{
if (this.enabled)
{
try
{
this.processSocket(this.Connection.Accept());
}
catch
{
}
}
Thread.Sleep(1);
}
}
public void Enable()
{
if (!this.enabled)
{
this.Connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.Connection.Bind(new IPEndPoint(IPAddress.Any, this.port));
this.Connection.Listen(100);
this.enabled = true;
}
}
public void Enable(ushort port)
{
this.port = port;
this.Connection.Bind(new IPEndPoint(IPAddress.Any, this.port));
this.Connection.Listen(100);
this.enabled = true;
this.BruteforceProtection = new ConcurrentDictionary();
}
public void Enable(ushort port, string ip, bool BigSend = false)
{
this.ipString = ip;
this.port = port;
this.Connection.Bind(new IPEndPoint(IPAddress.Parse(this.ipString), this.port));
this.Connection.Listen(0x7fffffff);
if (BigSend)
{
this.Connection.ReceiveBufferSize = 0xffff;
this.Connection.SendBufferSize = 0xffff;
}
this.enabled = true;
this.BruteforceProtection = new ConcurrentDictionary();
}
public void InvokeDisconnect(ClientWrapper Client)
{
if (this.OnClientDisconnect != null)
{
this.OnClientDisconnect(Client);
}
}
private void processSocket(Socket socket)
{
try
{
string str = (socket.RemoteEndPoint as IPEndPoint).Address.ToString();
string str2 = (socket.LocalEndPoint as IPEndPoint).Address.ToString();
int hashCode = str.GetHashCode();
if (!Program.ALEXPC)
{
int num3;
int num2 = Time32.Now.GetHashCode();
if (!this.BruteforceProtection.TryGetValue(hashCode, out num3))
{
this.BruteforceProtection[hashCode] = num2;
}
else
{
if ((num2 - num3) < 0x3a98)
{
if (this.PrintoutIPs)
{
XMeGo.Console.WriteLine("Dropped connection: " + str, ConsoleColor.DarkYellow);
}
socket.Disconnect(false);
socket.Close();
return;
}
this.BruteforceProtection[hashCode] = num2;
if (this.PrintoutIPs)
{
XMeGo.Console.WriteLine("Allowed connection: " + str, ConsoleColor.DarkYellow);
}
}
}
ClientWrapper wrapper = new ClientWrapper();
wrapper.Create(socket, this, this.OnClientReceive);
wrapper.Alive = true;
wrapper.IP = str;
wrapper.LocalIp = str2;
if (this.OnClientConnect != null)
{
this.OnClientConnect(wrapper);
}
}
catch (Exception exception)
{
XMeGo.Console.WriteLine(exception, ConsoleColor.DarkYellow);
}
}
public void Reset()
{
this.Disable();
this.Enable();
}
public void stoptheard()
{
this.thread = null;
}
public bool Enabled
{
get
{
return this.enabled;
}
}
}
}