diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Services/HypergridService/UserAgentService.cs | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/OpenSim/Services/HypergridService/UserAgentService.cs b/OpenSim/Services/HypergridService/UserAgentService.cs new file mode 100644 index 0000000..15379b5 --- /dev/null +++ b/OpenSim/Services/HypergridService/UserAgentService.cs | |||
@@ -0,0 +1,217 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Net; | ||
4 | using System.Reflection; | ||
5 | |||
6 | using OpenSim.Framework; | ||
7 | using OpenSim.Services.Connectors.Hypergrid; | ||
8 | using OpenSim.Services.Interfaces; | ||
9 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; | ||
10 | using OpenSim.Server.Base; | ||
11 | |||
12 | using OpenMetaverse; | ||
13 | using log4net; | ||
14 | using Nini.Config; | ||
15 | |||
16 | namespace OpenSim.Services.HypergridService | ||
17 | { | ||
18 | /// <summary> | ||
19 | /// This service is for HG1.5 only, to make up for the fact that clients don't | ||
20 | /// keep any private information in themselves, and that their 'home service' | ||
21 | /// needs to do it for them. | ||
22 | /// Once we have better clients, this shouldn't be needed. | ||
23 | /// </summary> | ||
24 | public class UserAgentService : IUserAgentService | ||
25 | { | ||
26 | private static readonly ILog m_log = | ||
27 | LogManager.GetLogger( | ||
28 | MethodBase.GetCurrentMethod().DeclaringType); | ||
29 | |||
30 | // This will need to go into a DB table | ||
31 | static Dictionary<UUID, TravelingAgentInfo> m_TravelingAgents = new Dictionary<UUID, TravelingAgentInfo>(); | ||
32 | |||
33 | static bool m_Initialized = false; | ||
34 | |||
35 | protected static IPresenceService m_PresenceService; | ||
36 | protected static IGridService m_GridService; | ||
37 | protected static GatekeeperServiceConnector m_GatekeeperConnector; | ||
38 | |||
39 | public UserAgentService(IConfigSource config) | ||
40 | { | ||
41 | if (!m_Initialized) | ||
42 | { | ||
43 | m_log.DebugFormat("[HOME USERS SECURITY]: Starting..."); | ||
44 | |||
45 | IConfig serverConfig = config.Configs["UserAgentService"]; | ||
46 | if (serverConfig == null) | ||
47 | throw new Exception(String.Format("No section UserAgentService in config file")); | ||
48 | |||
49 | string gridService = serverConfig.GetString("GridService", String.Empty); | ||
50 | string presenceService = serverConfig.GetString("PresenceService", String.Empty); | ||
51 | |||
52 | if (gridService == string.Empty || presenceService == string.Empty) | ||
53 | throw new Exception(String.Format("Incomplete specifications, UserAgent Service cannot function.")); | ||
54 | |||
55 | Object[] args = new Object[] { config }; | ||
56 | m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args); | ||
57 | m_PresenceService = ServerUtils.LoadPlugin<IPresenceService>(presenceService, args); | ||
58 | m_GatekeeperConnector = new GatekeeperServiceConnector(); | ||
59 | |||
60 | m_Initialized = true; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | public GridRegion GetHomeRegion(UUID userID, out Vector3 position, out Vector3 lookAt) | ||
65 | { | ||
66 | position = new Vector3(128, 128, 0); lookAt = Vector3.UnitY; | ||
67 | |||
68 | m_log.DebugFormat("[USER AGENT SERVICE]: Request to get home region of user {0}", userID); | ||
69 | |||
70 | GridRegion home = null; | ||
71 | PresenceInfo[] presences = m_PresenceService.GetAgents(new string[] { userID.ToString() }); | ||
72 | if (presences != null && presences.Length > 0) | ||
73 | { | ||
74 | UUID homeID = presences[0].HomeRegionID; | ||
75 | if (homeID != UUID.Zero) | ||
76 | { | ||
77 | home = m_GridService.GetRegionByUUID(UUID.Zero, homeID); | ||
78 | position = presences[0].HomePosition; | ||
79 | lookAt = presences[0].HomeLookAt; | ||
80 | } | ||
81 | if (home == null) | ||
82 | { | ||
83 | List<GridRegion> defs = m_GridService.GetDefaultRegions(UUID.Zero); | ||
84 | if (defs != null && defs.Count > 0) | ||
85 | home = defs[0]; | ||
86 | } | ||
87 | } | ||
88 | |||
89 | return home; | ||
90 | } | ||
91 | |||
92 | public bool LoginAgentToGrid(AgentCircuitData agentCircuit, GridRegion gatekeeper, GridRegion finalDestination, out string reason) | ||
93 | { | ||
94 | m_log.DebugFormat("[USER AGENT SERVICE]: Request to login user {0} {1} to grid {2}", | ||
95 | agentCircuit.firstname, agentCircuit.lastname, gatekeeper.ExternalHostName +":"+ gatekeeper.HttpPort); | ||
96 | |||
97 | // Take the IP address + port of the gatekeeper (reg) plus the info of finalDestination | ||
98 | GridRegion region = new GridRegion(gatekeeper); | ||
99 | region.RegionName = finalDestination.RegionName; | ||
100 | region.RegionID = finalDestination.RegionID; | ||
101 | region.RegionLocX = finalDestination.RegionLocX; | ||
102 | region.RegionLocY = finalDestination.RegionLocY; | ||
103 | |||
104 | // Generate a new service session | ||
105 | agentCircuit.ServiceSessionID = "http://" + region.ExternalHostName + ":" + region.HttpPort + ";" + UUID.Random(); | ||
106 | TravelingAgentInfo old = UpdateTravelInfo(agentCircuit, region); | ||
107 | |||
108 | bool success = m_GatekeeperConnector.CreateAgent(region, agentCircuit, (uint)Constants.TeleportFlags.ViaLogin, out reason); | ||
109 | |||
110 | if (!success) | ||
111 | { | ||
112 | m_log.DebugFormat("[USER AGENT SERVICE]: Unable to login user {0} {1} to grid {2}, reason: {3}", | ||
113 | agentCircuit.firstname, agentCircuit.lastname, region.ExternalHostName + ":" + region.HttpPort, reason); | ||
114 | |||
115 | // restore the old travel info | ||
116 | lock (m_TravelingAgents) | ||
117 | m_TravelingAgents[agentCircuit.SessionID] = old; | ||
118 | |||
119 | return false; | ||
120 | } | ||
121 | |||
122 | return true; | ||
123 | } | ||
124 | |||
125 | TravelingAgentInfo UpdateTravelInfo(AgentCircuitData agentCircuit, GridRegion region) | ||
126 | { | ||
127 | TravelingAgentInfo travel = new TravelingAgentInfo(); | ||
128 | TravelingAgentInfo old = null; | ||
129 | lock (m_TravelingAgents) | ||
130 | { | ||
131 | if (m_TravelingAgents.ContainsKey(agentCircuit.SessionID)) | ||
132 | { | ||
133 | old = m_TravelingAgents[agentCircuit.SessionID]; | ||
134 | } | ||
135 | |||
136 | m_TravelingAgents[agentCircuit.SessionID] = travel; | ||
137 | } | ||
138 | travel.UserID = agentCircuit.AgentID; | ||
139 | travel.GridExternalName = region.ExternalHostName + ":" + region.HttpPort; | ||
140 | travel.ServiceToken = agentCircuit.ServiceSessionID; | ||
141 | if (old != null) | ||
142 | travel.ClientToken = old.ClientToken; | ||
143 | |||
144 | return old; | ||
145 | } | ||
146 | |||
147 | public void LogoutAgent(UUID userID, UUID sessionID) | ||
148 | { | ||
149 | m_log.DebugFormat("[USER AGENT SERVICE]: User {0} logged out", userID); | ||
150 | |||
151 | lock (m_TravelingAgents) | ||
152 | { | ||
153 | List<UUID> travels = new List<UUID>(); | ||
154 | foreach (KeyValuePair<UUID, TravelingAgentInfo> kvp in m_TravelingAgents) | ||
155 | if (kvp.Value == null) // do some clean up | ||
156 | travels.Add(kvp.Key); | ||
157 | else if (kvp.Value.UserID == userID) | ||
158 | travels.Add(kvp.Key); | ||
159 | foreach (UUID session in travels) | ||
160 | m_TravelingAgents.Remove(session); | ||
161 | } | ||
162 | } | ||
163 | |||
164 | // We need to prevent foreign users with the same UUID as a local user | ||
165 | public bool AgentIsComingHome(UUID sessionID, string thisGridExternalName) | ||
166 | { | ||
167 | if (!m_TravelingAgents.ContainsKey(sessionID)) | ||
168 | return false; | ||
169 | |||
170 | TravelingAgentInfo travel = m_TravelingAgents[sessionID]; | ||
171 | return travel.GridExternalName == thisGridExternalName; | ||
172 | } | ||
173 | |||
174 | public bool VerifyClient(UUID sessionID, string token) | ||
175 | { | ||
176 | return true; | ||
177 | |||
178 | // Commenting this for now until I understand better what part of a sender's | ||
179 | // info stays unchanged throughout a session | ||
180 | // | ||
181 | //if (m_TravelingAgents.ContainsKey(sessionID)) | ||
182 | //{ | ||
183 | // // Aquiles heel. Must trust the first grid upon login | ||
184 | // if (m_TravelingAgents[sessionID].ClientToken == string.Empty) | ||
185 | // { | ||
186 | // m_TravelingAgents[sessionID].ClientToken = token; | ||
187 | // return true; | ||
188 | // } | ||
189 | // return m_TravelingAgents[sessionID].ClientToken == token; | ||
190 | //} | ||
191 | //return false; | ||
192 | } | ||
193 | |||
194 | public bool VerifyAgent(UUID sessionID, string token) | ||
195 | { | ||
196 | if (m_TravelingAgents.ContainsKey(sessionID)) | ||
197 | { | ||
198 | m_log.DebugFormat("[USER AGENT SERVICE]: Verifying agent token {0} against {1}", token, m_TravelingAgents[sessionID].ServiceToken); | ||
199 | return m_TravelingAgents[sessionID].ServiceToken == token; | ||
200 | } | ||
201 | |||
202 | m_log.DebugFormat("[USER AGENT SERVICE]: Token verification for session {0}: no such session", sessionID); | ||
203 | |||
204 | return false; | ||
205 | } | ||
206 | |||
207 | } | ||
208 | |||
209 | class TravelingAgentInfo | ||
210 | { | ||
211 | public UUID UserID; | ||
212 | public string GridExternalName = string.Empty; | ||
213 | public string ServiceToken = string.Empty; | ||
214 | public string ClientToken = string.Empty; | ||
215 | } | ||
216 | |||
217 | } | ||