IPtoCountry snippet
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

IPtoCountry snippet

Started by Mardeg, 01 July, 2006, 01:39:35

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mardeg

This is just a simple code snippet that relies on an IP being resolvable to a hostname so it can convert the domain at the end to its corresponding country. I include it in a customised getInfo command myself, feel free to use it however you want.

Usage: IPtoCountry(user.sIP)

Notes:
? Either save this as a separate .lua file and dofile(thename.lua) in your script's Main() function, or paste the code inside the ------------- directly into your script's Main() function.
? This requires both the pxwsa dll (http://www.pxextension.piratez.dk/downloads/PxWSA-L51-0.1.1.1-Beta.zip) and borland dependancies dll (http://www.pxextension.piratez.dk/downloads/cc3270.zip) to be in ptokax's scripts directory

-----------------------------
ibinit = package.loadlib("pxwsa_l51.dll", "_libinit")
libinit()
WSA.Init()
------------------------------ where ip4 is user.sIP and pn is user.sName
function IPtoCountry(ip4,pn)
	local ipdns = WSA.GetHostByAddr(ip4)
	if ipdns == nil then ipdns = "Unknown"
	else
		local cc = string.match(ipdns,"%.(%a-)$")
		ipdns = ccodes[string.lower(cc)] or string.upper(cc)
	end
	if ipdns ~= "Unknown" then return ipdns
	else
		curSocket = nil
		bConnected = false
		bNonBlock = true
		rHost = "whois.lacnic.net"
		rPort = 43
		sInBuf = ""
		psn = pn
		SetTimer(1) -- 1 sec
		StartTimer()
		return lookup(ip4,pn)
	end
end
function lookup(ip4,pn)
	if (DoConnect() == "Unknown") or (SendWhoisReq(ip4) == "Unknown") then return "Unknown" end
	return "Pending"
end
function DoConnect()
	errorCode, errorStr, curSocket = WSA.NewSocket(0)
	if errorCode then
		return "Unknown"
	end
	errorCode, errorStr = WSA.Connect(curSocket, rHost, rPort)
	if errorCode then
		bConnected = false
		return "Unknown"
	else
		bConnected = true
		if bNonBlock == true then
			errorCode, errorStr = WSA.MarkNonBlocking(curSocket)
			if errorCode then
				return "Unknown"
			end
		end
	end
end
function SendWhoisReq(ip4)
	errorCode, errorStr, bytesSent = WSA.Send(curSocket, ip4.."\n")
	if errorCode then
		WSA.Close(curSocket)
		bConnected = false
		sInBuf = ""
		return "Unknown"
	end
end

function GetWhoisResponse(pn)
	errorCode, errorStr, sData, bytesRead = WSA.Receive(curSocket)
	if errorCode then
		if errorCode == 0 then
	    	WSA.Close(curSocket)
	    	bConnected = false
	    	sInBuf = string.match(sInBuf,"ountry:%s+(%u+)%U") or string.match(sInBuf,"domain%s+%(%.(%a+)%)")
	    	sInBuf = ccodes[string.lower(sInBuf or "info")]
	    	SendPmToOps("OpChat", pn.." is from "..sInBuf)
	    	end
	    	sInBuf = ""
    	elseif errorCode ~= 10035 then
	    	WSA.Close(curSocket)
	    	bConnected = false
	    	sInBuf = ""
		end
	else
	    sInBuf = sInBuf..sData
	end
end
function OnTimer()
  if bConnected then
    GetWhoisResponse(psn)
  end
end
function OnExit()
  errorCode, errorStr = WSA.Dispose()
end
ccodes = {
	["ac"] = "Ascension Island",
	["ad"] = "Andorra",
	["ae"] = "United Arab Emirates",
	["af"] = "Afghanistan",
	["ag"] = "Antigua and Barbuda",
	["ai"] = "Anguilla",
	["al"] = "Albania",
	["am"] = "Armenia",
	["an"] = "Netherlands Antilles",
	["ao"] = "Angola",
	["aq"] = "Antarctica",
	["ar"] = "Argentina",
	["as"] = "American Samoa",
	["at"] = "Austria",
	["au"] = "Australia",
	["aw"] = "Aruba",
	["ax"] = "Aland Islands",
	["az"] = "Azerbaijan",
	["ba"] = "Bosnia and Herzegovina",
	["bb"] = "Barbados",
	["bd"] = "Bangladesh",
	["be"] = "Belgium",
	["bf"] = "Burkina Faso",
	["bg"] = "Bulgaria",
	["bh"] = "Bahrain",
	["bi"] = "Burundi",
	["bj"] = "Benin",
	["bm"] = "Bermuda",
	["bn"] = "Brunei Darussalam",
	["bo"] = "Bolivia",
	["br"] = "Brazil",
	["bs"] = "Bahamas",
	["bt"] = "Bhutan",
	["bv"] = "Bouvet Island",
	["bw"] = "Botswana",
	["by"] = "Belarus",
	["bz"] = "Belize",
	["ca"] = "Canada",
	["cc"] = "Comath.cos (Keeling) Islands",
	["cd"] = "The Democratic Republic of the Congo",
	["cf"] = "Central African Republic",
	["cg"] = "Republic of Congo",
	["ch"] = "Switzerland",
	["ci"] = "Cote d'Ivoire",
	["ck"] = "Cook Islands",
	["cl"] = "Chile",
	["cm"] = "Cameroon",
	["cn"] = "China",
	["co"] = "Colombia",
	["cr"] = "Costa Rica",
	["cs"] = "Serbia and Montenegro",
	["cu"] = "Cuba",
	["cv"] = "Cape Verde",
	["cx"] = "Christmas Island",
	["cy"] = "Cyprus",
	["cz"] = "Czech Republic",
	["de"] = "Germany",
	["dj"] = "Djibouti",
	["dk"] = "Denmark",
	["dm"] = "Dominica",
	["do"] = "Dominican Republic",
	["dz"] = "Algeria",
	["ec"] = "Ecuador",
	["ee"] = "Estonia",
	["eg"] = "Egypt",
	["eh"] = "Western Sahara",
	["er"] = "Eritrea",
	["es"] = "Spain",
	["et"] = "Ethiopia",
	["fi"] = "Finland",
	["fj"] = "Fiji",
	["fk"] = "Falkland Islands (Malvinas)",
	["fm"] = "Micronesia",
	["fo"] = "Faroe Islands",
	["fr"] = "France",
	["ga"] = "Gabon",
	["gb"] = "United Kingdom",
	["gd"] = "Grenada",
	["ge"] = "Georgia",
	["gf"] = "French Guiana",
	["gg"] = "Guernsey",
	["gh"] = "Ghana",
	["gi"] = "Gibraltar",
	["gl"] = "Greenland",
	["gm"] = "Gambia",
	["gn"] = "Guinea",
	["gp"] = "Guadeloupe",
	["gq"] = "Equatorial Guinea",
	["gr"] = "Greece",
	["gs"] = "South Georgia and the South Sandwich Islands",
	["gt"] = "Guatemala",
	["gu"] = "Guam",
	["gw"] = "Guinea-Bissau",
	["gy"] = "Guyana",
	["hk"] = "Hong Kong",
	["hm"] = "Heard and McDonald Islands",
	["hn"] = "Honduras",
	["hr"] = "Croatia/Hrvatska",
	["ht"] = "Haiti",
	["hu"] = "Hungary",
	["id"] = "Indonesia",
	["ie"] = "Ireland",
	["il"] = "Israel",
	["im"] = "Isle of Man",
	["in"] = "India",
	["io"] = "British Indian Ocean Territory",
	["iq"] = "Iraq",
	["ir"] = "Iran",
	["is"] = "Iceland",
	["it"] = "Italy",
	["je"] = "Jersey",
	["jm"] = "Jamaica",
	["jo"] = "Jordan",
	["jp"] = "Japan",
	["ke"] = "Kenya",
	["kg"] = "Kyrgyzstan",
	["kh"] = "Cambodia",
	["ki"] = "Kiribati",
	["km"] = "Comoros",
	["kn"] = "Saint Kitts and Nevis",
	["kp"] = "Democratic People's Republic of Korea",
	["kr"] = "Republic of Korea",
	["kw"] = "Kuwait",
	["ky"] = "Cayman Islands",
	["kz"] = "Kazakhstan",
	["la"] = "Lao People's Democratic Republic",
	["lb"] = "Lebanon",
	["lc"] = "Saint Lucia",
	["li"] = "Liechtenstein",
	["lk"] = "Sri Lanka",
	["lr"] = "Liberia",
	["ls"] = "Lesotho",
	["lt"] = "Lithuania",
	["lu"] = "Luxembourg",
	["lv"] = "Latvia",
	["ly"] = "Libyan Arab Jamahiriya",
	["ma"] = "Morocco",
	["mc"] = "Monaco",
	["md"] = "Moldova",
	["mg"] = "Madagascar",
	["mh"] = "Marshall Islands",
	["mk"] = "The Former Yugoslav Republic of Macedonia",
	["ml"] = "Mali",
	["mm"] = "Myanmar",
	["mn"] = "Mongolia",
	["mo"] = "Macau",
	["mp"] = "Northern Mariana Islands",
	["mq"] = "Martinique",
	["mr"] = "Mauritania",
	["ms"] = "Montserrat",
	["mt"] = "Malta",
	["mu"] = "Mauritius",
	["mv"] = "Maldives",
	["mw"] = "Malawi",
	["mx"] = "Mexico",
	["my"] = "Malaysia",
	["mz"] = "Mozambique",
	["na"] = "Namibia",
	["nc"] = "New Caledonia",
	["ne"] = "Niger",
	["nf"] = "Norfolk Island",
	["ng"] = "Nigeria",
	["ni"] = "Nicaragua",
	["nl"] = "Netherlands",
	["no"] = "Norway",
	["np"] = "Nepal",
	["nr"] = "Nauru",
	["nt"] = "Northern Territory",
	["nu"] = "Niue",
	["nz"] = "New Zealand",
	["om"] = "Oman",
	["pa"] = "Panama",
	["pe"] = "Peru",
	["pf"] = "French Polynesia",
	["pg"] = "Papua New Guinea",
	["ph"] = "Philippines",
	["pk"] = "Pakistan",
	["pl"] = "Poland",
	["pm"] = "Saint Pierre and Miquelon",
	["pn"] = "Pitcairn Island",
	["pr"] = "Puerto Rico",
	["ps"] = "Occupied Palestinian Territory",
	["pt"] = "Portugal",
	["pw"] = "Palau",
	["py"] = "Paraguay",
	["qa"] = "Qatar",
	["re"] = "Reunion Island",
	["ro"] = "Romania",
	["ru"] = "Russian Federation",
	["rw"] = "Rwanda",
	["sa"] = "Saudi Arabia",
	["sb"] = "Solomon Islands",
	["sc"] = "Seychelles",
	["sd"] = "Sudan",
	["se"] = "Sweden",
	["sg"] = "Singapore",
	["sh"] = "Saint Helena",
	["si"] = "Slovenia",
	["sj"] = "Svalbard and Jan Mayen Islands",
	["sk"] = "Slovak Republic",
	["sm"] = "San Marino",
	["sn"] = "Senegal",
	["so"] = "Somalia",
	["sr"] = "Suriname",
	["st"] = "Sao Tome and Principe",
	["sv"] = "El Salvador",
	["sy"] = "Syrian Arab Republic",
	["sz"] = "Swaziland",
	["tc"] = "Turks and Caicos Islands",
	["td"] = "Chad",
	["tf"] = "French Southern Territories",
	["tg"] = "Togo",
	["th"] = "Thailand",
	["tj"] = "Tajikistan",
	["tk"] = "Tokelau",
	["tl"] = "Timor-Leste",
	["tm"] = "Turkmenistan",
	["tn"] = "Tunisia",
	["to"] = "Tonga",
	["tp"] = "East Timor",
	["tr"] = "Turkey",
	["tt"] = "Trinidad and Tobago",
	["tv"] = "Tuvalu",
	["tw"] = "Taiwan",
	["tz"] = "Tanzania",
	["ua"] = "Ukraine",
	["ug"] = "Uganda",
	["uk"] = "United Kingdom",
	["um"] = "United States Minor Outlying Islands",
	["com"] = "Unknown",
	["net"] = "Unknown",
	["biz"] = "Unknown",
	["gov"] = "Unknown",
	["edu"] = "Unknown",
	["mil"] = "Unknown",
	["org"] = "Unknown",
	["web"] = "Unknown",
	["info"] = "Unknown",
	["museum"] = "Unknown",
	["us"] = "United States",
	["uy"] = "Uruguay",
	["uz"] = "Uzbekistan",
	["va"] = "Vatican City",
	["vc"] = "Saint Vincent and the Grenadines",
	["ve"] = "Venezuela",
	["vg"] = "Virgin Islands, British",
	["vi"] = "Virgin Islands, US",
	["vn"] = "Vietnam",
	["vu"] = "Vanuatu",
	["wf"] = "Wallis and Futuna Islands",
	["ws"] = "Western Samoa",
	["ye"] = "Yemen",
	["yt"] = "Mayotte",
	["yu"] = "Yugoslavia (Serbia and Montenegro)",
	["za"] = "South Africa",
	["zm"] = "Zambia",
	["zw"] = "Zimbabwe",
}

Alexinno

can someone convert this to lua 5 ?

Markitos

#2
Hi, what's wrong with the code?

Code Removed -// 3/7/2006

Dessamator

There's no IptoCountry function in ur script, it will probably show you a syntax error.
Ignorance is Bliss.

Mardeg

Quote from: Mutor on 01 July, 2006, 02:22:04
Unused vars...
bNonBlock = true
curSocket = nil
sInBuf = ""


I know this is a snippet, but seems they serve no purpose here.
Thanks for that Mutor. I hadn't checked if it would work without them. Infact I don't even know if WSA.init is required since the 5.1 example of WSA sockets has it commented out.
I'm not sure if a DNS lookup would freeze the hub till it gets a result without the bNonBlock = true, but I don't even know if that applies to lookups.

Mardeg

snippet updated, it was putting all .com .net etc people in the United States. Now for that and any unresolvable IP it will use the lacnic joint whois which appears to be linked to every other database.

SMF spam blocked by CleanTalk