Browse Source

rewrote socket accept logic

pull/123/head
notgiven688 3 years ago
parent
commit
22d00e2c26
4 changed files with 136 additions and 135 deletions
  1. +1
    -1
      server/Server/Fleck/Interfaces/ISocket.cs
  2. +2
    -0
      server/Server/Fleck/Interfaces/IWebSocketServer.cs
  3. +4
    -4
      server/Server/Fleck/SocketWrapper.cs
  4. +129
    -130
      server/Server/Fleck/WebSocketServer.cs

+ 1
- 1
server/Server/Fleck/Interfaces/ISocket.cs View File

@ -40,7 +40,7 @@ namespace Fleck
bool NoDelay { get; set; }
EndPoint LocalEndPoint { get; }
Task<ISocket> Accept(Action<ISocket> callback, Action<Exception> error);
Task<ISocket> Accept(Action<ISocket> callback, Action setHandle, Action<Exception> error);
Task Send(byte[] buffer, Action callback, Action<Exception> error);
Task<int> Receive(byte[] buffer, Action<int> callback, Action<Exception> error, int offset = 0);
Task Authenticate(X509Certificate2 certificate, SslProtocols enabledSslProtocols, Action callback, Action<Exception> error);

+ 2
- 0
server/Server/Fleck/Interfaces/IWebSocketServer.cs View File

@ -28,5 +28,7 @@ namespace Fleck
public interface IWebSocketServer : IDisposable
{
void Start(Action<IWebSocketConnection> config);
void Stop();
}
}

+ 4
- 4
server/Server/Fleck/SocketWrapper.cs View File

@ -175,13 +175,13 @@ namespace Fleck
return null;
}
}
public Task<ISocket> Accept(Action<ISocket> callback, Action<Exception> error)
public Task<ISocket> Accept(Action<ISocket> callback, Action setHandle, Action<Exception> error)
{
Func<IAsyncResult, ISocket> end = r => _tokenSource.Token.IsCancellationRequested ? null : new SocketWrapper(_socket.EndAccept(r));
var task = _taskFactory.FromAsync(_socket.BeginAccept, end, null);
task.ContinueWith(t => callback(t.Result), TaskContinuationOptions.OnlyOnRanToCompletion)
.ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
task.ContinueWith(t => { setHandle(); callback(t.Result); }, TaskContinuationOptions.OnlyOnRanToCompletion)
.ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted); // wmp change
task.ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
return task;
}

+ 129
- 130
server/Server/Fleck/WebSocketServer.cs View File

