Пример #1
0
 static void CommandListener()
 {
     //PrintHint();
     while (true)
     {
         var cmd = Console.ReadLine();
         if (cmd == "")
         {
             continue;           //Skip blank line.
         }
         if (cmd.ToLower() == "relogin")
         {
             Login(); continue;
         }
         var cmdList = Tools00.ResolveCommand(cmd);
         if (cmdList[0].PackTotal.ToUpper() == "RUNAS")
         {
             var tempAuth = ObtainAuth();
             cmdList.RemoveAt(0);
             ServerController.Control(tempAuth, cmdList.ToArray());
         }
         else
         {
             ServerController.Control(Auth, cmdList.ToArray());
         }
         //PrintHint();
     }
 }
Пример #2
0
        public PipelineData Process(PipelineData Input)
        {
            HttpListenerRoutedContext context = Input.PrimaryData as HttpListenerRoutedContext;
            var         path0 = context.Request.Url.LocalPath.Substring(1);
            StorageItem Result;

            if (context.Request.HttpMethod == HttpMethod.Get.Method)
            {
                if (ApplicationStorage.ObtainItemFromRelativeURL(path0, out Result, false))
                {
                    if (Result.StorageItemType == StorageItemType.Folder)
                    {
                        StorageFile DefaultPage;
                        if (((StorageFolder)Result).GetFile(GlobalConfiguration.GetDefaultPage(LWMSCoreServer.TrustedInstallerAuth), out DefaultPage, false))
                        {
                            Tools00.SendFile(context, DefaultPage);
                            (Input.SecondaryData as HttpPipelineArguments).isHandled = true;
                            return(Input);
                        }
                    }
                    else
                    {
                        Tools00.SendFile(context, Result.ToStorageFile());
                        (Input.SecondaryData as HttpPipelineArguments).isHandled = true;
                        return(Input);
                    }
                }
            }
            return(Input);
        }
Пример #3
0
        public PipelineData Process(PipelineData Input)
        {
            //if (((HttpPipelineArguments)Input.SecondaryData).isHandled == true) return Input;
            HttpListenerRoutedContext context = Input.PrimaryData as HttpListenerRoutedContext;

            Trace.WriteLine(Language.Query("LWMS.ErrorResponseUnit.UnhandledRequest", "Unhandled Http Pipeline. Request: {0}", HttpUtility.UrlDecode((context).Request.RawUrl)));


            if (context.Request.HttpMethod == HttpMethod.Get.Method)
            {
                var _404 = GlobalConfiguration.GetPage404(LWMSCoreServer.TrustedInstallerAuth);
                if (File.Exists(_404))
                {
                    Tools00.SendFile(context, new FileInfo(_404), HttpStatusCode.NotFound);
                }
                else
                {
                    Tools00.SendMessage(context, "<html><body><h1>404 File Not Found</h1><hr/><p>Hosted with LWMS.</p></body></html>", HttpStatusCode.NotFound);
                }
            }
            else
            {
                Tools00.SendMessage(context, "<html><body><h1>Error 501</h1><hr/><p>Your request is not support yet by the server.</p></body></html>", HttpStatusCode.NotImplemented);
            }
            (Input.SecondaryData as HttpPipelineArguments).isHandled = true;
            return(Input);
        }
Пример #4
0
 public static void ProcessContent(HttpListenerRoutedContext content, string path)
 {
     string Content = File.ReadAllText(path);
     {
         //Process.
         StringBuilder stringBuilder = new StringBuilder(Content);
         stringBuilder = stringBuilder.Replace("{OS.Description}", RuntimeInformation.OSDescription);
         stringBuilder = stringBuilder.Replace("{Runtime.Framework}", RuntimeInformation.FrameworkDescription);
         stringBuilder = stringBuilder.Replace("{LWMS.Core.Version}", Assembly.GetAssembly(typeof(LWMSCoreServer)).GetName().Version.ToString());
         stringBuilder = stringBuilder.Replace("{LWMS.Shell.Version}", Assembly.GetEntryAssembly().GetName().Version.ToString());
         stringBuilder = stringBuilder.Replace("{LWMS.Architect}", RuntimeInformation.ProcessArchitecture.ToString());
         stringBuilder = stringBuilder.Replace("{OS.Architect}", RuntimeInformation.OSArchitecture.ToString());
         stringBuilder = stringBuilder.Replace("{DateTime.Now}", DateTime.Now.ToString());
         foreach (var item in Prefixes)
         {
             stringBuilder = stringBuilder.Replace($"{item.Key}", item.Value);
         }
         Tools00.SendMessage(content, stringBuilder.ToString());
     }
 }
