예제 #1
0
        public static void SetSource(dynamic p)
        {
            if (p == null)
            {
                throw new ArgumentNullException(nameof(p));
            }

            SourcePath = PathResolver.Build((string)p.Source);
            if (!SourcePath[SourcePath.Length - 1].Equals(System.IO.Path.DirectorySeparatorChar))
            {
                SourcePath += System.IO.Path.DirectorySeparatorChar;
            }
            SettingsFilePath = SourcePath + FilePath.Build("root", "settings.json");
            BasePath         = SourcePath;// + $"base{System.IO.Path.DirectorySeparatorChar}";
            RootPath         = BasePath + $"root{System.IO.Path.DirectorySeparatorChar}";
            ResourcePath     = BasePath + $"res{System.IO.Path.DirectorySeparatorChar}";
            FeatureSetPath   = p.FeaturesSet == null ? null : (string)p.FeaturesSet;
            Features         = new System.Collections.Generic.Dictionary <string, Feature>();
            if (FeatureSetPath != null)
            {
                ProcessorArchitecture arch = Assembly.GetExecutingAssembly().GetName().ProcessorArchitecture;
                if (arch == ProcessorArchitecture.MSIL)
                {
                    System.Runtime.InteropServices.Architecture osarch = System.Runtime.InteropServices.RuntimeInformation.OSArchitecture;
                    arch = osarch == System.Runtime.InteropServices.Architecture.Arm ? ProcessorArchitecture.Arm :
                           osarch == System.Runtime.InteropServices.Architecture.X64 ? ProcessorArchitecture.Amd64 :
                           osarch == System.Runtime.InteropServices.Architecture.X86 ? ProcessorArchitecture.X86 : ProcessorArchitecture.X86;
                }
                dynamic features = Newtonsoft.Json.JsonConvert.DeserializeObject(System.IO.File.ReadAllText(FeatureSetPath + "config.json"));
                foreach (dynamic feature in features)
                {
                    ProcessorArchitecture featureArch = GetArchitecture(((string)feature.Architecture).Trim(), arch);
                    if (!arch.Equals(featureArch))
                    {
                        if (arch == ProcessorArchitecture.Amd64 && featureArch == ProcessorArchitecture.X86)
                        {
                            ;
                        }
                        else if ((arch == ProcessorArchitecture.Amd64 || arch == ProcessorArchitecture.X86) && Environment.Is64BitProcess)
                        {
                            ;
                        }
                        else
                        {
                            ErrorLogger.Warn("[Settings] : Feature '" + (string)feature.Name + "' Unknown architecture '" + arch + "', feature dropped >> " + featureArch.ToString());
                            continue;
                        }
                    }


                    Feature f = new()
                    {
                        Name = (string)feature.Name,
                        Path = FeatureSetPath + (string)feature.Name + System.IO.Path.DirectorySeparatorChar + (
                            featureArch == ProcessorArchitecture.Amd64 ? "x64" + System.IO.Path.DirectorySeparatorChar :
                            featureArch == ProcessorArchitecture.X86 ? "x86" + System.IO.Path.DirectorySeparatorChar :
                            featureArch == ProcessorArchitecture.Arm ? "arm" + System.IO.Path.DirectorySeparatorChar : null
                            ),
                        TargetName           = (string)feature.Target,
                        RequireServerIOFocus = (bool)feature.RequireServerIOFocus,
                        WaitForExit          = (bool)feature.WaitForExit
                    };

                    f.TargetExcutable = f.Path + f.TargetName;

                    if (!System.IO.File.Exists(f.TargetExcutable) || f.Path == null)
                    {
                        ErrorLogger.Warn("[Settings] : Feature '" + (string)feature.Name + "' Target file not found, won't be available");
                        continue;
                    }
                    if (!(bool)feature.Enabled)
                    {
                        continue;
                    }
                    if (Features.ContainsKey(f.Name))
                    {
                        ErrorLogger.Warn("[Settings] : Feature '" + (string)feature.Name + "' already defined. duplicates ignored");
                        continue;
                    }

                    Features.Add(f.Name, f);
                }

                if (!Features.ContainsKey("cpython"))
                {
                    throw new Exception("Cannot initialize feature-set without cpython");
                }
            }
        }