@ -24,56 +24,50 @@
#define __MonoCS__
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.AccessControl;
using System.Security.Authentication;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Fleck.Helpers;
using System.Collections;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
using System.Security;
using System.Runtime.InteropServices;
using System.IO;
using System.Security.Principal;
using System.Security.AccessControl;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
using Fleck.Helpers;
namespace Fleck
{
public class WebSocketServer : IWebSocketServer
{
namespace Fleck {
public class WebSocketServer : IWebSocketServer {
private readonly string _scheme;
private readonly IPAddress _locationIP;
private Action<IWebSocketConnection> _config;
private const int BytesPerLong = 4; // 32 / 8
private const int BitsPerByte = 8;
private bool _running = true;
private const int BytesPerLong = 4; // 32 / 8
private const int BitsPerByte = 8;
public WebSocketServer(string location)
{
var uri = new Uri(location);
public WebSocketServer (string location) {
var uri = new Uri (location);
Port = uri.Port;
Location = location;
_locationIP = ParseIPAddress(uri);
_locationIP = ParseIPAddress (uri);
_scheme = uri.Scheme;
var socket = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);
if(!MonoHelper.IsRunningOnMono()){
#if __MonoCS__
#else
socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
#endif
var socket = new Socket (_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);
if (!MonoHelper.IsRunningOnMono ()) {
#if __MonoCS__
#else
socket.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
#endif
}
ListenerSocket = new SocketWrapper(socket);
ListenerSocket = new SocketWrapper (socket);
SupportedSubProtocols = new string[0];
}
@ -83,65 +77,79 @@ namespace Fleck
public X509Certificate2 Certificate { get; set; }
public SslProtocols EnabledSslProtocols { get; set; }
public IEnumerable<string> SupportedSubProtocols { get; set; }
public bool RestartAfterListenError {get; set; }
public bool RestartAfterListenError { get; set; }
public bool IsSecure
{
public bool IsSecure {
get { return _scheme == "wss" && Certificate != null; }
}
public void Dispose()
{
ListenerSocket.Dispose();
public void Dispose () {
ListenerSocket.Dispose ();
}
private IPAddress ParseIPAddress(Uri uri)
{
public void Stop() {
_running = false;
}
private IPAddress ParseIPAddress (Uri uri) {
string ipStr = uri.Host;
if (ipStr == "0.0.0.0" ){
if (ipStr == "0.0.0.0") {
return IPAddress.Any;
}else if(ipStr == "[0000:0000:0000:0000:0000:0000:0000:0000]")
{
} else if (ipStr == "[0000:0000:0000:0000:0000:0000:0000:0000]") {
return IPAddress.IPv6Any;
} else {
try {
return IPAddress.Parse(ipStr);
return IPAddress.Parse (ipStr);
} catch (Exception ex) {
throw new FormatException("Failed to parse the IP address part of the location. Please make sure you specify a valid IP address. Use 0.0.0.0 or [::] to listen on all interfaces.", ex);
throw new FormatException ("Failed to parse the IP address part of the location. Please make sure you specify a valid IP address. Use 0.0.0.0 or [::] to listen on all interfaces.", ex);
}
}
}
public void Start(Action<IWebSocketConnection> config)
{
var ipLocal = new IPEndPoint(_locationIP, Port);
ListenerSocket.Bind(ipLocal);
ListenerSocket.Listen(100);
Port = ((IPEndPoint)ListenerSocket.LocalEndPoint).Port;
FleckLog.Info(string.Format("Server started at {0} (actual port {1})", Location, Port));
if (_scheme == "wss")
{
if (Certificate == null)
{
FleckLog.Error("Scheme cannot be 'wss' without a Certificate");
public void Start (Action<IWebSocketConnection> config) {
var ipLocal = new IPEndPoint (_locationIP, Port);
ListenerSocket.Bind (ipLocal);
ListenerSocket.Listen (100);
Port = ((IPEndPoint) ListenerSocket.LocalEndPoint).Port;
FleckLog.Info (string.Format ("Server started at {0} (actual port {1})", Location, Port));
if (_scheme == "wss") {
if (Certificate == null) {
FleckLog.Error ("Scheme cannot be 'wss' without a Certificate");
return;
}
if (EnabledSslProtocols == SslProtocols.None)
{
EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
if (EnabledSslProtocols == SslProtocols.None) {
EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
//EnabledSslProtocols = SslProtocols.Tls; // changed by wmp
FleckLog.Debug("Using default TLS 1.0 security protocol.");
FleckLog.Debug ("Using default TLS 1.0 security protocol.");
}
}
ListenForClients();
ListenForClients ();
_config = config;
}
private void ListenForClients()
{
ListenerSocket.Accept(OnClientConnect, e => {
private System.Threading.ManualResetEvent acceptDone = new System.Threading.ManualResetEvent (false);
private void ListenForClients () {
// wmp
// free us from unnecessary, complicated and faulty code.
Task.Run( () => {
while (_running) {
acceptDone.Reset ();
ListenerSocket.Accept( OnClientConnect, () => acceptDone.Set(),
e => FleckLog.Error ("An error occurred while accepting a client connection", e) );
acceptDone.WaitOne ();
}
});
/*ListenerSocket.Accept(OnClientConnect, e => {
FleckLog.Error("Listener socket is closed", e);
if(RestartAfterListenError){
FleckLog.Info("Listener socket restarting");
@ -158,81 +166,72 @@ namespace Fleck
FleckLog.Error("Listener could not be restarted", ex);
}
}
});
});*/
}
private void OnClientConnect(ISocket clientSocket)
{
private void OnClientConnect (ISocket clientSocket) {
if (clientSocket == null) return; // socket closed
// experimental removed by wmp
// experimental removed by wmp
//FleckLog.Debug(String.Format("Client connected from {0}:{1}", clientSocket.RemoteIpAddress, clientSocket.RemotePort.ToString()));
//Console.WriteLine(String.Format("Client connected from {0}:{1}", clientSocket.RemoteIpAddress, clientSocket.RemotePort.ToString()));
string rep = string.Empty;
bool failed = false;
try {
rep = clientSocket.RemoteIpAddress;
Console.WriteLine("Connecting: " + rep);
}
catch{
Console.WriteLine ("Started but IP not available.");
failed = true;
}
ListenForClients();
if (failed) {
try{ clientSocket.Close (); }catch{}
try{ clientSocket.Stream.Close();}catch{}
try{ clientSocket.Dispose ();}catch{}
return;
}
//Console.WriteLine(String.Format("Client connected from {0}:{1}", clientSocket.RemoteIpAddress, clientSocket.RemotePort.ToString()));
string rep = string.Empty;
bool failed = false;
try {
rep = clientSocket.RemoteIpAddress;
Console.WriteLine ("Connecting: " + rep);
} catch {
Console.WriteLine ("Started but IP not available.");
failed = true;
}
//ListenForClients ();
if (failed) {
try { clientSocket.Close (); } catch { }
try { clientSocket.Stream.Close (); } catch { }
try { clientSocket.Dispose (); } catch { }
return;
}
WebSocketConnection connection = null;
connection = new WebSocketConnection(
connection = new WebSocketConnection (
clientSocket,
_config,
bytes => RequestParser.Parse(bytes, _scheme),
r => HandlerFactory.BuildHandler(r,
s => connection.OnMessage(s),
connection.Close,
b => connection.OnBinary(b),
b => connection.OnPing(b),
b => connection.OnPong(b)),
s => SubProtocolNegotiator.Negotiate(SupportedSubProtocols, s));
if (IsSecure)
{
FleckLog.Debug("Authenticating Secure Connection");
bytes => RequestParser.Parse (bytes, _scheme),
r => HandlerFactory.BuildHandler (r,
s => connection.OnMessage (s),
connection.Close,
b => connection.OnBinary (b),
b => connection.OnPing (b),
b => connection.OnPong (b)),
s => SubProtocolNegotiator.Negotiate (SupportedSubProtocols, s));
if (IsSecure) {
FleckLog.Debug ("Authenticating Secure Connection");
clientSocket
.Authenticate(Certificate,
EnabledSslProtocols,
() =>
{
Console.WriteLine("Authenticated {0}", rep);
Server.Firewall.Update(rep, Server.Firewall.UpdateEntry.AuthSuccess);
connection.StartReceiving();
}
,e =>
{
FleckLog.Warn("Failed to Authenticate " + rep, e);
// here we could add connection.Close() ! wmp
Server.Firewall.Update(rep, Server.Firewall.UpdateEntry.AuthFailure);
connection.Close();
});
}
else
{
Server.Firewall.Update(rep, Server.Firewall.UpdateEntry.AuthSuccess);
.Authenticate (Certificate,
EnabledSslProtocols,
() => {
Console.WriteLine ("Authenticated {0}", rep);
Server.Firewall.Update (rep, Server.Firewall.UpdateEntry.AuthSuccess);
connection.StartReceiving ();
}, e => {
FleckLog.Warn ("Failed to Authenticate " + rep, e);
// here we could add connection.Close() ! wmp
Server.Firewall.Update (rep, Server.Firewall.UpdateEntry.AuthFailure);
connection.Close ();
});
} else {
Server.Firewall.Update (rep, Server.Firewall.UpdateEntry.AuthSuccess);
connection.StartReceiving();
connection.StartReceiving ();
}
}
}
}
}

Loading…
Cancel
Save