Пример #5
0
        public PipelineData Process(PipelineData Input)
        {
            {
                HttpListenerRoutedContext context = Input.PrimaryData as HttpListenerRoutedContext;
                var path0 = context.Request.Url.LocalPath.Substring(1);
                if (path0.ToUpper().StartsWith("BLOGS"))
                {
                    var         path1 = path0.Substring(path0.IndexOf("/") + 1);
                    StorageFile f;
                    if (path1.ToUpper().EndsWith(".INFO"))
                    {
                    }
                    else
                    {
                        if (SharedResources.Articles.GetFile(path1, out f, false))
                        {
                            Trace.WriteLine("MDBlog>>Article:" + f.Name); if (!f.ItemPath.ToUpper().EndsWith(".MD"))
                            {
                                Tools00.SendFile(context, f);
                                (Input.SecondaryData as HttpPipelineArguments).isHandled = true;
                                return(Input);
                            }
                            var MDContnet = File.ReadAllText(f.ItemPath);
                            if (SharedResources.isMarkdownUnavailable is false)
                            {
                                MDContnet = Markdown.ToHtml(MDContnet);
                                Trace.WriteLine("MDBlog>>Article>>MD");
                            }
                            string      Title = f.Name;
                            StorageFile info;
                            if (SharedResources.Articles.GetFile(path1 + ".info", out info, false))
                            {
                                var infos = File.ReadAllLines(info.ItemPath);
                                if (infos.Length > 0)
                                {
                                    Title = infos[0];
                                }
                            }

                            var FinalContent = SharedResources.ArticleTemplate_.Replace("%Content%", MDContnet).Replace("%Title%", Title);
                            context.Response.OutputStream.Write(Encoding.UTF8.GetBytes(FinalContent));
                            (Input.SecondaryData as HttpPipelineArguments).isHandled = true;
                            return(Input);
                        }
                    }
                    {
                        Trace.WriteLine("MDBlog>>MainPage");
                        var    list         = SharedResources.Articles.GetFiles();
                        var    MainContent  = SharedResources.ArticleListTemplate_;
                        var    ItemTemplate = SharedResources.ArticleListItemTemplate_;
                        var    ItemList     = "";
                        string LinkPrefix   = "./Blogs/";
                        if (path0.ToUpper().StartsWith("BLOGS/"))
                        {
                            LinkPrefix = "./";
                        }
                        foreach (var item in list)
                        {
                            if (item.Name.ToUpper().EndsWith(".INFO"))
                            {
                                var infos = File.ReadAllLines(item.ItemPath);
                                if (infos.Length > 0)
                                {
                                    ItemList += ItemTemplate.Replace("%Title%", infos[0]).Replace("%Link%", LinkPrefix + item.Name.Substring(0, item.Name.Length - 5));
                                }
                            }
                        }
                        var FinalContent = MainContent.Replace("%Content%", ItemList).Replace("%Title%", ApplicationConfiguration.Current.GetValue("BlogTitle", "LWMS Blog"));
                        Tools00.SendMessage(context, FinalContent);
                        (Input.SecondaryData as HttpPipelineArguments).isHandled = true;
                        return(Input);
                    }
                }
                (Input.SecondaryData as HttpPipelineArguments).isHandled = false;
                return(Input);
            }
        }
