HttpServerBase.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.Threading;
  30. using System.Reflection;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Console;
  33. using OpenSim.Framework.Servers.HttpServer;
  34. using log4net;
  35. using Nini.Config;
  36. namespace OpenSim.Server.Base
  37. {
  38. public class HttpServerBase : ServicesServerBase
  39. {
  40. // Logger
  41. //
  42. private static readonly ILog m_Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. // The http server instance
  44. //
  45. protected BaseHttpServer m_HttpServer = null;
  46. protected uint m_Port = 0;
  47. protected Dictionary<uint, BaseHttpServer> m_Servers =
  48. new Dictionary<uint, BaseHttpServer>();
  49. protected uint m_consolePort = 0;
  50. public IHttpServer HttpServer
  51. {
  52. get { return m_HttpServer; }
  53. }
  54. public uint DefaultPort
  55. {
  56. get { return m_Port; }
  57. }
  58. public IHttpServer GetHttpServer(uint port)
  59. {
  60. m_Log.InfoFormat("[SERVER]: Requested port {0}", port);
  61. if (port == m_Port)
  62. return HttpServer;
  63. if (m_Servers.ContainsKey(port))
  64. return m_Servers[port];
  65. m_Servers[port] = new BaseHttpServer(port);
  66. m_Log.InfoFormat("[SERVER]: Starting new HTTP server on port {0}", port);
  67. m_Servers[port].Start();
  68. return m_Servers[port];
  69. }
  70. // Handle all the automagical stuff
  71. //
  72. public HttpServerBase(string prompt, string[] args) : base(prompt, args)
  73. {
  74. }
  75. protected override void ReadConfig()
  76. {
  77. IConfig networkConfig = m_Config.Configs["Network"];
  78. if (networkConfig == null)
  79. {
  80. System.Console.WriteLine("Section 'Network' not found, server can't start");
  81. Thread.CurrentThread.Abort();
  82. }
  83. uint port = (uint)networkConfig.GetInt("port", 0);
  84. if (port == 0)
  85. {
  86. Thread.CurrentThread.Abort();
  87. }
  88. //
  89. bool ssl_main = networkConfig.GetBoolean("https_main",false);
  90. bool ssl_listener = networkConfig.GetBoolean("https_listener",false);
  91. m_consolePort = (uint)networkConfig.GetInt("ConsolePort", 0);
  92. m_Port = port;
  93. //
  94. // This is where to make the servers:
  95. //
  96. //
  97. // Make the base server according to the port, etc.
  98. // ADD: Possibility to make main server ssl
  99. // Then, check for https settings and ADD a server to
  100. // m_Servers
  101. //
  102. if ( !ssl_main )
  103. {
  104. m_HttpServer = new BaseHttpServer(port);
  105. }
  106. else
  107. {
  108. string cert_path = networkConfig.GetString("cert_path",String.Empty);
  109. if ( cert_path == String.Empty )
  110. {
  111. System.Console.WriteLine("Path to X509 certificate is missing, server can't start.");
  112. Thread.CurrentThread.Abort();
  113. }
  114. string cert_pass = networkConfig.GetString("cert_pass",String.Empty);
  115. if ( cert_pass == String.Empty )
  116. {
  117. System.Console.WriteLine("Password for X509 certificate is missing, server can't start.");
  118. Thread.CurrentThread.Abort();
  119. }
  120. m_HttpServer = new BaseHttpServer(port, ssl_main, cert_path, cert_pass);
  121. }
  122. MainServer.Instance = m_HttpServer;
  123. // If https_listener = true, then add an ssl listener on the https_port...
  124. if ( ssl_listener == true ) {
  125. uint https_port = (uint)networkConfig.GetInt("https_port", 0);
  126. string cert_path = networkConfig.GetString("cert_path",String.Empty);
  127. if ( cert_path == String.Empty )
  128. {
  129. System.Console.WriteLine("Path to X509 certificate is missing, server can't start.");
  130. Thread.CurrentThread.Abort();
  131. }
  132. string cert_pass = networkConfig.GetString("cert_pass",String.Empty);
  133. if ( cert_pass == String.Empty )
  134. {
  135. System.Console.WriteLine("Password for X509 certificate is missing, server can't start.");
  136. Thread.CurrentThread.Abort();
  137. }
  138. // Add our https_server
  139. BaseHttpServer server = null;
  140. server = new BaseHttpServer(https_port, ssl_listener, cert_path, cert_pass);
  141. if (server != null)
  142. {
  143. m_Log.InfoFormat("[SERVER]: Starting HTTPS server on port {0}", https_port);
  144. m_Servers.Add(https_port,server);
  145. }
  146. else
  147. System.Console.WriteLine(String.Format("Failed to start HTTPS server on port {0}",https_port));
  148. }
  149. }
  150. protected override void Initialise()
  151. {
  152. m_Log.InfoFormat("[SERVER]: Starting HTTP server on port {0}", m_HttpServer.Port);
  153. m_HttpServer.Start();
  154. if (m_Servers.Count > 0)
  155. {
  156. foreach (BaseHttpServer s in m_Servers.Values)
  157. {
  158. if (!s.UseSSL)
  159. m_Log.InfoFormat("[SERVER]: Starting HTTP server on port {0}", s.Port);
  160. else
  161. m_Log.InfoFormat("[SERVER]: Starting HTTPS server on port {0}", s.Port);
  162. s.Start();
  163. }
  164. }
  165. if (MainConsole.Instance is RemoteConsole)
  166. {
  167. if (m_consolePort == 0)
  168. ((RemoteConsole)MainConsole.Instance).SetServer(m_HttpServer);
  169. else
  170. ((RemoteConsole)MainConsole.Instance).SetServer(GetHttpServer(m_consolePort));
  171. }
  172. }
  173. }
  174. }