Esempio n. 1
0
        private void ProcessRequest(object obj)
        {
            m_semaphore.WaitOne();

            HttpListenerContext context = obj as HttpListenerContext;
            KeyAuthRequest request = new KeyAuthRequest(context);

            m_semaphore.Release();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            PrintMsg("\n\n\tStarting Ithaca Key Validation Service v" + version + ".\n", MessageType.Status);

#if DEBUG
#if LINUX
	    if (File.Exists("config.xml"))
            {
                File.Delete("config.xml");
                File.Copy("../../config.xml", "config.xml");
            }
            else
            {
                File.Copy("../../config.xml", "config.xml");
            }
#else
            if (File.Exists("config.xml"))
            {
                File.Delete("config.xml");
                File.Copy("..\\..\\config.xml", "config.xml");
            }
            else
            {
                File.Copy("..\\..\\config.xml", "config.xml");
            }
#endif

#else

            if (!File.Exists("config.xml")) 
            {
                exitCode = 1;
                PrintMsg("No config.xml found. Cannot proceed.", MessageType.Fatal);
                PrintExitMsg(exitCode);
                return;
            }

#endif
            config = new Configuration();
            using (FileStream fs = File.OpenRead("config.xml"))
            {
                config.ParseConfigXml(fs);
            }

            logFlag = (bool)config.GetConfigValue("logoutput");
            if (logFlag)
            {
                logFilePath = (string)config.GetConfigValue("logfile");
                if (!File.Exists(logFilePath)) File.Create(logFilePath).Close();
                PrintMsg("Opening log file at " + logFilePath, MessageType.Status);
            }

            PrintMsg("Setting configuration variables...", MessageType.Status);
            string[] unmatched = SetOptionFlags(args);
            for (int i = 0; i < unmatched.Length; i++)
            {
                string opt = unmatched[i];
                opt = opt.TrimStart('-');
                string[] optSects = opt.Split(':');
                if (optSects.Length == 2)
                {
                    // It takes the form --name:val
                    string name = optSects[0];
                    string val = optSects[1];
                    config.SetConfigValue(name, val);
                }
                else if (optSects.Length == 3 && optSects[0] == "config")
                {
                    // It takes the form --config:name:val
                    string name = optSects[1];
                    string val = optSects[2];
                    config.SetConfigValue(name, val);
                }
                else
                {
                    PrintMsg("Unknown option '" + unmatched[i] + "'. Use --help for usage", MessageType.Fatal);
                    return;
                }
            }

            string keyFilePath = (string)config.GetConfigValue("keyfile");
            if (!File.Exists(keyFilePath)) File.Create(keyFilePath).Close();
            keyMgr = new KeyFileManager(keyFilePath);

#if ADDKEYS
            keyMgr.CreateKeys(128, "You have been whitelisted on the server!");
#endif

            try
            {
                keyMgr.BuildKeyHashtable();
            }
            catch (Exception e)
            {
                PrintExceptionMsg(e);
            }

            if (helpFlag)
            {
                Usage();
                return;
            }

            HttpListener httpListener = new HttpListener();
            portNumber = (int)config.GetConfigValue("port");

            try
            {
                string prefixString = "http://+:" + portNumber.ToString() + "/";

                // Accept all requests sent to port 8080.
                httpListener.Prefixes.Add(prefixString);
                PrintMsg("Setting accepted url prefix to " + prefixString, MessageType.Status);

                // Must be run as administrator to call Start().
                httpListener.Start();
            }
            catch (Exception e)
            {
                exitCode = 1;

                PrintExceptionMsg(e);
                PrintExitMsg();
                return;
            }

            PrintMsg("HTTP Listener has begun listening for requests.", MessageType.Status);

            using (RequestProcessingManager processor = new RequestProcessingManager(ProcessRequest))
            {

                // Request processing loop
                while (exitCode == 0)
                {
                    try
                    {
                        HttpListenerContext context = httpListener.GetContext();
                        
                        // Create new Request object
                        KeyAuthRequest request = new KeyAuthRequest(context);
                        
                        // Queue it
                        processor.QueueWorkItem(request);
                    }
                    catch (Exception e)
                    {
                        PrintExceptionMsg(e);
                        PrintMsg("Continuing...", MessageType.Status);
                    }
                }

                keyMgr.Close();
            }
        }