Beispiel #1
0
        // constructor
        public Blink1Server()
        {
            Log("Blink1Server!");
            //Blink1Server.setBlink1Server(this);
            blink1.open();
            fadeToRGB(0.4, Color.Black);

            loadSettings();

            Log("Running on port " + httpPortDefault);
            Log("blink1Id:" + blink1Id);
            long updateMillis = (long)(inputUpdateInterval * 1000);

            inputsTimer = new Timer(updateInputs, null, updateMillis, updateMillis);

            saveSettings();

            try {
                VirtualDirectory root = new VirtualDirectory();
                // note: this works because there is an xcopy as a post build-build event
                string baseDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                baseDir += "\\html\\blink_1";
                DriveDirectory htmlDir = new DriveDirectory(baseDir);
                Log("htmlDir: " + htmlDir.Path);
                root.AddDirectory(htmlDir.Path);

                VirtualDirectory blink1dir  = new VirtualDirectory("blink1", root);
                VirtualDirectory inputdir   = new VirtualDirectory("input", blink1dir);
                VirtualDirectory patterndir = new VirtualDirectory("pattern", blink1dir);

                // FIXME: the below is completely gross, how to do good HTTP routing with MiniHttpd?
                Blink1JSONFile id = new Blink1JSONFile("id", blink1dir, this);
                id.GetStringResponse = Ublink1Id;
                blink1dir.AddFile(id);   //add a virtual file for each json method

                Blink1JSONFile enumerate = new Blink1JSONFile("enumerate", blink1dir, this);
                enumerate.GetStringResponse = Ublink1Enumerate;
                blink1dir.AddFile(enumerate);   //add a virtual file for each json method

                Blink1JSONFile regen = new Blink1JSONFile("regenerateblink1id", blink1dir, this);
                regen.GetStringResponse = Ublink1RegenerateBlink1Id;
                blink1dir.AddFile(regen);

                Blink1JSONFile fadeRGB = new Blink1JSONFile("fadeToRGB", blink1dir, this);
                fadeRGB.GetStringResponse = Ublink1FadeToRGB;
                blink1dir.AddFile(fadeRGB);

                Blink1JSONFile on = new Blink1JSONFile("on", blink1dir, this);
                on.GetStringResponse = Ublink1On;
                blink1dir.AddFile(on);

                Blink1JSONFile off = new Blink1JSONFile("off", blink1dir, this);
                off.GetStringResponse = Ublink1Off;
                blink1dir.AddFile(off);

                Blink1JSONFile lastColor = new Blink1JSONFile("lastColor", blink1dir, this);
                lastColor.GetStringResponse = Ublink1LastColor;
                blink1dir.AddFile(lastColor);


                Blink1JSONFile pattern = new Blink1JSONFile("patterns", blink1dir, this);
                pattern.GetStringResponse = Ublink1Pattern;
                blink1dir.AddFile(pattern);

                Blink1JSONFile pattadd = new Blink1JSONFile("add", patterndir, this);
                pattadd.GetStringResponse = Ublink1PatternAdd;
                patterndir.AddFile(pattadd);

                Blink1JSONFile pattdel = new Blink1JSONFile("del", patterndir, this);
                pattdel.GetStringResponse = Ublink1PatternDel;
                patterndir.AddFile(pattdel);

                Blink1JSONFile pattdelall = new Blink1JSONFile("delall", patterndir, this);
                pattdelall.GetStringResponse = Ublink1PatternDelAll;
                patterndir.AddFile(pattdelall);

                Blink1JSONFile pattplay = new Blink1JSONFile("play", patterndir, this);
                pattplay.GetStringResponse = Ublink1PatternPlay;
                patterndir.AddFile(pattplay);

                Blink1JSONFile pattstop = new Blink1JSONFile("stop", patterndir, this);
                pattstop.GetStringResponse = Ublink1PatternStop;
                patterndir.AddFile(pattstop);

                Blink1JSONFile pattstopall = new Blink1JSONFile("stopall", patterndir, this);
                pattstopall.GetStringResponse = Ublink1PatternStopAll;
                patterndir.AddFile(pattstopall);


                Blink1JSONFile input = new Blink1JSONFile("inputs", blink1dir, this);
                input.GetStringResponse = Ublink1Input;
                blink1dir.AddFile(input);

                Blink1JSONFile inputdel = new Blink1JSONFile("del", inputdir, this);
                inputdel.GetStringResponse = Ublink1InputDel;
                inputdir.AddFile(inputdel);

                Blink1JSONFile inputdelall = new Blink1JSONFile("delall", inputdir, this);
                inputdelall.GetStringResponse = Ublink1InputDelAll;
                inputdir.AddFile(inputdelall);

                Blink1JSONFile inputurl = new Blink1JSONFile("url", inputdir, this);
                inputurl.GetStringResponse = Ublink1InputUrl;
                inputdir.AddFile(inputurl);

                Blink1JSONFile inputfile = new Blink1JSONFile("file", inputdir, this);
                inputfile.GetStringResponse = Ublink1InputFile;
                inputdir.AddFile(inputfile);

                Blink1JSONFile inputscript = new Blink1JSONFile("script", inputdir, this);
                inputscript.GetStringResponse = Ublink1InputScript;
                inputdir.AddFile(inputscript);

                Blink1JSONFile inputifttt = new Blink1JSONFile("ifttt", inputdir, this);
                inputifttt.GetStringResponse = Ublink1InputIfttt;
                inputdir.AddFile(inputifttt);

                // TESTING
                // embedding slashes in path name does not work
                //Blink1JSONFile foobar = new Blink1JSONFile("foo/bar", blink1dir, this);
                //foobar.GetStringResponse = Ufoobar;
                //blink1dir.AddFile(foobar);   //add a virtual file for each json method
                //VirtualDirectory blarg = new VirtualDirectory();
                //Blink1Directory bd = new Blink1Directory("floop", root);
                //root.AddDirectory(bd);
                // TESTING END

                blink1dir.AddDirectory(inputdir);
                blink1dir.AddDirectory(patterndir);
                root.AddDirectory(blink1dir);
                httpServer.Root = root;

                ConsoleWriter writer = new ConsoleWriter();
                httpServer.Log         = writer;
                httpServer.LogRequests = true;
                writer.OnWrite        += new ConsoleWriter.WriteEventHandler(writer_OnWrite);

                httpServer.Start();
            }
            catch (Exception e) {
                Log(e.ToString());
            }
        }