Пример #6
0
        public bool Handle(HttpListenerRoutedContext context, string HttpPrefix)
        {
            var MappedDirectory = ApplicationConfiguration.Current.GetValue(HttpPrefix, null);

            if (MappedDirectory == null)
            {
                MappedDirectory = ApplicationConfiguration.Current.GetValue("*");
                if (MappedDirectory == null)
                {
                    MappedDirectory = GlobalConfiguration.GetWebSiteContentRoot(context.PipelineAuth);
                }
            }
            string CutPath = context.Request.Url.LocalPath.Substring(1).Substring(HttpPrefix.Length);
            var    path    = Path.Combine(MappedDirectory, CutPath);

            Trace.WriteLine($"Final:{CutPath}");
            if (Directory.Exists(path))
            {
                //Visit Directory
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                StringBuilder folders       = new StringBuilder();
                StringBuilder files         = new StringBuilder();
                string        Readme        = "";
                {
                    if (CutPath != "")
                    {
                        folders.Append(SharedResources.DirectoryItemFolderTemplate.Replace("%Name%", "..").Replace("%Date%", "")).Replace("%Link%", "./../");
                    }
                    foreach (var item in directoryInfo.EnumerateDirectories())
                    {
                        folders.Append(SharedResources.DirectoryItemFolderTemplate.Replace("%Name%", item.Name).Replace("%Date%", item.LastWriteTimeUtc + "")).Replace("%Link%", "./" + item.Name + "/");
                    }
                    foreach (var item in directoryInfo.EnumerateFiles())
                    {
                        if (item.Name.ToUpper() == "README")
                        {
                            Readme = File.ReadAllText(item.FullName);
                        }
                        files.Append(SharedResources.DirectoryItemFileTemplate.Replace("%Name%", item.Name).Replace("%Date%", item.LastWriteTimeUtc + "").Replace("%Size%", Math.Round(((double)item.Length) / 1024.0) + " KB")).Replace("%Link%", "./" + item.Name);
                    }
                }
                var content = SharedResources.DirectoryTemplate.Replace("%Folders%", folders.ToString()).Replace("%Files%", files.ToString()).Replace("%Address%", ToHTMLString(context.Request.Url.LocalPath)).Replace("%Readme%", Readme);
                Tools00.SendMessage(context, content);
                return(true);
            }
            else
            {
                //Visit File
                if (File.Exists(path))
                {
                    FileInfo fi = new FileInfo(path);
                    if (Tools00.ObtainMimeType(fi.Extension).StartsWith("text"))
                    {
                        var content = SharedResources.TextViewerTemplate.Replace("%Content%", ToHTMLString(File.ReadAllText(fi.FullName))).Replace("%Address%", ToHTMLString(context.Request.Url.LocalPath));
                        Tools00.SendMessage(context, content);
                        return(true);
                    }
                    else
                    {
                        Tools00.SendFile(context, fi);
                        return(true);
                    }
                }
            }
            return(false);
        }
Пример #7
0
        static void Main(string[] args)
        {
            Environment.CurrentDirectory = (new FileInfo(typeof(Program).Assembly.Location)).Directory.FullName;
            //Force working directory to where the program exists.
            {
                var Auth0 = CLUNL.Utilities.RandomTool.GetRandomString(32, CLUNL.Utilities.RandomStringRange.R3);
                var Auth1 = CLUNL.Utilities.RandomTool.GetRandomString(32, CLUNL.Utilities.RandomStringRange.R3);
                Auth = OperatorAuthentication.ObtainRTAuth(Auth0, Auth1);
                OperatorAuthentication.SetLocalHostAuth(Auth);
                LWMSCoreServer.FirstInit();
            }
            Console.WriteLine("Copyright (C) 2020-2022 Creeper Lv");
            Console.WriteLine("This software is licensed under the MIT License");
            var  _commands  = Tools00.ResolveCommand(Environment.CommandLine);
            bool ignore_Arg = false;

            _commands.RemoveAt(0);//Remove start command.
            if (_commands.Count > 0)
            {
                if (_commands[0].PackTotal.ToUpper() == "/PREBOOT")
                {
                    _commands.RemoveAt(0);
                    LWMSCoreServer.LoadCommandsFromManifest();
                    ServerController.Control(Auth, _commands.ToArray());
                    ignore_Arg = true;
                }
                else if (_commands[0].PackTotal.ToUpper() == "/NOBOOT")
                {
                    _commands.RemoveAt(0);
                    LWMSCoreServer.LoadCommandsFromManifest();
                    ServerController.Control(Auth, _commands.ToArray());
                    return;
                }
                else
                {
                }
            }
            Console.OutputEncoding = Encoding.UTF8;
            Console.InputEncoding  = Encoding.UTF8;
            LWMSTraceListener.SetProperty(Auth, 0, true);
            //LWMSTraceListener.BeautifyConsoleOutput = true;
            Console.BackgroundColor = ConsoleColor.Red;
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("This software is in active development and extremely unstable, do not use it in production environment!");
            Console.ResetColor();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("LWMS - LightWeight Managed Server");
            Console.WriteLine();
            Check00();
            LWMSCoreServer coreServer = new LWMSCoreServer();

            //coreServer.Bind("http://+:8080/");
            //string p = Configuration.BasePath;
            coreServer.Start(8);
            Console.WriteLine("The server is now running good.");
            Login();
            if (ignore_Arg == false)
            {
                if (args.Length > 0)
                {
                    var cmd = new List <CommandPack>();
                    foreach (var item in _commands)
                    {
                        cmd.Add(item);
                    }
                    ServerController.Control(Auth, cmd.ToArray());
                }
            }
            CommandListener();
        }
