SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 21/05/2020, 09:28 AM   #11
hopeonxanny
Huge Clucker
 
hopeonxanny's Avatar
 
Join Date: Apr 2020
Location: SA-MP Awesomeness: 1998
Posts: 238
Reputation: 4
Default Re: I need a working detection of country script

thanks, Kwarde.

i've been looking for a more optimised & shorter code. didn't know about that site, doesn't require api & things...
for a begginer and after i have read some other scripts and documentation, i don't think that "http://" needs to be there.

i have tested it now. having http:// in front doesn't work for me.
i have removed the http:// and works like a charm.

a question.. what if i get no response? does it show something or no?
__________________
hopeonxanny is offline   Reply With Quote
Old 21/05/2020, 12:05 PM   #12
NeXTGoD
Banned
 
Join Date: Jul 2018
Posts: 118
Reputation: 9
Default Re: I need a working detection of country script

Quote:
Originally Posted by Kwarde View Post
This whole thing can be done so much easier:

pawn Code:
//Get IP and send that data:
new string[45];
GetPlayerIp(playerid, string, 16);
format(string, sizeof(string), "http://ip-api.com/csv/%s?fields=3", string);
HTTP(playerid, HTTP_GET, string, "", "OnLookupIP");

//Retrieve the data:
foward OnLookupIP(index, response_code, data[]);
public OnLookupIP(index, response_code, data[]) //Not checking if response was OK (200) here
{
    new Country[64], cCode[2];
    sscanf(data, "p<,>s[64]s[2]", Country, cCode);
    //Now you know the country (Country) and country code (cCode) of "index" (index = playerid)
}

http://ip-api.com/csv/IP_HERE?fields=3 returns nothing but: Country,Country_Code. If you just need the country name, use ?fields=1. Then you won't even need sscanf and you can just use "data".

NOTE: HTTP() is threaded. So if you want to retrieve the country before doing anything else (such as checking if the player is registered) you should use this under OnPlayerConnect as the very first thing, and proceed once the data comes in at OnLookupIP().

EDIT:
If you want to fetch all data like in the post above, you should use ip-api.com/csv/IP_HERE (without ?fields=x)
This returns (if success):
Code:
success,COUNTRY_NAME,COUNTRY_CODE(2 chars),REGION_CODE(2 chars),REGION_NAME,CITY,LAT,LON,TIMEZONE,ISP,ORG,AS,QUERY(which is the IP)
If it failed (eg. by using a local IP):
Code:
fail,message,query
'message' in such a case would be "private range" or "reserved range" if you're connecting as localhost (127.0.0.1)


Since the /csv/ returns the data seperated by comma's you won't need multiple kind of functions to fetch the data, you can simply use sscanf. Example:
pawn Code:
//Get IP and send that data:
new string[37];
GetPlayerIp(playerid, string, 16);
format(string, sizeof(string), "http://ip-api.com/csv/%s", string);
HTTP(playerid, HTTP_GET, string, "", "OnLookupIP");

foward OnLookupIP(index, response_code, data[]);
public OnLookupIP(index, response_code, data[])
{
    new success[8], Country[64], cCode[2], rCode[2], Region[64], City[64], Float:gps_lat, Float:gps_lon, timezone[64], IspName[64], IspOrg[64], AS[10];
    sscanf(data, "p<,>s[8]s[64]s[2]s[2]s[64]s[64]ffs[64]s[64]s[64]s[10]", success, Country, cCode, rCode, Region, City, gps_lat, gps_lon, timezone, IspName, IspOrg, AS);
}

If success is "fail", use "Country" to get the message error.
Note how I by default used array sizes of 64 cells. No idea what maximum names can be for regions, ISP, cities etc. Might wanna tweak it a bit if it turns out they're too small or too big.
WOOOW BILLION THANKS TO YOU OMG !

+REPP
NeXTGoD is offline   Reply With Quote
Old 21/05/2020, 12:33 PM   #13
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,713
Reputation: 1671
Default Re: I need a working detection of country script

Quote:
Originally Posted by hopeonxanny View Post
a question.. what if i get no response? does it show something or no?
https://wiki.sa-mp.com/wiki/HTTP ;

Code:
HTTP_ERROR_BAD_HOST			1
HTTP_ERROR_NO_SOCKET			2
HTTP_ERROR_CANT_CONNECT	                3
HTTP_ERROR_CANT_WRITE			4
HTTP_ERROR_CONTENT_TOO_BIG		5
HTTP_ERROR_MALFORMED_RESPONSE	        6
As well as the response codes listed above, there are also all of the typical HTTP responses such as 404 (Page not found), 500 (Server error) or 403 (forbidden).

So yes, it does show something.
When response is good the response_code will be 200. You can find it all on the wiki


EDIT:
Speaking of, just looked at it since I opened it anyway and 'http://' must not be used indeed
__________________
When the opportunity presents itself to flip-a da table, uh, you flip-a da table.

Discord: Kwarde#8009
Kwarde is offline   Reply With Quote
Old 21/05/2020, 01:17 PM   #14
NeXTGoD
Banned
 
Join Date: Jul 2018
Posts: 118
Reputation: 9
Default Re: I need a working detection of country script

Quote:
Originally Posted by Kwarde View Post
https://wiki.sa-mp.com/wiki/HTTP ;

Code:
HTTP_ERROR_BAD_HOST			1
HTTP_ERROR_NO_SOCKET			2
HTTP_ERROR_CANT_CONNECT	                3
HTTP_ERROR_CANT_WRITE			4
HTTP_ERROR_CONTENT_TOO_BIG		5
HTTP_ERROR_MALFORMED_RESPONSE	        6
As well as the response codes listed above, there are also all of the typical HTTP responses such as 404 (Page not found), 500 (Server error) or 403 (forbidden).

So yes, it does show something.
When response is good the response_code will be 200. You can find it all on the wiki


EDIT:
Speaking of, just looked at it since I opened it anyway and 'http://' must not be used indeed
Do you know if I can detect a proxy user that fakes his IP with VPN?
NeXTGoD is offline   Reply With Quote
Old 21/05/2020, 02:10 PM   #15
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,713
Reputation: 1671
Default Re: I need a working detection of country script

Nope.
Might wanna read this article: https://www.comparitech.com/blog/vpn...-detect-a-vpn/

To sum everything up from that article (it's literally a quote of it)
Quote:
It usually takes a number of techniques or observations to compile enough indicators which indicate a VPN is in use, and even then it can be hard to be 100% sure
__________________
When the opportunity presents itself to flip-a da table, uh, you flip-a da table.

Discord: Kwarde#8009
Kwarde is offline   Reply With Quote
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
I want gamemode script for my country khadeptrai Scripting Help 1 02/10/2018 05:06 AM
Leg hit bodypart detection not working. Dokins Scripting Help 1 15/02/2015 11:36 PM
Working on my script need help KingyKings Scripting Help 3 30/11/2011 04:31 PM
How to make country detection on connect? NiXeL Help Archive 4 21/06/2010 10:36 AM
[FilterScript] [FS] Short and simple teleport dialog from country to country bartje01 Filterscripts 2 18/03/2010 01:31 PM


All times are GMT. The time now is 05:39 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.