Skip to content

Commit

Permalink
Fixed some compilation warnings and code formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielga committed Sep 27, 2017
1 parent a4a33b6 commit bb96e77
Show file tree
Hide file tree
Showing 6 changed files with 915 additions and 905 deletions.
276 changes: 137 additions & 139 deletions source/filecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,189 +14,187 @@

namespace filecheck
{
enum ValidationMode
{
ValidationModeNone,
ValidationModeFixed,
ValidationModeLua
};

enum ValidationMode
{
ValidationModeNone,
ValidationModeFixed,
ValidationModeLua
};

typedef bool ( *CNetChan__IsValidFileForTransfer_t )( const char *filepath );
typedef bool( *CNetChan__IsValidFileForTransfer_t )( const char *filepath );

#if defined SYSTEM_WINDOWS

static const char CNetChan__IsValidFileForTransfer_sig[] =
"\x55\x8B\xEC\x53\x8B\x5D\x08\x85\xDB\x0F\x84\x2A\x2A\x2A\x2A\x80\x3B";
static const size_t CNetChan__IsValidFileForTransfer_siglen =
sizeof( CNetChan__IsValidFileForTransfer_sig ) - 1;
static const char CNetChan__IsValidFileForTransfer_sig[] =
"\x55\x8B\xEC\x53\x8B\x5D\x08\x85\xDB\x0F\x84\x2A\x2A\x2A\x2A\x80\x3B";
static const size_t CNetChan__IsValidFileForTransfer_siglen =
sizeof( CNetChan__IsValidFileForTransfer_sig ) - 1;

#elif defined SYSTEM_LINUX

static const char CNetChan__IsValidFileForTransfer_sig[] =
"@_ZN8CNetChan22IsValidFileForTransferEPKc";
static const size_t CNetChan__IsValidFileForTransfer_siglen = 0;
static const char CNetChan__IsValidFileForTransfer_sig[] =
"@_ZN8CNetChan22IsValidFileForTransferEPKc";
static const size_t CNetChan__IsValidFileForTransfer_siglen = 0;

#elif defined SYSTEM_MACOSX

static const char CNetChan__IsValidFileForTransfer_sig[] =
"@__ZN8CNetChan22IsValidFileForTransferEPKc";
static const size_t CNetChan__IsValidFileForTransfer_siglen = 0;
static const char CNetChan__IsValidFileForTransfer_sig[] =
"@__ZN8CNetChan22IsValidFileForTransferEPKc";
static const size_t CNetChan__IsValidFileForTransfer_siglen = 0;

#endif

static CNetChan__IsValidFileForTransfer_t CNetChan__IsValidFileForTransfer_original = nullptr;
static const char file_hook_name[] = "IsValidFileForTransfer";
static const char downloads_dir[] = "downloads" CORRECT_PATH_SEPARATOR_S;
static ValidationMode validation_mode = ValidationModeNone;
static GarrysMod::Lua::ILuaInterface *lua_interface = nullptr;
static INetworkStringTable *downloads = nullptr;
static Detouring::Hook hook;
static CNetChan__IsValidFileForTransfer_t CNetChan__IsValidFileForTransfer_original = nullptr;
static const char file_hook_name[] = "IsValidFileForTransfer";
static const char downloads_dir[] = "downloads" CORRECT_PATH_SEPARATOR_S;
static ValidationMode validation_mode = ValidationModeNone;
static GarrysMod::Lua::ILuaInterface *lua_interface = nullptr;
static INetworkStringTable *downloads = nullptr;
static Detouring::Hook hook;

inline bool SetFileDetourStatus( ValidationMode mode )
{
if( mode != ValidationModeNone ? hook.Enable( ) : hook.Disable( ) )
inline bool SetFileDetourStatus( ValidationMode mode )
{
validation_mode = mode;
return true;
}

return false;
}

LUA_FUNCTION_STATIC( EnableFileValidation )
{
if( LUA->Top( ) < 1 )
LUA->ArgError( 1, "boolean or number expected, got nil" );
if( mode != ValidationModeNone ? hook.Enable( ) : hook.Disable( ) )
{
validation_mode = mode;
return true;
}

ValidationMode mode = ValidationModeFixed;
if( LUA->IsType( 1, GarrysMod::Lua::Type::BOOL ) )
{
mode = LUA->GetBool( 1 ) ? ValidationModeFixed : ValidationModeNone;
return false;
}
else if( LUA->IsType( 1, GarrysMod::Lua::Type::NUMBER ) )
{
int32_t num = static_cast<int32_t>( LUA->GetNumber( 1 ) );
if( num < 0 || num > 2 )
LUA->ArgError( 1, "invalid mode value, must be 0, 1 or 2" );

mode = static_cast<ValidationMode>( num );
}
else
LUA_FUNCTION_STATIC( EnableFileValidation )
{
LUA->ArgError( 1, "boolean or number expected" );
}
if( LUA->Top( ) < 1 )
LUA->ArgError( 1, "boolean or number expected, got nil" );

LUA->PushBool( SetFileDetourStatus( mode ) );
return 1;
}
ValidationMode mode = ValidationModeFixed;
if( LUA->IsType( 1, GarrysMod::Lua::Type::BOOL ) )
{
mode = LUA->GetBool( 1 ) ? ValidationModeFixed : ValidationModeNone;
}
else if( LUA->IsType( 1, GarrysMod::Lua::Type::NUMBER ) )
{
int32_t num = static_cast<int32_t>( LUA->GetNumber( 1 ) );
if( num < 0 || num > 2 )
LUA->ArgError( 1, "invalid mode value, must be 0, 1 or 2" );

inline bool Call( const char *filepath )
{
return hook.GetTrampoline<CNetChan__IsValidFileForTransfer_t>( )( filepath );
}
mode = static_cast<ValidationMode>( num );
}
else
{
LUA->ArgError( 1, "boolean or number expected" );
}

inline bool BlockDownload( const char *filepath )
{
DebugWarning( "[ServerSecure] Blocking download of \"%s\"\n", filepath );
return false;
}
LUA->PushBool( SetFileDetourStatus( mode ) );
return 1;
}

