Main.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections;
  31. using System.IO;
  32. using libsecondlife;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications.Cache;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Framework.Servers;
  37. using Nwc.XmlRpc;
  38. namespace OpenSim.Grid.UserServer
  39. {
  40. /// <summary>
  41. /// </summary>
  42. public class OpenUser_Main : conscmd_callback
  43. {
  44. private UserConfig Cfg;
  45. public UserManager m_userManager;
  46. public UserLoginService m_loginService;
  47. public MessageServersConnector m_messagesService;
  48. private LogBase m_console;
  49. private LLUUID m_lastCreatedUser = LLUUID.Random();
  50. private Boolean m_rexMode;
  51. [STAThread]
  52. public static void Main(string[] args)
  53. {
  54. Console.WriteLine("Launching UserServer...");
  55. OpenUser_Main userserver = new OpenUser_Main();
  56. userserver.Startup();
  57. userserver.Work();
  58. }
  59. private OpenUser_Main()
  60. {
  61. if (!Directory.Exists(Util.logDir()))
  62. {
  63. Directory.CreateDirectory(Util.logDir());
  64. }
  65. m_console =
  66. new LogBase((Path.Combine(Util.logDir(), "opengrid-userserver-console.log")), "OpenUser", this, true);
  67. MainLog.Instance = m_console;
  68. }
  69. private void Work()
  70. {
  71. m_console.Notice("Enter help for a list of commands\n");
  72. while (true)
  73. {
  74. m_console.MainLogPrompt();
  75. }
  76. }
  77. public void Startup()
  78. {
  79. Cfg = new UserConfig("USER SERVER", (Path.Combine(Util.configDir(), "UserServer_Config.xml")));
  80. MainLog.Instance.Verbose("REGION", "Establishing data connection");
  81. m_userManager = new UserManager();
  82. m_userManager._config = Cfg;
  83. m_userManager.AddPlugin(Cfg.DatabaseProvider);
  84. m_loginService = new UserLoginService(
  85. m_userManager, new LibraryRootFolder(), Cfg, Cfg.DefaultStartupMsg);
  86. m_rexMode = Cfg.RexMode;
  87. m_messagesService = new MessageServersConnector(MainLog.Instance);
  88. m_loginService.OnUserLoggedInAtLocation += NotifyMessageServersUserLoggedInToLocation;
  89. MainLog.Instance.Verbose("REGION", "Starting HTTP process");
  90. BaseHttpServer httpServer = new BaseHttpServer(Cfg.HttpPort);
  91. httpServer.AddXmlRPCHandler("login_to_simulator", m_loginService.XmlRpcLoginMethod);
  92. httpServer.SetLLSDHandler(m_loginService.LLSDLoginMethod);
  93. if (!m_rexMode) {
  94. httpServer.AddXmlRPCHandler("get_user_by_name", m_userManager.XmlRPCGetUserMethodName);
  95. httpServer.AddXmlRPCHandler("get_user_by_uuid", m_userManager.XmlRPCGetUserMethodUUID);
  96. httpServer.AddXmlRPCHandler("get_avatar_picker_avatar", m_userManager.XmlRPCGetAvatarPickerAvatar);
  97. httpServer.AddXmlRPCHandler("add_new_user_friend", m_userManager.XmlRpcResponseXmlRPCAddUserFriend);
  98. httpServer.AddXmlRPCHandler("remove_user_friend", m_userManager.XmlRpcResponseXmlRPCRemoveUserFriend);
  99. httpServer.AddXmlRPCHandler("update_user_friend_perms", m_userManager.XmlRpcResponseXmlRPCUpdateUserFriendPerms);
  100. httpServer.AddXmlRPCHandler("get_user_friend_list", m_userManager.XmlRpcResponseXmlRPCGetUserFriendList);
  101. // Message Server ---> User Server
  102. httpServer.AddXmlRPCHandler("register_messageserver", m_messagesService.XmlRPCRegisterMessageServer);
  103. httpServer.AddXmlRPCHandler("agent_change_region", m_messagesService.XmlRPCUserMovedtoRegion);
  104. httpServer.AddXmlRPCHandler("deregister_messageserver", m_messagesService.XmlRPCDeRegisterMessageServer);
  105. }
  106. else {
  107. httpServer.AddXmlRPCHandler("get_user_by_name", new RexRemoteHandler("get_user_by_name").rexRemoteXmlRPCHandler);
  108. httpServer.AddXmlRPCHandler("get_user_by_uuid", new RexRemoteHandler("get_user_by_uuid").rexRemoteXmlRPCHandler);
  109. httpServer.AddXmlRPCHandler("get_avatar_picker_avatar", new RexRemoteHandler("get_avatar_picker_avatar").rexRemoteXmlRPCHandler);
  110. httpServer.AddXmlRPCHandler("add_new_user_friend", new RexRemoteHandler("add_new_user_friend").rexRemoteXmlRPCHandler);
  111. httpServer.AddXmlRPCHandler("remove_user_friend", new RexRemoteHandler("remove_user_friend").rexRemoteXmlRPCHandler);
  112. httpServer.AddXmlRPCHandler("update_user_friend_perms", new RexRemoteHandler("update_user_friend_perms").rexRemoteXmlRPCHandler);
  113. httpServer.AddXmlRPCHandler("get_user_friend_list", new RexRemoteHandler("get_user_friend_list").rexRemoteXmlRPCHandler);
  114. // Message Server ---> User Server
  115. httpServer.AddXmlRPCHandler("register_messageserver", new RexRemoteHandler("register_messageserver").rexRemoteXmlRPCHandler);
  116. httpServer.AddXmlRPCHandler("agent_change_region", new RexRemoteHandler("agent_change_region").rexRemoteXmlRPCHandler);
  117. httpServer.AddXmlRPCHandler("deregister_messageserver", new RexRemoteHandler("deregister_messageserver").rexRemoteXmlRPCHandler);
  118. }
  119. httpServer.AddStreamHandler(
  120. new RestStreamHandler("DELETE", "/usersessions/", m_userManager.RestDeleteUserSessionMethod));
  121. httpServer.Start();
  122. m_console.Status("SERVER", "Userserver 0.4 - Startup complete");
  123. }
  124. public void do_create(string what)
  125. {
  126. switch (what)
  127. {
  128. case "user":
  129. string tempfirstname;
  130. string templastname;
  131. string tempMD5Passwd;
  132. uint regX = 1000;
  133. uint regY = 1000;
  134. tempfirstname = m_console.CmdPrompt("First name");
  135. templastname = m_console.CmdPrompt("Last name");
  136. tempMD5Passwd = m_console.PasswdPrompt("Password");
  137. regX = Convert.ToUInt32(m_console.CmdPrompt("Start Region X"));
  138. regY = Convert.ToUInt32(m_console.CmdPrompt("Start Region Y"));
  139. tempMD5Passwd = Util.Md5Hash(Util.Md5Hash(tempMD5Passwd) + ":" + "");
  140. LLUUID userID = new LLUUID();
  141. try
  142. {
  143. userID =
  144. m_userManager.AddUserProfile(tempfirstname, templastname, tempMD5Passwd, regX, regY);
  145. } catch (Exception ex)
  146. {
  147. m_console.Error("SERVER", "Error creating user: {0}", ex.ToString());
  148. }
  149. try
  150. {
  151. RestObjectPoster.BeginPostObject<Guid>(m_userManager._config.InventoryUrl + "CreateInventory/",
  152. userID.UUID);
  153. }
  154. catch (Exception ex)
  155. {
  156. m_console.Error("SERVER", "Error creating inventory for user: {0}", ex.ToString());
  157. }
  158. m_lastCreatedUser = userID;
  159. break;
  160. }
  161. }
  162. public void RunCmd(string cmd, string[] cmdparams)
  163. {
  164. switch (cmd)
  165. {
  166. case "help":
  167. m_console.Notice("create user - create a new user");
  168. m_console.Notice("shutdown - shutdown the grid (USE CAUTION!)");
  169. break;
  170. case "create":
  171. do_create(cmdparams[0]);
  172. break;
  173. case "shutdown":
  174. m_loginService.OnUserLoggedInAtLocation -= NotifyMessageServersUserLoggedInToLocation;
  175. m_console.Close();
  176. Environment.Exit(0);
  177. break;
  178. case "test-inventory":
  179. // RestObjectPosterResponse<List<InventoryFolderBase>> requester = new RestObjectPosterResponse<List<InventoryFolderBase>>();
  180. // requester.ReturnResponseVal = TestResponse;
  181. // requester.BeginPostObject<LLUUID>(m_userManager._config.InventoryUrl + "RootFolders/", m_lastCreatedUser);
  182. List<InventoryFolderBase> folders =
  183. SynchronousRestObjectPoster.BeginPostObject<LLUUID, List<InventoryFolderBase>>("POST",
  184. m_userManager.
  185. _config.
  186. InventoryUrl +
  187. "RootFolders/",
  188. m_lastCreatedUser);
  189. break;
  190. }
  191. }
  192. public void TestResponse(List<InventoryFolderBase> resp)
  193. {
  194. Console.WriteLine("response got");
  195. }
  196. public void NotifyMessageServersUserLoggedInToLocation(LLUUID agentID, LLUUID sessionID, LLUUID RegionID, ulong regionhandle, LLVector3 Position)
  197. {
  198. m_messagesService.TellMessageServersAboutUser(agentID, sessionID, RegionID, regionhandle, Position);
  199. }
  200. /*private void ConfigDB(IGenericConfig configData)
  201. {
  202. try
  203. {
  204. string attri = "";
  205. attri = configData.GetAttribute("DataBaseProvider");
  206. if (attri == "")
  207. {
  208. StorageDll = "OpenSim.Framework.Data.DB4o.dll";
  209. configData.SetAttribute("DataBaseProvider", "OpenSim.Framework.Data.DB4o.dll");
  210. }
  211. else
  212. {
  213. StorageDll = attri;
  214. }
  215. configData.Commit();
  216. }
  217. catch
  218. {
  219. }
  220. }*/
  221. public void Show(string ShowWhat)
  222. {
  223. }
  224. }
  225. /// <summary>
  226. /// for forwarding some requests to authentication server
  227. /// </summary>
  228. class RexRemoteHandler
  229. {
  230. private string methodName;
  231. public RexRemoteHandler(String method)
  232. {
  233. methodName = method;
  234. }
  235. public XmlRpcResponse rexRemoteXmlRPCHandler(XmlRpcRequest request)
  236. {
  237. Hashtable requestData = (Hashtable)request.Params[0];
  238. string authAddr;
  239. if (requestData.Contains("AuthenticationAddress") && requestData["AuthenticationAddress"] != null)
  240. {
  241. authAddr = (string)requestData["AuthenticationAddress"];
  242. }
  243. else
  244. {
  245. return CreateErrorResponse("unknown_authentication",
  246. "Request did not contain authentication address");
  247. }
  248. ArrayList SendParams = new ArrayList();
  249. foreach (Object obj in request.Params)
  250. {
  251. SendParams.Add(obj);
  252. }
  253. XmlRpcRequest req = new XmlRpcRequest(methodName, SendParams);
  254. if(!authAddr.StartsWith("http://"))
  255. authAddr = "http://"+ authAddr;
  256. XmlRpcResponse res = req.Send(authAddr, 30000);
  257. return res;
  258. }
  259. public XmlRpcResponse CreateErrorResponse(string type, string desc)
  260. {
  261. XmlRpcResponse response = new XmlRpcResponse();
  262. Hashtable responseData = new Hashtable();
  263. responseData["error_type"] = type;
  264. responseData["error_desc"] = desc;
  265. response.Value = responseData;
  266. return response;
  267. }
  268. }
  269. }