1
0

BaseOpenSimServer.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. using System.Threading;
  32. using System.Timers;
  33. using System.Net;
  34. using System.Net.Security;
  35. using System.Security.Cryptography.X509Certificates;
  36. using log4net;
  37. using OpenMetaverse;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Monitoring;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using Timer=System.Timers.Timer;
  42. using Nini.Config;
  43. using System.Runtime.InteropServices;
  44. namespace OpenSim.Framework.Servers
  45. {
  46. /// <summary>
  47. /// Common base for the main OpenSimServers (user, grid, inventory, region, etc)
  48. /// </summary>
  49. public abstract class BaseOpenSimServer : ServerBase
  50. {
  51. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. /// <summary>
  53. /// Used by tests to suppress Environment.Exit(0) so that post-run operations are possible.
  54. /// </summary>
  55. public bool SuppressExit { get; set; }
  56. /// <summary>
  57. /// This will control a periodic log printout of the current 'show stats' (if they are active) for this
  58. /// server.
  59. /// </summary>
  60. private int m_periodDiagnosticTimerMS = 60 * 60 * 1000;
  61. private Timer m_periodicDiagnosticsTimer = new Timer(60 * 60 * 1000);
  62. /// <summary>
  63. /// Random uuid for private data
  64. /// </summary>
  65. protected string m_osSecret = String.Empty;
  66. protected BaseHttpServer m_httpServer;
  67. public BaseHttpServer HttpServer
  68. {
  69. get { return m_httpServer; }
  70. }
  71. public BaseOpenSimServer() : base()
  72. {
  73. // Random uuid for private data
  74. m_osSecret = UUID.Random().ToString();
  75. }
  76. private static bool m_NoVerifyCertChain = true;
  77. private static bool m_NoVerifyCertHostname = true;
  78. public static bool ValidateServerCertificate(
  79. object sender,
  80. X509Certificate certificate,
  81. X509Chain chain,
  82. SslPolicyErrors sslPolicyErrors)
  83. {
  84. if (m_NoVerifyCertChain)
  85. sslPolicyErrors &= ~SslPolicyErrors.RemoteCertificateChainErrors;
  86. if (m_NoVerifyCertHostname)
  87. sslPolicyErrors &= ~SslPolicyErrors.RemoteCertificateNameMismatch;
  88. if (sslPolicyErrors == SslPolicyErrors.None)
  89. return true;
  90. return false;
  91. }
  92. /// <summary>
  93. /// Must be overriden by child classes for their own server specific startup behaviour.
  94. /// </summary>
  95. protected virtual void StartupSpecific()
  96. {
  97. StatsManager.SimExtraStats = new SimExtraStatsCollector();
  98. RegisterCommonCommands();
  99. RegisterCommonComponents(Config);
  100. IConfig startupConfig = Config.Configs["Startup"];
  101. m_NoVerifyCertChain = startupConfig.GetBoolean("NoVerifyCertChain", m_NoVerifyCertChain);
  102. m_NoVerifyCertHostname = startupConfig.GetBoolean("NoVerifyCertHostname", m_NoVerifyCertHostname);
  103. ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
  104. WebUtil.SetupHTTPClients(m_NoVerifyCertChain, m_NoVerifyCertHostname, null, 32 );
  105. int logShowStatsSeconds = startupConfig.GetInt("LogShowStatsSeconds", m_periodDiagnosticTimerMS / 1000);
  106. m_periodDiagnosticTimerMS = logShowStatsSeconds * 1000;
  107. m_periodicDiagnosticsTimer.Elapsed += new ElapsedEventHandler(LogDiagnostics);
  108. if (m_periodDiagnosticTimerMS != 0)
  109. {
  110. m_periodicDiagnosticsTimer.Interval = m_periodDiagnosticTimerMS;
  111. m_periodicDiagnosticsTimer.Enabled = true;
  112. }
  113. }
  114. protected override void ShutdownSpecific()
  115. {
  116. Watchdog.Enabled = false;
  117. base.ShutdownSpecific();
  118. MainServer.Stop();
  119. Thread.Sleep(500);
  120. Util.StopThreadPool();
  121. WorkManager.Stop();
  122. RemovePIDFile();
  123. m_log.Info("[SHUTDOWN]: Shutdown processing on main thread complete. Exiting...");
  124. if (!SuppressExit)
  125. Environment.Exit(0);
  126. }
  127. /// <summary>
  128. /// Provides a list of help topics that are available. Overriding classes should append their topics to the
  129. /// information returned when the base method is called.
  130. /// </summary>
  131. ///
  132. /// <returns>
  133. /// A list of strings that represent different help topics on which more information is available
  134. /// </returns>
  135. protected virtual List<string> GetHelpTopics() { return new List<string>(); }
  136. /// <summary>
  137. /// Print statistics to the logfile, if they are active
  138. /// </summary>
  139. protected void LogDiagnostics(object source, ElapsedEventArgs e)
  140. {
  141. StringBuilder sb = new StringBuilder("DIAGNOSTICS\n\n");
  142. sb.Append(GetUptimeReport());
  143. sb.Append(StatsManager.SimExtraStats.Report());
  144. sb.Append(Environment.NewLine);
  145. sb.Append(GetThreadsReport());
  146. m_log.Debug(sb);
  147. }
  148. /// <summary>
  149. /// Performs initialisation of the scene, such as loading configuration from disk.
  150. /// </summary>
  151. public virtual void Startup()
  152. {
  153. m_log.Info("[STARTUP]: Beginning startup processing");
  154. m_log.Info("[STARTUP]: version: " + m_version);
  155. m_log.Info($"[STARTUP]: Operating system version: {Environment.OSVersion}, .NET platform {Util.RuntimePlatformStr}, Runtime {Environment.Version}");
  156. m_log.Info($"[STARTUP]: Processor Architecture: {RuntimeInformation.ProcessArchitecture}({(BitConverter.IsLittleEndian ? "le" : "be")} {(Environment.Is64BitProcess ? "64" : "32")}bit)");
  157. try
  158. {
  159. StartupSpecific();
  160. }
  161. catch(Exception e)
  162. {
  163. m_log.Fatal("Fatal error: " + e.ToString());
  164. Environment.Exit(1);
  165. }
  166. }
  167. public string osSecret
  168. {
  169. // Secret uuid for the simulator
  170. get { return m_osSecret; }
  171. }
  172. public string StatReport(IOSHttpRequest httpRequest)
  173. {
  174. httpRequest.QueryAsDictionary.TryGetValue("region", out string id);
  175. // If we catch a request for "callback", wrap the response in the value for jsonp
  176. if (httpRequest.QueryAsDictionary.TryGetValue("callback", out string cb) && ! string.IsNullOrEmpty(cb))
  177. {
  178. return cb + "(" + StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version, id) + ");";
  179. }
  180. else
  181. {
  182. return StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version, id);
  183. }
  184. }
  185. }
  186. }