1
0

ServerMain.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.IO;
  32. using System.Net;
  33. using System.Net.Security;
  34. using System.Security.Cryptography.X509Certificates;
  35. using System.Collections.Generic;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Servers;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenSim.Server.Base;
  40. using OpenSim.Server.Handlers.Base;
  41. namespace OpenSim.Server
  42. {
  43. public class OpenSimServer
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType);
  46. protected static HttpServerBase m_Server = null;
  47. protected static List<IServiceConnector> m_ServiceConnectors = new();
  48. protected static PluginLoader loader;
  49. private static bool m_NoVerifyCertChain = false;
  50. private static bool m_NoVerifyCertHostname = false;
  51. public static bool ValidateServerCertificate(
  52. object sender,
  53. X509Certificate certificate,
  54. X509Chain chain,
  55. SslPolicyErrors sslPolicyErrors)
  56. {
  57. if (m_NoVerifyCertChain)
  58. sslPolicyErrors &= ~SslPolicyErrors.RemoteCertificateChainErrors;
  59. if (m_NoVerifyCertHostname)
  60. sslPolicyErrors &= ~SslPolicyErrors.RemoteCertificateNameMismatch;
  61. if (sslPolicyErrors == SslPolicyErrors.None)
  62. return true;
  63. return false;
  64. }
  65. /// <summary>
  66. /// Opens a file and uses it as input to the console command parser.
  67. /// </summary>
  68. /// <param name="fileName">name of file to use as input to the console</param>
  69. private static void PrintFileToConsole(string fileName)
  70. {
  71. if (File.Exists(fileName))
  72. {
  73. using(StreamReader readFile = File.OpenText(fileName))
  74. {
  75. string currentLine;
  76. while ((currentLine = readFile.ReadLine()) is not null)
  77. {
  78. m_log.InfoFormat("[!]" + currentLine);
  79. }
  80. }
  81. }
  82. }
  83. public static int Main(string[] args)
  84. {
  85. Culture.SetCurrentCulture();
  86. Culture.SetDefaultCurrentCulture();
  87. ServicePointManager.DefaultConnectionLimit = 64;
  88. ServicePointManager.MaxServicePointIdleTime = 30000;
  89. ServicePointManager.Expect100Continue = false;
  90. ServicePointManager.UseNagleAlgorithm = false;
  91. ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
  92. m_Server = new HttpServerBase("R.O.B.U.S.T.", args);
  93. string registryLocation;
  94. IConfig serverConfig = m_Server.Config.Configs["Startup"];
  95. if (serverConfig == null)
  96. {
  97. System.Console.WriteLine("Startup config section missing in .ini file");
  98. throw new Exception("Configuration error");
  99. }
  100. int dnsTimeout = serverConfig.GetInt("DnsTimeout", 30000);
  101. try { ServicePointManager.DnsRefreshTimeout = dnsTimeout; } catch { }
  102. m_NoVerifyCertChain = serverConfig.GetBoolean("NoVerifyCertChain", m_NoVerifyCertChain);
  103. m_NoVerifyCertHostname = serverConfig.GetBoolean("NoVerifyCertHostname", m_NoVerifyCertHostname);
  104. WebUtil.SetupHTTPClients(m_NoVerifyCertChain, m_NoVerifyCertHostname, null, 32);
  105. string connList = serverConfig.GetString("ServiceConnectors", string.Empty);
  106. registryLocation = serverConfig.GetString("RegistryLocation",".");
  107. IConfig servicesConfig = m_Server.Config.Configs["ServiceList"];
  108. if (servicesConfig != null)
  109. {
  110. List<string> servicesList = new();
  111. if (!string.IsNullOrEmpty(connList))
  112. servicesList.Add(connList);
  113. foreach (string k in servicesConfig.GetKeys())
  114. {
  115. string v = servicesConfig.GetString(k);
  116. if (!string.IsNullOrEmpty(v))
  117. servicesList.Add(v);
  118. }
  119. connList = string.Join(",", servicesList.ToArray());
  120. }
  121. // temporay set the platform dependent System.Drawing.Common.dll
  122. string targetdll = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
  123. "System.Drawing.Common.dll");
  124. string src = targetdll + (Util.IsWindows() ? ".win" : ".linux");
  125. try
  126. {
  127. if (!File.Exists(targetdll))
  128. File.Copy(src, targetdll);
  129. else
  130. {
  131. FileInfo targetInfo = new(targetdll);
  132. FileInfo srcInfo = new(src);
  133. if (targetInfo.Length != srcInfo.Length)
  134. File.Copy(src, targetdll, true);
  135. }
  136. }
  137. catch (Exception e)
  138. {
  139. m_log.Error("Failed to copy System.Drawing.Common.dll for current platform" + e.Message);
  140. throw;
  141. }
  142. string[] conns = connList.Split(new char[] {',', ' ', '\n', '\r', '\t'});
  143. foreach (string c in conns)
  144. {
  145. if (string.IsNullOrEmpty(c))
  146. continue;
  147. string configName = string.Empty;
  148. string conn = c;
  149. uint port = 0;
  150. string[] split1 = conn.Split(new char[] {'/'});
  151. if (split1.Length > 1)
  152. {
  153. conn = split1[1];
  154. string[] split2 = split1[0].Split(new char[] {'@'});
  155. if (split2.Length > 1)
  156. {
  157. configName = split2[0];
  158. port = Convert.ToUInt32(split2[1]);
  159. }
  160. else
  161. {
  162. port = Convert.ToUInt32(split1[0]);
  163. }
  164. }
  165. string[] parts = conn.Split(new char[] {':'});
  166. string friendlyName = parts[0];
  167. if (parts.Length > 1)
  168. friendlyName = parts[1];
  169. BaseHttpServer server;
  170. if (port != 0)
  171. server = (BaseHttpServer)MainServer.GetHttpServer(port);
  172. else
  173. server = MainServer.Instance;
  174. if (friendlyName == "LLLoginServiceInConnector")
  175. server.AddSimpleStreamHandler(new IndexPHPHandler(server));
  176. m_log.InfoFormat("[SERVER]: Loading {0} on port {1}", friendlyName, server.Port);
  177. IServiceConnector connector = null;
  178. object[] modargs = new object[] { m_Server.Config, server, configName };
  179. connector = ServerUtils.LoadPlugin<IServiceConnector>(conn, modargs);
  180. if (connector == null)
  181. {
  182. modargs = new object[] { m_Server.Config, server };
  183. connector = ServerUtils.LoadPlugin<IServiceConnector>(conn, modargs);
  184. }
  185. if (connector != null)
  186. {
  187. m_ServiceConnectors.Add(connector);
  188. m_log.InfoFormat("[SERVER]: {0} loaded successfully", friendlyName);
  189. }
  190. else
  191. {
  192. m_log.ErrorFormat("[SERVER]: Failed to load {0}", conn);
  193. }
  194. }
  195. PrintFileToConsole("robuststartuplogo.txt");
  196. loader = new PluginLoader(m_Server.Config, registryLocation);
  197. int res = m_Server.Run();
  198. m_Server?.Shutdown();
  199. Util.StopThreadPool();
  200. Environment.Exit(res);
  201. return 0;
  202. }
  203. }
  204. }