|
namespace KhaledMohamed.Network.Sockets
{
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using KhaledMohamed;
using System.Net;
public class ClientWrapper
{
public Socket Socket { get; private set; }
public ServerSocket Server { get; private set; }
public IPEndPoint RemoteEndPoint { get; private set; }
private readonly byte[] _buffer;
public object Owner;
public Boolean IsAlive { get { return Socket.Connected; } }
public String IP
{
get
{
if (Socket != null)
return (Socket.RemoteEndPoint as IPEndPoint).Address.ToString();
else
return null;
}
}
public ClientWrapper(ServerSocket server, Socket socket, Int32 bufferLength)
{
//IsAlive = true;
Server = server;
Socket = socket;
_buffer = new byte[bufferLength];
RemoteEndPoint = (IPEndPoint)Socket.RemoteEndPoint;
Socket.NoDelay = true;
}
public void BeginReceive()
{
try
{
Socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(Receive), null);
}
catch (SocketException)
{
Server.InvokeOnDisconnect(this);
}
}
private void Receive(IAsyncResult result)
{
if (Socket != null)
{
try
{
SocketError error;
Int32 length = Socket.EndReceive(result, out error);
if (IsAlive && error == SocketError.Success)
{
if (length > 0)
{
try
{
Server.InvokeOnReceive(_buffer, length, this);
}
catch (Exception e)
{
System.Console.WriteLine(e.ToString());
}
finally
{
BeginReceive();
}
}
else
{
Server.InvokeOnDisconnect(this);
}
}
}
catch (SocketException)
{
Server.InvokeOnDisconnect(this);
}
}
}
public void Send(byte[] packet)
{
if (IsAlive)
{
try
{
Socket.BeginSend(packet, 0, packet.Length, SocketFlags.None, new AsyncCallback(EndSend), null);
}
catch (SocketException)
{
Server.InvokeOnDisconnect(this);
}
}
}
private void EndSend(IAsyncResult result)
{
try
{
Socket.EndSend(result);
}
catch (SocketException)
{
Server.InvokeOnDisconnect(this);
}
}
public void Disconnect()
{
try
{
Socket.Disconnect(false);
}
catch (SocketException)
{
}
Server.InvokeOnDisconnect(this);
}
public override string ToString()
{
return RemoteEndPoint.ToString();
}
}
}
namespace KhaledMohamed.Network.Sockets
{
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using KhaledMohamed;
public delegate void NetworkClientConnection(ClientWrapper client);
public delegate void NetworkClientReceive(Byte[] buffer, Int32 length, ClientWrapper client);
public class ServerSocket
{
public Socket Socket { get; private set; }
public IPEndPoint LocalEndPoint { get; private set; }
public String Name { get; private set; }
public NetworkClientConnection OnConnect;
public NetworkClientReceive OnReceive;
public NetworkClientConnection OnDisconnect;
public BruteForceAttackProtection AttackProtector;
public Int32 ClientBufferSize;
public ServerSocket(String name, UInt32 maximum, UInt32 banTime)
{
Name = name;
AttackProtector = new BruteForceAttackProtection(maximum, banTime);
}
/*
The Set IP Protection Level method enables restricting an a IPv6 or IP socket to listen on a specified scope, such as addresses with the same link local or site local prefix. This socket option enables applications to place access restrictions on IPv6 or IP sockets. Such restrictions enable an application running on a private LAN to simply and robustly harden itself against external attacks. This socket option can also be used to remove access restrictions if the level parameter is set to Unrestricted. This socket option widens or narrows the scope of a listening socket, enabling unrestricted access from public and private users when appropriate, or restricting access only to the same site, as required.
This socket option has defined protection levels specified in the IPProtectionLevel enumeration.
The SetIPProtectionLevel method is used to enable or disable Network Address Traversal (NAT) for a Socket instance. NAT traversal may be provided using Teredo, 6to4, or an ISATAP tunnel.
When the level parameter is set to EdgeRestricted, or Restricted, this explicitly disables NAT traversal for a Socket instance.
When the level parameter is set to EdgeRestricted, this may allow NAT traversal for a Socket depending on firewall rules in place on the system.
*/
public void Prepare(int port, IPProtectionLevel protectionLevel)
{
LocalEndPoint = new IPEndPoint(IPAddress.Any, port);
Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Socket.Bind(LocalEndPoint);
Socket.SetIPProtectionLevel(protectionLevel);
Socket.NoDelay = true;
Socket.Listen((int)SocketOptionName.MaxConnections);
}
public void BeginAccept()
{
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, false);
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
Socket.BeginAccept(Accept, null);
}
private void Accept(IAsyncResult result)
{
Socket clientSocket;
try
{
clientSocket = Socket.EndAccept(result);
}
catch (SocketException)
{
BeginAccept();
return;
}
if (AttackProtector.Authenticate(clientSocket))
{
clientSocket.ReceiveBufferSize = ClientBufferSize;
var client = new ClientWrapper(this, clientSocket, ClientBufferSize);
InvokeOnConnect(client);
client.BeginReceive();
}
else
clientSocket.Disconnect(false);
BeginAccept();
}
public void InvokeOnConnect(ClientWrapper client)
{
if (OnConnect != null) OnConnect(client);
}
public void InvokeOnReceive(Byte[] buffer, Int32 length, ClientWrapper client)
{
if (OnReceive != null) OnReceive(buffer, length, client);
}
public void InvokeOnDisconnect(ClientWrapper client)
{
if (!client.IsAlive) return;
// client.IsAlive = false;
if (OnDisconnect != null) OnDisconnect(client);
}
}
}
namespace khaledmohamed.network.sockets
{
using system;
using system.collections.generic;
using system.net.sockets;
using system.runtime.interopservices;
using system.threading;
using khaledmohamed;
using system.net;
public class clientwrapper
{
public socket socket { get; private set; }
public serversocket server { get; private set; }
public ipendpoint remoteendpoint { get; private set; }
private readonly byte[] _buffer;
public object owner;
public boolean isalive { get { return socket.connected; } }
public string ip
{
get
{
if (socket != null)
return (socket.remoteendpoint as ipendpoint).address.tostring();
else
return null;
}
}
public clientwrapper(serversocket server, socket socket, int32 bufferlength)
{
//isalive = true;
server = server;
socket = socket;
_buffer = new byte[bufferlength];
remoteendpoint = (ipendpoint)socket.remoteendpoint;
socket.nodelay = true;
}
public void beginreceive()
{
try
{
socket.beginreceive(_buffer, 0, _buffer.length, socketflags.none, new asynccallback(receive), null);
}
catch (socketexception)
{
server.invokeondisconnect(this);
}
}
private void receive(iasyncresult result)
{
if (socket != null)
{
try
{
socketerror error;
int32 length = socket.endreceive(result, out error);
if (isalive && error == socketerror.success)
{
if (length > 0)
{
try
{
server.invokeonreceive(_buffer, length, this);
}
catch (exception e)
{
system.console.writeline(e.tostring());
}
finally
{
beginreceive();
}
}
else
{
server.invokeondisconnect(this);
}
}
}
catch (socketexception)
{
server.invokeondisconnect(this);
}
}
}
public void send(byte[] packet)
{
if (isalive)
{
try
{
socket.beginsend(packet, 0, packet.length, socketflags.none, new asynccallback(endsend), null);
}
catch (socketexception)
{
server.invokeondisconnect(this);
}
}
}
private void endsend(iasyncresult result)
{
try
{
socket.endsend(result);
}
catch (socketexception)
{
server.invokeondisconnect(this);
}
}
public void disconnect()
{
try
{
socket.disconnect(false);
}
catch (socketexception)
{
}
server.invokeondisconnect(this);
}
public override string tostring()
{
return remoteendpoint.tostring();
}
}
}
namespace khaledmohamed.network.sockets
{
using system;
using system.collections.concurrent;
using system.net;
using system.net.sockets;
using system.runtime.interopservices;
using system.threading;
using khaledmohamed;
public delegate void networkclientconnection(clientwrapper client);
public delegate void networkclientreceive(byte[] buffer, int32 length, clientwrapper client);
public class serversocket
{
public socket socket { get; private set; }
public ipendpoint localendpoint { get; private set; }
public string name { get; private set; }
public networkclientconnection onconnect;
public networkclientreceive onreceive;
public networkclientconnection ondisconnect;
public bruteforceattackprotection attackprotector;
public int32 clientbuffersize;
public serversocket(string name, uint32 maximum, uint32 bantime)
{
name = name;
attackprotector = new bruteforceattackprotection(maximum, bantime);
}
/*
the set ip protection level method enables restricting an a ipv6 or ip socket to listen on a specified scope, such as addresses with the same link local or site local prefix. This socket option enables applications to place access restrictions on ipv6 or ip sockets. Such restrictions enable an application running on a private lan to simply and robustly harden itself against external attacks. This socket option can also be used to remove access restrictions if the level parameter is set to unrestricted. This socket option widens or narrows the scope of a listening socket, enabling unrestricted access from public and private users when appropriate, or restricting access only to the same site, as required.
This socket option has defined protection levels specified in the ipprotectionlevel enumeration.
The setipprotectionlevel method is used to enable or disable network address traversal (nat) for a socket instance. Nat traversal may be provided using teredo, 6to4, or an isatap tunnel.
When the level parameter is set to edgerestricted, or restricted, this explicitly disables nat traversal for a socket instance.
When the level parameter is set to edgerestricted, this may allow nat traversal for a socket depending on firewall rules in place on the system.
*/
public void prepare(int port, ipprotectionlevel protectionlevel)
{
localendpoint = new ipendpoint(ipaddress.any, port);
socket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);
socket.bind(localendpoint);
socket.setipprotectionlevel(protectionlevel);
socket.nodelay = true;
socket.listen((int)socketoptionname.maxconnections);
}
public void beginaccept()
{
socket.setsocketoption(socketoptionlevel.socket, socketoptionname.keepalive, false);
socket.setsocketoption(socketoptionlevel.socket, socketoptionname.dontlinger, true);
socket.beginaccept(accept, null);
}
private void accept(iasyncresult result)
{
socket clientsocket;
try
{
clientsocket = socket.endaccept(result);
}
catch (socketexception)
{
beginaccept();
return;
}
if (attackprotector.authenticate(clientsocket))
{
clientsocket.receivebuffersize = clientbuffersize;
var client = new clientwrapper(this, clientsocket, clientbuffersize);
invokeonconnect(client);
client.beginreceive();
}
else
clientsocket.disconnect(false);
beginaccept();
}
public void invokeonconnect(clientwrapper client)
{
if (onconnect != null) onconnect(client);
}
public void invokeonreceive(byte[] buffer, int32 length, clientwrapper client)
{
if (onreceive != null) onreceive(buffer, length, client);
}
public void invokeondisconnect(clientwrapper client)
{
if (!client.isalive) return;
// client.isalive = false;
if (ondisconnect != null) ondisconnect(client);
}
}
}
الذين يشاهدون محتوى الموضوع الآن : 2 ( الأعضاء 0 والزوار 2) | |
|
الموضوع | كاتب الموضوع | المنتدى | مشاركات | آخر مشاركة |
ثغره في سورس متركس | محمودمحمدسالم | تطوير سيرفرات كونكر | 4 | 2020-05-16 11:53 AM |
مشاكل سورس متركس | محمودمحمدسالم | مشكلات السيرفيرات كونكر الشخصيه | 3 | 2020-02-18 12:46 PM |
مشكله الكلام في سورس متركس | taha | مشكلات السيرفيرات كونكر الشخصيه | 1 | 2020-01-22 10:37 PM |
ايرور في سورس متركس | Mawdo3jded | مشكلات السيرفيرات كونكر الشخصيه | 2 | 2019-12-24 09:53 PM |
فاعل خير سورس متركس بعد ما تسرق | ahmedfathy | سورسات كونكر | 5 | 2019-12-19 02:33 AM |