├── .gitattributes ├── .github └── workflows │ └── publish.yml ├── .gitignore ├── README.md ├── pom.xml └── src └── main └── java └── net └── vpnblocker └── api ├── Response.java └── VPNDetection.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish package to GitHub Packages 2 | on: 3 | release: 4 | types: [created] 5 | jobs: 6 | publish: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v2 10 | - uses: actions/setup-java@v1 11 | with: 12 | java-version: 16 13 | - name: Publish package 14 | run: mvn -B deploy 15 | env: 16 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | # Intellij 4 | .idea/ 5 | *.iml 6 | *.iws 7 | 8 | # Maven 9 | target/ 10 | pom.xml.tag 11 | pom.xml.releaseBackup 12 | pom.xml.versionsBackup 13 | pom.xml.next 14 | release.properties 15 | dependency-reduced-pom.xml 16 | buildNumber.properties 17 | .mvn/timing.properties 18 | # https://github.com/takari/maven-wrapper#usage-without-binary-jar 19 | .mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java VPN Detection 2 | 3 | Allows you to detect whether or not a specified IPv4 Address belongs to a hosting or vpn / proxy organization. 4 | This library facilitates and simplifies the usage of the [VPN Blocker Web API](https://vpnblocker.net) - https://vpnblocker.net, and allows you to easily implement the functionality in your java applications. 5 | 6 | 7 | ## Usage 8 | 9 | First things, first.. remember to import the library: 10 | 11 | ```java 12 | import net.vpnblocker.api.*; 13 | ``` 14 | 15 | 16 | A very basic usage example: 17 | 18 | ```java 19 | String ipToLookup = "192.184.93.53"; 20 | Boolean isHostingorVPN = new VPNDetection().getResponse(ipToLookup).hostip; 21 | System.out.println(isHostingorVPN); 22 | ``` 23 | 24 | A more advanced example: 25 | 26 | ```java 27 | VPNDetection vpn_detection = new VPNDetection(); 28 | new Thread(() -> { 29 | try { 30 | String ipToLookup = "192.184.93.53"; 31 | Response api_response = vpn_detection.getResponse(ipToLookup); 32 | 33 | if(api_response.status.equals("success")) { 34 | System.out.println("Package: " + api_response.getPackage); 35 | if(api_response.getPackage.equals("Free")) { 36 | System.out.println("Remaining Requests: " + api_response.remaining_requests); 37 | } 38 | System.out.println("IP Address: " + api_response.ipaddress); 39 | System.out.println("Is this IP a VPN or Hosting Network? " + api_response.hostip); 40 | System.out.println("Organisation: " + api_response.org); 41 | if(api_response.country != null) { 42 | System.out.println("Country: " + api_response.country.name); 43 | } 44 | 45 | } else { 46 | System.out.println("Error: " + api_response.msg); 47 | } 48 | 49 | } catch (IOException ex) { 50 | System.out.println("Error: " + ex.getMessage()); 51 | } 52 | }).start(); 53 | ``` 54 | 55 | ## Jar Download 56 | * [View Packages](https://github.com/faiqsohail/Java-VPNDetection/packages) 57 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | net.vpnblocker.api 8 | detection 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 17 13 | 17 14 | 15 | 16 | 17 | 18 | com.google.code.gson 19 | gson 20 | 2.8.9 21 | 22 | 23 | 24 | 25 | 26 | github 27 | GitHub faiqsohail Apache Maven Packages 28 | https://maven.pkg.github.com/faiqsohail/Java-VPNDetection 29 | 30 | 31 | 32 | 33 | 34 | 35 | maven-assembly-plugin 36 | 37 | 38 | 39 | net.vpnblocker.api.VPNDetection 40 | 41 | 42 | 43 | jar-with-dependencies 44 | 45 | 46 | 47 | 48 | make-assembly 49 | package 50 | 51 | single 52 | 53 | 54 | 55 | 56 | 57 | org.apache.maven.plugins 58 | maven-compiler-plugin 59 | 60 | 16 61 | 16 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /src/main/java/net/vpnblocker/api/Response.java: -------------------------------------------------------------------------------- 1 | package net.vpnblocker.api; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | /* 6 | * 7 | * @author HiddenMotives 8 | */ 9 | 10 | public class Response { 11 | public String status; 12 | public String msg; 13 | 14 | @SerializedName("package") 15 | public String getPackage; 16 | 17 | public String remaining_requests; 18 | public String ipaddress; 19 | 20 | @SerializedName("host-ip") 21 | public boolean hostip; 22 | 23 | public String hostname; 24 | public String org; 25 | 26 | public CS country; 27 | public CS subdivision; 28 | 29 | public String city; 30 | public String postal; 31 | 32 | public latlon location; 33 | 34 | public class CS { 35 | public String name; 36 | public String code; 37 | } 38 | 39 | public class latlon { 40 | public double lat; 41 | 42 | @SerializedName("long") 43 | public double lon; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/net/vpnblocker/api/VPNDetection.java: -------------------------------------------------------------------------------- 1 | package net.vpnblocker.api; 2 | 3 | import com.google.gson.Gson; 4 | import java.io.BufferedReader; 5 | import java.io.IOException; 6 | import java.io.InputStreamReader; 7 | import java.net.MalformedURLException; 8 | import java.net.URL; 9 | import java.net.URLConnection; 10 | 11 | /** 12 | * Allows you to detect whether or not a specified IPv4 Address belongs to a 13 | * hosting or vpn / proxy organization. 14 | * 15 | * This class facilitates and simplifies using the web API, and allows you to 16 | * easily implement the functionality in your applications. 17 | * 18 | * API Homepage: https://vpnblocker.net 19 | * 20 | * 21 | * @author HiddenMotives 22 | */ 23 | public final class VPNDetection { 24 | 25 | private String api_key; 26 | private String api_url = "http://api.vpnblocker.net/v2/json/"; 27 | private int api_timeout = 5000; 28 | 29 | public VPNDetection() { 30 | this.api_key = null; 31 | } 32 | 33 | public VPNDetection(String key) { 34 | this.api_key = key; 35 | } 36 | 37 | public VPNDetection(String key, int timeout) { 38 | this.api_key = key; 39 | this.api_timeout = timeout; 40 | } 41 | 42 | /** 43 | * You can obtain a API key from: https://vpnblocker.net 44 | * (optional) 45 | * 46 | * @param key 47 | */ 48 | public void set_api_key(String key) { 49 | this.api_key = key; 50 | } 51 | 52 | /** 53 | * Units are in milliseconds Allows you to set the timeout of the API 54 | * web request 55 | * 56 | * @param timeout 57 | */ 58 | public void set_api_timeout(int timeout) { 59 | this.api_timeout = timeout; 60 | } 61 | 62 | /** 63 | * Allows you to use SSL on the API Query, you must have the appropriate 64 | * package from the API provider in order to use this feature. 65 | */ 66 | public void useSSL() { 67 | this.api_url = this.api_url.replace("http://", "https://"); 68 | } 69 | 70 | /** 71 | * Queries the API server, gets the JSON result and parses using Gson. 72 | * 73 | * @param ip 74 | * @return 75 | * @throws IOException 76 | */ 77 | public Response getResponse(String ip) throws IOException { 78 | String query_url = this.get_query_url(ip); 79 | String query_result = this.query(query_url, this.api_timeout, "Java-VPNDetection Library"); 80 | return new Gson().fromJson(query_result, Response.class); 81 | } 82 | 83 | /** 84 | * The Generated API Query URL 85 | * 86 | * @param ip 87 | * @return 88 | */ 89 | public String get_query_url(String ip) { 90 | return (this.api_key == null) ? this.api_url + ip : this.api_url + ip + "/" + this.api_key; 91 | } 92 | 93 | /** 94 | * Function that reads and returns the contents of a URL. Using the 95 | * specified user agent and timeout when making the URL request. 96 | * 97 | * @param url 98 | * @param timeout 99 | * @param userAgent 100 | * @return 101 | * @throws MalformedURLException 102 | * @throws IOException 103 | */ 104 | public String query(String url, int timeout, String userAgent) 105 | throws MalformedURLException, IOException { 106 | StringBuilder response = new StringBuilder(); 107 | URL website = new URL(url); 108 | URLConnection connection = website.openConnection(); 109 | connection.setConnectTimeout(timeout); 110 | connection.setRequestProperty("User-Agent", userAgent); 111 | try (BufferedReader in = new BufferedReader( 112 | new InputStreamReader( 113 | connection.getInputStream()))) { 114 | while ((url = in.readLine()) != null) { 115 | response.append(url); 116 | } 117 | } 118 | 119 | return response.toString(); 120 | } 121 | } 122 | --------------------------------------------------------------------------------