Raised This Month: $7 Target: $400
 1% 

Module: Curl


Post New Thread Reply   
 
Thread Tools Display Modes
ConorCC
Member
Join Date: Feb 2014
Old 05-30-2021 , 07:11   Re: Module: Curl
Reply With Quote #31

I tried to run the example code. Works fine with HTTP. With HTTPS I receive the following error: Problem with the SSL CA cert (path? access rights?) The only thing I want to call APIs and work with response head, body, statuscode. Security is important for authentication.

Linux based HLDS hosted by a game server hosting.
ConorCC is offline
Th3822
Member
Join Date: Jan 2013
Location: Venezuela
Old 05-30-2021 , 07:18   Re: Module: Curl
Reply With Quote #32

Quote:
Originally Posted by ConorCC View Post
I tried to run the example code. Works fine with HTTP. With HTTPS I receive the following error: Problem with the SSL CA cert (path? access rights?) The only thing I want to call APIs and work with response head, body, statuscode. Security is important for authentication.

Linux based HLDS hosted by a game server hosting.
See: https://curl.se/docs/sslcerts.html#:...erforms%20peer

Get the CA file here: https://curl.se/docs/caextract.html
Th3822 is offline
ConorCC
Member
Join Date: Feb 2014
Old 05-30-2021 , 08:16   Re: Module: Curl
Reply With Quote #33

Quote:
Originally Posted by Th3822 View Post

Thank You! Do you think I have to set a full path to the file? Cause I tried to user "/cacert.pem" as CAINFO and the result is the same. While I turn to insecure mode the request is fine.

Edit: Nevermind! Working with full path. Thank you one more!

Last edited by ConorCC; 05-30-2021 at 08:26.
ConorCC is offline
sssss1
Junior Member
Join Date: Jan 2022
Old 02-15-2022 , 10:34   Re: Module: Curl
Reply With Quote #34

Quote:
Originally Posted by Th3822 View Post
Here is a full plugin that does use a discord's webhooks:

PHP Code:
#include <amxmodx>
#include <json>
#include <curl>

new const g_szURL[] = "https://discordapp.com/api/webhooks/{webhook.id}/{webhook.token}";
new const 
g_iTimeBetweenCalls 30;

new 
g_iLastCallbool:g_bIsWorkingCURL:g_cURLHandlecurl_slist:g_cURLHeadersg_szHostname[129], g_szNetAddress[22];

public 
plugin_init()
{
    
register_plugin("[cURL] Discord !admin Webhook""1.0""Th3-822");

    
register_clcmd("say !admin""cmd_admincall");
}

public 
plugin_cfg()
{
    
// Add a delay to wait for the values for g_szHostname and g_szNetAddress
    
g_iLastCall get_systime();
    
set_task(10.0"plugin_cfg_delayed");
}

public 
plugin_cfg_delayed()
{
    
get_cvar_string("hostname"g_szHostnamecharsmax(g_szHostname));
    
get_cvar_string("net_address"g_szNetAddresscharsmax(g_szNetAddress));
}

public 
plugin_end()
{
    if (
g_cURLHandle)
    {
        
curl_easy_cleanup(g_cURLHandle);
        
g_cURLHandle CURL:0;
    }
    if (
g_cURLHeaders)
    {
        
curl_slist_free_all(g_cURLHeaders);
        
g_cURLHeaders curl_slist:0;
    }
}

// Replace MB Chars with "."
_fixName(name[])
{
    new 
0;
    while (
name[i] != 0)
    {
        if (!(
<= name[i] <= 255))
        {
            
name[i] = '.';
        }
        
i++;
    }
}

public 
cmd_admincall(id)
{
    static 
iCurTime;

    if (!
g_bIsWorking && ((iCurTime get_systime()) - g_iLastCall) > g_iTimeBetweenCalls)
    {
        
g_iLastCall iCurTime;

        static 
szName[32], szAuthId[35], szBuffer[129], JSON:jEmbeds[1], JSON:jEmbedJSON:jWebhook;
        
get_user_name(idszNamecharsmax(szName));
        
get_user_authid(idszAuthIdcharsmax(szAuthId));
        
_fixName(szName);

        
// Create array of embed objects
        
jEmbed json_create();
        
json_set_string(jEmbed"title"g_szHostname);
        
formatex(szBuffercharsmax(szBuffer), "Click to enter on the server: steam://connect/%s"g_szNetAddress);
        
json_set_string(jEmbed"description"szBuffer);
        
jEmbeds[0] = jEmbed;

        
// Create webhook request object
        
jWebhook json_create();
        
formatex(szBuffercharsmax(szBuffer), "@here ^"%s^" <%s> is calling an Admin."szNameszAuthId);
        
json_set_string(jWebhook"content"szBuffer);
        
json_set_array(jWebhook"embeds"jEmbedssizeof(jEmbeds), _JSON_Object);

        
// Send It
        
postJSON(g_szURLjWebhook);
        
json_destroy(jWebhook);
        
// json_destroy(jEmbed); // Destroyed in chain
    
}
    else 
client_print(idprint_chat" ** Admins Can Be Called Once Each %d Seconds **"g_iTimeBetweenCalls);

    return 
PLUGIN_HANDLED;
}

