svaningelgem / location_ipfire_db_reader

MIT License
0 stars 0 forks source link

failure with message: `Can't find asn object with id 18734` #4

Closed svaningelgem closed 9 months ago

svaningelgem commented 9 months ago
          > _edit_: I uploaded the new version (1.0.5). So you should be able to get it directly from PyPi now.

it works as expected with some IPs!!!

PS D:\Github\location_ipfire_db_reader\location_ipfire_db_reader> python.exe .\ip_location_script.py 1.32.251.111

    IP: 1.32.251.111
    Subnet Mask: 24
    Network Address: 1.32.251.0
    IP with CIDR: 1.32.251.0/24

    ASN: 64050
    ASN Name: BGP Consultancy Pte Ltd
    Country Code: AP
    Country Name: Asia/Pacific
    Country Continent: AS

    Is Anonymous Proxy: False
    Is Satellite Provider: False
    Is Anycast: False
    Is Drop: False

PS D:\Github\location_ipfire_db_reader\location_ipfire_db_reader> python.exe .\ip_location_script.py 113.74.8.78

    IP: 113.74.8.78
    Subnet Mask: 11
    Network Address: 113.64.0.0
    IP with CIDR: 113.64.0.0/11

    ASN: 4134
    ASN Name: Chinanet Backbone
    Country Code: CN
    Country Name: China
    Country Continent: AS

    Is Anonymous Proxy: False
    Is Satellite Provider: False
    Is Anycast: False
    Is Drop: False

PS D:\Github\location_ipfire_db_reader\location_ipfire_db_reader> python.exe .\ip_location_script.py 212.107.28.52

    IP: 212.107.28.52
    Subnet Mask: 24
    Network Address: 212.107.28.0
    IP with CIDR: 212.107.28.0/24

    ASN: 41378
    ASN Name: Kirino LLC
    Country Code: AP
    Country Name: Asia/Pacific
    Country Continent: AS

    Is Anonymous Proxy: False
    Is Satellite Provider: False
    Is Anycast: False
    Is Drop: False

But I'm not sure what happened with 201.148.95.249

http://ip-api.com/csv/201.148.95.249

