Esempio n. 1
0
        private void AddFile(string id, HFTMessageAddFile data)
        {
            string filename = data.filename;

            byte[] bytes = System.Convert.FromBase64String(data.data);
            log_.Info("Receive File: " + filename + ", size: " + bytes.Length);
            HFTWebFileDB.GetInstance().AddFile(filename, bytes);
        }
Esempio n. 2
0
        public void StartServer()
        {
            m_log.Info("Start Server");
            m_listening = true;

            // Where should this be checked?
            string controllerPath = "/" + m_options.controllerFilename;

            if (!HFTWebFileDB.GetInstance().FileExists(controllerPath))
            {
                throw new System.ArgumentException(
                          "\"Assets/WebPlayerTemplates/HappyFunTimes" + controllerPath + "\" does not exist. Did you forget to set \"controllerFilename\" in your \"PlayerSpawner\" or \"PlayerConnector\"?");
            }

            #if UNITY_STANDALONE_OSX
            // TODO make 2 classes, one for running internal server, one for external?
            if (m_options.startExternalServer)
            {
                StartExternalServer(true);
                return;
            }
            #endif

            List <string> addresses = new List <string>();
            addresses.Add("http://[::0]:" + m_options.serverPort);
            #if UNITY_STANDALONE_WIN
            addresses.Add("http://0.0.0.0:" + m_options.serverPort);
            #endif

            if (m_options.installationMode)
            {
                addresses.Add("http://[::0]:80");
                #if UNITY_STANDALONE_WIN
                addresses.Add("http://0.0.0.0:80");
                #endif
            }
            else
            {
                var hftOptions = new HFTSite.Options();
                //hftOptions.port = ??
                HFTUtil.SetIfNotNullOrEmpty(m_options.rendezvousUrl, ref hftOptions.rendezvousUrl);
                HFTUtil.SetIfNotNullOrEmpty(m_options.serverPort, ref hftOptions.port);
                m_hftSite = m_gameObject.AddComponent <HFTSite>();
                m_hftSite.Init(hftOptions);
            }

            string ipv4Address = String.IsNullOrEmpty(m_options.ipv4DnsAddress) ? HFTIpUtils.GetLocalIPv4Address() : m_options.ipv4DnsAddress;
            string ipv6Address = String.IsNullOrEmpty(m_options.ipv6DnsAddress) ? HFTIpUtils.GetLocalIPv6Address() : m_options.ipv6DnsAddress;

            m_webServer = new HFTWebServer(m_options, addresses.ToArray());
            m_webServer.Start();

            if (m_options.dns || m_options.installationMode)
            {
                m_dnsRunner = new HFTDnsRunner();
                m_dnsRunner.Start(ipv4Address, ipv6Address, 53);
            }
        }
Esempio n. 3
0
 static public HFTWebFileDB GetInstance()
 {
     if (s_instance == null)
     {
         s_instance = new HFTWebFileDB();
         s_instance.Init();
     }
     return(s_instance);
 }