postJSON(const link[], JSON:jObject)
{
    if (!
g_cURLHandle)
    {
        if (!(
g_cURLHandle curl_easy_init()))
        {
            
log_amx("[Fatal Error] Cannot Init cURL Handle.");
            
pause("d");
            return;
        }
        if (!
g_cURLHeaders)
        {
            
// Init g_cURLHeaders with "Content-Type: application/json"
            
g_cURLHeaders curl_slist_append(g_cURLHeaders"Content-Type: application/json");
            
curl_slist_append(g_cURLHeaders"User-Agent: 822_AMXX_PLUGIN/1.0"); // User-Agent
            
curl_slist_append(g_cURLHeaders"Connection: Keep-Alive"); // Keep-Alive
        
}

        
// Static Options
        
curl_easy_setopt(g_cURLHandleCURLOPT_SSL_VERIFYPEER0);
        
curl_easy_setopt(g_cURLHandleCURLOPT_SSL_VERIFYHOST0);
        
curl_easy_setopt(g_cURLHandleCURLOPT_SSLVERSIONCURL_SSLVERSION_TLSv1);
        
curl_easy_setopt(g_cURLHandleCURLOPT_FAILONERROR0);
        
curl_easy_setopt(g_cURLHandleCURLOPT_FOLLOWLOCATION0);
        
curl_easy_setopt(g_cURLHandleCURLOPT_FORBID_REUSE0);
        
curl_easy_setopt(g_cURLHandleCURLOPT_FRESH_CONNECT0);
        
curl_easy_setopt(g_cURLHandleCURLOPT_CONNECTTIMEOUT10);
        
curl_easy_setopt(g_cURLHandleCURLOPT_TIMEOUT10);
        
curl_easy_setopt(g_cURLHandleCURLOPT_HTTPHEADERg_cURLHeaders);
        
curl_easy_setopt(g_cURLHandleCURLOPT_POST1);
    }

    static 
szPostdata[513];
    
json_encode(jObjectszPostdatacharsmax(szPostdata));
    
//log_amx("[DEBUG] POST: %s", szPostdata);

    
curl_easy_setopt(g_cURLHandleCURLOPT_URLlink);
    
curl_easy_setopt(g_cURLHandleCURLOPT_COPYPOSTFIELDSszPostdata);

    
g_bIsWorking true;
    
curl_easy_perform(g_cURLHandle"postJSON_done");
}

public 
postJSON_done(CURL:curlCURLcode:code)
{
    
g_bIsWorking false;
    if (
code == CURLE_OK)
    {
        static 
statusCode;
        
curl_easy_getinfo(curlCURLINFO_RESPONSE_CODEstatusCode);
        if (
statusCode >= 400)
        {
            
log_amx("[Error] HTTP Error: %d"statusCode);
        }
    }
    else
    {
        
log_amx("[Error] cURL Error: %d"code);
        
curl_easy_cleanup(g_cURLHandle);
        
g_cURLHandle CURL:0;
    }

btw, curl_easy_cleanup and curl_slist_free_all should set the pointer to a invalid value to avoid a crash.
compile error
sssss1 is offline
Th3822
Member
Join Date: Jan 2013
Location: Venezuela
Old 02-15-2022 , 14:21   Re: Module: Curl
Reply With Quote #35

Quote:
Originally Posted by sssss1 View Post
compile error
plugin was made using this json.inc for compiling with 1.8.2 too

download that .inc, rename it to avoid using the new json.inc from 1.9 to... Dunno "json_old.inc"
and rename it on the plugin too: #include <json_old>

Last edited by Th3822; 02-16-2022 at 21:11. Reason: typo
Th3822 is offline
sssss1
Junior Member
Join Date: Jan 2022
Old 02-16-2022 , 04:52   Re: Module: Curl
Reply With Quote #36

Quote:
Originally Posted by Th3822 View Post
plugin was maded with this json.inc for compiling with 1.8.2 too

download that .inc, rename it to avoid using the new json.inc from 1.9 to... Dunno "json_old.inc"
and rename it on the plugin too: #include <json_old>
is there such a thing for 1.8.3?
not something complicated
order "/report"
send a "We have a hacker on the server" message on the discord server
sssss1 is offline
Th3822
Member
Join Date: Jan 2013
Location: Venezuela
Old 02-16-2022 , 21:13   Re: Module: Curl
Reply With Quote #37

Quote:
Originally Posted by sssss1 View Post
is there such a thing for 1.8.3?
not something complicated
order "/report"
send a "We have a hacker on the server" message on the discord server
it should compile fine with 1.8.3 as long you follow the instructions and download and rename the include
(btw, you can also compile with 1.8.2 and it will work with 1.8.3, 1.9 & 1.10 too)
Th3822 is offline
Reply


Thread Tools
Display Modes

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 05:12.


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