OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
HttpServerBase.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.Threading;
31 using System.Reflection;
32 using OpenSim.Framework;
33 using OpenSim.Framework.Console;
34 using OpenSim.Framework.Servers;
35 using OpenSim.Framework.Servers.HttpServer;
36 using log4net;
37 using Nini.Config;
38 
39 namespace OpenSim.Server.Base
40 {
42  {
43  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
44 
45  private uint m_consolePort;
46 
47  // Handle all the automagical stuff
48  //
49  public HttpServerBase(string prompt, string[] args) : base(prompt, args)
50  {
51  }
52 
53  protected override void ReadConfig()
54  {
55  IConfig networkConfig = Config.Configs["Network"];
56 
57  if (networkConfig == null)
58  {
59  System.Console.WriteLine("ERROR: Section [Network] not found, server can't start");
60  Environment.Exit(1);
61  }
62 
63  uint port = (uint)networkConfig.GetInt("port", 0);
64 
65  if (port == 0)
66  {
67  System.Console.WriteLine("ERROR: No 'port' entry found in [Network]. Server can't start");
68  Environment.Exit(1);
69  }
70 
71  bool ssl_main = networkConfig.GetBoolean("https_main",false);
72  bool ssl_listener = networkConfig.GetBoolean("https_listener",false);
73  bool ssl_external = networkConfig.GetBoolean("https_external",false);
74 
75  m_consolePort = (uint)networkConfig.GetInt("ConsolePort", 0);
76 
78 
79  //
80  // This is where to make the servers:
81  //
82  //
83  // Make the base server according to the port, etc.
84  // ADD: Possibility to make main server ssl
85  // Then, check for https settings and ADD a server to
86  // m_Servers
87  //
88  if (!ssl_main)
89  {
90  httpServer = new BaseHttpServer(port);
91  }
92  else
93  {
94  string cert_path = networkConfig.GetString("cert_path",String.Empty);
95  if (cert_path == String.Empty)
96  {
97  System.Console.WriteLine("ERROR: Path to X509 certificate is missing, server can't start.");
98  Environment.Exit(1);
99  }
100 
101  string cert_pass = networkConfig.GetString("cert_pass",String.Empty);
102  if (cert_pass == String.Empty)
103  {
104  System.Console.WriteLine("ERROR: Password for X509 certificate is missing, server can't start.");
105  Environment.Exit(1);
106  }
107 
108  httpServer = new BaseHttpServer(port, ssl_main, cert_path, cert_pass);
109  }
110 
111  MainServer.AddHttpServer(httpServer);
112  MainServer.Instance = httpServer;
113 
114  // If https_listener = true, then add an ssl listener on the https_port...
115  if (ssl_listener == true)
116  {
117  uint https_port = (uint)networkConfig.GetInt("https_port", 0);
118 
119  m_log.WarnFormat("[SSL]: External flag is {0}", ssl_external);
120  if (!ssl_external)
121  {
122  string cert_path = networkConfig.GetString("cert_path",String.Empty);
123  if ( cert_path == String.Empty )
124  {
125  System.Console.WriteLine("Path to X509 certificate is missing, server can't start.");
126  Thread.CurrentThread.Abort();
127  }
128  string cert_pass = networkConfig.GetString("cert_pass",String.Empty);
129  if ( cert_pass == String.Empty )
130  {
131  System.Console.WriteLine("Password for X509 certificate is missing, server can't start.");
132  Thread.CurrentThread.Abort();
133  }
134 
135  MainServer.AddHttpServer(new BaseHttpServer(https_port, ssl_listener, cert_path, cert_pass));
136  }
137  else
138  {
139  m_log.WarnFormat("[SSL]: SSL port is active but no SSL is used because external SSL was requested.");
140  MainServer.AddHttpServer(new BaseHttpServer(https_port));
141  }
142  }
143  }
144 
145  protected override void Initialise()
146  {
147  foreach (BaseHttpServer s in MainServer.Servers.Values)
148  s.Start();
149 
150  MainServer.RegisterHttpConsoleCommands(MainConsole.Instance);
151 
153  {
154  if (m_consolePort == 0)
155  ((RemoteConsole)MainConsole.Instance).SetServer(MainServer.Instance);
156  else
157  ((RemoteConsole)MainConsole.Instance).SetServer(MainServer.GetHttpServer(m_consolePort));
158  }
159  }
160  }
161 }
static IHttpServer GetHttpServer(uint port)
Get the default http server or an http server for a specific port.
Definition: MainServer.cs:318
static Dictionary< uint, BaseHttpServer > Servers
Get all the registered servers.
Definition: MainServer.cs:104
static ICommandConsole Instance
Definition: MainConsole.cs:35
HttpServerBase(string prompt, string[] args)