Ejemplo n.º 1
0
 private Task SendBytes(byte[] bytes, Action callback = null)
 {
     return(Socket.Send(bytes, () =>
     {
         FleckLog.Debug("Sent " + bytes.Length + " bytes");
         Log.Insert(DateTime.Now, "WebSocketConnection.cs", string.Format("Sent: {0} bytes", bytes.Length), "white");
         if (callback != null)
         {
             callback();
         }
     },
                        e =>
     {
         if (e is IOException)
         {
             FleckLog.Debug("Failed to send. Disconnecting.", e);
             Log.Insert(DateTime.Now, "WebSocketConnection.cs", string.Format("Failed to send. Disconnecting: {0}", e), "white");
         }
         else
         {
             FleckLog.Info("Failed to send. Disconnecting.", e);
             Log.Insert(DateTime.Now, "WebSocketConnection.cs", string.Format("Failed to send. Disconnecting: {0}", e), "white");
         }
         CloseSocket();
     }));
 }
Ejemplo n.º 2
0
 //private Task SendBytes
 private void SendBytes(byte[] bytes, Action callback = null)
 {
     //return
     Socket.Send(
         bytes,
         (i) =>
     {
         FleckLog.Debug("Sent " + bytes.Length + " bytes");
         if (callback != null)
         {
             callback();
         }
     },
         e =>
     {
         if (e is IOException)
         {
             FleckLog.Debug("Failed to send. Disconnecting.", e);
         }
         else
         {
             FleckLog.Info("Failed to send. Disconnecting.", e);
         }
         CloseSocket();
     }
         );
 }
Ejemplo n.º 3
0
        public void Send(byte[] message, string logMessage, Action successCallback = null)
        {
            Socket.Send(message,
                        () =>
            {
                FleckLog.Debug("Sent " + message.Length + " bytes. " + logMessage);
                if (successCallback != null)
                {
                    successCallback();
                }
            },
                        e =>
            {
                if (e is IOException)
                {
                    FleckLog.Debug("Failed to send. Disconnecting. " + logMessage, e);
                }
                else
                {
                    FleckLog.Info("Failed to send. Disconnecting. " + logMessage, e);
                }

                Close();
            });
        }
Ejemplo n.º 4
0
 private void ListenForClients()
 {
     ListenerSocket.Accept(
         OnClientConnect,
         e => {
         FleckLog.Error("Listener socket is closed", e);
         if (RestartAfterListenError)
         {
             FleckLog.Info("Listener socket restarting");
             try
             {
                 ListenerSocket.Dispose();
                 var socket     = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);
                 ListenerSocket = new SocketWrapper(socket);
                 Start(_config);
                 FleckLog.Info("Listener socket restarted");
             }
             catch (Exception ex)
             {
                 FleckLog.Error("Listener could not be restarted", ex);
             }
         }
     }
         );
 }
Ejemplo n.º 5
0
        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.Tls;
                    FleckLog.Debug("Using default TLS 1.0 security protocol.");
                }
            }
            ListenForClients();
            _config = config;
        }
Ejemplo n.º 6
0
        public void Start(Action <IWebSocketConnection> config)
        {
            if (CheckIfPortAvailable(Port))
            {
                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.Tls;
                        FleckLog.Debug("Using default TLS 1.0 security protocol.");
                    }
                }
                ListenForClients();
                _config = config;
            }
            else
            {
                FleckLog.Error(string.Format("Server cannot start port {0}, port already in use", Port));
                throw new Exception("Error: listen EADDRINUSE: address already in use :::" + Port);
            }
        }
Ejemplo n.º 7
0
        public void Start()
        {
            ListenerSocket.Bind(_ipEndPoint);
            ListenerSocket.Listen(100);

            FleckLog.Info("Policy-Server started at " + Location);

            ListenForClients();
        }
Ejemplo n.º 8
0
        //when new ts file generated
        public void OnFileHasCome(object sender, FileInfoEventArg arg)
        {
            var arrayBits = arg.GetfileBytes();

            Socket.Send(arrayBits, () =>
            {
                FleckLog.Info("send " + arrayBits.Length + " bytes");
            }, e =>
            {
                FleckLog.Error("send error" + e.Message);
            });
        }
Ejemplo n.º 9
0
        private void HandleWriteError(Exception e)
        {
            if (e is IOException)
            {
                FleckLog.Debug("Failed to send. Disconnecting.", e);
            }
            else
            {
                FleckLog.Info("Failed to send. Disconnecting.", e);
            }

            CloseSocket();
        }
Ejemplo n.º 10
0
 private void TryRestart()
 {
     FleckLog.Info("Listener socket restarting");
     try {
         ListenerSocket.Dispose();
         var socket = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);
         ListenerSocket = new SocketWrapper(socket);
         Start(_config);
         FleckLog.Info("Listener socket restarted");
     } catch (Exception ex) {
         FleckLog.Error("Listener socket could not be restarted", ex);
     }
 }
Ejemplo n.º 11
0
        public void Start(Action <IWebSocketConnection> config)
        {
            var ipLocal = new HostName(Location);

            ListenerSocket.Bind(ipLocal, Port.ToString());
            ListenerSocket.Listen(100);
            FleckLog.Info("Server started at " + Location);
            if (_scheme == "wss")
            {
                FleckLog.Error("Scheme cannot be 'wss' without a Certificate");
                return;
            }
            ListenForClients();
            _config = config;
        }
