BaseOpenSimServer.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.IO;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.Text.RegularExpressions;
  33. using System.Threading;
  34. using System.Timers;
  35. using System.Net;
  36. using System.Net.Security;
  37. using System.Security.Cryptography.X509Certificates;
  38. using log4net;
  39. using log4net.Appender;
  40. using log4net.Core;
  41. using log4net.Repository;
  42. using OpenMetaverse;
  43. using OpenMetaverse.StructuredData;
  44. using OpenSim.Framework;
  45. using OpenSim.Framework.Console;
  46. using OpenSim.Framework.Monitoring;
  47. using OpenSim.Framework.Servers;
  48. using OpenSim.Framework.Servers.HttpServer;
  49. using Timer=System.Timers.Timer;
  50. using Nini.Config;
  51. namespace OpenSim.Framework.Servers
  52. {
  53. /// <summary>
  54. /// Common base for the main OpenSimServers (user, grid, inventory, region, etc)
  55. /// </summary>
  56. public abstract class BaseOpenSimServer : ServerBase
  57. {
  58. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  59. /// <summary>
  60. /// Used by tests to suppress Environment.Exit(0) so that post-run operations are possible.
  61. /// </summary>
  62. public bool SuppressExit { get; set; }
  63. /// <summary>
  64. /// This will control a periodic log printout of the current 'show stats' (if they are active) for this
  65. /// server.
  66. /// </summary>
  67. private int m_periodDiagnosticTimerMS = 60 * 60 * 1000;
  68. private Timer m_periodicDiagnosticsTimer = new Timer(60 * 60 * 1000);
  69. /// <summary>
  70. /// Random uuid for private data
  71. /// </summary>
  72. protected string m_osSecret = String.Empty;
  73. protected BaseHttpServer m_httpServer;
  74. public BaseHttpServer HttpServer
  75. {
  76. get { return m_httpServer; }
  77. }
  78. public BaseOpenSimServer() : base()
  79. {
  80. // Random uuid for private data
  81. m_osSecret = UUID.Random().ToString();
  82. }
  83. private static bool m_NoVerifyCertChain = false;
  84. private static bool m_NoVerifyCertHostname = false;
  85. public static bool ValidateServerCertificate(
  86. object sender,
  87. X509Certificate certificate,
  88. X509Chain chain,
  89. SslPolicyErrors sslPolicyErrors)
  90. {
  91. if (m_NoVerifyCertChain)
  92. sslPolicyErrors &= ~SslPolicyErrors.RemoteCertificateChainErrors;
  93. if (m_NoVerifyCertHostname)
  94. sslPolicyErrors &= ~SslPolicyErrors.RemoteCertificateNameMismatch;
  95. if (sslPolicyErrors == SslPolicyErrors.None)
  96. return true;
  97. return false;
  98. }
  99. /// <summary>
  100. /// Must be overriden by child classes for their own server specific startup behaviour.
  101. /// </summary>
  102. protected virtual void StartupSpecific()
  103. {
  104. StatsManager.SimExtraStats = new SimExtraStatsCollector();
  105. RegisterCommonCommands();
  106. RegisterCommonComponents(Config);
  107. IConfig startupConfig = Config.Configs["Startup"];
  108. m_NoVerifyCertChain = startupConfig.GetBoolean("NoVerifyCertChain", m_NoVerifyCertChain);
  109. m_NoVerifyCertHostname = startupConfig.GetBoolean("NoVerifyCertHostname", m_NoVerifyCertHostname);
  110. ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
  111. int logShowStatsSeconds = startupConfig.GetInt("LogShowStatsSeconds", m_periodDiagnosticTimerMS / 1000);
  112. m_periodDiagnosticTimerMS = logShowStatsSeconds * 1000;
  113. m_periodicDiagnosticsTimer.Elapsed += new ElapsedEventHandler(LogDiagnostics);
  114. if (m_periodDiagnosticTimerMS != 0)
  115. {
  116. m_periodicDiagnosticsTimer.Interval = m_periodDiagnosticTimerMS;
  117. m_periodicDiagnosticsTimer.Enabled = true;
  118. }
  119. }
  120. protected override void ShutdownSpecific()
  121. {
  122. Watchdog.Enabled = false;
  123. base.ShutdownSpecific();
  124. MainServer.Stop();
  125. Thread.Sleep(500);
  126. Util.StopThreadPool();
  127. WorkManager.Stop();
  128. RemovePIDFile();
  129. m_log.Info("[SHUTDOWN]: Shutdown processing on main thread complete. Exiting...");
  130. if (!SuppressExit)
  131. Environment.Exit(0);
  132. }
  133. /// <summary>
  134. /// Provides a list of help topics that are available. Overriding classes should append their topics to the
  135. /// information returned when the base method is called.
  136. /// </summary>
  137. ///
  138. /// <returns>
  139. /// A list of strings that represent different help topics on which more information is available
  140. /// </returns>
  141. protected virtual List<string> GetHelpTopics() { return new List<string>(); }
  142. /// <summary>
  143. /// Print statistics to the logfile, if they are active
  144. /// </summary>
  145. protected void LogDiagnostics(object source, ElapsedEventArgs e)
  146. {
  147. StringBuilder sb = new StringBuilder("DIAGNOSTICS\n\n");
  148. sb.Append(GetUptimeReport());
  149. sb.Append(StatsManager.SimExtraStats.Report());
  150. sb.Append(Environment.NewLine);
  151. sb.Append(GetThreadsReport());
  152. m_log.Debug(sb);
  153. }
  154. /// <summary>
  155. /// Performs initialisation of the scene, such as loading configuration from disk.
  156. /// </summary>
  157. public virtual void Startup()
  158. {
  159. m_log.Info("[STARTUP]: Beginning startup processing");
  160. m_log.Info("[STARTUP]: version: " + m_version + Environment.NewLine);
  161. // clr version potentially is more confusing than helpful, since it doesn't tell us if we're running under Mono/MS .NET and
  162. // the clr version number doesn't match the project version number under Mono.
  163. //m_log.Info("[STARTUP]: Virtual machine runtime version: " + Environment.Version + Environment.NewLine);
  164. m_log.InfoFormat(
  165. "[STARTUP]: Operating system version: {0}, .NET platform {1}, {2}-bit\n",
  166. Environment.OSVersion, Environment.OSVersion.Platform, Environment.Is64BitProcess ? "64" : "32");
  167. // next code can be changed on .net 4.7.x
  168. if(Util.IsWindows())
  169. m_log.InfoFormat("[STARTUP]: Processor Architecture: {0}({1})",
  170. System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE", EnvironmentVariableTarget.Machine),
  171. BitConverter.IsLittleEndian ?"le":"be");
  172. // on other platforms we need to wait for .net4.7.1
  173. try
  174. {
  175. StartupSpecific();
  176. }
  177. catch(Exception e)
  178. {
  179. m_log.Fatal("Fatal error: " + e.ToString());
  180. Environment.Exit(1);
  181. }
  182. //TimeSpan timeTaken = DateTime.Now - m_startuptime;
  183. // MainConsole.Instance.OutputFormat(
  184. // "PLEASE WAIT FOR LOGINS TO BE ENABLED ON REGIONS ONCE SCRIPTS HAVE STARTED. Non-script portion of startup took {0}m {1}s.",
  185. // timeTaken.Minutes, timeTaken.Seconds);
  186. }
  187. public string osSecret
  188. {
  189. // Secret uuid for the simulator
  190. get { return m_osSecret; }
  191. }
  192. public string StatReport(IOSHttpRequest httpRequest)
  193. {
  194. // If we catch a request for "callback", wrap the response in the value for jsonp
  195. if (httpRequest.Query.ContainsKey("callback"))
  196. {
  197. return httpRequest.Query["callback"].ToString() + "(" + StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version) + ");";
  198. }
  199. else
  200. {
  201. return StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version);
  202. }
  203. }
  204. }
  205. }