Ejemplo n.º 1
0
 public FireHTTPServer(ServerSettings serverSettings)
     : base(serverSettings.ListenPort)
 {
     //Set members
     IPAddress hostAddress;
     _serverSettings = serverSettings;
     //Build deny list
     _denyFileNames = new[] {CommonVariables.ConfigFileName};
     //Initialize Server
     var port = Port;
     VirtualHosts = _serverSettings.VirtualHosts;
     VirtualHosts.ToList().ForEach(vhost => vhost.DocumentRoot = Path.GetFullPath(vhost.DocumentRoot));
     //Get full path of document root
     VirtualHosts.Select(vhost => vhost.DocumentRoot)
         .Select(
             wwwroot =>
                 wwwroot.EndsWith(_dirSep, StringComparison.CurrentCulture)
                     ? wwwroot.Substring(0, wwwroot.Length - 1)
                     : wwwroot); //Remove trailing slash
     _documentRootCache = VirtualHosts.ToDictionary(vhost => vhost.ServerName, vhost => vhost.DocumentRoot);
     var isIp = IPAddress.TryParse(_serverSettings.ListenAddress, out hostAddress);
     if (!isIp)
     {
         CommonName = _serverSettings.ListenAddress;
         hostAddress = Dns.GetHostEntry(_serverSettings.ListenAddress).AddressList[0];
         //.MapToIPv6(); MapToIPv6 doesn't always work on Mono
     }
     ListenAddress = hostAddress;
     var hostEndpoint = new IPEndPoint(hostAddress, port);
     _hostController = new FireHTTPHostController();
     _loadedHookPlugins = new List<AvailableHookPlugin>();
     _loadedScriptLangPlugins = new List<AvailableLangPlugin>();
     _executableFileTypes = new List<string>();
     _scriptInterpreterCache = new Dictionary<string, IFireHTTPScriptingLang>();
     GlobalDynamicObjectCache = new ExpandoObject();
     LoadPluginsIntoServer();
     Logger.WriteLine($"Initialized FireHTTPServer listening on {hostAddress}:{port}");
     _currentHookExecutionScope = new ExpandoObject();
 }
Ejemplo n.º 2
0
 public static void LaunchServer(string[] args)
 {
     var singleInstance = false;
     string singleInstanceHost = null;
     string singleInstanceDocRoot = null;
     if (args.Length == 3)
     {
         Console.WriteLine(
             @"[Single Instance Mode] SINGLE INSTANCE MODE HAS BEEN DEPRECATED! Please use environment variables in the configuration file instead.");
         singleInstance = args[0] == "-singleinstance";
         singleInstanceHost = args[1];
         singleInstanceDocRoot = args[2];
     }
     Console.WriteLine($"FireHTTPServer v{ServerVersion}");
     Console.WriteLine(Resources.CopyrightText);
     var dirSep = Path.DirectorySeparatorChar;
     AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; //Register process exit
     _serverList = new List<FireHTTPServer>();
     //Load configuration
     const string fireHttpConfFileName = "firehttp.json";
     if (!File.Exists(fireHttpConfFileName))
     {
         GenerateConfigFile(fireHttpConfFileName);
     }
     var configJson = ReplaceEnvironmentVariables(File.ReadAllText(fireHttpConfFileName));
     var serverconfigurations = JsonConvert.DeserializeObject<ServerSettings[]>(configJson);
     //Start servers normally
     if (!singleInstance)
     {
         foreach (var serverConfiguration in serverconfigurations)
         {
             Logger.EnableLogging = serverConfiguration.EnableLogging;
             var httpServer = new FireHTTPServer(serverConfiguration);
             _serverList.Add(httpServer);
             SpawnNewThread(httpServer.StartListener);
         }
     }
     else
     {
         //Start in singleInstance mode
         var addressParts = singleInstanceHost.Split(':');
         var serverConfiguration = new ServerSettings
         {
             EnableLogging = true,
             ListenAddress = addressParts[0],
             ListenPort = int.Parse(addressParts[1]),
             VirtualHosts = new[]
             {
                 new VirtualHost
                 {
                     DocumentRoot = singleInstanceDocRoot,
                     ServerName = "*"
                 }
             },
             SingleInstance = true,
             PluginSettings = new Dictionary<string, PluginSettings>(),
             LoadPluginsExplicitly = false
         };
         var httpServer = new FireHTTPServer(serverConfiguration);
         _serverList.Add(httpServer);
         SpawnNewThread(httpServer.StartListener);
     }
 }
Ejemplo n.º 3
0
 private static void GenerateConfigFile(string configFileName)
 {
     var defaultserverconfig = new ServerSettings
     {
         ListenAddress = "0.0.0.0",
         ListenPort = 4000,
         VirtualHosts = new[]
         {
             new VirtualHost
             {
                 DocumentRoot = "www",
                 ServerName = "*"
             }
         },
         EnableLogging = true
     };
     var serverconfigurations = new[] {defaultserverconfig};
     var configJson = JsonConvert.SerializeObject(serverconfigurations, Formatting.Indented);
     File.WriteAllText(configFileName, configJson);
 }