Exemple #1
0
        private void WaitForConnection(object objListener)
        {
            while (running)
            {
                HttpListenerContext context;

                try
                {
                    context = listener.GetContext();
                }
                catch (HttpListenerException)
                {
                    // Occurs when we stop the listener.
                    break;
                }
                catch (Exception ex)
                {
                    ServerException?.Invoke(this, new ServerExceptionEventArgs()
                    {
                        Exception = ex
                    });
                    // Other exceptions should be handled elsewhere.
                    break;
                }

                if (context.Request.RawUrl.StartsWith("/.well-known/acme-challenge"))
                {
                    string challengeFile = context.Request.RawUrl.RightOfRightmostOf('/');

                    if (File.Exists(challengeFile))
                    {
                        string data = File.ReadAllText(challengeFile);

                        context.Response.StatusCode      = 200;
                        context.Response.ContentType     = "text/text";
                        context.Response.ContentEncoding = Encoding.UTF8;

                        byte[] byteData = Encoding.ASCII.GetBytes(data);
                        context.Response.ContentLength64 = byteData.Length;
                        context.Response.OutputStream.Write(byteData, 0, byteData.Length);
                    }
                }

                context.Response.Close();
            }
        }
Exemple #2
0
        private static void BeginRead(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            try
            {
                // If we're not connected, stop reading //
                if (!_instanceNetworkManager.Socket.Connected)
                {
                    return;
                }

                var streamReader = _instanceNetworkManager.Client.GetStream();
                var chunky       = string.Empty;

                while (streamReader.CanRead)
                {
                    var bytes = new byte[short.MaxValue * 2];

                    var length = streamReader.Read(bytes, 0, short.MaxValue * 2);

                    var packet = Encoding.UTF8.GetString(bytes).Substring(0, length);

                    chunky += packet;

                    var chunks = chunky.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

                    var last = chunks.Last();

                    if (!last.EndsWith("}"))
                    {
                        // If it's not a valid json, we assuming its not finished //
                        chunky = last;
                        chunks = chunks.Take(chunks.Length - 1).ToArray();
                    }
                    else
                    {
                        chunky = string.Empty;
                    }

                    try
                    {
                        PacketListener?.Invoke(chunks);
                    }
                    catch (Exception e)
                    {
                        // Ignore
                    }
                }
            }
            catch (Exception e)
            {
                Disconnect();

                try
                {
                    ServerException?.Invoke(e);
                }
                catch
                {
                    // Ignore
                }
            }
        }