Ejemplo n.º 1
0
        /// <summary>
        /// Called when the file is requested by a client.
        /// </summary>
        /// <param name="request">The <see cref="HttpRequest"/> requesting the file.</param>
        /// <param name="directory">The <see cref="IDirectory"/> of the parent directory.</param>
        public void OnFileRequested(HttpRequest request, IDirectory directory)
        {
            ICollection dirs;
            ICollection files;

            try
            {
                dirs  = directory.GetDirectories();
                files = directory.GetFiles();
            }
            catch (UnauthorizedAccessException)
            {
                throw new HttpRequestException("403");
            }

            request.Response.BeginChunkedOutput();
            StreamWriter writer = new StreamWriter(request.Response.ResponseContent);

            writer.WriteLine("<html>");
            writer.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
            writer.WriteLine("<head><title>Index of " + HttpWebServer.GetDirectoryPath(directory) + "</title></head>");
            writer.WriteLine("<body>");

            PrintBody(writer, request, directory, dirs, files);

            writer.WriteLine("<hr>" + request.Server.ServerName);
            writer.WriteLine("</body></html>");

            writer.WriteLine("</body>");
            writer.WriteLine("</html>");
            writer.Flush();
        }
Ejemplo n.º 2
0
        internal virtual void PrintBody(StreamWriter writer,
                                        HttpRequest request,
                                        IDirectory directory,
                                        ICollection dirs,
                                        ICollection files
                                        )
        {
            writer.WriteLine("<h2>Index of " + HttpWebServer.GetDirectoryPath(directory) + "</h2>");

            if (directory.Parent != null)
            {
                writer.WriteLine("<a href=\"..\">[..]</a><br>");
            }

            foreach (IDirectory dir in dirs)
            {
                //if(dir is IPhysicalResource)
                //	if((File.GetAttributes((dir as IPhysicalResource).Path) & FileAttributes.Hidden) != 0)
                //		continue;

                writer.WriteLine("<a href=\"" + UrlEncoding.Encode(dir.Name) + "/\">[" + dir.Name + "]</a><br>");
            }

            foreach (IFile file in files)
            {
                //if(file is IPhysicalResource)
                //	if((File.GetAttributes((file as IPhysicalResource).Path) & FileAttributes.Hidden) != 0)
                //		continue;
                writer.WriteLine("<a href=\"" + UrlEncoding.Encode(file.Name) + "\">" + file.Name + "</a><br>");
            }
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Pumping the DBus...");
            new Thread((ThreadStart)DBusIterator).Start();

            server = new HttpWebServer(8000);
            VirtualDirectory root = new VirtualDirectory();
            server.Root = root;

            root.AddFile(new HTTPd.Index("index.html", root, "Index.template"));
            root.AddFile(new HTTPd.MarqueeCommit("marquee.commit.html", root, "Redirect.template"));
            server.Authenticator = new WebAuthenticator(null);
            server.Start();
            Console.WriteLine("Web server started on port 8000");
        }
