Skip to content

Commit 2a87d8e

Browse files
committed
Add ipinfo.io, improve tracing
1 parent f663233 commit 2a87d8e

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
v4.1.0
22
- Add black.box
33
- Fix issue with verb setting being ignored in some UserDefined ovpns
4+
- Improve detection of IP address by adding ipinfo.io
45

56
v4.0.9
67
- Fix XML

libs/common.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ def getIPInfo(addon):
142142

143143
if isAutoSelect(source):
144144
source = getAutoSource()
145-
146-
debugTrace("Getting IP info from " + source)
145+
147146
retry = 0
148147
bad_response = False
149148
while retry < 6:
@@ -181,12 +180,11 @@ def getIPInfo(addon):
181180
# Worked, exit loop
182181
break
183182
retry = retry + 1
184-
183+
185184
# Check to see if the call was good (after 5 retries)
186-
if ip == "no_info" or ip == "error":
185+
if ip == "no info" or ip == "error":
187186
return source, "no info", "unknown", "unknown"
188187

189-
190188
location = ""
191189
if not (region == "-" or region == "Not Available"): location = region
192190
if not (country == "-" or country == "Not Available"):

libs/ipinfo.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727

2828

29-
ip_sources = ["Auto select", "IP-API", "IPInfoDB", "freegeoip.net"]
30-
ip_urls = ["", "http://ip-api.com/json", "http://www.ipinfodb.com/my_ip_location.php", "http://freegeoip.net/json/"]
29+
ip_sources = ["Auto select", "ipinfo.io", "IP-API", "freegeoip.net"]
30+
ip_urls = ["", "http://ipinfo.io/json", "http://ip-api.com/json", "http://freegeoip.net/json/"]
3131
LIST_DEFAULT = "0,0,0"
3232

3333
MAX_ERROR = 64
@@ -38,7 +38,7 @@ def getIPInfoFrom(source):
3838
# No info generated from call is "no info", "unknown", "unknown", "unknown", url response
3939
# Or general error is "error", "error", "error", reason, url response
4040
link = ""
41-
try:
41+
try:
4242
# Determine the URL, make the call and read the response
4343
url = getIPSourceURL(source)
4444
if url == "": return "error", "error", "error", "unknown source", ""
@@ -47,23 +47,45 @@ def getIPInfoFrom(source):
4747
response = urllib2.urlopen(req)
4848
link = response.read()
4949
response.close()
50-
51-
# Call the right routine to parse the reply using regex.
52-
# If the website changes, this parsing can fail...sigh
53-
if source == "IPInfoDB": match = getIPInfoDB(link)
50+
except Exception as e:
51+
errorTrace("ipinfo.py", "Couldn't connect to IP provider " + source)
52+
errorTrace("ipinfo.py", str(e))
53+
recordError(source)
54+
return "error", "error", "error", "call failed", link
55+
56+
try:
57+
# This makes stupid regex easier
58+
link = link.replace("\n","")
59+
debugTrace("IP provider " + source + " returned " + link)
60+
# Call the right routine to parse the reply using regex
61+
# These all return JSON so probably a JSON parser should really be used
62+
if source == "ipinfo.io": match = getipinfo(link)
5463
if source == "IP-API": match = getIPAPI(link)
5564
if source == "freegeoip.net": match = getFreeGeoIP(link)
56-
if len(match) > 0:
65+
66+
if not match == None:
5767
recordWorking(source)
5868
for ip, country, region, city, isp in match:
5969
return ip, country, region, city, isp
6070
else:
71+
errorTrace("ipinfo.py", "No matches found for IP provider " + "source" + " " + link)
6172
recordError(source)
6273
return "no info", "unknown location", "unknown location", "no matches", link
63-
except:
74+
except Exception as e:
75+
errorTrace("ipinfo.py", "Couldn't parse response from IP provider " + source + " " + link)
76+
errorTrace("ipinfo.py", str(e))
6477
recordError(source)
65-
return "error", "error", "error", "call failed", link
78+
return "error", "error", "error", "parse failed", link
6679

80+
81+
def getipinfo(link):
82+
match = re.compile(ur'"ip": "(.*?)".*"city": "(.*?)".*"region": "(.*?)".*"country": "(.*?)".*"org": "(.*?)"').findall(link)
83+
if len(match) > 0:
84+
for ip, city, region, country, isp in match:
85+
return [(ip, country, region, city, isp)]
86+
else:
87+
return None
88+
6789

6890
def getIPAPI(link):
6991
match = re.compile(ur'"city":"(.*?)".*"country":"(.*?)".*"isp":"(.*?)".*"query":"(.*?)".*"regionName":"(.*?)"').findall(link)
@@ -74,15 +96,6 @@ def getIPAPI(link):
7496
return None
7597

7698

77-
def getIPInfoDB(link):
78-
match = re.compile(ur'<h5>Your IP address.*</h5>.*\s*.*<br>.*IP2Location.*\s*.*\s*<li>IP address.*<strong>(.+?)</strong>.*\s*\s*<li>Country : (.+?) <img.*\s*<li>State.*: (.+?)</li>.*\s*<li>City : (.+?)</li>').findall(link)
79-
if len(match) > 0:
80-
for ip, country, region, city in match:
81-
return [(ip, country, region, city, "Unknown")]
82-
else:
83-
return None
84-
85-
8699
def getFreeGeoIP(link):
87100
match = re.compile(ur'"ip":"(.*?)".*"country_name":"(.*?)".*"region_name":"(.*?).*"city":"(.*?)".*').findall(link)
88101
if len(match) > 0:

resources/language/English/strings.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,11 @@ msgid "Auto select"
587587
msgstr ""
588588

589589
msgctxt "#32173"
590-
msgid "IP-API"
590+
msgid "ipinfo.io"
591591
msgstr ""
592592

593593
msgctxt "#32174"
594-
msgid "IPInfoDB"
594+
msgid "IP-API"
595595
msgstr ""
596596

597597
msgctxt "#32175"

service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def checkConnections():
9191
for i in range (1, 11):
9292
next_conn = (addon.getSetting(str(i)+"_vpn_validated"))
9393
if not next_conn == "" and not xbmcvfs.exists(next_conn):
94+
errorTrace("service.py", "Checking connections and couldn't find connection " + str(i) + ", " + next_conn)
9495
return False
9596
return True
9697

@@ -215,9 +216,10 @@ def onSettingsChanged( self ):
215216

216217
if not primary_path == "" and not xbmcvfs.exists(primary_path):
217218
vpn_provider = getVPNLocation(addon.getSetting("vpn_provider_validated"))
218-
infoTrace("service.py", "New install, but was using good VPN previously (" + vpn_provider + "). Regenerate OVPNs")
219+
infoTrace("service.py", "New install, but was using good VPN previously (" + vpn_provider + ", " + primary_path + "). Regenerate OVPNs")
219220
populateSupportingFromGit(vpn_provider)
220221
if not fixOVPNFiles(vpn_provider, addon.getSetting("vpn_locations_list")) or not checkConnections():
222+
errorTrace("service.py", "VPN connection is not available for " + vpn_provider + " with list " + addon.getSetting("vpn_locations_list") + ", need to revalidate")
221223
xbmcgui.Dialog().ok(addon_name, "One of the VPN connections you were using previously is no longer available. Please re-validate all connections.")
222224
removeGeneratedFiles()
223225
resetVPNConfig(addon, 1)

0 commit comments

Comments
 (0)