Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
7.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Holds the CGCSession class
  4. //
  5. //=============================================================================
  6. #ifndef GCSESSION_H
  7. #define GCSESSION_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "scheduledfunction.h"
  12. #include "framefunction.h"
  13. #include "gcsdk/gc_sharedobjectcache.h"
  14. namespace GCSDK
  15. {
  16. class CGCGSSession;
  17. // Spew group for anything related to sessions
  18. extern CGCEmitGroup g_EGSessions;
  19. //-----------------------------------------------------------------------------
  20. // Utility class to handle rate limiting based upon a steam ID and message using two console variables to control rate
  21. //-----------------------------------------------------------------------------
  22. //utility class to handle rate limiting based upon a steam ID
  23. class CSteamIDRateLimit
  24. {
  25. public:
  26. CSteamIDRateLimit( const GCConVar& cvNumPerPeriod, const GCConVar* pcvPeriodS = NULL );
  27. ~CSteamIDRateLimit();
  28. //given a steam ID, this will determine if it should be rate limited
  29. bool BIsRateLimited( CSteamID steamID, uint32 unMsgType );
  30. //frame function to clear the list after a period of time
  31. bool OnFrameFn( const CLimitTimer& timer );
  32. private:
  33. //the last time we cleared our list
  34. RTime32 m_LastClear;
  35. //the frame function so we can detect when we need to clear
  36. CFrameFunction< CSteamIDRateLimit> m_FrameFunction;
  37. //the map of messages we have tracked for each user
  38. CUtlHashMapLarge< CSteamID, uint32 > m_Msgs;
  39. //the console variables that track the time window and the messages allowed
  40. const GCConVar& m_cvNumPerPeriod;
  41. const GCConVar* m_pcvPeriodS;
  42. };
  43. //------------------------------------------------------------------------------------------
  44. // CMsgRateLimitTracker
  45. // A utility class to track when messages go over so that we can see users/msgs that are being spammed
  46. //------------------------------------------------------------------------------------------
  47. class CMsgRateLimitTracker
  48. {
  49. public:
  50. CMsgRateLimitTracker();
  51. //called to track a message that was rate limited
  52. void TrackRateLimitedMsg( const CSteamID steamID, MsgType_t eMsgType );
  53. //called to report the collected rate limiting stats
  54. void ReportMsgStats() const;
  55. void ReportTopUsers( uint32 nMinMsgs, uint32 nListTop ) const;
  56. void ReportUserStats() const;
  57. //called to clear all collected stats
  58. void ClearStats();
  59. private:
  60. //the time we started collecting stats at
  61. RTime32 m_StartTime;
  62. //map detailing the number of messages of each type that have been dropped
  63. CUtlHashMapLarge< MsgType_t, uint32 > m_MsgStats;
  64. CUtlHashMapLarge< CSteamID, uint32 > m_UserStats;
  65. };
  66. extern CMsgRateLimitTracker g_RateLimitTracker;
  67. //-----------------------------------------------------------------------------
  68. // Purpose: Base class for sessions in the GC
  69. //-----------------------------------------------------------------------------
  70. class CGCSession
  71. {
  72. public:
  73. CGCSession( const CSteamID & steamID, CGCSharedObjectCache *pCache );
  74. virtual ~CGCSession();
  75. const CSteamID & GetSteamID() const { return m_steamID; }
  76. const CGCSharedObjectCache *GetSOCache() const { return m_pSOCache; }
  77. CGCSharedObjectCache *GetSOCache() { return m_pSOCache; }
  78. void RemoveSOCache() { m_pSOCache = NULL; }
  79. EOSType GetOSType() const { return m_osType; };
  80. bool IsTestSession() const { return m_bIsTestSession; }
  81. uint32 GetIPPublic() const { return m_unIPPublic; }
  82. bool IsSecure() const { return m_bIsSecure; }
  83. bool BIsShuttingDown() const { return m_bIsShuttingDown; }
  84. void SetIsShuttingDown( bool bIsShuttingDown ) { m_bIsShuttingDown = bIsShuttingDown; }
  85. virtual void Dump( bool bFull = true ) const = 0;
  86. bool BRateLimitMessage( MsgType_t unMsgType );
  87. CJobTime GetLastPingSendTime() const { return m_jtTimeSentPing; }
  88. CJobTime GetLastMessageReceiveTime() const { return m_jtLastMessageReceived; }
  89. void SendPing() const;
  90. virtual void MarkAccess() { }
  91. virtual void Run();
  92. virtual void YieldingSOCacheReloaded() {}
  93. #ifdef DBGFLAG_VALIDATE
  94. virtual void Validate( CValidator &validator, const char *pchName );
  95. #endif // DBGFLAG_VALIDATE
  96. // Geolocation
  97. bool HasGeoLocation() const { return m_haveGeoLocation; }
  98. bool GetGeoLocation( float &latitude, float &longittude ) const;
  99. virtual void SetGeoLocation( float latitude, float longittude );
  100. //track whether or not this session has been initialized or not
  101. bool BIsInitialized() const { return m_bInitialized; }
  102. void SetInitialized( bool b ) { m_bInitialized = b; }
  103. private:
  104. CSteamID m_steamID;
  105. CGCSharedObjectCache *m_pSOCache;
  106. // Tracks how many messages we've gotten this second so we can block attacks
  107. RTime32 m_rtLastMessageReceived;
  108. uint32 m_unMessagesRecievedThisSecond;
  109. CJobTime m_jtLastMessageReceived;
  110. // This is mutable because we update it when we send pings, but sending a
  111. // ping to a user/server isn't really a session changing event, so we don't
  112. // want to require locking the session to ping it and update its last
  113. // sent ping time.
  114. mutable CJobTime m_jtTimeSentPing;
  115. EOSType m_osType : 16;
  116. bool m_bIsShuttingDown : 1;
  117. bool m_bIsTestSession : 1;
  118. bool m_bIsSecure : 1;
  119. bool m_bInitialized : 1;
  120. protected:
  121. bool m_haveGeoLocation : 1;
  122. float m_flLatitude;
  123. float m_flLongitude;
  124. uint32 m_unIPPublic;
  125. friend class CGCBase;
  126. };
  127. //-----------------------------------------------------------------------------
  128. // Purpose: Base class for user sessions in the GC
  129. //-----------------------------------------------------------------------------
  130. class CGCUserSession : public CGCSession
  131. {
  132. public:
  133. CGCUserSession( const CSteamID & steamID, CGCSharedObjectCache *pCache ) : CGCSession( steamID, pCache ) { }
  134. virtual ~CGCUserSession();
  135. virtual bool BInit();
  136. const CSteamID &GetSteamIDGS() const { return m_steamIDGS; }
  137. const CSteamID &GetSteamIDGSPrev() const { return m_steamIDGSPrev; }
  138. virtual bool BSetServer( const CSteamID &steamIDGS );
  139. virtual bool BLeaveServer();
  140. virtual void Dump( bool bFull = true ) const;
  141. private:
  142. CSteamID m_steamIDGS;
  143. CSteamID m_steamIDGSPrev;
  144. };
  145. //-----------------------------------------------------------------------------
  146. // Purpose: Base class for gameserver sessions in the GC
  147. //-----------------------------------------------------------------------------
  148. class CGCGSSession : public CGCSession
  149. {
  150. public:
  151. CGCGSSession( const CSteamID & steamID, CGCSharedObjectCache *pCache, uint32 unServerAddr, uint16 usServerPort ) ;
  152. virtual ~CGCGSSession();
  153. uint32 GetAddr() const { return m_unServerAddr; }
  154. uint16 GetPort() const { return m_usServerPort; }
  155. void SetIPAndPort( uint32 unServerAddr, uint16 usServerPort );
  156. int GetUserCount() const { return m_vecUsers.Count(); }
  157. CSteamID GetUserID( int nIndex ) const { return m_vecUsers[nIndex]; }
  158. // Manages users on the server. It is very important that these are not
  159. // virtual and not yielding. For custom behavior override the Pre*() hooks below
  160. bool BAddUser( const CSteamID &steamIDUser );
  161. bool BRemoveUser( const CSteamID &steamIDUser );
  162. void RemoveAllUsers();
  163. virtual void Dump( bool bFull = true ) const;
  164. protected:
  165. // Hooks to trigger custom behavior when users are added and removed. It is
  166. // very important that these do not yield. If you need to yield, start a job instead
  167. virtual void PreAddUser( const CSteamID &steamIDUser ) {}
  168. virtual void PostAddUser( const CSteamID &steamIDUser ) {}
  169. virtual void PreRemoveUser( const CSteamID &steamIDUser ) {}
  170. virtual void PostRemoveUser( const CSteamID &steamIDUser ) {}
  171. virtual void PreRemoveAllUsers() {}
  172. virtual void PostRemoveAllUsers() {}
  173. public:
  174. float m_lastUpdateTime; // Last time we received a message from the server
  175. #ifdef DBGFLAG_VALIDATE
  176. virtual void Validate( CValidator &validator, const char *pchName );
  177. #endif // DBGFLAG_VALIDATE
  178. protected:
  179. CUtlVector<CSteamID> m_vecUsers;
  180. // These are the address of the server as connected to Steam
  181. uint32 m_unServerAddr;
  182. uint16 m_usServerPort;
  183. };
  184. } // namespace GCSDK
  185. #endif // GCSESSION_H