HttpServerBase.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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;
  34. using OpenSim.Framework.Servers.HttpServer;
  35. using log4net;
  36. using Nini.Config;
  37. namespace OpenSim.Server.Base
  38. {
  39. public class HttpServerBase : ServicesServerBase
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private uint m_consolePort;
  43. // Handle all the automagical stuff
  44. //
  45. public HttpServerBase(string prompt, string[] args) : base(prompt, args)
  46. {
  47. }
  48. protected override void ReadConfig()
  49. {
  50. IConfig networkConfig = Config.Configs["Network"];
  51. if (networkConfig == null)
  52. {
  53. System.Console.WriteLine("ERROR: Section [Network] not found, server can't start");
  54. Environment.Exit(1);
  55. }
  56. uint port = (uint)networkConfig.GetInt("port", 0);
  57. if (port == 0)
  58. {
  59. System.Console.WriteLine("ERROR: No 'port' entry found in [Network]. Server can't start");
  60. Environment.Exit(1);
  61. }
  62. bool ssl_main = networkConfig.GetBoolean("https_main",false);
  63. bool ssl_listener = networkConfig.GetBoolean("https_listener",false);
  64. bool ssl_external = networkConfig.GetBoolean("https_external",false);
  65. m_consolePort = (uint)networkConfig.GetInt("ConsolePort", 0);
  66. BaseHttpServer httpServer = null;
  67. //
  68. // This is where to make the servers:
  69. //
  70. //
  71. // Make the base server according to the port, etc.
  72. // ADD: Possibility to make main server ssl
  73. // Then, check for https settings and ADD a server to
  74. // m_Servers
  75. //
  76. if (!ssl_main)
  77. {
  78. httpServer = new BaseHttpServer(port);
  79. }
  80. else
  81. {
  82. string cert_path = networkConfig.GetString("cert_path",String.Empty);
  83. if (cert_path == String.Empty)
  84. {
  85. System.Console.WriteLine("ERROR: Path to X509 certificate is missing, server can't start.");
  86. Environment.Exit(1);
  87. }
  88. string cert_pass = networkConfig.GetString("cert_pass",String.Empty);
  89. if (cert_pass == String.Empty)
  90. {
  91. System.Console.WriteLine("ERROR: Password for X509 certificate is missing, server can't start.");
  92. Environment.Exit(1);
  93. }
  94. httpServer = new BaseHttpServer(port, ssl_main, cert_path, cert_pass);
  95. }
  96. MainServer.AddHttpServer(httpServer);
  97. MainServer.Instance = httpServer;
  98. // If https_listener = true, then add an ssl listener on the https_port...
  99. if (ssl_listener == true)
  100. {
  101. uint https_port = (uint)networkConfig.GetInt("https_port", 0);
  102. m_log.WarnFormat("[SSL]: External flag is {0}", ssl_external);
  103. if (!ssl_external)
  104. {
  105. string cert_path = networkConfig.GetString("cert_path",String.Empty);
  106. if ( cert_path == String.Empty )
  107. {
  108. System.Console.WriteLine("Path to X509 certificate is missing, server can't start.");
  109. Thread.CurrentThread.Abort();
  110. }
  111. string cert_pass = networkConfig.GetString("cert_pass",String.Empty);
  112. if ( cert_pass == String.Empty )
  113. {
  114. System.Console.WriteLine("Password for X509 certificate is missing, server can't start.");
  115. Thread.CurrentThread.Abort();
  116. }
  117. MainServer.AddHttpServer(new BaseHttpServer(https_port, ssl_listener, cert_path, cert_pass));
  118. }
  119. else
  120. {
  121. m_log.WarnFormat("[SSL]: SSL port is active but no SSL is used because external SSL was requested.");
  122. MainServer.AddHttpServer(new BaseHttpServer(https_port));
  123. }
  124. }
  125. }
  126. protected override void Initialise()
  127. {
  128. foreach (BaseHttpServer s in MainServer.Servers.Values)
  129. s.Start();
  130. MainServer.RegisterHttpConsoleCommands(MainConsole.Instance);
  131. MethodInfo mi = m_console.GetType().GetMethod("SetServer", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(BaseHttpServer) }, null);
  132. if (mi != null)
  133. {
  134. if (m_consolePort == 0)
  135. mi.Invoke(MainConsole.Instance, new object[] { MainServer.Instance });
  136. else
  137. mi.Invoke(MainConsole.Instance, new object[] { MainServer.GetHttpServer(m_consolePort) });
  138. }
  139. }
  140. }
  141. }