예제 #2
0
        private void LoadSettings()
        {
            try
            {
                string lines = System.IO.File.ReadAllText(SettingsFilePath, System.Text.Encoding.UTF8);
                if (string.IsNullOrEmpty(lines))
                {
                    throw new System.IO.IOException("failed to read file:" + SettingsFilePath);
                }

                Current = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(lines);

                if (Current.MainPage == null)
                {
                    throw new InvalidPropertyValue("[Settings] : settings.json must contains main-page property");
                }

                if (Current.FileNotFoundPage == null)
                {
                    throw new InvalidPropertyValue("[Settings] : settings.json must contains fnf-page property");
                }

                if (Current.WebSocketIdelChances == null || Current.WebSocketIdelChances == null)
                {
                    throw new InvalidPropertyValue("[Settings] : settings.json must contains WebSocketIdleTimeout and WebSocketIdelChances");
                }

                Current.MainPage         = BasePath + ((string)Current.MainPage).Replace('/', System.IO.Path.DirectorySeparatorChar);
                Current.FileNotFoundPage = BasePath + ((string)Current.FileNotFoundPage).Replace('/', System.IO.Path.DirectorySeparatorChar);
                RepositoriesRules?.Clear();

                foreach (string repositoryRule in Current.Repos)
                {
                    string ruleFile = ResourcePath + $"{repositoryRule}{System.IO.Path.DirectorySeparatorChar}.rules";
                    if (!System.IO.File.Exists(ruleFile))
                    {
                        ErrorLogger.Warn("[Settings] : Repository '" + repositoryRule + "' has no .rules file which might cause issues, won't be available");
                        continue;
                    }
                    AppendRule(repositoryRule, ruleFile);
                }



                if ((bool)Current.AllowSocketControlFlow && Current.SocketControlFlow != null)
                {
                    if (SocketControlFlow.Count > 0)
                    {
                        SocketControlFlow.Clear();
                    }

                    foreach (dynamic item in Current.SocketControlFlow)
                    {
                        if (!(bool)item.Enabled)
                        {
                            continue;
                        }

                        SocketControlFlow controlFlow = new SocketControlFlow();
                        if (controlFlow.Setup((string)item.Filter, BasePath + ((string)item.Target).Replace('/', System.IO.Path.DirectorySeparatorChar)))
                        {
                            SocketControlFlow.Add(controlFlow);
                        }
                    }
                }

                if ((bool)Current.EnableVirtualLinks && Current.VirtualLinks != null)
                {
                    if (VirtualLinks.Count > 0)
                    {
                        VirtualLinks.Clear();
                    }

                    foreach (dynamic vlink in Current.VirtualLinks)
                    {
                        if ((bool)vlink.Dupricated)
                        {
                            continue;
                        }


                        VirtualLinks virtualLink = new();
                        virtualLink.Enabled = (bool)vlink.Enabled;
                        virtualLink.Link    = (string)vlink.Link;
                        virtualLink.Target  = (string)vlink.Target;

                        VirtualLinks.Add(virtualLink.Link, virtualLink);
                    }
                }

                ReceiveTimeout = (Current?.ReceiveTimeout != null) ? (int)Current?.ReceiveTimeout : System.Threading.Timeout.Infinite;
                SendTimeout    = (Current.SendTimeout != null) ? (int)Current.SendTimeout : System.Threading.Timeout.Infinite;



                IsReady = true;
            }
            catch (Exception exp)
            {
                if (Current == null)
                {
                    ErrorLogger.WithTrace(string.Format("[Warning][Server error => LoadSettings()] : exception-message : {0}.\nstacktrace : {1}\n", exp.Message, exp.StackTrace), GetType());
                }
                else
                {
                    ErrorLogger.WithTrace(this, string.Format("[Warning][Server error => LoadSettings()] : exception-message : {0}.\nstacktrace : {1}\n", exp.Message, exp.StackTrace), GetType());
                }

                IsReady = false;
            }
        }