AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   SQL Get column as cvar. (https://forums.alliedmods.net/showthread.php?t=276540)

XmasterOfficial 12-23-2015 03:43

SQL Get column as cvar.
 
So I tryed for like one day and one night without figuring this how it works:
Code:

public MySql_Init()
{
g_SqlTuple = SQL_MakeDbTuple(Host,User,Pass,Db)
new ErrorCode,Handle:g_SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
if(g_SqlConnection == Empty_Handle)
{
set_fail_state(g_Error)
}
new szIP[20]
get_user_ip(0, szIP, charsmax(szIP), false)
new Handle:query = SQL_PrepareQuery(g_SqlConnection,"SELECT '%s' FROM `licenses` WHERE 1", szIP, szIP)
if (!SQL_Execute(query) || !SQL_NumResults(query))
{
unlicensed = 1
SQL_FreeHandle(query)
return PLUGIN_HANDLED
}
SQL_FreeHandle(query)
unlicensed = 0
SQL_FreeHandle(g_SqlConnection)
return PLUGIN_CONTINUE
}

I have this in plugin_init:
Code:

set_task(1.0, "MySql_Init")
So what I try to do is to get a column that is an server ip from my DB and compare it with the server ip and if the server ip is the same with the ip from the column then it will set unlicensed to 0 else it will set it to 1 (no matter if is an SQL error) My database tree would be like:
Code:

public_licenses
        licenses = 192.168.1.3:27015 (type varchar 60)


Neeeeeeeeeel.- 12-23-2015 12:42

Re: SQL Get column as cvar.
 
A licence system for AMXX is useless beucase it can be removed easily. Remember that you always have to provide the source code along the binaries you share.

XmasterOfficial 12-23-2015 16:32

Re: SQL Get column as cvar.
 
Quote:

Originally Posted by Neeeeeeeeeel.- (Post 2375582)
A licence system for AMXX is useless beucase it can be removed easily. Remember that you always have to provide the source code along the binaries you share.

Ok but I am still interested on what's wrong with my script since I tried I think more than 12 hours finding a result...
Edit: I was thinking and I want to explain what exactly I want to do. The above script will check if the plugin is authorised by me so if it is (unlicensed = 0) then it will do other querry to put the server stats on my website (player kills, etc). So what is above for me is like a passcode to check if the plugin is authorised to post data on my website. So after checking if the server IP is the same as the IPs on the column then if there is, it will set unlicensed to 0 otherwise to 1 then if unlicensed is 0 then it will post data from server on my website and if unlicensed is 0 it will be a return plugin handled.

Bugsy 12-24-2015 11:16

Re: SQL Get column as cvar.
 
Can you show me how your table is designed? Your query can be made better.

SELECT '%s' FROM `licenses` WHERE 1

I would do something like

SELECT * FROM tblLicenses WHERE ServerIP='%s'

No results means unlicensed. Or if you have a column that holds a license status (0/1) for if you withdraw a license without deleting the record from the table you can then check that.

Where does a cvar come into play?

XmasterOfficial 12-27-2015 17:44

Re: SQL Get column as cvar.
 
Quote:

Originally Posted by Bugsy (Post 2375903)
Can you show me how your table is designed? Your query can be made better.

SELECT '%s' FROM `licenses` WHERE 1

I would do something like

SELECT * FROM tblLicenses WHERE ServerIP='%s'

No results means unlicensed. Or if you have a column that holds a license status (0/1) for if you withdraw a license without deleting the record from the table you can then check that.

Where does a cvar come into play?

This is the database. Is just for test. There would be only IPs in that table.
postimg . org/image/xr7zxuqbt/6d5156f3/

I set a variable "unlicensed = 1" at the beginning so the plugin will check if that variable is 1 and if it is 1 then it will say that the server is not authorised to use this plugin bla bla. However the script above should check at the beginning if the server IP is inside the table and if it is then it should set unlicensed to 0.

Bugsy 12-27-2015 23:04

Re: SQL Get column as cvar.
 
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 );



XmasterOfficial 12-28-2015 21:00

Re: SQL Get column as cvar.
 
Quote:

Originally Posted by Bugsy (Post 2377109)
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.

Bugsy 12-28-2015 21:28

Re: SQL Get column as cvar.
 
I edited the above code to use get_user_ip( 0 ) and it's working fine for me. Just let the plugin create the table and use it. I don't know what you mean by 'all the columns would be server IP's'. You should have 1 column with rows of IP's.

XmasterOfficial 12-29-2015 02:26

Re: SQL Get column as cvar.
 
Quote:

Originally Posted by Bugsy (Post 2377500)
I edited the above code to use get_user_ip( 0 ) and it's working fine for me. Just let the plugin create the table and use it. I don't know what you mean by 'all the columns would be server IP's'. You should have 1 column with rows of IP's.

I did this:
Code:

#include <amxmodx>
#include <sqlx>

new const Version[] = "0.1"
new const szHost[] = "host"
new const szUser[] = "user"
new const szPass[] = "pass"
new const szDB[] = "plugin_licenses"

new Handle:g_SQLTuple , g_szBuffer[ 128 ]

public plugin_init()
{
register_plugin( "Check IP License" , Version , "bugsy" )
g_SQLTuple = SQL_MakeDbTuple( szHost , szUser , szPass , szDB )
new szIP[20]
get_user_ip( 0 , szIP , charsmax( szIP ) , 1 )
server_print( "%s",szIP )
formatex( g_szBuffer , charsmax( g_szBuffer ) , "SELECT '%s' FROM Licenses WHERE 1;" , szIP )
ExecuteQuery( g_szBuffer , 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( "License not found!" )
} else {
server_print( "Server license found!" )
}
}
SQL_FreeHandle( query )
SQL_FreeHandle( SqlConnection )
}

But, it allways tells "Server license found!". I edited a little bit so it should detect if the IP of the server is in the table "Licenses". Every column in "Licenses" have the IP of the servers. For example: Name: 192.168.1.2 and Value: 1. What could be the problem?

Bugsy 12-29-2015 07:46

Re: SQL Get column as cvar.
 
What I gave you works why do you keep changing it


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

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