UserAgentServerConnector.cs 19 KB

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