UserAgentServerConnector.cs 17 KB

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