ServerMain.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 Nini.Config;
  28. using log4net;
  29. using System.Reflection;
  30. using System;
  31. using System.Net;
  32. using System.Net.Security;
  33. using System.Security.Cryptography.X509Certificates;
  34. using System.Collections.Generic;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenSim.Server.Base;
  39. using OpenSim.Server.Handlers.Base;
  40. using Mono.Addins;
  41. namespace OpenSim.Server
  42. {
  43. public class OpenSimServer
  44. {
  45. private static readonly ILog m_log =
  46. LogManager.GetLogger(
  47. MethodBase.GetCurrentMethod().DeclaringType);
  48. protected static HttpServerBase m_Server = null;
  49. protected static List<IServiceConnector> m_ServiceConnectors =
  50. new List<IServiceConnector>();
  51. protected static PluginLoader loader;
  52. private static bool m_NoVerifyCertChain = false;
  53. private static bool m_NoVerifyCertHostname = false;
  54. public static bool ValidateServerCertificate(
  55. object sender,
  56. X509Certificate certificate,
  57. X509Chain chain,
  58. SslPolicyErrors sslPolicyErrors)
  59. {
  60. if (m_NoVerifyCertChain)
  61. sslPolicyErrors &= ~SslPolicyErrors.RemoteCertificateChainErrors;
  62. if (m_NoVerifyCertHostname)
  63. sslPolicyErrors &= ~SslPolicyErrors.RemoteCertificateNameMismatch;
  64. if (sslPolicyErrors == SslPolicyErrors.None)
  65. return true;
  66. return false;
  67. }
  68. public static int Main(string[] args)
  69. {
  70. Culture.SetCurrentCulture();
  71. Culture.SetDefaultCurrentCulture();
  72. ServicePointManager.DefaultConnectionLimit = 32;
  73. ServicePointManager.MaxServicePointIdleTime = 30000;
  74. ServicePointManager.Expect100Continue = true; // needed now to suport auto redir without writecache
  75. ServicePointManager.UseNagleAlgorithm = false;
  76. ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
  77. m_Server = new HttpServerBase("R.O.B.U.S.T.", args);
  78. string registryLocation;
  79. IConfig serverConfig = m_Server.Config.Configs["Startup"];
  80. if (serverConfig == null)
  81. {
  82. System.Console.WriteLine("Startup config section missing in .ini file");
  83. throw new Exception("Configuration error");
  84. }
  85. int dnsTimeout = serverConfig.GetInt("DnsTimeout", 30000);
  86. try { ServicePointManager.DnsRefreshTimeout = dnsTimeout; } catch { }
  87. m_NoVerifyCertChain = serverConfig.GetBoolean("NoVerifyCertChain", m_NoVerifyCertChain);
  88. m_NoVerifyCertHostname = serverConfig.GetBoolean("NoVerifyCertHostname", m_NoVerifyCertHostname);
  89. string connList = serverConfig.GetString("ServiceConnectors", String.Empty);
  90. registryLocation = serverConfig.GetString("RegistryLocation",".");
  91. IConfig servicesConfig = m_Server.Config.Configs["ServiceList"];
  92. if (servicesConfig != null)
  93. {
  94. List<string> servicesList = new List<string>();
  95. if (connList != String.Empty)
  96. servicesList.Add(connList);
  97. foreach (string k in servicesConfig.GetKeys())
  98. {
  99. string v = servicesConfig.GetString(k);
  100. if (v != String.Empty)
  101. servicesList.Add(v);
  102. }
  103. connList = String.Join(",", servicesList.ToArray());
  104. }
  105. string[] conns = connList.Split(new char[] {',', ' ', '\n', '\r', '\t'});
  106. // int i = 0;
  107. foreach (string c in conns)
  108. {
  109. if (c == String.Empty)
  110. continue;
  111. string configName = String.Empty;
  112. string conn = c;
  113. uint port = 0;
  114. string[] split1 = conn.Split(new char[] {'/'});
  115. if (split1.Length > 1)
  116. {
  117. conn = split1[1];
  118. string[] split2 = split1[0].Split(new char[] {'@'});
  119. if (split2.Length > 1)
  120. {
  121. configName = split2[0];
  122. port = Convert.ToUInt32(split2[1]);
  123. }
  124. else
  125. {
  126. port = Convert.ToUInt32(split1[0]);
  127. }
  128. }
  129. string[] parts = conn.Split(new char[] {':'});
  130. string friendlyName = parts[0];
  131. if (parts.Length > 1)
  132. friendlyName = parts[1];
  133. BaseHttpServer server;
  134. if (port != 0)
  135. server = (BaseHttpServer)MainServer.GetHttpServer(port);
  136. else
  137. server = MainServer.Instance;
  138. if (friendlyName == "LLLoginServiceInConnector")
  139. server.AddSimpleStreamHandler(new IndexPHPHandler(server));
  140. m_log.InfoFormat("[SERVER]: Loading {0} on port {1}", friendlyName, server.Port);
  141. IServiceConnector connector = null;
  142. Object[] modargs = new Object[] { m_Server.Config, server, configName };
  143. connector = ServerUtils.LoadPlugin<IServiceConnector>(conn, modargs);
  144. if (connector == null)
  145. {
  146. modargs = new Object[] { m_Server.Config, server };
  147. connector = ServerUtils.LoadPlugin<IServiceConnector>(conn, modargs);
  148. }
  149. if (connector != null)
  150. {
  151. m_ServiceConnectors.Add(connector);
  152. m_log.InfoFormat("[SERVER]: {0} loaded successfully", friendlyName);
  153. }
  154. else
  155. {
  156. m_log.ErrorFormat("[SERVER]: Failed to load {0}", conn);
  157. }
  158. }
  159. loader = new PluginLoader(m_Server.Config, registryLocation);
  160. int res = m_Server.Run();
  161. if(m_Server != null)
  162. m_Server.Shutdown();
  163. Util.StopThreadPool();
  164. Environment.Exit(res);
  165. return 0;
  166. }
  167. }
  168. }