2using System.Collections.Generic;
4using System.Net.NetworkInformation;
5using System.Net.Sockets;
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 async
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 await 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 await 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 e.
Value.Connection1?.Dispose();
205 e.
Value.Connection2?.Dispose();
207 return Task.CompletedTask;
215 this.disposed =
true;
217 this.connections?.
Clear();
219 this.connections =
null;
221 if (!(this.listeners is
null))
223 LinkedList<TcpListener> Listeners = this.listeners;
224 this.listeners =
null;
226 foreach (TcpListener Listener
in Listeners)
238 SortedDictionary<int, bool> Open =
new SortedDictionary<int, bool>();
240 if (!(this.listeners is
null))
242 IPEndPoint IPEndPoint;
244 foreach (TcpListener Listener
in this.listeners)
246 IPEndPoint = Listener.LocalEndpoint as IPEndPoint;
247 if (!(IPEndPoint is
null))
248 Open[IPEndPoint.Port] =
true;
252 int[] Result =
new int[Open.Count];
253 Open.Keys.CopyTo(Result, 0);
266 return !(this.listeners?.First is
null);
270 private async
void AcceptTcpClientCallback(IAsyncResult ar)
277 TcpListener Listener = (TcpListener)ar.AsyncState;
283 TcpClient Client = Listener.EndAcceptTcpClient(ar);
284 await this.
Information(
"Connection accepted from " + Client.Client.RemoteEndPoint.ToString() +
".");
296 Listener.BeginAcceptTcpClient(this.AcceptTcpClientCallback, Listener);
300 catch (SocketException)
304 catch (ObjectDisposedException)
308 catch (NullReferenceException)
314 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.
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.
Task 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...