|
Junior Member
|

12-28-2015
, 21:00
Re: SQL Get column as cvar.
|
#7
|
Quote:
Originally Posted by Bugsy
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_szServerIP[ 16 ];
public plugin_init() { register_plugin( "Check IP License" , Version , "bugsy" ); new szHost[ 64 ] , szUser[ 64 ] , szPass[ 64 ] , szDB[ 64 ]; get_cvar_string( "amx_sql_host" , szHost , charsmax( szHost ) ); get_cvar_string( "amx_sql_user" , szUser , charsmax( szUser ) ); get_cvar_string( "amx_sql_pass" , szPass , charsmax( szPass ) ); get_cvar_string( "amx_sql_db" , szDB , charsmax( szDB ) ); g_SQLTuple = SQL_MakeDbTuple( szHost , szUser , szPass , szDB ); ExecuteQuery( "CREATE TABLE IF NOT EXISTS tblLicenses (IPAddress VARCHAR(16) PRIMARY KEY, Licensed INT);" , false ); set_task( 3.0 , "CheckServerIP" ); }
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 { if ( bool:SQL_ReadResult( query , 0 ) == true ) { server_print( "Server license found!" ); } else { set_fail_state( "This server is not licensed!" ); } } } SQL_FreeHandle( query ); SQL_FreeHandle( SqlConnection ); }
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_send( iSocket , "GET / HTTP/1.1^nHost: checkip.dyndns.com^n^n" , 45 ); new iData[ 2 ]; iData[ 0 ] = iSocket; iData[ 1 ] = get_systime(); set_task( 0.1 , "RecvData" , TASK_GETIP , iData , sizeof( iData ) , "b" ); }
public RecvData( iData[ 2 ] ) { static szData[ 256 ] , iStartPos , iEndPos; new iSocket = iData[ 0 ]; new iRequestTime = iData[ 1 ]; if ( socket_change( iSocket ) ) { socket_recv( iSocket , szData , charsmax( szData ) ); iStartPos = strfind( szData , "Current IP Address: " ); if ( iStartPos != -1 ) { iEndPos = strfind( szData , "</body>" ); copy( g_szServerIP , iEndPos - ( iStartPos + 20 ) , szData[ iStartPos + 20 ] ); socket_close( iSocket ); remove_task( TASK_GETIP ); formatex( szData , charsmax( szData ) , "SELECT Licensed FROM tblLicenses WHERE IPAddress='%s';" , g_szServerIP ); ExecuteQuery( szData , true ); } } if ( ( get_systime() - iRequestTime ) >= 5 ) { socket_close( iSocket ); remove_task( TASK_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.
|
|