success,Mexico,MX,CMX,Mexico City,Mexico City,01010,19.3531,-99.2091,America/Mexico_City,"Operbes, S.A. de C.V.","Operbes, S.A. de C.V","AS18734 Operbes, S.A. de C.V.",201.148.95.249
PS D:\Github\location_ipfire_db_reader\location_ipfire_db_reader> python.exe .\ip_location_script.py 201.148.95.249
Traceback (most recent call last):
  File "D:\Github\location_ipfire_db_reader\location_ipfire_db_reader\ip_location_script.py", line 31, in <module>
    main(ip_address)
  File "D:\Github\location_ipfire_db_reader\location_ipfire_db_reader\ip_location_script.py", line 14, in main
    ASN Name: {ip_info.asn_name}
               ^^^^^^^^^^^^^^^^
  File "C:\Users\nucrpl\AppData\Local\Programs\Python\Python312\Lib\functools.py", line 995, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nucrpl\AppData\Local\Programs\Python\Python312\Lib\site-packages\location_ipfire_db_reader\ip_information.py", line 95, in asn_name
    obj = self._find_object(
          ^^^^^^^^^^^^^^^^^^
  File "C:\Users\nucrpl\AppData\Local\Programs\Python\Python312\Lib\site-packages\location_ipfire_db_reader\ip_information.py", line 84, in _find_object
    raise ValueError(f"Can't find asn object with id {self.asn}!")
ValueError: Can't find asn object with id 18734!

_Originally posted by @MaurUppi in https://github.com/svaningelgem/location_ipfire_db_reader/issues/1#issuecomment-1903640126_

svaningelgem commented 9 months ago

Thanks for the report @MaurUppi! I've never had a better bughunter than you ;-).

I'll look into it and see why this behaves that way.

MaurUppi commented 9 months ago

Thanks for the report @MaurUppi! I've never had a better bughunter than you ;-).

I'll look into it and see why this behaves that way.

I'm sharing one more example with you. This case differs from others cause this IP should be in a reserved IP range.

100.64.0.0-100.127.255.255, DBIP_ZZ, IP2LOCATION_-, IPInfo_ Let me explain to you.

The IP2Location will display the "-" in country field when the IP address range is still unallocated to any countries. It is also known as reserved IP address range.

For this case, I think you can output No such record or You're checking reserved IP address

127.0.0.0-127.255.255.255, DBIP_ZZ, IP2LOCATION_-, IPInfo_
224.0.0.0-255.255.255.255, DBIP_ZZ, IP2LOCATION_-, IPInfo_
100.64.0.0-100.127.255.255, DBIP_ZZ, IP2LOCATION_-, IPInfo_
172.16.0.0-172.31.255.255, DBIP_ZZ, IP2LOCATION_-, IPInfo_
0.0.0.0-0.255.255.255, DBIP_ZZ, IP2LOCATION_-, IPInfo_
192.88.99.0-192.88.99.255, DBIP_ZZ, IP2LOCATION_-, IPInfo_
192.168.0.0-192.168.255.255, DBIP_ZZ, IP2LOCATION_-, IPInfo_
169.254.0.0-169.254.255.255, DBIP_ZZ, IP2LOCATION_-, IPInfo_
10.0.0.0-10.255.255.255, DBIP_ZZ, IP2LOCATION_-, IPInfo_
198.18.0.0-198.19.255.255, DBIP_ZZ, IP2LOCATION_-, IPInfo_
PS D:\Github\location_ipfire_db_reader\location_ipfire_db_reader> python.exe .\ip_location_script.py 100.127.255.25
Traceback (most recent call last):
  File "D:\Github\location_ipfire_db_reader\location_ipfire_db_reader\ip_location_script.py", line 31, in <module>
    main(ip_address)
  File "D:\Github\location_ipfire_db_reader\location_ipfire_db_reader\ip_location_script.py", line 6, in main
    ip_info = db[ip_address]
              ~~^^^^^^^^^^^^
  File "C:\Users\nucrpl\AppData\Local\Programs\Python\Python312\Lib\site-packages\location_ipfire_db_reader\database.py", line 14, in __getitem__
    return IpInformation(self, ip, *self._find_network_information(ip))
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nucrpl\AppData\Local\Programs\Python\Python312\Lib\site-packages\location_ipfire_db_reader\database_reader.py", line 132, in _find_network_information
    raise ValueError("Cannot find anything anymore!")
ValueError: Cannot find anything anymore!
svaningelgem commented 9 months ago

Thanks, I implemented some better exceptions in these cases. And adjusted the README in this way:

Exceptions

All exceptions within this package will inherit from LocationIPFireDBReaderException. So if you want a blanket-capture-all. That's what you'll need.

However, these are more fine-tuned versions:

  • UnknownASNName: will be raised when an ASN is found, but there is no known name for it.
  • IPAddressError: will be raised when an IP lookup fails. This happens with reserved IPs so far.

So these will be thrown instead of the ValueErrors before.

I am doubting a little bit if I should just return an empty value instead of an exception though?

Comparison

MaurUppi commented 9 months ago

I think the below two are similar to 201.148.95.249 case.

http://ip-api.com/csv/88.218.67.25 success,United Kingdom,GB,ENG,England,London,W1B,51.5074,-0.127758,Europe/London,TrafficTransitSolution LLC,TrafficTransitSolution LLC,,88.218.67.25

PS D:\Github\location_ipfire_db_reader\location_ipfire_db_reader> python.exe .\ip_location_script.py 88.218.67.25
Traceback (most recent call last):
  File "D:\Github\location_ipfire_db_reader\location_ipfire_db_reader\ip_location_script.py", line 31, in <module>
    main(ip_address)
  File "D:\Github\location_ipfire_db_reader\location_ipfire_db_reader\ip_location_script.py", line 14, in main
    ASN Name: {ip_info.asn_name}
               ^^^^^^^^^^^^^^^^
  File "C:\Users\nucrpl\AppData\Local\Programs\Python\Python312\Lib\functools.py", line 995, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nucrpl\AppData\Local\Programs\Python\Python312\Lib\site-packages\location_ipfire_db_reader\ip_information.py", line 95, in asn_name
    obj = self._find_object(
          ^^^^^^^^^^^^^^^^^^
  File "C:\Users\nucrpl\AppData\Local\Programs\Python\Python312\Lib\site-packages\location_ipfire_db_reader\ip_information.py", line 84, in _find_object
    raise ValueError(f"Can't find asn object with id {self.asn}!")
ValueError: Can't find asn object with id 0!

http://ip-api.com/csv/202.37.126.25 success,New Zealand,NZ,HKB,Hawke's Bay,Napier City,4143,-39.5109,176.876,Pacific/Auckland,RUAWHARO,,,202.37.126.25

PS D:\Github\location_ipfire_db_reader\location_ipfire_db_reader> python.exe .\ip_location_script.py 202.37.126.25
Traceback (most recent call last):
  File "D:\Github\location_ipfire_db_reader\location_ipfire_db_reader\ip_location_script.py", line 31, in <module>
    main(ip_address)
  File "D:\Github\location_ipfire_db_reader\location_ipfire_db_reader\ip_location_script.py", line 14, in main
    ASN Name: {ip_info.asn_name}
               ^^^^^^^^^^^^^^^^
  File "C:\Users\nucrpl\AppData\Local\Programs\Python\Python312\Lib\functools.py", line 995, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nucrpl\AppData\Local\Programs\Python\Python312\Lib\site-packages\location_ipfire_db_reader\ip_information.py", line 95, in asn_name
    obj = self._find_object(
          ^^^^^^^^^^^^^^^^^^
  File "C:\Users\nucrpl\AppData\Local\Programs\Python\Python312\Lib\site-packages\location_ipfire_db_reader\ip_information.py", line 84, in _find_object
    raise ValueError(f"Can't find asn object with id {self.asn}!")
ValueError: Can't find asn object with id 0!
svaningelgem commented 9 months ago

It looks like ipfire hasn't all the information about these names (yet). I will submit a bug report to them with the ones they are missing and hopefully it'll be incorporated soon(ish).

svaningelgem commented 9 months ago

Please find the bug report here: Missing AS names

Now on to the "asn object id 0" part of your report: https://ipinfo.io/202.37.126.25 <-- here, no asn is known. Same for the other one. No as number is known.

What should I do here? Just return an empty value (0 for the number and "" for the name)? Or simply raise the exception?

MaurUppi commented 9 months ago

Please find the bug report here: Missing AS names

Now on to the "asn object id 0" part of your report: https://ipinfo.io/202.37.126.25 <-- here, no asn is known. Same for the other one. No as number is known.

What should I do here? Just return an empty value (0 for the number and "" for the name)? Or simply raise the exception?

My idea is raise the executions OR simply output Empty msg.

such as:

ip: 8.8.8.8
subnet_mask: 24
network_address: 8.8.8.0
ip_with_cidr: 8.8.8.0/24

asn: 
asn_name: 
country_code: US
country_name: United States of America
country_continent: NA

is_anonymous_proxy: False
is_satellite_provider: False
is_anycast: True
is_drop: False
svaningelgem commented 9 months ago

Thanks for your valuable input. I adjusted the sources now to have the ability to allow for no exceptions raised

What if you don't want exceptions?

If you don't like to handle exceptions, you can always initialize your LocationDatabase like this:

db = LocationDatabase(raise_exceptions=False)

Now if for example the AS isn't known, it will output 0 for the asn, and "" for the AS name. Instead of raising an exception.

This is not yet in a release (PR #5 pending for that). But the effect will be somewhat like you mentioned. However, for 8.8.8.8, the asn is 15169, and the asn will always return a number (as it stands for "autonomous system number").