Main.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 override void Startup()
  75. {
  76. base.Startup();
  77. Cfg = new UserConfig("USER SERVER", (Path.Combine(Util.configDir(), "UserServer_Config.xml")));
  78. m_stats = StatsManager.StartCollectingUserStats();
  79. m_log.Info("[REGION]: Establishing data connection");
  80. m_userManager = new UserManager();
  81. m_userManager._config = Cfg;
  82. m_userManager.AddPlugin(Cfg.DatabaseProvider, Cfg.DatabaseConnect);
  83. m_loginService = new UserLoginService(
  84. m_userManager, new LibraryRootFolder(), Cfg, Cfg.DefaultStartupMsg);
  85. m_messagesService = new MessageServersConnector();
  86. m_loginService.OnUserLoggedInAtLocation += NotifyMessageServersUserLoggedInToLocation;
  87. m_userManager.OnLogOffUser += NotifyMessageServersUserLoggOff;
  88. m_log.Info("[REGION]: Starting HTTP process");
  89. m_httpServer = new BaseHttpServer(Cfg.HttpPort);
  90. AddHttpHandlers();
  91. m_httpServer.Start();
  92. m_log.Info("[SERVER]: Userserver 0.5 - Startup complete");
  93. }
  94. protected void AddHttpHandlers()
  95. {
  96. m_httpServer.AddXmlRPCHandler("login_to_simulator", m_loginService.XmlRpcLoginMethod);
  97. m_httpServer.AddHTTPHandler("login", m_loginService.ProcessHTMLLogin);
  98. if (Cfg.EnableLLSDLogin)
  99. {
  100. m_httpServer.SetLLSDHandler(m_loginService.LLSDLoginMethod);
  101. }
  102. m_httpServer.AddXmlRPCHandler("get_user_by_name", m_userManager.XmlRPCGetUserMethodName);
  103. m_httpServer.AddXmlRPCHandler("get_user_by_uuid", m_userManager.XmlRPCGetUserMethodUUID);
  104. m_httpServer.AddXmlRPCHandler("get_avatar_picker_avatar", m_userManager.XmlRPCGetAvatarPickerAvatar);
  105. m_httpServer.AddXmlRPCHandler("add_new_user_friend", m_userManager.XmlRpcResponseXmlRPCAddUserFriend);
  106. m_httpServer.AddXmlRPCHandler("remove_user_friend", m_userManager.XmlRpcResponseXmlRPCRemoveUserFriend);
  107. m_httpServer.AddXmlRPCHandler("update_user_friend_perms", m_userManager.XmlRpcResponseXmlRPCUpdateUserFriendPerms);
  108. m_httpServer.AddXmlRPCHandler("get_user_friend_list", m_userManager.XmlRpcResponseXmlRPCGetUserFriendList);
  109. m_httpServer.AddXmlRPCHandler("get_avatar_appearance", m_userManager.XmlRPCGetAvatarAppearance);
  110. m_httpServer.AddXmlRPCHandler("update_avatar_appearance", m_userManager.XmlRPCUpdateAvatarAppearance);
  111. m_httpServer.AddXmlRPCHandler("update_user_current_region", m_userManager.XmlRPCAtRegion);
  112. m_httpServer.AddXmlRPCHandler("logout_of_simulator", m_userManager.XmlRPCLogOffUserMethodUUID);
  113. m_httpServer.AddXmlRPCHandler("get_agent_by_uuid", m_userManager.XmlRPCGetAgentMethodUUID);
  114. // Message Server ---> User Server
  115. m_httpServer.AddXmlRPCHandler("register_messageserver", m_messagesService.XmlRPCRegisterMessageServer);
  116. m_httpServer.AddXmlRPCHandler("agent_change_region", m_messagesService.XmlRPCUserMovedtoRegion);
  117. m_httpServer.AddXmlRPCHandler("deregister_messageserver", m_messagesService.XmlRPCDeRegisterMessageServer);
  118. m_httpServer.AddStreamHandler(
  119. new RestStreamHandler("DELETE", "/usersessions/", m_userManager.RestDeleteUserSessionMethod));
  120. m_httpServer.AddXmlRPCHandler("update_user_profile", m_userManager.XmlRpcResponseXmlRPCUpdateUserProfile);
  121. }
  122. public void do_create(string what)
  123. {
  124. switch (what)
  125. {
  126. case "user":
  127. string tempfirstname;
  128. string templastname;
  129. string tempMD5Passwd;
  130. uint regX = 1000;
  131. uint regY = 1000;
  132. tempfirstname = m_console.CmdPrompt("First name");
  133. templastname = m_console.CmdPrompt("Last name");
  134. //tempMD5Passwd = m_console.PasswdPrompt("Password");
  135. tempMD5Passwd = m_console.CmdPrompt("Password");
  136. regX = Convert.ToUInt32(m_console.CmdPrompt("Start Region X"));
  137. regY = Convert.ToUInt32(m_console.CmdPrompt("Start Region Y"));
  138. if (null != m_userManager.GetUserProfile(tempfirstname, templastname))
  139. {
  140. m_log.ErrorFormat(
  141. "[USERS]: A user with the name {0} {1} already exists!", tempfirstname, templastname);
  142. break;
  143. }
  144. tempMD5Passwd = Util.Md5Hash(Util.Md5Hash(tempMD5Passwd) + ":" + String.Empty);
  145. LLUUID userID = new LLUUID();
  146. try
  147. {
  148. userID = m_userManager.AddUserProfile(tempfirstname, templastname, tempMD5Passwd, regX, regY);
  149. }
  150. catch (Exception ex)
  151. {
  152. m_log.ErrorFormat("[USERS]: Error creating user: {0}", ex.ToString());
  153. }
  154. try
  155. {
  156. bool created
  157. = SynchronousRestObjectPoster.BeginPostObject<Guid, bool>(
  158. "POST", m_userManager._config.InventoryUrl + "CreateInventory/", userID.UUID);
  159. if (!created)
  160. {
  161. throw new Exception(
  162. String.Format(
  163. "The inventory creation request for user {0} did not succeed."
  164. + " Please contact your inventory service provider for more information.",
  165. userID));
  166. }
  167. }
  168. catch (WebException)
  169. {
  170. m_log.ErrorFormat(
  171. "[USERS]: Could not contact the inventory service at {0} to create an inventory for {1}",
  172. m_userManager._config.InventoryUrl + "CreateInventory/", userID.UUID);
  173. }
  174. catch (Exception e)
  175. {
  176. m_log.ErrorFormat("[USERS]: Error creating inventory for user: {0}", e);
  177. }
  178. m_lastCreatedUser = userID;
  179. break;
  180. }
  181. }
  182. public override void RunCmd(string cmd, string[] cmdparams)
  183. {
  184. base.RunCmd(cmd, cmdparams);
  185. switch (cmd)
  186. {
  187. case "help":
  188. m_console.Notice("create user - create a new user");
  189. m_console.Notice("logoff-user <firstname> <lastname> <message> - logs off the specified user from the grid");
  190. break;
  191. case "create":
  192. do_create(cmdparams[0]);
  193. break;
  194. case "test-inventory":
  195. // RestObjectPosterResponse<List<InventoryFolderBase>> requester = new RestObjectPosterResponse<List<InventoryFolderBase>>();
  196. // requester.ReturnResponseVal = TestResponse;
  197. // requester.BeginPostObject<LLUUID>(m_userManager._config.InventoryUrl + "RootFolders/", m_lastCreatedUser);
  198. SynchronousRestObjectPoster.BeginPostObject<LLUUID, List<InventoryFolderBase>>("POST",
  199. m_userManager.
  200. _config.
  201. InventoryUrl +
  202. "RootFolders/",
  203. m_lastCreatedUser);
  204. break;
  205. case "logoff-user":
  206. if (cmdparams.Length >= 3)
  207. {
  208. string firstname = cmdparams[0];
  209. string lastname = cmdparams[1];
  210. string message = "";
  211. for (int i = 2; i < cmdparams.Length; i++)
  212. message += " " + cmdparams[i];
  213. UserProfileData theUser = null;
  214. try
  215. {
  216. theUser = m_loginService.GetTheUser(firstname, lastname);
  217. }
  218. catch (Exception)
  219. {
  220. m_log.Error("[LOGOFF]: Error getting user data from the database.");
  221. }
  222. if (theUser != null)
  223. {
  224. if (theUser.CurrentAgent != null)
  225. {
  226. if (theUser.CurrentAgent.AgentOnline)
  227. {
  228. m_log.Info("[LOGOFF]: Logging off requested user!");
  229. m_loginService.LogOffUser(theUser, message);
  230. theUser.CurrentAgent.AgentOnline = false;
  231. m_loginService.CommitAgent(ref theUser);
  232. }
  233. else
  234. {
  235. m_log.Info("[LOGOFF]: User Doesn't appear to be online, sending the logoff message anyway.");
  236. m_loginService.LogOffUser(theUser, message);
  237. theUser.CurrentAgent.AgentOnline = false;
  238. m_loginService.CommitAgent(ref theUser);
  239. }
  240. }
  241. else
  242. {
  243. m_log.Error("[LOGOFF]: Unable to logoff-user. User doesn't have an agent record so I can't find the simulator to notify");
  244. }
  245. }
  246. else
  247. {
  248. m_log.Info("[LOGOFF]: User doesn't exist in the database");
  249. }
  250. }
  251. else
  252. {
  253. m_log.Error("[LOGOFF]: Invalid amount of parameters. logoff-user takes at least three. Firstname, Lastname, and message");
  254. }
  255. break;
  256. }
  257. }
  258. public override void Shutdown()
  259. {
  260. m_loginService.OnUserLoggedInAtLocation -= NotifyMessageServersUserLoggedInToLocation;
  261. base.Shutdown();
  262. }
  263. public void TestResponse(List<InventoryFolderBase> resp)
  264. {
  265. m_console.Notice("response got");
  266. }
  267. public void NotifyMessageServersUserLoggOff(LLUUID agentID)
  268. {
  269. m_messagesService.TellMessageServersAboutUserLogoff(agentID);
  270. }
  271. public void NotifyMessageServersUserLoggedInToLocation(LLUUID agentID, LLUUID sessionID, LLUUID RegionID,
  272. ulong regionhandle, float positionX, float positionY,
  273. float positionZ, string firstname, string lastname)
  274. {
  275. m_messagesService.TellMessageServersAboutUser(agentID, sessionID, RegionID, regionhandle, positionX,
  276. positionY, positionZ, firstname, lastname);
  277. }
  278. }
  279. }