UserAgentServerConnector.cs 19 KB

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