Ejemplo n.º 1
0
        protected override int Invoke(string[] args)
        {
            using var hosts = Configuration.AdjustHostsFile ? new HostsFileManager() : null;
            using var certs = Configuration.AdjustCertificateStore ? new CertificateManager(443) : null;

            if (_cleanup)
            {
                _log.Basic("Cleaning up proxy server system changes...");

                foreach (var region in Configuration.Regions)
                {
                    hosts?.RemoveEntry(ServerListParameters.Uris[region].Host,
                                       Configuration.ServerListBaseAddress);
                }

                return(0);
            }

            if (Configuration.Loggers.Contains(FileLogger.Name))
            {
                Log.Loggers.Add(new FileLogger(true, true, true, Configuration.LogDirectory,
                                               Configuration.LogFileNameFormat));
            }

            try
            {
                AppDomain.CurrentDomain.ProcessExit += ProcessExit;
                Console.CancelKeyPress += CancelKeyPress;
                ConsoleUtility.AddConsoleEventHandler(ConsoleEvent);

                certs?.Activate();

                var servers = Configuration.Regions.Select(x => StartServer(x, hosts)).ToArray();

                _log.Basic("{0} server started", nameof(Alkahest));

                _exiting.Wait();

                foreach (var server in servers)
                {
                    StopServer(server);
                }

                _exited.Set();
            }
            finally
            {
                ConsoleUtility.RemoveConsoleEventHandler(ConsoleEvent);
                Console.CancelKeyPress -= CancelKeyPress;
                AppDomain.CurrentDomain.ProcessExit -= ProcessExit;
            }

            _log.Basic("{0} server stopped", nameof(Alkahest));

            return(0);
        }
Ejemplo n.º 2
0
        protected override int Invoke(string[] args)
        {
            if (_cleanup)
            {
                var sls     = ServerListParameters.Uris[Configuration.Region];
                var slsPort = Configuration.ServerListPort;

                if (slsPort == 0)
                {
                    slsPort = sls.Port;
                }

                using var hostsMgr = Configuration.AdjustHostsFile ? new HostsFileManager() : null;
                using var certMgr  = Configuration.AdjustCertificateStore && sls.Scheme == Uri.UriSchemeHttps ?
                                     new CertificateManager(slsPort) : null;

                hostsMgr?.RemoveEntry(sls.Host, Configuration.ServerListBaseAddress);

                return(0);
            }

            if (Configuration.Loggers.Contains(FileLogger.Name))
            {
                Log.Loggers.Add(new FileLogger(Configuration.LogDirectory, Configuration.LogFileNameFormat));
            }

            try
            {
                AppDomain.CurrentDomain.ProcessExit += ProcessExit;
                Console.CancelKeyPress += CancelKeyPress;
                ConsoleUtility.AddConsoleEventHandler(ConsoleEvent);

                _log.Basic("Proxy server starting...");

                var region  = Configuration.Region;
                var sls     = ServerListParameters.Uris[region];
                var slsPort = Configuration.ServerListPort;

                if (slsPort == 0)
                {
                    slsPort = sls.Port;
                }

                using var hostsMgr = Configuration.AdjustHostsFile ? new HostsFileManager() : null;
                using var certMgr  = Configuration.AdjustCertificateStore && sls.Scheme == Uri.UriSchemeHttps ?
                                     new CertificateManager(slsPort) : null;

                var slsHost    = sls.Host;
                var slsAddress = Configuration.ServerListBaseAddress;

                hostsMgr?.RemoveEntry(slsHost, slsAddress);

                var real = Dns.GetHostEntry(slsHost).AddressList[0];

                _log.Basic("Resolved official server list address: {0} -> {1}", slsHost, real);

                hostsMgr?.AddEntry(slsHost, slsAddress);

                using var slsProxy = new ServerListProxy(new ServerListParameters(real,
                                                                                  Configuration.ServerListBaseAddress, slsPort, Configuration.GameBaseAddress,
                                                                                  Configuration.GameBasePort, region, Configuration.ServerListTimeout,
                                                                                  Configuration.ServerListRetries));

                if (Configuration.ServerListEnabled)
                {
                    slsProxy.Start();
                }

                var pool = new ObjectPool <SocketAsyncEventArgs>(() => new SocketAsyncEventArgs(),
                                                                 x => x.Reset(), Configuration.PoolLimit != 0 ? (int?)Configuration.PoolLimit : null);
                var version = DataCenter.Versions[region];
                var proc    = new PacketProcessor(new CompilerPacketSerializer(region,
                                                                               new GameMessageTable(version), new SystemMessageTable(version)));
                var proxies = slsProxy.Servers.Select(x => new GameProxy(x, pool, proc,
                                                                         Configuration.GameBacklog, Configuration.GameTimeout)
                {
                    MaxClients = Configuration.GameMaxClients,
                }).ToArray();

                foreach (var proxy in proxies)
                {
                    proxy.Start();
                }

                var path = Path.ChangeExtension(Path.Combine(Configuration.AssetDirectory,
                                                             DataCenter.FileNames[region]), ".dec");
                var loader = new PluginLoader(new PluginContext(File.Exists(path) ?
                                                                new DataCenter(path, Configuration.DataCenterInterning) :
                                                                new DataCenter(version)), Configuration.PluginDirectory,
                                              Configuration.PluginPattern, Configuration.DisablePlugins);

                loader.Start(proxies);

                _log.Basic("Proxy server started");

                _running.Wait();

                _log.Basic("Proxy server stopping...");

                loader.Stop(proxies);

                foreach (var proxy in proxies)
                {
                    proxy.Dispose();
                }

                _exited.Set();

                _log.Basic("Proxy server stopped");
            }
            finally
            {
                ConsoleUtility.RemoveConsoleEventHandler(ConsoleEvent);
                Console.CancelKeyPress -= CancelKeyPress;
                AppDomain.CurrentDomain.ProcessExit -= ProcessExit;
            }

            return(0);
        }