UserAgentServerConnector.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Server.Base;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Server.Handlers.Base;
  38. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  39. using log4net;
  40. using Nwc.XmlRpc;
  41. using OpenMetaverse;
  42. namespace OpenSim.Server.Handlers.Hypergrid
  43. {
  44. public class UserAgentServerConnector : ServiceConnector
  45. {
  46. // private static readonly ILog m_log =
  47. // LogManager.GetLogger(
  48. // MethodBase.GetCurrentMethod().DeclaringType);
  49. private IUserAgentService m_HomeUsersService;
  50. public IUserAgentService HomeUsersService
  51. {
  52. get { return m_HomeUsersService; }
  53. }
  54. private string[] m_AuthorizedCallers;
  55. private bool m_VerifyCallers = false;
  56. public UserAgentServerConnector(IConfigSource config, IHttpServer server) :
  57. this(config, server, (IFriendsSimConnector)null)
  58. {
  59. }
  60. public UserAgentServerConnector(IConfigSource config, IHttpServer server, string configName) :
  61. this(config, server)
  62. {
  63. }
  64. public UserAgentServerConnector(IConfigSource config, IHttpServer server, IFriendsSimConnector friendsConnector) :
  65. base(config, server, String.Empty)
  66. {
  67. IConfig gridConfig = config.Configs["UserAgentService"];
  68. if (gridConfig != null)
  69. {
  70. string serviceDll = gridConfig.GetString("LocalServiceModule", string.Empty);
  71. Object[] args = new Object[] { config, friendsConnector };
  72. m_HomeUsersService = ServerUtils.LoadPlugin<IUserAgentService>(serviceDll, args);
  73. }
  74. if (m_HomeUsersService == null)
  75. throw new Exception("UserAgent server connector cannot proceed because of missing service");
  76. string loginServerIP = gridConfig.GetString("LoginServerIP", "127.0.0.1");
  77. bool proxy = gridConfig.GetBoolean("HasProxy", false);
  78. m_VerifyCallers = gridConfig.GetBoolean("VerifyCallers", false);
  79. string csv = gridConfig.GetString("AuthorizedCallers", "127.0.0.1");
  80. csv = csv.Replace(" ", "");
  81. m_AuthorizedCallers = csv.Split(',');
  82. server.AddXmlRPCHandler("agent_is_coming_home", AgentIsComingHome, false);
  83. server.AddXmlRPCHandler("get_home_region", GetHomeRegion, false);
  84. server.AddXmlRPCHandler("verify_agent", VerifyAgent, false);
  85. server.AddXmlRPCHandler("verify_client", VerifyClient, false);
  86. server.AddXmlRPCHandler("logout_agent", LogoutAgent, false);
  87. server.AddXmlRPCHandler("status_notification", StatusNotification, false);
  88. server.AddXmlRPCHandler("get_online_friends", GetOnlineFriends, false);
  89. server.AddXmlRPCHandler("get_user_info", GetUserInfo, false);
  90. server.AddXmlRPCHandler("get_server_urls", GetServerURLs, false);
  91. server.AddXmlRPCHandler("locate_user", LocateUser, false);
  92. server.AddXmlRPCHandler("get_uui", GetUUI, false);
  93. server.AddXmlRPCHandler("get_uuid", GetUUID, false);
  94. server.AddStreamHandler(new HomeAgentHandler(m_HomeUsersService, loginServerIP, proxy));
  95. }
  96. public XmlRpcResponse GetHomeRegion(XmlRpcRequest request, IPEndPoint remoteClient)
  97. {
  98. Hashtable requestData = (Hashtable)request.Params[0];
  99. //string host = (string)requestData["host"];
  100. //string portstr = (string)requestData["port"];
  101. string userID_str = (string)requestData["userID"];
  102. UUID userID = UUID.Zero;
  103. UUID.TryParse(userID_str, out userID);
  104. Vector3 position = Vector3.UnitY, lookAt = Vector3.UnitY;
  105. GridRegion regInfo = m_HomeUsersService.GetHomeRegion(userID, out position, out lookAt);
  106. Hashtable hash = new Hashtable();
  107. if (regInfo == null)
  108. hash["result"] = "false";
  109. else
  110. {
  111. hash["result"] = "true";
  112. hash["uuid"] = regInfo.RegionID.ToString();
  113. hash["x"] = regInfo.RegionLocX.ToString();
  114. hash["y"] = regInfo.RegionLocY.ToString();
  115. hash["size_x"] = regInfo.RegionSizeX.ToString();
  116. hash["size_y"] = regInfo.RegionSizeY.ToString();
  117. hash["region_name"] = regInfo.RegionName;
  118. hash["hostname"] = regInfo.ExternalHostName;
  119. hash["http_port"] = regInfo.HttpPort.ToString();
  120. hash["internal_port"] = regInfo.InternalEndPoint.Port.ToString();
  121. hash["position"] = position.ToString();
  122. hash["lookAt"] = lookAt.ToString();
  123. }
  124. XmlRpcResponse response = new XmlRpcResponse();
  125. response.Value = hash;
  126. return response;
  127. }
  128. public XmlRpcResponse AgentIsComingHome(XmlRpcRequest request, IPEndPoint remoteClient)
  129. {
  130. Hashtable requestData = (Hashtable)request.Params[0];
  131. //string host = (string)requestData["host"];
  132. //string portstr = (string)requestData["port"];
  133. string sessionID_str = (string)requestData["sessionID"];
  134. UUID sessionID = UUID.Zero;
  135. UUID.TryParse(sessionID_str, out sessionID);
  136. string gridName = (string)requestData["externalName"];
  137. bool success = m_HomeUsersService.IsAgentComingHome(sessionID, gridName);
  138. Hashtable hash = new Hashtable();
  139. hash["result"] = success.ToString();
  140. XmlRpcResponse response = new XmlRpcResponse();
  141. response.Value = hash;
  142. return response;
  143. }
  144. public XmlRpcResponse VerifyAgent(XmlRpcRequest request, IPEndPoint remoteClient)
  145. {
  146. Hashtable requestData = (Hashtable)request.Params[0];
  147. //string host = (string)requestData["host"];
  148. //string portstr = (string)requestData["port"];
  149. string sessionID_str = (string)requestData["sessionID"];
  150. UUID sessionID = UUID.Zero;
  151. UUID.TryParse(sessionID_str, out sessionID);
  152. string token = (string)requestData["token"];
  153. bool success = m_HomeUsersService.VerifyAgent(sessionID, token);
  154. Hashtable hash = new Hashtable();
  155. hash["result"] = success.ToString();
  156. XmlRpcResponse response = new XmlRpcResponse();
  157. response.Value = hash;
  158. return response;
  159. }
  160. public XmlRpcResponse VerifyClient(XmlRpcRequest request, IPEndPoint remoteClient)
  161. {
  162. Hashtable requestData = (Hashtable)request.Params[0];
  163. //string host = (string)requestData["host"];
  164. //string portstr = (string)requestData["port"];
  165. string sessionID_str = (string)requestData["sessionID"];
  166. UUID sessionID = UUID.Zero;
  167. UUID.TryParse(sessionID_str, out sessionID);
  168. string token = (string)requestData["token"];
  169. bool success = m_HomeUsersService.VerifyClient(sessionID, token);
  170. Hashtable hash = new Hashtable();
  171. hash["result"] = success.ToString();
  172. XmlRpcResponse response = new XmlRpcResponse();
  173. response.Value = hash;
  174. return response;
  175. }
  176. public XmlRpcResponse LogoutAgent(XmlRpcRequest request, IPEndPoint remoteClient)
  177. {
  178. Hashtable requestData = (Hashtable)request.Params[0];
  179. //string host = (string)requestData["host"];
  180. //string portstr = (string)requestData["port"];
  181. string sessionID_str = (string)requestData["sessionID"];
  182. UUID sessionID = UUID.Zero;
  183. UUID.TryParse(sessionID_str, out sessionID);
  184. string userID_str = (string)requestData["userID"];
  185. UUID userID = UUID.Zero;
  186. UUID.TryParse(userID_str, out userID);
  187. m_HomeUsersService.LogoutAgent(userID, sessionID);
  188. Hashtable hash = new Hashtable();
  189. hash["result"] = "true";
  190. XmlRpcResponse response = new XmlRpcResponse();
  191. response.Value = hash;
  192. return response;
  193. }
  194. [Obsolete]
  195. public XmlRpcResponse StatusNotification(XmlRpcRequest request, IPEndPoint remoteClient)
  196. {
  197. Hashtable hash = new Hashtable();
  198. hash["result"] = "false";
  199. Hashtable requestData = (Hashtable)request.Params[0];
  200. //string host = (string)requestData["host"];
  201. //string portstr = (string)requestData["port"];
  202. if (requestData.ContainsKey("userID") && requestData.ContainsKey("online"))
  203. {
  204. string userID_str = (string)requestData["userID"];
  205. UUID userID = UUID.Zero;
  206. UUID.TryParse(userID_str, out userID);
  207. List<string> ids = new List<string>();
  208. foreach (object key in requestData.Keys)
  209. {
  210. if (key is string && ((string)key).StartsWith("friend_") && requestData[key] != null)
  211. ids.Add(requestData[key].ToString());
  212. }
  213. bool online = false;
  214. bool.TryParse(requestData["online"].ToString(), out online);
  215. // let's spawn a thread for this, because it may take a long time...
  216. List<UUID> friendsOnline = m_HomeUsersService.StatusNotification(ids, userID, online);
  217. if (friendsOnline.Count > 0)
  218. {
  219. int i = 0;
  220. foreach (UUID id in friendsOnline)
  221. {
  222. hash["friend_" + i.ToString()] = id.ToString();
  223. i++;
  224. }
  225. }
  226. else
  227. hash["result"] = "No Friends Online";
  228. }
  229. XmlRpcResponse response = new XmlRpcResponse();
  230. response.Value = hash;
  231. return response;
  232. }
  233. [Obsolete]
  234. public XmlRpcResponse GetOnlineFriends(XmlRpcRequest request, IPEndPoint remoteClient)
  235. {
  236. Hashtable hash = new Hashtable();
  237. Hashtable requestData = (Hashtable)request.Params[0];
  238. //string host = (string)requestData["host"];
  239. //string portstr = (string)requestData["port"];
  240. if (requestData.ContainsKey("userID"))
  241. {
  242. string userID_str = (string)requestData["userID"];
  243. UUID userID = UUID.Zero;
  244. UUID.TryParse(userID_str, out userID);
  245. List<string> ids = new List<string>();
  246. foreach (object key in requestData.Keys)
  247. {
  248. if (key is string && ((string)key).StartsWith("friend_") && requestData[key] != null)
  249. ids.Add(requestData[key].ToString());
  250. }
  251. //List<UUID> online = m_HomeUsersService.GetOnlineFriends(userID, ids);
  252. //if (online.Count > 0)
  253. //{
  254. // int i = 0;
  255. // foreach (UUID id in online)
  256. // {
  257. // hash["friend_" + i.ToString()] = id.ToString();
  258. // i++;
  259. // }
  260. //}
  261. //else
  262. // hash["result"] = "No Friends Online";
  263. }
  264. XmlRpcResponse response = new XmlRpcResponse();
  265. response.Value = hash;
  266. return response;
  267. }
  268. public XmlRpcResponse GetUserInfo(XmlRpcRequest request, IPEndPoint remoteClient)
  269. {
  270. Hashtable hash = new Hashtable();
  271. Hashtable requestData = (Hashtable)request.Params[0];
  272. // This needs checking!
  273. if (requestData.ContainsKey("userID"))
  274. {
  275. string userID_str = (string)requestData["userID"];
  276. UUID userID = UUID.Zero;
  277. UUID.TryParse(userID_str, out userID);
  278. //int userFlags = m_HomeUsersService.GetUserFlags(userID);
  279. Dictionary<string,object> userInfo = m_HomeUsersService.GetUserInfo(userID);
  280. if (userInfo.Count > 0)
  281. {
  282. foreach (KeyValuePair<string, object> kvp in userInfo)
  283. {
  284. hash[kvp.Key] = kvp.Value;
  285. }
  286. }
  287. else
  288. {
  289. hash["result"] = "failure";
  290. }
  291. }
  292. XmlRpcResponse response = new XmlRpcResponse();
  293. response.Value = hash;
  294. return response;
  295. }
  296. public XmlRpcResponse GetServerURLs(XmlRpcRequest request, IPEndPoint remoteClient)
  297. {
  298. Hashtable hash = new Hashtable();
  299. Hashtable requestData = (Hashtable)request.Params[0];
  300. //string host = (string)requestData["host"];
  301. //string portstr = (string)requestData["port"];
  302. if (requestData.ContainsKey("userID"))
  303. {
  304. string userID_str = (string)requestData["userID"];
  305. UUID userID = UUID.Zero;
  306. UUID.TryParse(userID_str, out userID);
  307. Dictionary<string, object> serverURLs = m_HomeUsersService.GetServerURLs(userID);
  308. if (serverURLs.Count > 0)
  309. {
  310. foreach (KeyValuePair<string, object> kvp in serverURLs)
  311. hash["SRV_" + kvp.Key] = kvp.Value.ToString();
  312. }
  313. else
  314. hash["result"] = "No Service URLs";
  315. }
  316. XmlRpcResponse response = new XmlRpcResponse();
  317. response.Value = hash;
  318. return response;
  319. }
  320. /// <summary>
  321. /// Locates the user.
  322. /// This is a sensitive operation, only authorized IP addresses can perform it.
  323. /// </summary>
  324. /// <param name="request"></param>
  325. /// <param name="remoteClient"></param>
  326. /// <returns></returns>
  327. public XmlRpcResponse LocateUser(XmlRpcRequest request, IPEndPoint remoteClient)
  328. {
  329. Hashtable hash = new Hashtable();
  330. bool authorized = true;
  331. if (m_VerifyCallers)
  332. {
  333. authorized = false;
  334. foreach (string s in m_AuthorizedCallers)
  335. if (s == remoteClient.Address.ToString())
  336. {
  337. authorized = true;
  338. break;
  339. }
  340. }
  341. if (authorized)
  342. {
  343. Hashtable requestData = (Hashtable)request.Params[0];
  344. //string host = (string)requestData["host"];
  345. //string portstr = (string)requestData["port"];
  346. if (requestData.ContainsKey("userID"))
  347. {
  348. string userID_str = (string)requestData["userID"];
  349. UUID userID = UUID.Zero;
  350. UUID.TryParse(userID_str, out userID);
  351. string url = m_HomeUsersService.LocateUser(userID);
  352. if (url != string.Empty)
  353. hash["URL"] = url;
  354. else
  355. hash["result"] = "Unable to locate user";
  356. }
  357. }
  358. XmlRpcResponse response = new XmlRpcResponse();
  359. response.Value = hash;
  360. return response;
  361. }
  362. /// <summary>
  363. /// Returns the UUI of a user given a UUID.
  364. /// </summary>
  365. /// <param name="request"></param>
  366. /// <param name="remoteClient"></param>
  367. /// <returns></returns>
  368. public XmlRpcResponse GetUUI(XmlRpcRequest request, IPEndPoint remoteClient)
  369. {
  370. Hashtable hash = new Hashtable();
  371. Hashtable requestData = (Hashtable)request.Params[0];
  372. //string host = (string)requestData["host"];
  373. //string portstr = (string)requestData["port"];
  374. if (requestData.ContainsKey("userID") && requestData.ContainsKey("targetUserID"))
  375. {
  376. string userID_str = (string)requestData["userID"];
  377. UUID userID = UUID.Zero;
  378. UUID.TryParse(userID_str, out userID);
  379. string tuserID_str = (string)requestData["targetUserID"];
  380. UUID targetUserID = UUID.Zero;
  381. UUID.TryParse(tuserID_str, out targetUserID);
  382. string uui = m_HomeUsersService.GetUUI(userID, targetUserID);
  383. if (uui != string.Empty)
  384. hash["UUI"] = uui;
  385. else
  386. hash["result"] = "User unknown";
  387. }
  388. XmlRpcResponse response = new XmlRpcResponse();
  389. response.Value = hash;
  390. return response;
  391. }
  392. /// <summary>
  393. /// Gets the UUID of a user given First name, Last name.
  394. /// </summary>
  395. /// <param name="request"></param>
  396. /// <param name="remoteClient"></param>
  397. /// <returns></returns>
  398. public XmlRpcResponse GetUUID(XmlRpcRequest request, IPEndPoint remoteClient)
  399. {
  400. Hashtable hash = new Hashtable();
  401. Hashtable requestData = (Hashtable)request.Params[0];
  402. //string host = (string)requestData["host"];
  403. //string portstr = (string)requestData["port"];
  404. if (requestData.ContainsKey("first") && requestData.ContainsKey("last"))
  405. {
  406. string first = (string)requestData["first"];
  407. string last = (string)requestData["last"];
  408. UUID uuid = m_HomeUsersService.GetUUID(first, last);
  409. hash["UUID"] = uuid.ToString();
  410. }
  411. XmlRpcResponse response = new XmlRpcResponse();
  412. response.Value = hash;
  413. return response;
  414. }
  415. }
  416. }