Main.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 OpenSim 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.IO;
  30. using System.Net;
  31. using System.Reflection;
  32. using libsecondlife;
  33. using log4net;
  34. using log4net.Config;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Communications.Cache;
  37. using OpenSim.Framework.Console;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Framework.Statistics;
  40. namespace OpenSim.Grid.UserServer
  41. {
  42. /// <summary>
  43. /// </summary>
  44. public class OpenUser_Main : BaseOpenSimServer, conscmd_callback
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private UserConfig Cfg;
  48. public UserManager m_userManager;
  49. public UserLoginService m_loginService;
  50. public MessageServersConnector m_messagesService;
  51. private LLUUID m_lastCreatedUser = LLUUID.Random();
  52. [STAThread]
  53. public static void Main(string[] args)
  54. {
  55. XmlConfigurator.Configure();
  56. m_log.Info("Launching UserServer...");
  57. OpenUser_Main userserver = new OpenUser_Main();
  58. userserver.Startup();
  59. userserver.Work();
  60. }
  61. private OpenUser_Main()
  62. {
  63. m_console = new ConsoleBase("User", this);
  64. MainConsole.Instance = m_console;
  65. }
  66. private void Work()
  67. {
  68. m_console.Notice("Enter help for a list of commands\n");
  69. while (true)
  70. {
  71. m_console.Prompt();
  72. }
  73. }
  74. public void Startup()
  75. {
  76. Cfg = new UserConfig("USER SERVER", (Path.Combine(Util.configDir(), "UserServer_Config.xml")));
  77. m_stats = StatsManager.StartCollectingUserStats();
  78. m_log.Info("[REGION]: Establishing data connection");
  79. m_userManager = new UserManager();
  80. m_userManager._config = Cfg;
  81. m_userManager.AddPlugin(Cfg.DatabaseProvider, Cfg.DatabaseConnect);
  82. m_loginService = new UserLoginService(
  83. m_userManager, new LibraryRootFolder(), Cfg, Cfg.DefaultStartupMsg);
  84. m_messagesService = new MessageServersConnector();
  85. m_loginService.OnUserLoggedInAtLocation += NotifyMessageServersUserLoggedInToLocation;
  86. m_userManager.OnLogOffUser += NotifyMessageServersUserLoggOff;
  87. m_log.Info("[REGION]: Starting HTTP process");
  88. m_httpServer = new BaseHttpServer(Cfg.HttpPort);
  89. AddHttpHandlers();
  90. m_httpServer.Start();
  91. m_log.Info("[SERVER]: Userserver 0.5 - Startup complete");
  92. }
  93. protected void AddHttpHandlers()
  94. {
  95. m_httpServer.AddXmlRPCHandler("login_to_simulator", m_loginService.XmlRpcLoginMethod);
  96. m_httpServer.AddHTTPHandler("login", m_loginService.ProcessHTMLLogin);
  97. m_httpServer.SetLLSDHandler(m_loginService.LLSDLoginMethod);
  98. m_httpServer.AddXmlRPCHandler("get_user_by_name", m_userManager.XmlRPCGetUserMethodName);
  99. m_httpServer.AddXmlRPCHandler("get_user_by_uuid", m_userManager.XmlRPCGetUserMethodUUID);
  100. m_httpServer.AddXmlRPCHandler("get_avatar_picker_avatar", m_userManager.XmlRPCGetAvatarPickerAvatar);
  101. m_httpServer.AddXmlRPCHandler("add_new_user_friend", m_userManager.XmlRpcResponseXmlRPCAddUserFriend);
  102. m_httpServer.AddXmlRPCHandler("remove_user_friend", m_userManager.XmlRpcResponseXmlRPCRemoveUserFriend);
  103. m_httpServer.AddXmlRPCHandler("update_user_friend_perms", m_userManager.XmlRpcResponseXmlRPCUpdateUserFriendPerms);
  104. m_httpServer.AddXmlRPCHandler("get_user_friend_list", m_userManager.XmlRpcResponseXmlRPCGetUserFriendList);
  105. m_httpServer.AddXmlRPCHandler("get_avatar_appearance", m_userManager.XmlRPCGetAvatarAppearance);
  106. m_httpServer.AddXmlRPCHandler("update_avatar_appearance", m_userManager.XmlRPCUpdateAvatarAppearance);
  107. m_httpServer.AddXmlRPCHandler("logout_of_simulator", m_userManager.XmlRPCLogOffUserMethodUUID);
  108. // Message Server ---> User Server
  109. m_httpServer.AddXmlRPCHandler("register_messageserver", m_messagesService.XmlRPCRegisterMessageServer);
  110. m_httpServer.AddXmlRPCHandler("agent_change_region", m_messagesService.XmlRPCUserMovedtoRegion);
  111. m_httpServer.AddXmlRPCHandler("deregister_messageserver", m_messagesService.XmlRPCDeRegisterMessageServer);
  112. m_httpServer.AddStreamHandler(
  113. new RestStreamHandler("DELETE", "/usersessions/", m_userManager.RestDeleteUserSessionMethod));
  114. m_httpServer.AddXmlRPCHandler("update_user_profile", m_userManager.XmlRpcResponseXmlRPCUpdateUserProfile);
  115. }
  116. public void do_create(string what)
  117. {
  118. switch (what)
  119. {
  120. case "user":
  121. string tempfirstname;
  122. string templastname;
  123. string tempMD5Passwd;
  124. uint regX = 1000;
  125. uint regY = 1000;
  126. tempfirstname = m_console.CmdPrompt("First name");
  127. templastname = m_console.CmdPrompt("Last name");
  128. //tempMD5Passwd = m_console.PasswdPrompt("Password");
  129. tempMD5Passwd = m_console.CmdPrompt("Password");
  130. regX = Convert.ToUInt32(m_console.CmdPrompt("Start Region X"));
  131. regY = Convert.ToUInt32(m_console.CmdPrompt("Start Region Y"));
  132. if (null != m_userManager.GetUserProfile(tempfirstname, templastname))
  133. {
  134. m_log.ErrorFormat(
  135. "[USERS]: A user with the name {0} {1} already exists!", tempfirstname, templastname);
  136. break;
  137. }
  138. tempMD5Passwd = Util.Md5Hash(Util.Md5Hash(tempMD5Passwd) + ":" + String.Empty);
  139. LLUUID userID = new LLUUID();
  140. try
  141. {
  142. userID = m_userManager.AddUserProfile(tempfirstname, templastname, tempMD5Passwd, regX, regY);
  143. }
  144. catch (Exception ex)
  145. {
  146. m_log.ErrorFormat("[USERS]: Error creating user: {0}", ex.ToString());
  147. }
  148. try
  149. {
  150. bool created
  151. = SynchronousRestObjectPoster.BeginPostObject<Guid, bool>(
  152. "POST", m_userManager._config.InventoryUrl + "CreateInventory/", userID.UUID);
  153. if (!created)
  154. {
  155. throw new Exception(
  156. String.Format(
  157. "The inventory creation request for user {0} did not succeed."
  158. + " Please contact your inventory service provider for more information.",
  159. userID));
  160. }
  161. }
  162. catch (WebException)
  163. {
  164. m_log.ErrorFormat(
  165. "[USERS]: Could not contact the inventory service at {0} to create an inventory for {1}",
  166. m_userManager._config.InventoryUrl + "CreateInventory/", userID.UUID);
  167. }
  168. catch (Exception e)
  169. {
  170. m_log.ErrorFormat("[USERS]: Error creating inventory for user: {0}", e);
  171. }
  172. m_lastCreatedUser = userID;
  173. break;
  174. }
  175. }
  176. public override void RunCmd(string cmd, string[] cmdparams)
  177. {
  178. base.RunCmd(cmd, cmdparams);
  179. switch (cmd)
  180. {
  181. case "help":
  182. m_console.Notice("create user - create a new user");
  183. break;
  184. case "create":
  185. do_create(cmdparams[0]);
  186. break;
  187. case "test-inventory":
  188. // RestObjectPosterResponse<List<InventoryFolderBase>> requester = new RestObjectPosterResponse<List<InventoryFolderBase>>();
  189. // requester.ReturnResponseVal = TestResponse;
  190. // requester.BeginPostObject<LLUUID>(m_userManager._config.InventoryUrl + "RootFolders/", m_lastCreatedUser);
  191. SynchronousRestObjectPoster.BeginPostObject<LLUUID, List<InventoryFolderBase>>("POST",
  192. m_userManager.
  193. _config.
  194. InventoryUrl +
  195. "RootFolders/",
  196. m_lastCreatedUser);
  197. break;
  198. }
  199. }
  200. public override void Shutdown()
  201. {
  202. m_loginService.OnUserLoggedInAtLocation -= NotifyMessageServersUserLoggedInToLocation;
  203. base.Shutdown();
  204. }
  205. public void TestResponse(List<InventoryFolderBase> resp)
  206. {
  207. m_console.Notice("response got");
  208. }
  209. public void NotifyMessageServersUserLoggOff(LLUUID agentID)
  210. {
  211. m_messagesService.TellMessageServersAboutUserLogoff(agentID);
  212. }
  213. public void NotifyMessageServersUserLoggedInToLocation(LLUUID agentID, LLUUID sessionID, LLUUID RegionID,
  214. ulong regionhandle, float positionX, float positionY,
  215. float positionZ, string firstname, string lastname)
  216. {
  217. m_messagesService.TellMessageServersAboutUser(agentID, sessionID, RegionID, regionhandle, positionX,
  218. positionY, positionZ, firstname, lastname);
  219. }
  220. }
  221. }