Ejemplo n.º 4
0
        public deviMobile(string[] arg)
        {
            // Parse the command line arguments. See CommandLine.cs.
            CommandLineArgs args = new CommandLineArgs(arg);
            // Set various options if they're specified.
            string gridfile = "Grids.txt";
            if (args["gridfile"] != null)
            {
                gridfile = args["gridfile"];
            }
            // Read in the grids. Loop through the space-separated file, adding them to the dictionary.
            // Since there's no way of maintaining order, we also store the default separately.

            Console.WriteLine("[MAIN] Reading grids from " + gridfile);
            string[] grids = File.ReadAllLines(gridfile);
            LoginServers = new Dictionary<string, string>();
            bool defaulted = false;
            foreach (string grid in grids)
            {
                string[] split = new string[1];
                split[0] = " ";
                string[] griddata = grid.Trim().Split(split, 2, StringSplitOptions.RemoveEmptyEntries);
                LoginServers.Add(griddata[1], griddata[0]);
                if (!defaulted)
                {
                    DefaultLoginServer = griddata[1];
                    defaulted = true;
                }
                Console.WriteLine("[MAIN] Grids loaded " + griddata[1] + " (" + griddata[0] + ")");
            }
            Console.WriteLine("[MAIN] Default grid: " + DEFAULT_LOGIN_SERVER);

            // More fun option setting.
            if (args["root"] != null)
            {
                StaticRoot = args["root"];
            }
            if(!StaticRoot.EndsWith("/"))
            {
                StaticRoot += "/";
            }
            Console.WriteLine("[MAIN] Static root: " + STATIC_ROOT);
            if (args["texturecache"] != null)
            {
                TextureCache = args["texturecache"];
            }
            // TextureCache must end with a forward slash. Make sure it does.
            if (!TextureCache.EndsWith("/"))
            {
                TextureCache += "/";
            }
            if(args["texturebucket"] != null)
            {
                TextureBucket = args["texturebucket"];
            }
            if(args["textureroot"] != null)
            {
                TextureRoot = args["textureroot"];
            }
            if (args["mac"] != null)
            {
                MacAddress = args["mac"];
            }
            Console.WriteLine("[MAIN] Using MAC address: " + MAC_ADDRESS);
            if (args["id0"] != null)
            {
                Id0 = args["id0"];
            }
            Console.WriteLine("[MAIN] Using id0: " + (ID0 == "" ? "[blank]" : ID0));
            if (args["banlist"] != null)
            {
                BanList = args["banlist"];
            }
            if(BanList != "")
            {
                Console.WriteLine("[MAIN] Using banlist at " + BanList);
                if (args["banupdate"] != null)
                {
                    BanUpdateTime = double.Parse(args["banupdate"]);
                }
                if(BanUpdateTime > 0.0)
                {
                    Console.WriteLine("[MAIN] Updating the banlist every " + BanUpdateTime + " seconds.");
                }
                else
                {
                    Console.WriteLine("[MAIN] Banlist updating disabled.");
                }
            }
            else
            {
                Console.WriteLine("[MAIN] Not using ban list.");
            }
            HandleContentEncoding = (args["doencoding"] != null);
            Console.WriteLine("[MAIN] Handling content encoding: " + (HANDLE_CONTENT_ENCODING ? "Yes" : "No"));
            if(args["spamdebug"] != null)
            {
                DebugMode = true;
                Settings.LOG_LEVEL = Helpers.LogLevel.Debug;
            }
            else if(args["debug"] != null)
            {
                DebugMode = true;
                Settings.LOG_LEVEL = Helpers.LogLevel.Info;
            }
            else
            {
                Settings.LOG_LEVEL = Helpers.LogLevel.Error;
            }
            Console.WriteLine("[MAIN] Debug mode: " + (DEBUG_MODE ? "On" : "Off"));
            // Create an empty dictionary for the users. This is defined as public further up.
            Users = new Dictionary<Guid, User>();

            // Make a web server!
            HttpWebServer webserver = new HttpWebServer((args["port"]!=null)?int.Parse(args["port"]):8080);
            try
            {
                // If the "private" CLI argument was specified, make it private by making us only
                // listen to the loopback address (127.0.0.0)
                if (args["private"] != null)
                {
                    webserver.LocalAddress = System.Net.IPAddress.Loopback;
                    Console.WriteLine("[MAIN] Using private mode.");
                }
            }
            catch
            {
                // If we can't make it private, oh well.
            }

            // Make sure we have a usable texture cache, create it if not.
            // If we're using S3, this is just used for conversions. If we're using
            // our own texture system, we store textures here for client use.
            Console.WriteLine("[MAIN] Checking texture cache...");
            if (!Directory.Exists(TEXTURE_CACHE))
            {
                Console.WriteLine("[MAIN] Not found; Attempting to create texture cache...");
                try
                {
                    Directory.CreateDirectory(TEXTURE_CACHE);
                    Console.WriteLine("[MAIN] Created texture cache.");
                }
                catch
                {
                    Console.WriteLine("[MAIN] Failed to create texture cache at " + TEXTURE_CACHE + "; aborting.");
                    return;
                }
            }

            // Grab the S3 details off the command line if available.
            S3Config = new Affirma.ThreeSharp.ThreeSharpConfig();
            S3Config.AwsAccessKeyID = (args["s3key"] == null) ? AccessKey : args["s3key"];
            S3Config.AwsSecretAccessKey = (args["s3secret"] == null) ? PrivateAccessKey : args["s3secret"];
            // Check that, if we're using S3, we have enough information to do so.
            if(TextureBucket != "" && (S3Config.AwsAccessKeyID == "" || S3Config.AwsSecretAccessKey == "" || TextureRoot == ""))
            {
                Console.WriteLine("[MAIN] Error: To use S3 you must set s3key, s3secret, texturebucket and textureroot");
                return;
            }
            UseS3 = (TextureBucket != ""); // We're using S3 if TextureBucket is not blank.
            if (UseS3)
            {
                Console.WriteLine("[MAIN] Texture root: " + TEXTURE_ROOT);
                Console.WriteLine("[MAIN] Using Amazon S3 for textures:");
                Console.WriteLine("\tBucket: " + TEXTURE_BUCKET);
                Console.WriteLine("\tAccess key: " + S3Config.AwsAccessKeyID);
                Console.WriteLine("\tSecret: ".PadRight(S3Config.AwsSecretAccessKey.Length + 10, '*'));
            }
            else
            {
                TextureRoot = "textures/"; // Set the texture root to ourselves if not using S3.
                Console.WriteLine("[MAIN] Using internal server for textures:");
                Console.WriteLine("\tTexture root: " + TEXTURE_ROOT);
            }

            Console.WriteLine("[MAIN] Setting up pages...");
            // Set up the root.
            VirtualDirectory root = new VirtualDirectory();
            webserver.Root = root;
            #region Dynamic file setup
            // Create the virtual files, passing most of them (except index.html and differentorigin.kat,
            // as they don't need to deal with SL) the Users dictionary. Users is a reference object,
            // so changes are reflected in all the pages. The same goes for individual User objects.
            root.AddFile(new Html.MainPage("index.html", root, Users));
            //root.AddFile(new Html.Proxy("differentorigin.kat", root));
            root.AddFile(new Html.MakeFile("make.d", root));
            //root.AddFile(new Html.iPhone("iphone.kat", root));
            root.AddFile("robots.txt");
            // textures/ is only used if we aren't using S3 for textures.
            if(!UseS3)
            {
                root.AddDirectory(new DriveDirectory("textures", deviMobile.TEXTURE_CACHE, root));
            }
            // API stuff.
            VirtualDirectory api = new VirtualDirectory("api", root);
            root.AddDirectory(api);
            api.AddFile(new Html.BasicStats("stats", api, Users));
            api.AddFile(new Html.CreateSession("new", api, Users));
            api.AddFile(new Html.SendMessage("send", api, Users));
            api.AddFile(new Html.EventQueue("events", api, Users));
            api.AddFile(new Html.Logout("logout", api, Users));
            api.AddFile(new Html.Connect("login", api, Users));
            api.AddFile(new Html.LoginDetails("details", api, Users));

            // Core
            VirtualDirectory core = new VirtualDirectory("core", root);
            root.AddDirectory(core);
            core.AddFile(new Html.Exit("exit", core, Users));

            #endregion
            Console.WriteLine("[MAIN] Loading banlist...");
            BannedUsers = new BanList(); // Create BanList.

            Console.WriteLine("[MAIN] Starting server...");

            // Start the webserver.
            webserver.Start();
            // Set a timer to call timecheck() every five seconds to check for timed out sessions.
            System.Timers.Timer timer = new System.Timers.Timer(5000);
            timer.AutoReset = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timecheck);
            timer.Start();
            // Sleep forever. Note that this means nothing after this line ever gets executed.
            // We do this because no more processing takes place in this thread.
            //string cmd = Console.ReadLine();
            //if (cmd == "exit") Environment.Exit(1);
            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
            // We never get past this point, so all code past here has been deleted for now.
        }
		public HttpIntegrationServer(string rootDirectory, int port = 23)
		{
			_webServer = new HttpWebServer(IPAddress.Parse("127.0.0.1"), port, new DriveDirectory(rootDirectory));
			_webServer.Start();
		}