UserAgentServerConnector.cs 19 KB

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