Beispiel #2
0
        public AjaxLife(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("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("Loaded grid " + griddata[1] + " (" + griddata[0] + ")");
            }
            Console.WriteLine("Default grid: " + DEFAULT_LOGIN_SERVER);

            // More fun option setting.
            if (args["root"] != null)
            {
                StaticRoot = args["root"];
            }
            if (!StaticRoot.EndsWith("/"))
            {
                StaticRoot += "/";
            }
            Console.WriteLine("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("Using MAC address: " + MAC_ADDRESS);
            if (args["id0"] != null)
            {
                Id0 = args["id0"];
            }
            Console.WriteLine("Using id0: " + (ID0 == "" ? "[blank]" : ID0));
            if (args["banlist"] != null)
            {
                BanList = args["banlist"];
            }
            if (BanList != "")
            {
                Console.WriteLine("Using banlist at " + BanList);
                if (args["banupdate"] != null)
                {
                    BanUpdateTime = double.Parse(args["banupdate"]);
                }
                if (BanUpdateTime > 0.0)
                {
                    Console.WriteLine("Updating the banlist every " + BanUpdateTime + " seconds.");
                }
                else
                {
                    Console.WriteLine("Banlist updating disabled.");
                }
            }
            else
            {
                Console.WriteLine("Not using ban list.");
            }
            HandleContentEncoding = (args["doencoding"] != null);
            Console.WriteLine("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("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("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("Checking texture cache...");
            if (!Directory.Exists(TEXTURE_CACHE))
            {
                Console.WriteLine("Not found; Attempting to create texture cache...");
                try
                {
                    Directory.CreateDirectory(TEXTURE_CACHE);
                    Console.WriteLine("Created texture cache.");
                }
                catch
                {
                    Console.WriteLine("Failed to create texture cache at " + TEXTURE_CACHE + "; aborting.");
                    return;
                }
            }

            Console.WriteLine("Initialising RSA service...");
            RSA = new RSACrypto();
            // Create a new RSA keypair with the specified length. 1024 if unspecified.
            RSA.InitCrypto((args["keylength"] == null)?1024:int.Parse(args["keylength"]));
            RSAp = RSA.ExportParameters(true);
            Console.WriteLine("Generated " + ((args["keylength"] == null) ? 1024 : int.Parse(args["keylength"])) + "-bit key.");
            Console.WriteLine("RSA ready.");
            // 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("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("Texture root: " + TEXTURE_ROOT);
                Console.WriteLine("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("Using internal server for textures:");
                Console.WriteLine("\tTexture root: " + TEXTURE_ROOT);
            }
            Console.WriteLine("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.Index("index.html", root));
            root.AddFile(new Html.MainPage("ajaxlife.kat", root, Users));
            root.AddFile(new Html.Proxy("differentorigin.kat", root));
            root.AddFile(new Html.BasicStats("ping.kat", root, Users));
            root.AddFile(new Html.MakeFile("makefile.kat", 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 TextureDirectory("textures", root));
            }
            // API stuff.
            VirtualDirectory api = new VirtualDirectory("api", root);
            root.AddDirectory(api);
            api.AddFile(new Html.CreateSession("newsession", 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("sessiondetails", api, Users));
            #endregion
            Console.WriteLine("Loading banlist...");
            BannedUsers = new BanList(); // Create BanList.

            Console.WriteLine("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.
            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
            // We never get past this point, so all code past here has been deleted for now.
        }