PB_Geoip trouver un pays a partire de son ip! mise à jour

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
celtic88
Messages : 309
Inscription : sam. 12/sept./2015 14:31
Localisation : Alger

PB_Geoip trouver un pays a partire de son ip! mise à jour

Message par celtic88 »

salut tout le monde :)
voilla c est mon premier truc ici et j'espère que ce sera bon ,
et bon, dans mon projet j ai eu besoin de connaître le pays d un ip adresse,, et j ai chercher partout un exemple en Pb pour utiliser la base "Geoip.dat" mai son succes :(,, alors j ai dit tien pour quoi pas cree un pour ce super langage ,

-pour que sa marche il faux que vous télécharger la base de Geoip , et la poster devant l'exécutable ou le code source .
..a plus ;)

exemple

Code : Tout sélectionner

InitNetwork()
Procedure.s TCP_NameToIP(Dname.s)
  Protected *host.hostent
  Protected *Dma=AllocateMemory(StringByteLength(Dname,#PB_Ascii)+1)
  If Not *Dma
    ProcedureReturn 
  EndIf
  PokeS(*Dma,Dname,-1,#PB_Ascii)
  *host = gethostbyname_(*Dma)
  FreeMemory(*Dma)
  If *host
    ProcedureReturn PeekS(inet_ntoa_(PeekL(PeekL(*host\h_addr_list))),-1,#PB_Ascii)
  EndIf
EndProcedure

Debug CountryNames(_GeoIP_GetCountry(TCP_NameToIP("www.purebasic.com"))) 

Code : Tout sélectionner

;Author Celtic88
;la Base GeoIP telechrger par la : http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

Global Dim CountryNames.s(252) 
Global Dim CountryCodes.s(252) 
Define Ii
Restore CountryNames
For Ii = 0 To 252
  Read.s CountryNames(Ii)
Next
Restore CountryCodes
For Ii = 0 To 252
  Read.s CountryCodes(Ii)
Next

Global COUNTRY_BEGIN = 16776960
Global Error_GeoIPData=1
Global GeoIP_Cach_Memory
Global *GeoIP_datBuffer
Global OpGeoIP_dat

Procedure _intGeoIPData(GeoIP_Path.s="GeoIP.dat",FMemory = 1)
  Protected GetSize=FileSize(GeoIP_Path)
  If GetSize < 0 
    ProcedureReturn -1
  EndIf
  OpGeoIP_dat = OpenFile(#PB_Any,GeoIP_Path)
  If OpGeoIP_dat = 0 
    ProcedureReturn -2
  EndIf
  Protected Dim buf.b(2)
  Protected STRUCTURE_INFO_MAX_SIZE = 20,Ii
  If FMemory
    *GeoIP_datBuffer = AllocateMemory(GetSize)
    If  *GeoIP_datBuffer
      ReadData(OpGeoIP_dat,*GeoIP_datBuffer,GetSize)
      CloseFile(OpGeoIP_dat)
    Else
      ProcedureReturn -3
    EndIf
  EndIf
  GetSize-3
  If Not FMemory
    FileSeek(OpGeoIP_dat, GetSize)
  EndIf
  For Ii=0 To STRUCTURE_INFO_MAX_SIZE-1
    If FMemory
      CopyMemory(*GeoIP_datBuffer+GetSize, @BUF(), 3)
    Else
      ReadData(OpGeoIP_dat,@BUF(), 3)
    EndIf
    If (BUF(0) & 255) = 255 Or  (BUF(1) & 255) = 255 Or  (BUF(2) & 255) = 255 
      If FMemory
        FreeMemory(*GeoIP_datBuffer)
      Else
        CloseFile(OpGeoIP_dat)
      EndIf
      FreeArray(BUF())
      ProcedureReturn -4
    EndIf
    GetSize-4
    If Not FMemory
      FileSeek(OpGeoIP_dat, GetSize)
    EndIf
  Next
  GeoIP_Cach_Memory = FMemory
  Error_GeoIPData =0
  ProcedureReturn #True
EndProcedure

Procedure _SeekRecord(Numip.q)
  Protected Dim x.q(1)
  Protected Dim buf.b(5)
  Protected offset.q=0
  Protected PRT.l,depth,ITR,j
  For depth = 31 To 0 Step -1
    
    If GeoIP_Cach_Memory
      CopyMemory(*GeoIP_datBuffer+(offset*6), @BUF(), 6)
    Else
      FileSeek(OpGeoIP_dat,offset*6)
      ReadData(OpGeoIP_dat,@BUF(), 6)
    EndIf
    
    For ITR = 0 To 1
      x(ITR) = 0
      For j = 0 To 2
        PRT  = (BUF((ITR * 3) + j)) & 255
        x(ITR) + (PRT << (j * 8))
      Next j
    Next ITR
    
    If (Numip & (1 << depth)) <> 0 
      
      If x(1) >= COUNTRY_BEGIN
        ProcedureReturn x(1)
      Else
        Offset = x(1)
      EndIf
      
    Else
      
      If x(0) >= COUNTRY_BEGIN
        ProcedureReturn x(0)
      Else
        Offset = x(0)
      EndIf
      
    EndIf
    
  Next depth
  
EndProcedure

Procedure.q ConvertIPAddressToNumber_IPv4(Address.s)
  If CountString(Address, ".") = 3
    Protected adr1=Val(StringField(Address, 1, "."))
    Protected adr2=Val(StringField(Address, 2, "."))
    Protected adr3=Val(StringField(Address, 3, "."))
    Protected adr4=Val(StringField(Address, 4, "."))
    ProcedureReturn (Pow(256,3) * adr1) + (Pow(256,2) * adr2) + (256 * adr3) + adr4
  EndIf
EndProcedure

Procedure _GeoIP_GetCountry(IPNum.s)
  If Not Error_GeoIPData
    Protected ConvertIPAddressToNumber_IPv4.q=ConvertIPAddressToNumber_IPv4(IPNum)
    If ConvertIPAddressToNumber_IPv4
      Protected SeekCountry=_SeekRecord(ConvertIPAddressToNumber_IPv4)-COUNTRY_BEGIN
      If SeekCountry > 0 And SeekCountry < 253
        ProcedureReturn SeekCountry
      EndIf
    EndIf
  EndIf
  ProcedureReturn -1
EndProcedure

If _intGeoIPData("GeoIP.dat",0) < 0
  ;_Debug("Error Load GeoiPData")
EndIf

DataSection
  CountryCodes:
  Data.s "--","AP","EU","AD","AE","AF","AG","AI","AL","AM","AN","AO","AQ","AR","AS","AT","AU","AW","AZ","BA","BB","BD","BE","BF","BG","BH","BI","BJ","BM","BN","BO","BR","BS","BT","BV","BW","BY","BZ","CA","CC","CD","CF","CG","CH","CI","CK","CL","CM","CN","CO","CR","CU","CV","CX","CY","CZ","DE","DJ","DK","DM","DO","DZ","EC","EE","EG","EH","ER","ES","ET","FI","FJ","FK","FM","FO","FR","FX","GA","GB","GD","GE","GF","GH","GI","GL","GM","GN","GP","GQ","GR","GS","GT","GU","GW","GY","HK","HM","HN","HR","HT","HU","ID","IE","IL","IN","IO","IQ","IR","IS","IT","JM","JO","JP","KE","KG","KH","KI","KM","KN","KP","KR","KW","KY","KZ","LA","LB","LC","LI","LK","LR","LS","LT","LU","LV","LY","MA","MC","MD","MG","MH","MK","ML","MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV","MW","MX","MY","MZ","NA","NC","NE","NF","NG","NI","NL","NO","NP","NR","NU","NZ","OM","PA","PE","PF","PG","PH","PK","PL","PM","PN","PR","PS","PT","PW","PY","QA","RE","RO","RU","RW","SA","SB","SC","SD","SE","SG","SH","SI","SJ","SK","SL","SM","SN","SO","SR","ST","SV","SY","SZ","TC","TD","TF","TG","TH","TJ","TK","TM","TN","TO","TL","TR","TT","TV","TW","TZ","UA","UG","UM","US","UY","UZ","VA","VC","VE","VG","VI","VN","VU","WF","WS","YE","YT","RS","ZA","ZM","ME","ZW","A1","A2","O1","AX","GG","IM","JE","BL","MF"
  CountryNames:
  Data.s "LocalHost", "Asia/Pacific Region", "Europe", "Andorra", "United Arab Emirates", "Afghanistan", "Antigua and Barbuda", "Anguilla", "Albania", "Armenia", "Netherlands Antilles", "Angola", "Antarctica", "Argentina", "American Samoa", "Austria", "Australia", "Aruba", "Azerbaijan", "Bosnia and Herzegovina", "Barbados", "Bangladesh", "Belgium", "Burkina Faso", "Bulgaria", "Bahrain", "Burundi", "Benin", "Bermuda", "Brunei Darussalam", "Bolivia", "Brazil", "Bahamas", "Bhutan", "Bouvet Island", "Botswana", "Belarus", "Belize", "Canada", "Cocos (Keeling) Islands", "Congo, The Democratic Republic of the", "Central African Republic", "Congo", "Switzerland", "Cote D'Ivoire", "Cook Islands", "Chile", "Cameroon", "China", "Colombia", "Costa Rica", "Cuba", "Cape Verde", "Christmas Island", "Cyprus", "Czech Republic", "Germany", "Djibouti", "Denmark", "Dominica", "Dominican Republic", "Algeria", "Ecuador", "Estonia", "Egypt", "Western Sahara", "Eritrea", "Spain", "Ethiopia", "Finland", "Fiji", "Falkland Islands (Malvinas)", "Micronesia, Federated States of", "Faroe Islands", "France", "France, Metropolitan", "Gabon", "United Kingdom", "Grenada", "Georgia", "French Guiana", "Ghana", "Gibraltar", "Greenland", "Gambia", "Guinea", "Guadeloupe", "Equatorial Guinea", "Greece", "South Georgia and the South Sandwich Islands", "Guatemala", "Guam", "Guinea-Bissau", "Guyana", "Hong Kong", "Heard Island and McDonald Islands", "Honduras", "Croatia", "Haiti", "Hungary", "Indonesia", "Ireland", "Israel", "India", "British Indian Ocean Territory", "Iraq", "Iran, Islamic Republic of", "Iceland", "Italy", "Jamaica", "Jordan", "Japan", "Kenya", "Kyrgyzstan", "Cambodia", "Kiribati", "Comoros", "Saint Kitts and Nevis", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Cayman Islands", "Kazakstan", "Lao People's Democratic Republic", "Lebanon", "Saint Lucia", "Liechtenstein", "Sri Lanka", "Liberia", "Lesotho", "Lithuania", "Luxembourg", "Latvia", "Libyan Arab Jamahiriya", "Morocco", "Monaco", "Moldova, Republic of", "Madagascar", "Marshall Islands", "Macedonia, the Former Yugoslav Republic of", "Mali", "Myanmar", "Mongolia", "Macao", "Northern Mariana Islands", "Martinique", "Mauritania", "Montserrat", "Malta", "Mauritius", "Maldives", "Malawi", "Mexico", "Malaysia", "Mozambique", "Namibia", "New Caledonia", "Niger", "Norfolk Island", "Nigeria", "Nicaragua", "Netherlands", "Norway", "Nepal", "Nauru", "Niue", "New Zealand", "Oman", "Panama", "Peru", "French Polynesia", "Papua New Guinea", "Philippines", "Pakistan", "Poland", "Saint Pierre and Miquelon", "Pitcairn", "Puerto Rico", "Palestinian Territory, Occupied", "Portugal", "Palau", "Paraguay", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saudi Arabia", "Solomon Islands", "Seychelles", "Sudan", "Sweden", "Singapore", "Saint Helena", "Slovenia", "Svalbard and Jan Mayen", "Slovakia", "Sierra Leone", "San Marino", "Senegal", "Somalia", "Suriname", "Sao Tome and Principe", "El Salvador", "Syrian Arab Republic", "Swaziland", "Turks and Caicos Islands", "Chad", "French Southern Territories", "Togo", "Thailand", "Tajikistan", "Tokelau", "Turkmenistan", "Tunisia", "Tonga", "Timor-Leste", "Turkey", "Trinidad and Tobago", "Tuvalu", "Taiwan, Province of China", "Tanzania, United Republic of", "Ukraine", "Uganda", "United States Minor Outlying Islands", "United States", "Uruguay", "Uzbekistan", "Holy See (Vatican City State)", "Saint Vincent and the Grenadines", "Venezuela", "Virgin Islands, British", "Virgin Islands, U.S.", "Vietnam", "Vanuatu", "Wallis and Futuna", "Samoa", "Yemen", "Mayotte", "Yugoslavia", "South Africa", "Zambia", "Montenegro", "Zimbabwe", "Anonymous Proxy", "Satellite Provider", "Other", "Aland Islands", "Guernsey", "Isle of Man", "Jersey", "Saint Barthelemy", "Saint Martin"
EndDataSection
; IDE Options = PureBasic 5.24 LTS (Windows - x86)
; EnableUnicode
; EnableThread
Dernière modification par celtic88 le dim. 13/déc./2015 20:05, modifié 3 fois.
.....i Love Pb :)
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: PB_Geoip trouver un pays a partire de son ip!

Message par falsam »

Sympa ce code. Et même pas d'API. Merci Celtic :wink:
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
celtic88
Messages : 309
Inscription : sam. 12/sept./2015 14:31
Localisation : Alger

Re: PB_Geoip trouver un pays a partire de son ip!

Message par celtic88 »

falsam a écrit :Et même pas d'API. Merci Celtic :wink:
oui a cause de tes réclamation j ai arreter definitivement les api :p ,,merci pour votre reply :)
.....i Love Pb :)
Avatar de l’utilisateur
celtic88
Messages : 309
Inscription : sam. 12/sept./2015 14:31
Localisation : Alger

Re: PB_Geoip trouver un pays a partire de son ip!

Message par celtic88 »

Spock a écrit :pas mal le code :)

on peut faire plus court, et plus complet, en utilisant le Reseau :lol:
et avec Api :mrgreen:

voici le code qui en plus affiche la carte avec le lieu de l'ip
ps , si vous avez une demande d'installation de fichier MSI .... refusez !
et si le Script s'arrete , terminez le normalement
ps: je mettrai la version non crunché plus tard :)

