PDA

View Full Version : [CoD 1.1] How should I call curl from gsc?



raphael
26th March 2023, 07:38
Hello

I'm taking over PlayerConnect, to check if player uses a vpn ip before letting him in

For that, I need to call an online api using curl, to get a response about the ip info, it looks like json

I'm using codextended

I'm thinking about calling the api from the .gsc, would that be the good way?

Or should I build codextended to let it do the verification?

Do you know how I could call curl and get a response from .gsc?

Would you have an alternative to curl to suggest if you think there is a better option to use?

Paho
26th March 2023, 15:49
i use too vpn api. Codextended .php + database mysql

Paho
26th March 2023, 15:57
system("php vpn.php "+self getIP()+" "+self.id);
<?php
$ip = $argv[1];
$id = $argv[2];
$ch = curl_init('http://proxycheck.io/v2/'.$ip.'?key=mykey&vpn=1&asn=1&risk=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
$api_result = json_decode($json, true);
$proxy=0;
$risk=$api_result[$ip]['risk'];
if($risk>65)
$proxy=1;
$mi = new mysqli("url", "login", "password", "dbname");
$eboy=$mi->query("UPDATE `users` SET `city`='".$api_result[$ip]['city']."', `country`='".$api_result[$ip]['country']."', `isocode`='".$api_result[$ip]['isocode']."', `vpn`='".$proxy."' WHERE `realid`='".intval($id)."'");
$mi->close();
?>

raphael
26th March 2023, 19:18
Thank you very much :)

raphael
27th March 2023, 16:04
I never used this system() command

Can you get the curl/php response from it directly in gsc?

Or you use sql for that?

Paho
27th March 2023, 17:30
1. Run php script from gsc
2. Write in DataBase from .php
3. Read db-query in gsc
My example realisation:
1703
system libcod:
1704
You can try use return 1/0 or -1 in php script from gsc(libcod: system()) without db, but i cant test it now.
Sorry for my bad english

raphael
29th March 2023, 09:20
Finally I did it through codextended, below is how, hoping it could please someone.

Add cJSON.c and cJSON.h from https:// github.com/DaveGamble/cJSON to src folder.

sv_client.c


...
#include <stdbool.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

#include "cJSON.h"
...




...
NET_OutOfBandPrint( NS_SERVER, from, "error\nIllegal name.");
return;
}

//CHECK VPN
cvar_t* blockvpn = Cvar_Get("x_blockvpn", "", 0);
cvar_t* blockvpn_url = Cvar_Get("x_blockvpn_url", "", 0);
cvar_t* blockvpn_url_parameter1 = Cvar_Get("x_blockvpn_url_parameter1", "", 0);
cvar_t* blockvpn_apikey = Cvar_Get("x_blockvpn_apikey", "", 0);
cvar_t* blockvpn_message = Cvar_Get("x_blockvpn_message", "", 0);

