UserAgentServerConnector.cs 19 KB

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