Пример #8
0
        public bool Handle(HttpListenerRoutedContext context, string HttpPrefix)
        {
            {
                var path0 = context.Request.Url.LocalPath.Substring(1);
                if (path0.ToUpper().StartsWith(HttpPrefix.ToUpper()))
                {
                    var path1 = path0.Substring(HttpPrefix.Length);
                    //path1 = path1.Substring(path1.IndexOf("/") + 1);
                    StorageFile f;
                    if (path1.ToUpper().EndsWith(".INFO"))
                    {
                    }
                    else
                    {
                        if (SharedResources.Articles.GetFile(path1, out f, false))
                        {
                            Trace.WriteLine("MDBlog>>Article:" + f.Name);
                            if (!f.ItemPath.ToUpper().EndsWith(".MD"))
                            {
                                Tools00.SendFile(context, f);
                                return(true);
                            }
                            var MDContnet = File.ReadAllText(f.ItemPath);
                            if (SharedResources.isMarkdownUnavailable is false)
                            {
                                MDContnet = Markdown.ToHtml(MDContnet);
                            }
                            string      Title = f.Name;
                            StorageFile info;
                            if (SharedResources.Articles.GetFile(path1 + ".info", out info, false))
                            {
                                var infos = File.ReadAllLines(info.ItemPath);
                                if (infos.Length > 0)
                                {
                                    Title = infos[0];
                                }
                            }

                            var FinalContent = SharedResources.ArticleTemplate_.Replace("%Content%", MDContnet).Replace("%Title%", Title);
                            context.Response.OutputStream.Write(Encoding.UTF8.GetBytes(FinalContent));
                            return(true);
                        }
                    }
                    {
                        Trace.WriteLine("MDBlog>>MainPage");
                        try
                        {
                            var list = SharedResources.Articles.GetFiles();

                            var MainContent  = SharedResources.ArticleListTemplate_;
                            var ItemTemplate = SharedResources.ArticleListItemTemplate_;
                            var ItemList     = "";
                            //string LinkPrefix = "./" + (HttpPrefix.Split("/").Last()) + "/";

                            //if (path0.ToUpper().StartsWith(HttpPrefix.ToUpper()))
                            var LinkPrefix = "./";
                            foreach (var item in list)
                            {
                                if (item.Name.ToUpper().EndsWith(".INFO"))
                                {
                                    var infos = File.ReadAllLines(item.ItemPath);
                                    if (infos.Length > 0)
                                    {
                                        ItemList += ItemTemplate.Replace("%Title%", infos[0]).Replace("%Link%", LinkPrefix + item.Name.Substring(0, item.Name.Length - 5));
                                    }
                                }
                            }
                            var FinalContent = MainContent.Replace("%Content%", ItemList).Replace("%Title%", ApplicationConfiguration.Current.GetValue("BlogTitle", "LWMS Blog"));
                            Tools00.SendMessage(context, FinalContent);
                            //context.Response.OutputStream.Write(Encoding.UTF8.GetBytes(FinalContent));

                            return(true);
                        }
                        catch (Exception e)
                        {
                            Trace.WriteLine(e);
                        }
                    }
                }
                return(false);
            }
        }