Esempio n. 1
0
        private void GetPort()
        {
            JcfResult <string> prt = Program.Settings.GetString($"JWS.Listener.{ListenerState}.Port");

            if (prt)
            {
                if (int.TryParse((string)prt, out int port))
                {
                    Port = port;
                    Program.Logger.LogFormatted("Listener", $"Booting server to port {port}.", LogSeverity.Message);
                }
                else
                {
                    Program.Logger.LogFormatted("Listener", $"JWS.Listener.{ListenerState}.Port config variable is not an integer. Falling back to port 1300.",
                                                LogSeverity.Warning);
                    Port = 1300;
                }
            }
            else
            {
                Program.Logger.LogFormatted("Listener", $"JWS.Listener.{ListenerState}.Port config variable doesn't exist or is incorrect (maybe your state is undefined?). " +
                                            "Falling back to port 1300.", LogSeverity.Warning);
                //StaticLogger.LogWarning(this, $"JWS.Listener.{ListenerState}.Port config variable doesn't exist (maybe your state is undefined?). Falling back to port 1300.");
                Port = 1300;
            }
        }
Esempio n. 2
0
        public void SetServerName()
        {
            JcfResult <string> name = Program.Settings.GetString("JWS.Server.Name");

            if (name)
            {
                ServerName = (string)name;
                Program.Logger.LogFormatted("Listener", $"Succesfully set Server Name to {ServerName}.", LogSeverity.Debug);
            }
            else
            {
                Program.Logger.LogFormatted("Listener", $"JWS.Server.Name is undefined or incorrectly defined. Using fallback 'JWS'.", LogSeverity.Warning);
                ServerName = "JWS";
            }
        }
Esempio n. 3
0
        private void GetState()
        {
            JcfResult <string> state = Program.Settings.GetString("JWS.Listener.State");

            if (state)
            {
                ListenerState = (string)state;
                Program.Logger.LogFormatted("Listener", $"State is {ListenerState}.", LogSeverity.Message);
            }
            else
            {
                Program.Logger.LogFormatted("Listener", $"JWS.Listener.State config variable doesn't exist or is invalid. Falling back to Debug.", LogSeverity.Warning);
                ListenerState = "Debug";
            }
        }
Esempio n. 4
0
        //private string GetCommand(string ext) {}
        public static void Load()
        {
            JcfResult <string> state = Program.Settings.GetString("JWS.Dynamic.Enabled");

            if (state)
            {
                if ((string)state == "off")
                {
                    Program.Logger.LogFormatted("_boot_dyn", "Disabling Dynamic server...", LogSeverity.Message);
                    return;
                }
                else if ((string)state != "on")
                {
                    Program.Logger.LogFormatted("_boot_dyn", "Setting JWS.Dynamic.Enabled should be either 'on' or 'off'. Assuming off.", LogSeverity.Warning);
                    return;
                }
            }
            else
            {
                Program.Logger.LogFormatted("_boot_dyn", "JWS.Dynamic.Enabled is not (correctly) set. Assuming off.", LogSeverity.Warning);
            }
        }
Esempio n. 5
0
        private List <string> GetPrefixes()
        {
            GetState();
            GetPort();

            List <string>             prefixes = new List <string>();
            JcfResult <JcfEnumerable> lst      = Program.Settings.GetMappedList($"JWS.Listener.{ListenerState}.Prefixes", "Entry");

            if (lst)
            {
                ((JcfEnumerable)lst).Enumerate((maybe, ind) => {
                    if (maybe)
                    {
                        prefixes.Add($"{(string)maybe}:{Port}/");
                        Program.Logger.LogFormatted("Listener", $"Added {(string)maybe}.", LogSeverity.Debug);
                    }
                    else
                    {
                        Program.Logger.LogFormatted("Listener", $"Prefixes element {ind}: Entry is invalid; check the type and whether or not it has an Entry key. Ignoring.", LogSeverity.Warning);
                    }
                });
            }
            else
            {
                Program.Logger.LogFormatted("Listener", $"JWS.Listener.{ListenerState}.Prefixes config variable doesn't exist or is invalid (maybe your state is undefined?). " +
                                            "Falling back to 'https://localhost'.", LogSeverity.Warning);
                prefixes.Add("https://localhost:" + Port + "/");
            }

            if (prefixes.Count == 0)
            {
                Program.Logger.LogFormatted("Listener", $"No eligible prefixes found. Falling back to 'https://localhost'.", LogSeverity.Warning);
                prefixes.Add("https://localhost:" + Port + "/");
            }

            return(prefixes);
        }
Esempio n. 6
0
        public void LoadPaths()
        {
            JcfResult <string> _html = Program.Settings.GetString("JWS.Paths.HTML");

            if (_html)
            {
                string html = ((string)_html).Replace("@Home", Program.GetHome()).Replace("@Data", Program.Data());
                if (Directory.Exists(html))
                {
                    if (html.EndsWith("/"))
                    {
                        html = html.Substring(0, html.Length - 1);
                    }
                    HTMLDir = html;
                    Program.Logger.LogFormatted("Listener", $"Set HTML dir to {html}.", LogSeverity.Message);
                    //StaticLogger.LogMessage(this, $"Set HTML dir to {html}.");
                }
                else
                {
                    Program.Logger.LogFormatted("Listener", $"HTML Dir in config file ({html}) doesn't exist. No fallback available.", LogSeverity.Error);
                    //StaticLogger.LogError(this, $"HTML Dir in config file ({html}) doesn't exist. No fallback available.");
                    Program.Instance.Exit(3);
                }
            }
            else
            {
                string pth = Program.GetHome() + "/.config/jws/html/";
                Program.Logger.LogFormatted("Listener", $"HTML Dir not configured in config file (JWS.Paths.HTML). Trying fallback {pth}.", LogSeverity.Warning);
                //StaticLogger.LogWarning(this, $"HTML Dir not configured in config file (JWS.Paths.HTML). Trying fallback {pth}.");
                try
                {
                    if (Directory.Exists(pth))
                    {
                        HTMLDir = pth;
                        Program.Logger.LogFormatted("Listener", $"Set HTML dir to {pth}.", LogSeverity.Message);
                        //StaticLogger.LogMessage("Listener", $"Set HTML dir to {pth}.");
                    }
                    else
                    {
                        Program.Logger.LogFormatted("Listener", $"Fallback HTML Dir doesn't exist. No fallback available.", LogSeverity.Error);
                        //StaticLogger.LogError("Listener", $"Fallback HTML Dir doesn't exist. No fallback available.");
                        Program.Instance.Exit(3);
                    }
                }
                catch (IOException)
                {
                    Program.Logger.LogFormatted("Listener", $"Fallback HTML Dir doesn't exist. No fallback available.", LogSeverity.Error);
                    //StaticLogger.LogError("Listener", $"Fallback HTML Dir doesn't exist. No fallback available.");
                    Program.Instance.Exit(3);
                }
            }

            JcfResult <string> _err = Program.Settings.GetString("JWS.Paths.Error");

            if (_err)
            {
                try
                {
                    string err = ((string)_err).Replace("@Home", Program.GetHome()).Replace("@Data", Program.Data());
                    if (Directory.Exists(err))
                    {
                        if (err.EndsWith("/"))
                        {
                            err = err.Substring(0, err.Length - 1);
                        }
                        ErrorDir = err;
                        Program.Logger.LogFormatted("Listener", $"Set Error dir to {err}.", LogSeverity.Message);
                        //StaticLogger.LogMessage(this, $"Set Error dir to {err}.");
                    }
                    else
                    {
                        Program.Logger.LogFormatted("Listener", $"Error Dir in config file ({err}) doesn't exist. No fallback available.", LogSeverity.Error);
                        //StaticLogger.LogError(this, $"Error Dir in config file ({err}) doesn't exist. No fallback available.");
                        Program.Instance.Exit(4);
                    }
                }
                catch (IOException)
                {
                    Program.Logger.LogFormatted("Listener", $"Error Dir in config file doesn't exist. No fallback available.", LogSeverity.Error);
                    //StaticLogger.LogError(this, $"Error Dir in config file doesn't exist. No fallback available.");
                    Program.Instance.Exit(4);
                }
            }
            else
            {
                string pth = Program.GetHome() + "/.config/jws/error/";
                Program.Logger.LogFormatted("Listener", $"Error Dir not configured in config file (JWS.Paths.Error). Trying fallback {pth}.", LogSeverity.Warning);
                //StaticLogger.LogWarning("Listener", $"Error Dir not configured in config file (JWS.Paths.Error). Trying fallback {pth}.");
                try
                {
                    if (Directory.Exists(pth))
                    {
                        ErrorDir = pth;
                        Program.Logger.LogFormatted("Listener", $"Set Error dir to {pth}.", LogSeverity.Message);
                        //StaticLogger.LogMessage("Listener", $"Set Error dir to {pth}.");
                    }
                    else
                    {
                        Program.Logger.LogFormatted("Listener", $"Fallback Error Dir doesn't exist. No fallback available.", LogSeverity.Error);
                        //StaticLogger.LogError("Listener", $"Fallback Error Dir doesn't exist. No fallback available.");
                        Program.Instance.Exit(4);
                    }
                }
                catch (IOException)
                {
                    Program.Logger.LogFormatted("Listener", $"Fallback Error Dir doesn't exist. No fallback available.", LogSeverity.Error);
                    //StaticLogger.LogError("Listener", $"Fallback Error Dir doesn't exist. No fallback available.");
                    Program.Instance.Exit(4);
                }
            }
        }