Esempio n. 4
0
        public HFTWebServer(HFTRuntimeOptions options, string[] addresses)
        {
            m_log            = new HFTLog("HFTWebServer");
            m_options        = options;
            m_gamePath       = "/";
            m_webServerUtils = new HFTWebServerUtils(m_gamePath);

            // Touch the HFTWebFileDB
            // We do this be because we want it to get the list
            // of files BEFORE run the server. The server will
            // run in a different thread and HFTWebFileDB will
            // not be able to populate its database from that thread.
            HFTWebFileDB.GetInstance();

            // FIX: sysname and gamename
            string sysName = Environment.MachineName;

            if (sysName.EndsWith(".local"))
            {
                sysName = sysName.Substring(0, sysName.Length - 6);
            }
            string gameName = m_options.name;
            string ping     = Serializer.Serialize(new HFTPing(sysName + ": " + gameName, "HappyFunTimes"));

            m_ping = System.Text.Encoding.UTF8.GetBytes(ping);
            m_log.Info("Ping: " + ping);

            m_liveSettingsStr = "define([], function() { return " + Serializer.Serialize(new LiveSettings()) + "; })\n";
            m_liveSettings    = System.Text.Encoding.UTF8.GetBytes(m_liveSettingsStr);

            if (options.captivePortal || options.installationMode)
            {
                m_captivePortalHandler = new HFTCaptivePortalHandler(m_webServerUtils);
                m_getRouter.Add(m_captivePortalHandler.HandleRequest);
            }

            m_getRouter.Add(HandleRoot);
            m_getRouter.Add(HandleLiveSettings);
            m_getRouter.Add(HandleFile);
            m_getRouter.Add(HandleMissingRoute);
            m_getRouter.Add(HandleNotFound);

            m_postCmdHandlers["happyFunTimesPingForGame"] = HandleCmdPingForGame;
            m_postCmdHandlers["happyFunTimesPing"]        = HandleCmdPing;
            m_postCmdHandlers["happyFunTimesRedir"]       = HandleCmdRedir;
            m_postCmdHandlers["time"] = HandleCmdTime;
            m_postCmdHandlers["quit"] = HandleCmdQuit;

            m_addresses = addresses;
        }
        public bool GetGameFile(string path, out byte[] content)
        {
            bool found = HFTWebFileDB.GetInstance().GetFile(path, out content);

            if (!found)
            {
                if (path.StartsWith(m_gamePath))
                {
                    path  = path.Substring(m_gamePath.Length - 1);
                    found = HFTWebFileDB.GetInstance().GetFile(path, out content);
                }
            }
            return(found);
        }
Esempio n. 6
0
        public static int Main(string[] args)
        {
            HFTRuntimeOptions m_options;
            HFTArgParser      p      = HFTArgParser.GetInstance();
            string            argStr = "";

            if (p.TryGet <string> ("hft-args", ref argStr))
            {
                Deserializer d = new Deserializer();
                m_options = d.Deserialize <HFTRuntimeOptions>(argStr);
            }
            else
            {
                m_options = new HFTRuntimeOptions();
            }
            if (!HFTArgsToFields.Apply("hft", m_options))
            {
                System.Console.WriteLine("bad args!");
                return(1);
            }

            HFTLog.debug = m_options.debug;

            //using (System.IO.StreamWriter writer = new System.IO.StreamWriter(System.IO.File.Open("/Users/gregg/temp/hft-server.log", System.IO.FileMode.Create)))
            //{
            //    writer.WriteLine(System.DateTime.Now.ToString());
            //    writer.WriteLine(Serializer.Serialize(m_options, false, true));
            //}

            List <string> addresses = new List <string>();

            addresses.Add("http://[::0]:" + m_options.serverPort);
            //            addresses.Add("http://0.0.0.0:18679");

            if (m_options.installationMode)
            {
                addresses.Add("http://[::0]:80");
                //                addresses.Add("http://0.0.0.0:80");
            }

            // Do I want this option ever?
            // Good: Need from editor to test instalation mode in editor
            // Bad: If game is hacked and serve any folder. But if game
            // is hack you can probably own machine anyway.
            if (!String.IsNullOrEmpty(m_options.dataPath))
            {
                HFTWebFileDB.GetInstance().DataPath = m_options.dataPath;
            }

            string ipv4Address = String.IsNullOrEmpty(m_options.ipv4DnsAddress) ? HFTIpUtils.GetLocalIPv4Address() : m_options.ipv4DnsAddress;
            string ipv6Address = String.IsNullOrEmpty(m_options.ipv6DnsAddress) ? HFTIpUtils.GetLocalIPv6Address() : m_options.ipv6DnsAddress;

            HFTWebServer webServer = new HFTWebServer(m_options, addresses.ToArray());

            webServer.Start();

            if (m_options.dns || m_options.installationMode)
            {
                HFTDnsRunner dnsRunner = new HFTDnsRunner();
                dnsRunner.Start(ipv4Address, ipv6Address, 53);
            }

            // There's no HFTSite because were in installationMode which means there's no internet


            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

            return(0);
        }