4using System.Net.NetworkInformation;
6using System.Threading.Tasks;
29 private LinkedList<TcpListener> listeners =
new LinkedList<TcpListener>();
31 private bool disposed =
false;
61 this.connections.Removed += this.ClientConnections_Removed;
63 this.Initialize(Ports);
66 private void Initialize(
int[] Ports)
72 foreach (NetworkInterface Interface
in NetworkInterface.GetAllNetworkInterfaces())
74 if (Interface.OperationalStatus != OperationalStatus.Up)
77 IPInterfaceProperties Properties = Interface.GetIPProperties();
79 foreach (UnicastIPAddressInformation UnicastAddress
in Properties.UnicastAddresses)
81 if ((UnicastAddress.Address.AddressFamily == AddressFamily.InterNetwork && Socket.OSSupportsIPv4) ||
82 (UnicastAddress.Address.AddressFamily == AddressFamily.InterNetworkV6 && Socket.OSSupportsIPv6))
86 foreach (
int C2sPort
in Ports)
90 this.
Information(
"Opening port " + C2sPort.ToString() +
" on " + UnicastAddress.Address.ToString() +
".");
92 Listener =
new TcpListener(UnicastAddress.Address, C2sPort);
94 Listener.BeginAcceptTcpClient(this.AcceptTcpClientCallback, Listener);
95 this.listeners.AddLast(Listener);
97 this.
Information(
"Port " + C2sPort.ToString() +
" on " + UnicastAddress.Address.ToString() +
" opened.");
101 Log.
Exception(ex, UnicastAddress.Address.ToString() +
":" + C2sPort);
115 internal bool RegisterConnection(
string StreamId, Socks5Connection Connection)
117 if (this.connections.
TryGetValue(StreamId, out Pair P))
119 if (P.Connection2 is
null)
120 P.Connection2 = Connection;
126 this.connections[StreamId] =
new Pair()
128 Connection1 = Connection
135 internal void UnregisterStream(
string StreamId, Socks5Connection Connection)
137 if (this.connections.
TryGetValue(StreamId, out Pair P))
139 if (P.Connection1 == Connection)
143 this.connections.
Remove(StreamId);
145 P.Connection2?.CloseWhenDone();
147 else if (P.Connection2 == Connection)
151 this.connections.
Remove(StreamId);
153 P.Connection1?.CloseWhenDone();
158 internal bool TryGetRemoteEndPoint(
string StreamId, Socks5Connection Sender, out Socks5Connection Receiver)
160 if (this.connections.
TryGetValue(StreamId, out Pair P))
162 if (P.Connection1 == Sender)
163 Receiver = P.Connection2;
164 else if (P.Connection2 == Sender)
165 Receiver = P.Connection1;
172 return !(Receiver is
null);
175 internal bool Activate(
string StreamId)
177 if (!this.connections.
TryGetValue(StreamId, out Pair P))
180 if (P.Connection1 is
null ||
181 P.Connection2 is
null ||
182 !P.Connection1.WaitingForActivation ||
183 !P.Connection2.WaitingForActivation)
188 P.Connection1.Activate();
189 P.Connection2.Activate();
196 public Socks5Connection Connection1 =
null;
197 public Socks5Connection Connection2 =
null;
198 public bool Closed1 =
false;
199 public bool Closed2 =
false;
204 if (!(e.
Value.Connection1 is
null))
205 await e.
Value.Connection1.DisposeAsync();
207 if (!(e.
Value.Connection2 is
null))
208 await e.
Value.Connection2.DisposeAsync();
216 this.disposed =
true;
218 this.connections?.
Clear();
220 this.connections =
null;
222 if (!(this.listeners is
null))
224 LinkedList<TcpListener> Listeners = this.listeners;
225 this.listeners =
null;
227 foreach (TcpListener Listener
in Listeners)
239 SortedDictionary<int, bool> Open =
new SortedDictionary<int, bool>();
241 if (!(this.listeners is
null))
243 IPEndPoint IPEndPoint;
245 foreach (TcpListener Listener
in this.listeners)
247 IPEndPoint = Listener.LocalEndpoint as IPEndPoint;
248 if (!(IPEndPoint is
null))
249 Open[IPEndPoint.Port] =
true;
253 int[] Result =
new int[Open.Count];
254 Open.Keys.CopyTo(Result, 0);
267 return !(this.listeners?.First is
null);
271 private void AcceptTcpClientCallback(IAsyncResult ar)
278 TcpListener Listener = (TcpListener)ar.AsyncState;
284 TcpClient Client = Listener.EndAcceptTcpClient(ar);
297 Listener.BeginAcceptTcpClient(this.AcceptTcpClientCallback, Listener);
301 catch (SocketException)
305 catch (ObjectDisposedException)
309 catch (NullReferenceException)
315 if (this.listeners is
null)
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Implements a binary TCP Client, by encapsulating a TcpClient. It also makes the use of TcpClient safe...
void Bind()
Binds to a TcpClient that was already connected when provided to the constructor.
string RemoteEndPoint
Remote End-point of connection. This corresponds to the IP Endpoint of the remote party in normal cas...
void Continue()
Continues reading from the socket, if paused in an event handler.
Simple base class for classes implementing communication protocols.
ISniffer[] Sniffers
Registered sniffers.
void Information(string Comment)
Called to inform the viewer of something.
Module that controls the life cycle of communication.
static bool Stopping
If the system is stopping.
Class managing a connection.
Socks5Server(int[] Ports, params ISniffer[] Sniffers)
SOCKS5 server.
int[] OpenC2SPorts
C2S Ports successfully opened.
Socks5Server(params ISniffer[] Sniffers)
SOCKS5 server. Default port will be used.
Socks5Server(int Port, params ISniffer[] Sniffers)
SOCKS5 server.
const int DefaultPort
Default SOCKS5 Port (1080).
void Dispose()
IDisposable.Dispose
bool IsOpen
If the server is open and accepts incoming connections.
const int DefaultConnectionBacklog
Default Connection backlog (10).
Implements an in-memory cache.
void Dispose()
IDisposable.Dispose
bool Remove(KeyType Key)
Removes an item from the cache.
bool TryGetValue(KeyType Key, out ValueType Value)
Tries to get a value from the cache.
void Clear()
Clears the cache.
Event arguments for cache item removal events.
ValueType Value
Value of item that was removed.
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...