Esempio n. 7
0
        public static void Load()
        {
            Tokens = new List <string>();
            Hashes = LoadHashes();

            JcfResult <string> state = Program.Settings.GetString("JWS.ControlPanel.State");

            if (state)
            {
                if ((string)state == "off")
                {
                    Program.Logger.LogFormatted("_hook_cpanel", "Disabling ControlPanel...", LogSeverity.Message);
                    return;
                }
                else if ((string)state != "on")
                {
                    Program.Logger.LogFormatted("_hook_cpanel", "Setting JWS.ControlPanel.State should be either 'on' or 'off'. Assuming off.", LogSeverity.Warning);
                    return;
                }
            }
            else
            {
                Program.Logger.LogFormatted("_hook_cpanel", "JWS.ControlPanel.State is not (correctly) defined. Assuming 'off'.", LogSeverity.Warning);
                return;
            }

            CPath = "/cpanel/";
            JcfResult <string> path = Program.Settings.GetString("JWS.ControlPanel.URL");

            if (path)
            {
                Program.Logger.LogFormatted("_hook_cpanel", $"Set Control Panel URL to {path}.", LogSeverity.Debug);
                CPath = (string)path;
            }
            else
            {
                Program.Logger.LogFormatted("_hook_cpanel", "Control Panel URL (JWS.ControlPanel.URL) not (correctly) set. Using fallback /cpanel/.", LogSeverity.Warning);
            }

            JcfResult <string> title = Program.Settings.GetString("JWS.ControlPanel.LoginTitle");

            if (title)
            {
                Program.Logger.LogFormatted("_hook_cpanel", $"Set Control Panel Login Title to {title}", LogSeverity.Debug);
                LoginTitle = (string)title;
            }
            else
            {
                Program.Logger.LogFormatted("_hook_cpanel", "JWS.ControlPanel.LoginTitle is not (correctly) defined. Using fallback Control Panel Login.", LogSeverity.Warning);
            }

            title = Program.Settings.GetString("JWS.ControlPanel.CPanelTitle");
            if (title)
            {
                Program.Logger.LogFormatted("_hook_cpanel", $"Set Control Panel Title to {title}", LogSeverity.Debug);
                CPanelTitle = (string)title;
            }
            else
            {
                Program.Logger.LogFormatted("_hook_cpanel", "JWS.ControlPanel.CPanelTitle is not (correctly) defined. Using fallback Control Panel.", LogSeverity.Warning);
            }

            JcfResult <string> footer = Program.Settings.GetString("JWS.ControlPanel.Footer");

            if (footer)
            {
                Program.Logger.LogFormatted("_hook_cpanel", $"Set Footer to {footer}", LogSeverity.Debug);
                Footer = (string)footer;
            }
            else
            {
                Program.Logger.LogFormatted("_hook_cpanel", "JWS.ControlPanel.Footer is not (correctly) defined. Using fallback Control Panel - Only for Authorized Users.", LogSeverity.Warning);
            }

            JcfResult <Jcf> css = Program.Settings.GetBlock("JWS.ControlPanel.CSS");

            if (css)
            {
                ((Jcf)css).EnumerateKeys((key, value) => {
                    CSS[key] = value;
                    Program.Logger.LogFormatted("_css_cpanel", $"Found CSS for {key}: {value}.", LogSeverity.Debug);
                });
            }
            else
            {
                Program.Logger.LogFormatted("_hook_cpanel", "No CSS defined for Control Panel. If you want to define any, use the JWS.ControlPanel.CSS block (maybe the block is not a block?).", LogSeverity.Message);
            }

            Program.Logger.LogFormatted("_hook_cpanel", "Hooking into Comms (1 hook).", LogSeverity.Debug);
            Response.Hook(
                (req, resp) => req.Path == CPath,
                (req, resp) => GenerateCPanel(req, resp)
                );
            Program.Logger.LogFormatted("_hook_cpanel", "Hooking into Shutdown (1 hook).", LogSeverity.Debug);
            Program.Instance.OnExit += (s, e) => {
                Program.Logger.LogFormatted("_hook_cpanel", "CPanel shutting down.", LogSeverity.Debug);
                try {
                    File.WriteAllLines(Program.Data() + "/jws/hs", Hashes.JoinPairs('>'));
                }
                catch (UnauthorizedAccessException)
                {
                    Console.Error.WriteLine("Can't open hashes file " + Program.Data() + "/jws/hs.");
                }
                catch (IOException)
                {
                    Console.Error.WriteLine("Can't open hashes file " + Program.Data() + "/jws/hs.");
                }
            };
            Program.Logger.LogFormatted("_hook_cpanel", "Hooks successful!", LogSeverity.Debug);
        }