static bool CNetChan_IsValidFileForTransfer_detour( const char *filepath )
{
if( filepath == nullptr )
return BlockDownload(
"[ServerSecure] Invalid file to download (string pointer was NULL)\n"
);

std::string nicefile( filepath );
if( nicefile.empty( ) )
return BlockDownload(
"[ServerSecure] Invalid file to download (path length was 0)\n"
);

if( validation_mode == ValidationModeLua )
inline bool Call( const char *filepath )
{
if( !LuaHelpers::PushHookRun( lua_interface, file_hook_name ) )
return Call( filepath );
return hook.GetTrampoline<CNetChan__IsValidFileForTransfer_t>( )( filepath );
}

lua_interface->PushString( filepath );
inline bool BlockDownload( const char *filepath )
{
DebugWarning( "[ServerSecure] Blocking download of \"%s\"\n", filepath );
return false;
}

bool valid = true;
if( LuaHelpers::CallHookRun( lua_interface, 1, 1 ) )
static bool CNetChan_IsValidFileForTransfer_detour( const char *filepath )
{
if( filepath == nullptr )
return BlockDownload(
"[ServerSecure] Invalid file to download (string pointer was NULL)\n"
);

std::string nicefile( filepath );
if( nicefile.empty( ) )
return BlockDownload(
"[ServerSecure] Invalid file to download (path length was 0)\n"
);

if( validation_mode == ValidationModeLua )
{
if( lua_interface->IsType( -1, GarrysMod::Lua::Type::BOOL ) )
valid = lua_interface->GetBool( -1 );
if( !LuaHelpers::PushHookRun( lua_interface, file_hook_name ) )
return Call( filepath );

lua_interface->Pop( 1 );
}
lua_interface->PushString( filepath );

return valid;
}
bool valid = true;
if( LuaHelpers::CallHookRun( lua_interface, 1, 1 ) )
{
if( lua_interface->IsType( -1, GarrysMod::Lua::Type::BOOL ) )
valid = lua_interface->GetBool( -1 );

if( !V_RemoveDotSlashes( &nicefile[0] ) )
return BlockDownload( filepath );
lua_interface->Pop( 1 );
}

nicefile.resize( std::strlen( nicefile.c_str( ) ) );
filepath = nicefile.c_str( );
return valid;
}

DebugWarning( "[ServerSecure] Checking file \"%s\"\n", filepath );
if( !V_RemoveDotSlashes( &nicefile[0] ) )
return BlockDownload( filepath );

if( !Call( filepath ) )
return BlockDownload( filepath );
nicefile.resize( std::strlen( nicefile.c_str( ) ) );
filepath = nicefile.c_str( );

int32_t index = downloads->FindStringIndex( filepath );
if( index != INVALID_STRING_INDEX )
return true;
DebugWarning( "[ServerSecure] Checking file \"%s\"\n", filepath );