if (blockvpn->integer == 1
&& blockvpn_url->string[0]
&& blockvpn_url_parameter1->string[0]
&& blockvpn_apikey->string[0])
{
cprintf(PRINT_UNDERLINE | PRINT_DEBUG, "GOING TO CHECK VPN\n");

struct string s;
init_string(&s);
CURL *curl = curl_easy_init();
CURLcode res;
if (curl)
{
cprintf(PRINT_UNDERLINE | PRINT_DEBUG, "curl PASSED\n");

bool* ipIsOk = true;

char ip[16];
sprintf(ip, "%d.%d.%d.%d",
from.ip[0],
from.ip[1],
from.ip[2],
from.ip[3]);

char* url[
strlen(blockvpn_url->string)
+strlen(ip)
+strlen(blockvpn_url_parameter1->string)
+strlen(blockvpn_apikey->string)
+1];

strcpy(url, blockvpn_url->string);
strcat(url, ip);
strcat(url, blockvpn_url_parameter1->string);
strcat(url, blockvpn_apikey->string);

cprintf(PRINT_UNDERLINE | PRINT_DEBUG, "url = %s", url);
cprintf(PRINT_UNDERLINE | PRINT_DEBUG, "\n");

curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

if (res == CURLE_OK)
{
cprintf(PRINT_UNDERLINE | PRINT_DEBUG, "curl CURLE_OK\n");

cJSON* parsedJson = cJSON_Parse(s.ptr);
free(s.ptr);

cJSON* securityObject = cJSON_GetObjectItemCaseSensitive(parsedJson, "security");
cJSON* vpn = cJSON_GetObjectItemCaseSensitive(securityObject, "vpn");
cJSON* proxy = cJSON_GetObjectItemCaseSensitive(securityObject, "proxy");
cJSON* tor = cJSON_GetObjectItemCaseSensitive(securityObject, "tor");
cJSON* relay = cJSON_GetObjectItemCaseSensitive(securityObject, "relay");

if (cJSON_IsBool(vpn))
{
if (cJSON_IsTrue(vpn))
{
ipIsOk = false;
}
}
if (cJSON_IsBool(proxy))
{
if (cJSON_IsTrue(proxy))
{
ipIsOk = false;
}
}
if (cJSON_IsBool(tor))
{
if (cJSON_IsTrue(tor))
{
ipIsOk = false;
}
}
if (cJSON_IsBool(relay))
{
if (cJSON_IsTrue(relay))
{
ipIsOk = false;
}
}

if (!ipIsOk)
{
cprintf(PRINT_UNDERLINE | PRINT_DEBUG, "BAD IP DETECTED\n");

if (blockvpn_message->string[0])
{
NET_OutOfBandPrint(NS_SERVER, from, "error\n%s", blockvpn_message->string);
}
else
{
NET_OutOfBandPrint(NS_SERVER, from, "error\nIP address not accepted.");
}
return;
}
else
{
cprintf(PRINT_UNDERLINE | PRINT_DEBUG, "BAD IP NOT DETECTED\n");
}
}
else
{
cprintf(PRINT_UNDERLINE | PRINT_DEBUG, stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
}
else
{
cprintf(PRINT_UNDERLINE | PRINT_DEBUG, "curl NOT PASSED\n");
}
}
else
{
cprintf(PRINT_UNDERLINE | PRINT_DEBUG, "COULDN'T CHECK FOR VPN\n");
}

#if 0
if(strcmp(check_name, "php")) {
NET_OutOfBandPrint(NS_SERVER, from, "error\nkthxnbai m8\n");
...



build.sh


...
$compiler $params -c sv_commands.c -o obj/sv_commands.o

$compiler $params -c cJSON.c -o obj/cJSON.o

$compiler $params -c sv_client.c -o obj/sv_client.o
...




...
else
if [ $DEBUG = true ]; then
echo "LINKING CURL"
$compiler -m32 -shared -L/lib32 -L./lib -o ../bin/codextended.so $obj -lz $LINK_LIBS $STEAM_LINK -ldl -lm -Wall -lcurl
else
$compiler -m32 -shared -L/lib32 -L./lib -o ../bin/codextended.so $obj -Os -s -lz $LINK_LIBS $STEAM_LINK -ldl -lm -Wall
...



myserver.cfg


...
set scr_teambalance "1"

//Custom cvars
set x_blockvpn "1"
set x_blockvpn_url "https://vpnapi.io/api/"
set x_blockvpn_url_parameter1 "?key="
set x_blockvpn_apikey "XXXXXXXXXXXXXXX"
set x_blockvpn_message "VPN/Proxy/Tor/Relay IP address detected."


// Deathmatch
set scr_dm_scorelimit "50"
...



I build using bash build.sh -d

_____________

I used a debian 64bit vm to build
I use a debian 64bit vps for the server

Following commands are written from memory
To build I did on the vm:


apt-get remove curl libcurl4
dpkg --add-architecture i386
apt update
apt install curl:i386 libcurl4:i386 libcurl4-openssl-dev:i386


And to run on the server I did:


apt-get remove curl libcurl4
dpkg --add-architecture i386
apt update
apt install curl:i386 libcurl4:i386