Ejemplo n.º 12
0
 private void SendBytes(byte[] bytes, Action callback = null)
 {
     Socket.Send(bytes, () =>
     {
         FleckLog.Debug("Sent " + bytes.Length + " bytes");
         if (callback != null)
         {
             callback();
         }
     },
                 e =>
     {
         FleckLog.Info("Failed to send. Disconnecting.", e);
         CloseSocket();
     });
 }
Ejemplo n.º 13
0
        public void Start(Action <IWebSocketConnection> config)
        {
            var ipLocal = new IPEndPoint(IPAddress.Any, Port);

            ListenerSocket.Bind(ipLocal);
            ListenerSocket.Listen(100);
            FleckLog.Info("Server started at " + Location);
            if (_scheme == "wss")
            {
                if (Certificate == null)
                {
                    FleckLog.Warn("No certificate loaded, only ws:// (and not wss://) connections will be accepted");
                }
            }
            ListenForClients();
            _config = config;
        }
Ejemplo n.º 14
0
        public void Start(Action <IWebSocketConnection> config)
        {
            var ipLocal = new IPEndPoint(IPAddress.Any, Port);

            ListenerSocket.Bind(ipLocal);
            ListenerSocket.Listen(100);
            FleckLog.Info("Server started at " + Location);
            if (_scheme == "wss")
            {
                if (Certificate == null)
                {
                    FleckLog.Error("Scheme cannot be 'wss' without a Certificate");
                    return;
                }
            }
            ListenForClients();
            _config = config;
        }
Ejemplo n.º 15
0
        public void Start(Action <IWebSocketConnection> config)
        {
            var ipLocal    = new IPEndPoint(IPAddress.Any, Port);
            int actualPort = ListenerSocket.Bind(ipLocal);

            ListenerSocket.Listen(100);
            FleckLog.Info(_scheme + " server started at " + Location + (actualPort != Port ? (" (port " + actualPort + ")") : ""));
            SocketBound(this, "Web Socket Server listening on port " + actualPort + " (" + _scheme + ")");
            if (_scheme == "wss")
            {
                if (Certificate == null)
                {
                    FleckLog.Error("Scheme cannot be 'wss' without a Certificate");
                    return;
                }
            }
            ListenForClients();
            _config = config;
        }
Ejemplo n.º 16
0
        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($"Server started at {Location} (actual port {Port})");
            if (_scheme == "wss")
            {
                if (Certificate == null)
                {
                    FleckLog.Error("Scheme cannot be 'wss' without a Certificate");
                    return;
                }
            }
            ListenForClients();
            _config = config;
        }
Ejemplo n.º 17
0
        private void OnClientConnect(ISocket clientSocket)
        {
            FleckLog.Debug(String.Format("Policy-Client connected from {0}:{1}", clientSocket.RemoteIpAddress, clientSocket.RemotePort.ToString()));
            ListenForClients();

            var connection = new SocketConnection(clientSocket);

            connection.OnMessage = (message) =>
            {
                if (_regex.IsMatch(message))
                {
                    connection.Send(_policy, "policy", connection.Close);
                }
                else
                {
                    FleckLog.Info("Policy-Server invalid request: " + message);
                    connection.Close();
                }
            };
            connection.StartReceiving();
        }
Ejemplo n.º 18
0
        private void OnClientConnect(ISocket clientSocket)
        {
            FleckLog.Debug(String.Format("Client connected from {0}:{1}", clientSocket.RemoteIpAddress, clientSocket.RemotePort.ToString()));
            ListenForClients();

            WebSocketConnection connection = null;

            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));

            // Determine whether this is a ws or wss connection
            try
            {
                // Wait up to 5 seconds for the first handshake byte
                // (Only peek, so it's still in the buffer for the actual handshake handler)
                byte[] buffer = new byte[1];
                clientSocket.Socket.ReceiveTimeout = 5000;
                int BytesRead = clientSocket.Socket.Receive(buffer, 1, SocketFlags.Peek);
                clientSocket.Socket.ReceiveTimeout = 0;
                if (BytesRead == 1)
                {
                    if ((buffer[0] == 0x16) || (buffer[0] == 0x80))
                    {
                        // wss connection, ensure we have a certificate
                        if (IsSecure)
                        {
                            FleckLog.Info("Accepting wss:// Connection");
                            clientSocket
                            .Authenticate(Certificate,
                                          connection.StartReceiving,
                                          e =>
                            {
                                FleckLog.Warn("Failed to Authenticate", e);
                                connection.Close();
                            });
                        }
                        else
                        {
                            FleckLog.Warn("Rejecting wss:// connection (no certificate)");
                            connection.Close();
                        }
                    }
                    else
                    {
                        // ws connection
                        FleckLog.Info("Accepting ws:// Connection");
                        connection.StartReceiving();
                    }
                }
                else
                {
                    connection.Close();
                }
            }
            catch (Exception ex)
            {
                FleckLog.Error("Unable to read handshake byte from client", ex);
                connection.Close();
            }
        }