Code : Tout sélectionner


ip.s="137.129.1.2" ; Ip a rechercher



UsePNGImageDecoder():Enumeration:#win:#file:#webgadget:EndEnumeration;;;;;;;;;;;;;;;;
Declare.s searchstring(string.s,startstring.s,endstring.s,index):;;
adr$="http://www.localiser-ip.com/?ip="+ip.s:If OpenWindow(#win,0,0,800,800,"ip loc"+
"ator",#PB_Window_SystemMenu|#PB_Window_ScreenCentered):WebGadget(#webgadget,1,1,800,
600,adr$):tmpFile.s=Space(1024):URLDownloadToCacheFile_(0,adr$,tmpFile,1024,0,0);;;;;
if readfile(#file,tmpFile.s):While Eof(#file)=0:ligne.s=ligne.s+ReadString(#file);;;;
Wend:CloseFile(#file):Endif:total.s=SearchString(ligne.s,"<br>L\","</b>. </b>'",1);;;
debug total.s:utilisateur1.s=SearchString(total.s,"(","- <b>",1);;;;;;;;;;;;;;;;;;;;;
utilisateur2.s=SearchString(total.s," <b>","</b>",1):Lieu.s=SearchString(total.s,"e"+
"st situé à <b>",")",2):debug "nom utilisateur (hebergeur) :"+utilisateur1.s;;;;;;;;;
debug "nom :"+utilisateur2.s:debug "lieu estimé :"+Lieu.s:Repeat;;;;;;;;;;;;;;;;;;;;;
Until WaitWindowEvent()=#PB_Event_CloseWindow:EndIf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Procedure.s SearchString(String.s,StartString.s,EndString.s,index);;;;;;;;;;;;;;;;;;;
Global SearchStringIndex,SearchString.s:SearchStringIndex=index:SearchString=String;;
Start=FindString(SearchString,StartString,SearchStringIndex):If Start:Start=Start+;;;
Len(StartString):EndIndex=FindString(SearchString,EndString,Start):If EndIndex;;;;;;;
SearchStringIndex=EndIndex+Len(EndString):ProcedureReturn Mid(SearchString,Start,;;;;
EndIndex-Start):Else:EndIndex=Len(SearchString)+1:SearchStringIndex=EndIndex+;;;;;;;;
Len(EndString):ProcedureReturn Mid(SearchString,Start,EndIndex-Start):EndIf:Else;;;;;
Start=1:EndIndex=FindString(SearchString,EndString,Start):If EndIndex;;;;;;;;;;;;;;;;
SearchStringIndex=EndIndex+Len(EndString):ProcedureReturn Mid(SearchString,Start,;;;;
EndIndex-Start):Else:EndIndex=Len(SearchString)+1:SearchStringIndex=EndIndex+;;;;;;;;
Len(EndString):ProcedureReturn Mid(SearchString,Start,EndIndex-Start):EndIf:EndIf;;;;
EndProcedure;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
hi
@Spock, toujour tu me surprend avec tes code WAW. TOU DOU mec je suis debutant hi
.....i Love Pb :)
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: PB_Geoip trouver un pays a partire de son ip!

Message par Kwai chang caine »

Les 2 codes marchent.
Je suis surpris que celui de SPOCK marche ici, car en temps normal rien ne passe avec mon proxy... 8O
Merci à vous deux.... 8)
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Répondre