Such a process, called geo-blocking, is quite common over the Internet. Geo-blocking is only made possible by geolocation, which is the process of determining your visitors' physical location from their IP addresses.
As an overview, geolocation is only possible because blocks of IP addresses are assigned to countries in a segmented way. The distribution of those blocks is referenced in publicly accessible databases. The geolocation process is quite simple in appearance, as it's only a matter of comparing your visitors' IP addresses with those segments to determine which country your visitor is located in.
Developing a geolocalization service by yourself could be tempting. Still, it would require significant resources. You will first need to obtain the IP-to-countries allocation databases from all the different responsible organizations and set up a high-performance service to query those databases in real-time.
An economical solution in the long term is to use an external service, called an API, which can provide real-time response and precise geolocation of an IP address. APIs can be queried by a simple HTTP request and responds in JSON format. You will get the desired information in a few milliseconds without the need for an advanced implementation.
Most Internet users know that there are ways to circumvent geolocation systems, and therefore bypass geo-blocking, by accessing the Internet through a proxy or a VPN service. Indeed a VPN allows a user to appear to web servers as if he were connected from a different geographical location.
Depending on your geolocation policy, you may want to implement a mechanism to detect if your visitor modified his apparent geographical location using a VPN service so that you can act accordingly with such users. Once again, you could develop a service by yourself by getting a list of all publicly known IP addresses of VPN services and comparing your visitor's IP address to this list at run-time. But the same problem arises since the list of VPN IP addresses is huge and is constantly evolving. Hence it would require significant resources in terms of development and infrastructure.
The most economical solution is to use an API that specialized in providing information about an IP address, specifically its physical location, and can detect if it belongs to a network of VPNs.
Abstract provides such service, which has the advantages to be free, extremely fast, and that you can set-up in a matter of minutes. It allows you to detect in which geographical area your visitor is located and tell you if he is currently connected to the Internet through a VPN. To set up an effective geo-blocking system, it is advisable to use a reliable API such as the one provided by Abstract.