Raised This Month: $ Target: $400
 0% 

SQL Get column as cvar.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-27-2015 , 23:04   Re: SQL Get column as cvar.
Reply With Quote #1

Edited code to use get_user_ip( 0 ..

The plugin will create the table automatically on its first load. The table has 2 columns: IPAddress and Licensed.

If the query for the IP finds nothing OR if it finds the IPAddress and Licensed=0 then it is considered unlicensed.
PHP Code:

#include <amxmodx>
#include <sqlx>

new const Version[] = "0.1";

new 
Handle:g_SQLTuple g_szBuffer128 ];

public 
plugin_init() 
{
    
register_plugin"Check IP License" Version "bugsy" );
    
    new 
szHost64 ] , szUser64 ] , szPass64 ] , szDB64 ] , szIP16 ];
    
get_cvar_string"amx_sql_host" szHost charsmaxszHost ) );
    
get_cvar_string"amx_sql_user" szUser charsmaxszUser ) );
    
get_cvar_string"amx_sql_pass" szPass charsmaxszPass ) );
    
get_cvar_string"amx_sql_db" szDB charsmaxszDB ) );
    
g_SQLTuple SQL_MakeDbTupleszHost szUser szPass szDB );
    
    
ExecuteQuery"CREATE TABLE IF NOT EXISTS tblLicenses (IPAddress VARCHAR(16) PRIMARY KEY, Licensed INT);" false );
    
    
get_user_ipszIP charsmaxszIP ) , );
    
formatexg_szBuffer charsmaxg_szBuffer ) , "SELECT Licensed FROM tblLicenses WHERE IPAddress='%s';" szIP );
    
ExecuteQueryg_szBuffer true );
}

public 
ExecuteQuery( const szQuery[] , bool:bNeedResults 

    new 
szError64 ] , Handle:SqlConnection ErrorCode;
    
    
SqlConnection SQL_Connectg_SQLTuple ErrorCode szError charsmaxszError ) );
    
    if( 
SqlConnection == Empty_Handle 
        
set_fail_stateszError );
    
    new 
Handle:query SQL_PrepareQuerySqlConnection szQuery );
    
    
SQL_Executequery );
    
    if ( 
bNeedResults )
    {
        if ( !
SQL_NumResultsquery ) )
        {
            
set_fail_state"This server is not licensed!" );
        }
        else
        {
            if ( 
bool:SQL_ReadResultquery ) == true )
            {
                
server_print"Server license found!" );
            }
            else
            {
                
set_fail_state"This server is not licensed!" );
            }
        }
    }
    
    
SQL_FreeHandlequery );
    
SQL_FreeHandleSqlConnection );

__________________

Last edited by Bugsy; 12-28-2015 at 21:27.
Bugsy is offline
XmasterOfficial
Junior Member
Join Date: Oct 2014
Old 12-28-2015 , 21:00   Re: SQL Get column as cvar.
Reply With Quote #2

Quote:
Originally Posted by Bugsy View Post
You need a way to determine the servers internet IP address, not its local address which is retrieved with get_user_ip( 0 , string[] , size ). I used code that I wrote a long time ago that gets internet IP, I'm not sure how reliable it is though. I found at times it will cause lag, which will not affect game play since the check occurs at map change. What you need to do to avoid lag is use threaded/asynchronous sockets; you can try the sockets2 module from this post which uses IIRC the same format natives as the original sockets module. https://forums.alliedmods.net/showthread.php?t=169315

The plugin will create the table automatically on its first load. The table has 2 columns: IPAddress and Licensed.

If the query for the IP finds nothing OR if it finds the IPAddress and Licensed=0 then it is considered unlicensed.
PHP Code:
#include <amxmodx>
#include <sockets>
#include <sqlx>

new const Version[] = "0.1";

const 
TASK_GETIP 3467;

new 
Handle:g_SQLTuple g_szServerIP16 ];

public 
plugin_init() 
{
    
register_plugin"Check IP License" Version "bugsy" );
    
    new 
szHost64 ] , szUser64 ] , szPass64 ] , szDB64 ];
    
get_cvar_string"amx_sql_host" szHost charsmaxszHost ) );
    
get_cvar_string"amx_sql_user" szUser charsmaxszUser ) );
    
get_cvar_string"amx_sql_pass" szPass charsmaxszPass ) );
    
get_cvar_string"amx_sql_db" szDB charsmaxszDB ) );
    
g_SQLTuple SQL_MakeDbTupleszHost szUser szPass szDB );
    
    
ExecuteQuery"CREATE TABLE IF NOT EXISTS tblLicenses (IPAddress VARCHAR(16) PRIMARY KEY, Licensed INT);" false );
    
    
set_task3.0 "CheckServerIP" );
}