Esempio n. 8
0
        public static void Load()
        {
            JcfResult <string> state = Program.Settings.GetString("JWS.Templates.State");

            if (state)
            {
                switch ((string)state)
                {
                case "off":
                    Program.Logger.LogFormatted("_hook_template", "Deactivating templating system.", LogSeverity.Message);
                    return;

                case "on":
                    Program.Logger.LogFormatted("_hook_template", "Activating templating system.", LogSeverity.Message);
                    break;

                default:
                    Program.Logger.LogFormatted("_hook_template", "Templating state (JWS.Templates.State) incorrectly defined. Expecting on or off. Using fallback off.",
                                                LogSeverity.Warning);
                    return;
                }
            }
            else
            {
                Program.Logger.LogFormatted("_hook_template", "Templating state is not (correctly) defined (expecting in JWS.Templates.State). Using fallback off.", LogSeverity.Warning);
                return;
            }

            JcfResult <string> _path = Program.Settings.GetString("JWS.Paths.Template");
            string             path;

            if (_path)
            {
                try
                {
                    path = ((string)_path).Replace("@Home", Program.GetHome()).Replace("@Data", Program.Data());
                    if (Directory.Exists(path))
                    {
                        if (path.EndsWith("/"))
                        {
                            path = path.Substring(0, path.Length - 1);
                        }
                        TemplateDir = path;
                        Program.Logger.LogFormatted("_hook_template", $"Set Template Directory to {path}.", LogSeverity.Message);
                    }
                    else
                    {
                        Program.Logger.LogFormatted("_hook_template", $"Template dir in config file ({path}) doesn't exist. No fallback available.", LogSeverity.Error);
                        Program.Instance.Exit(3);
                    }
                }
                catch (IOException)
                {
                    Program.Logger.LogFormatted("Listener", $"Template dir not defined in config file. No fallback available.", LogSeverity.Error);
                    Program.Instance.Exit(3);
                }
            }
            else
            {
                string pth = Program.GetHome() + "/.config/jws/template";
                Program.Logger.LogFormatted("_hook_template", $"Template Dir not configured in config file (JWS.Paths.Template). Trying fallback {pth}.",
                                            LogSeverity.Warning);
                try
                {
                    if (Directory.Exists(pth))
                    {
                        TemplateDir = pth;
                        Program.Logger.LogFormatted("_hook_template", $"Set Template Dir to {pth}.", LogSeverity.Message);
                    }
                    else
                    {
                        Program.Logger.LogFormatted("_hook_template", $"Fallback Template Dir doesn't exist. No fallback available.", LogSeverity.Error);
                        Program.Instance.Exit(3);
                    }
                }
                catch (IOException)
                {
                    Program.Logger.LogFormatted("_hook_template", $"Fallback Template Dir doesn't exist. No fallback available.", LogSeverity.Error);
                    Program.Instance.Exit(3);
                }
            }

            JcfResult <string> temp = Program.Settings.GetString("JWS.Templates.Assoc");

            if (temp)
            {
                MIMETemplate = (string)temp;
                Program.Logger.LogFormatted("_hook_template", $"Template MIME type successfully set to {MIMETemplate}.", LogSeverity.Message);
            }
            else
            {
                MIMETemplate = "text/jwstemplate";
                Program.Logger.LogFormatted("_hook_template", "Template MIME type (JWS.Templates.Assoc) not (correctly) defined. Using fallback text/jwstemplate.", LogSeverity.Warning);
            }

            JcfResult <string> data = Program.Settings.GetString("JWS.Templates.Data");

            if (temp)
            {
                MIMEData = (string)data;
                Program.Logger.LogFormatted("_hook_template", $"Template data MIME type successfully set to {MIMEData}.", LogSeverity.Message);
            }
            else
            {
                MIMETemplate = "text/jwsdata";
                Program.Logger.LogFormatted("_hook_template", "Template data MIME type (JWS.Templates.Data) not (correctly) defined. Using fallback text/jwsdata.", LogSeverity.Warning);
            }

            JcfResult <string> prefix = Program.Settings.GetString("JWS.Templates.Prefix");

            if (prefix)
            {
                TemplatePrefix = (string)prefix;
                Program.Logger.LogFormatted("_hook_template", $"Set prefix to {(string)prefix}.", LogSeverity.Debug);
            }
            else
            {
                TemplatePrefix = "$";
                Program.Logger.LogFormatted("_hook_template", "Prefix not (correctly) defined (JWS.Templates.Prefix). Using fallback $.", LogSeverity.Warning);
            }

            JcfResult <string> index = Program.Settings.GetString("JWS.Templates.Indexing");

            if (index)
            {
                Indexing = (string)index;
                Program.Logger.LogFormatted("_hook_template", $"Set indexing operator to {index}.", LogSeverity.Debug);
            }
            else
            {
                Indexing = "::";
                Program.Logger.LogFormatted("_hook_template", "Indexing operator not (correctly) defined (JWS.Templates.Indexing). Using fallback ::.", LogSeverity.Warning);
            }

            Program.Logger.LogFormatted("_hook_template", "Attempting to hook into Comms (2 hooks).", LogSeverity.Debug);
            Response.Hook(
                ((req, resp) => resp.MIMEType == MIMETemplate),
                ((req, resp) => {
                Program.Logger.LogFormatted("Template", $"Client requested template page {req.Path}. Blocking.", LogSeverity.Message);
                resp.MIMEType = "text/html";
                resp.Content = resp.LoadError(403);
                resp.StatusCode = 403;
            })
                );
            Program.Logger.LogFormatted("_hook_template", "Added MIME block hook.", LogSeverity.Debug);
            Response.Hook(
                ((req, resp) => resp.MIMEType == MIMEData),
                ((req, resp) => Fill(req, resp))
                );
            Program.Logger.LogFormatted("_hook_template", "Added MIME translation hook.", LogSeverity.Debug);
            Program.Logger.LogFormatted("_hook_template", "Templating hooked!", LogSeverity.Message);
        }