Country is normally determined by doing a reverse DNS lookup, and then looking at the country code in the domain name. It's not very accurate, but you can do it for free without relying on any third party service or database.
Here is the libtorrent code that does it for every peer:
Here is the libtorrent code that does it for every peer:
https://github.com/arvidn/libtorrent/blob/8786d17e59c90aee2c...