public 
ExecuteQuery( const szQuery[] , bool:bNeedResults 

    new 
szError64 ] , Handle:SqlConnection ErrorCode;
    
    
SqlConnection SQL_Connectg_SQLTuple ErrorCode szError charsmaxszError ) );
    
    if( 
SqlConnection == Empty_Handle 
        
set_fail_stateszError );
    
    new 
Handle:query SQL_PrepareQuerySqlConnection szQuery );
    
    
SQL_Executequery );
    
    if ( 
bNeedResults )
    {
        if ( !
SQL_NumResultsquery ) )
        {
            
set_fail_state"This server is not licensed!" );
        }
        else
        {
            if ( 
bool:SQL_ReadResultquery ) == true )
            {
                
server_print"Server license found!" );
            }
            else
            {
                
set_fail_state"This server is not licensed!" );
            }
        }
    }
    
    
SQL_FreeHandlequery );
    
SQL_FreeHandleSqlConnection );
}

public 
CheckServerIP()
{
    new 
iSocket iError;
    
    
iSocket socket_open"checkip.dyndns.com" 80 SOCKET_TCP iError );
    
    if ( !
iSocket || iError )
    {
        
set_fail_state"Error validating server license" );
    }
        
    
socket_sendiSocket "GET / HTTP/1.1^nHost: checkip.dyndns.com^n^n" 45 );
    
    new 
iData];
    
iData] = iSocket;
    
iData] = get_systime();
    
    
set_task0.1 "RecvData" TASK_GETIP iData sizeofiData ) , "b" );
}

public 
RecvDataiData] )
{
    static 
szData256 ] , iStartPos iEndPos;
    new 
iSocket iData];
    new 
iRequestTime iData];
    
    if ( 
socket_changeiSocket ) )
    {
        
socket_recviSocket szData charsmaxszData ) );
        
        
iStartPos strfindszData "Current IP Address: " );
        
        if ( 
iStartPos != -)
        {
            
iEndPos strfindszData "</body>" );
            
copyg_szServerIP iEndPos - ( iStartPos 20 ) , szDataiStartPos 20 ] );
            
            
socket_closeiSocket );
            
remove_taskTASK_GETIP );
            
            
formatexszData charsmaxszData ) , "SELECT Licensed FROM tblLicenses WHERE IPAddress='%s';" g_szServerIP );
            
ExecuteQueryszData true );
        }
    }
    
    if ( ( 
get_systime() - iRequestTime ) >= )
    {
        
socket_closeiSocket );
        
remove_taskTASK_GETIP );
        
        
set_fail_state"Error validating server license" );
    }

Well... For me, the local IP is actualy the real IP since the server is hosted so get_user_ip( 0 , string[] , size ) is working well for me. I did this:
Code:
#include <amxmodx>
#include <sqlx>

new const Version[] = "0.4"
new const szHost[] = "host"
new const szUser[] = "user"
new const szPass[] = "password"
new const szDB[] = "plugin_licenses"
const TASK_GETIP = 3467

new Handle:g_SQLTuple

public plugin_init() 
{
register_plugin( "Check IP License" , Version , "bugsy" )
g_SQLTuple = SQL_MakeDbTuple( szHost , szUser , szPass , szDB )
set_task( 20.0 , "RecvData" , TASK_GETIP)
}

public RecvData()
{
new szIP[20]
static szData
get_user_ip(0, szIP, charsmax(szIP), false)
remove_task( TASK_GETIP )
formatex( szData , "SELECT %s FROM licenses WHERE %s='%s';" , szIP, szIP, szIP )
ExecuteQuery( szData , true )
}

public ExecuteQuery( const szQuery[] , bool:bNeedResults ) 
{ 
new szError[ 64 ] , Handle:SqlConnection , ErrorCode
SqlConnection = SQL_Connect( g_SQLTuple , ErrorCode , szError , charsmax( szError ) )
if( SqlConnection == Empty_Handle ) 
set_fail_state( szError )
new Handle:query = SQL_PrepareQuery( SqlConnection , szQuery )
SQL_Execute( query )
if ( bNeedResults )
{
if ( !SQL_NumResults( query ) )
{
set_fail_state( "This server is not licensed!" )
}
else
{
server_cmd( "sv_gravity 200")
}
}
SQL_FreeHandle( query )
SQL_FreeHandle( SqlConnection )
}
I don't know what is not working but I don't get the fail state nor the gravity changed. So actually it is doing nothing. Note that in the db = plugin_licenses I have the table licenses and all collumns would be server IPs. I set them as INT not VARCHAR so is easy. What I tried is to search for the collumn that is the server IP and if it exists then to set the gravity to 200 just to test it.

Last edited by XmasterOfficial; 12-28-2015 at 21:02.
XmasterOfficial is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:07.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode