BaseOpenSimServer.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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(5000);
  126. Util.StopThreadPool();
  127. WorkManager.Stop();
  128. Thread.Sleep(1000);
  129. RemovePIDFile();
  130. m_log.Info("[SHUTDOWN]: Shutdown processing on main thread complete. Exiting...");
  131. if (!SuppressExit)
  132. Environment.Exit(0);
  133. }
  134. /// <summary>
  135. /// Provides a list of help topics that are available. Overriding classes should append their topics to the
  136. /// information returned when the base method is called.
  137. /// </summary>
  138. ///
  139. /// <returns>
  140. /// A list of strings that represent different help topics on which more information is available
  141. /// </returns>
  142. protected virtual List<string> GetHelpTopics() { return new List<string>(); }
  143. /// <summary>
  144. /// Print statistics to the logfile, if they are active
  145. /// </summary>
  146. protected void LogDiagnostics(object source, ElapsedEventArgs e)
  147. {
  148. StringBuilder sb = new StringBuilder("DIAGNOSTICS\n\n");
  149. sb.Append(GetUptimeReport());
  150. sb.Append(StatsManager.SimExtraStats.Report());
  151. sb.Append(Environment.NewLine);
  152. sb.Append(GetThreadsReport());
  153. m_log.Debug(sb);
  154. }
  155. /// <summary>
  156. /// Performs initialisation of the scene, such as loading configuration from disk.
  157. /// </summary>
  158. public virtual void Startup()
  159. {
  160. m_log.Info("[STARTUP]: Beginning startup processing");
  161. m_log.Info("[STARTUP]: version: " + m_version + Environment.NewLine);
  162. // clr version potentially is more confusing than helpful, since it doesn't tell us if we're running under Mono/MS .NET and
  163. // the clr version number doesn't match the project version number under Mono.
  164. //m_log.Info("[STARTUP]: Virtual machine runtime version: " + Environment.Version + Environment.NewLine);
  165. m_log.InfoFormat(
  166. "[STARTUP]: Operating system version: {0}, .NET platform {1}, {2}-bit\n",
  167. Environment.OSVersion, Environment.OSVersion.Platform, Util.Is64BitProcess() ? "64" : "32");
  168. try
  169. {
  170. StartupSpecific();
  171. }
  172. catch(Exception e)
  173. {
  174. m_log.Fatal("Fatal error: " + e.ToString());
  175. Environment.Exit(1);
  176. }
  177. TimeSpan timeTaken = DateTime.Now - m_startuptime;
  178. // MainConsole.Instance.OutputFormat(
  179. // "PLEASE WAIT FOR LOGINS TO BE ENABLED ON REGIONS ONCE SCRIPTS HAVE STARTED. Non-script portion of startup took {0}m {1}s.",
  180. // timeTaken.Minutes, timeTaken.Seconds);
  181. }
  182. public string osSecret
  183. {
  184. // Secret uuid for the simulator
  185. get { return m_osSecret; }
  186. }
  187. public string StatReport(IOSHttpRequest httpRequest)
  188. {
  189. // If we catch a request for "callback", wrap the response in the value for jsonp
  190. if (httpRequest.Query.ContainsKey("callback"))
  191. {
  192. return httpRequest.Query["callback"].ToString() + "(" + StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version) + ");";
  193. }
  194. else
  195. {
  196. return StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version);
  197. }
  198. }
  199. }
  200. }