if( nicefile.size( ) == 22 &&
std::strncmp( filepath, downloads_dir, 10 ) == 0 &&
std::strncmp( filepath + nicefile.size( ) - 4, ".dat", 4 ) == 0 )
return true;
if( !Call( filepath ) )
return BlockDownload( filepath );

return BlockDownload( filepath );
}
int32_t index = downloads->FindStringIndex( filepath );
if( index != INVALID_STRING_INDEX )
return true;

void Initialize( GarrysMod::Lua::ILuaBase *LUA )
{
lua_interface = static_cast<GarrysMod::Lua::ILuaInterface *>( LUA );
if( nicefile.size( ) == 22 &&
std::strncmp( filepath, downloads_dir, 10 ) == 0 &&
std::strncmp( filepath + nicefile.size( ) - 4, ".dat", 4 ) == 0 )
return true;

{
SymbolFinder symfinder;

CNetChan__IsValidFileForTransfer_original =
reinterpret_cast<CNetChan__IsValidFileForTransfer_t>( symfinder.ResolveOnBinary(
global::engine_binary.c_str( ),
CNetChan__IsValidFileForTransfer_sig,
CNetChan__IsValidFileForTransfer_siglen
) );
return BlockDownload( filepath );
}

if( CNetChan__IsValidFileForTransfer_original == nullptr )
LUA->ThrowError( "unable to find CNetChan::IsValidFileForTransfer" );
void Initialize( GarrysMod::Lua::ILuaBase *LUA )
{
lua_interface = static_cast<GarrysMod::Lua::ILuaInterface *>( LUA );

if( !hook.Create( reinterpret_cast<void *>( CNetChan__IsValidFileForTransfer_original ),
reinterpret_cast<void *>( &CNetChan_IsValidFileForTransfer_detour ) ) )
LUA->ThrowError( "unable to create detour for CNetChan::IsValidFileForTransfer" );
{
SymbolFinder symfinder;

CNetChan__IsValidFileForTransfer_original =
reinterpret_cast<CNetChan__IsValidFileForTransfer_t>( symfinder.ResolveOnBinary(
global::engine_binary.c_str( ),
CNetChan__IsValidFileForTransfer_sig,
CNetChan__IsValidFileForTransfer_siglen
) );
}

INetworkStringTableContainer *networkstringtable =
global::engine_loader.GetInterface<INetworkStringTableContainer>(
INTERFACENAME_NETWORKSTRINGTABLESERVER
);
if( networkstringtable == nullptr )
LUA->ThrowError( "unable to get INetworkStringTableContainer" );
if( CNetChan__IsValidFileForTransfer_original == nullptr )
LUA->ThrowError( "unable to find CNetChan::IsValidFileForTransfer" );

downloads = networkstringtable->FindTable( "downloadables" );
if( downloads == nullptr )
LUA->ThrowError( "missing \"downloadables\" string table" );
if( !hook.Create( reinterpret_cast<void *>( CNetChan__IsValidFileForTransfer_original ),
reinterpret_cast<void *>( &CNetChan_IsValidFileForTransfer_detour ) ) )
LUA->ThrowError( "unable to create detour for CNetChan::IsValidFileForTransfer" );

LUA->PushCFunction( EnableFileValidation );
LUA->SetField( -2, "EnableFileValidation" );
}
INetworkStringTableContainer *networkstringtable =
global::engine_loader.GetInterface<INetworkStringTableContainer>(
INTERFACENAME_NETWORKSTRINGTABLESERVER
);
if( networkstringtable == nullptr )
LUA->ThrowError( "unable to get INetworkStringTableContainer" );

void Deinitialize( GarrysMod::Lua::ILuaBase * )
{
hook.Disable( );
}
downloads = networkstringtable->FindTable( "downloadables" );
if( downloads == nullptr )
LUA->ThrowError( "missing \"downloadables\" string table" );

LUA->PushCFunction( EnableFileValidation );
LUA->SetField( -2, "EnableFileValidation" );
}

void Deinitialize( GarrysMod::Lua::ILuaBase * )
{
hook.Disable( );
}
}
6 changes: 2 additions & 4 deletions source/filecheck.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace GarrysMod

namespace filecheck
{

void Initialize( GarrysMod::Lua::ILuaBase *LUA );
void Deinitialize( GarrysMod::Lua::ILuaBase *LUA );

void Initialize( GarrysMod::Lua::ILuaBase *LUA );
void Deinitialize( GarrysMod::Lua::ILuaBase *LUA );
}
Loading

0 comments on commit bb96e77

Please sign in to comment.