1 module ipfinder; 2 3 /+ 4 * Authors: Mohamed Ben rebia, mohamed@ipinfo.io 5 * Home page: https://ipfinder.io/ 6 * Version: 1.0.0 7 * Copyright: Apache-2.0 8 +/ 9 10 import std.stdio; 11 import std.net.curl; 12 import std.json; 13 import std.string; 14 import std.array : split; 15 import std.uri : encode; 16 import std.exception; 17 import std.conv; 18 import Validation.Asnvalidation; 19 import Validation.Domainvalidation; 20 import Validation.Firewallvalidation; 21 import Validation.Ipvalidation; 22 import Validation.Tokenvalidation; 23 import info; 24 //import Exception.IPfinderException; 25 26 /+ 27 * Ipfinder 28 +/ 29 class Ipfinder 30 { 31 /+ 32 DEFAULT BASE URL 33 +/ 34 const string DEFAULT_BASE_URL = "https://api.ipfinder.io/v1/"; // or add proxy pass with your domain 35 36 /+ 37 The free token 38 +/ 39 40 const string DEFAULT_API_TOKEN = "free"; // limited to 4,000 requests a day 41 42 /+ 43 DEFAULT FORMAT 44 +/ 45 const string FORMAT = "json"; 46 47 /+ 48 service status path 49 +/ 50 const string STATUS_PATH = "info"; 51 52 /+ 53 IP Address Ranges path 54 +/ 55 const string RANGES_PATH = "ranges/"; 56 57 /+ 58 Firewall path 59 +/ 60 const string FIREWALL_PATH = "firewall/"; 61 62 /+ 63 Domain IP path 64 +/ 65 const string DOMAIN_PATH = "domain/"; 66 67 /+ 68 Domain IP history path 69 +/ 70 const string DOMAIN_H_PATH = "domainhistory/"; 71 72 /+ 73 Domain By ASN, Country,Ranges path 74 +/ 75 const string DOMAIN_BY_PATH = "domainby/"; 76 77 public string token; 78 79 public string baseUrl; 80 81 public string format; 82 83 public string endpoint; 84 85 public this(string token = null, string baseUrl = null) 86 { 87 if (token == null) 88 { 89 this.token = DEFAULT_API_TOKEN; 90 } 91 else 92 { 93 Tokenvalidation.validate(token); 94 this.token = token; 95 } 96 if (baseUrl == null) 97 { 98 this.baseUrl = DEFAULT_BASE_URL; 99 } 100 else 101 { 102 this.baseUrl = baseUrl; 103 } 104 } 105 106 /+ 107 108 +/ 109 public JSONValue call(string path = null, string format = null) 110 { 111 112 if (format == null) 113 { 114 this.format = FORMAT; 115 } 116 else 117 { 118 this.format = format; 119 } 120 121 auto http = HTTP(); 122 http.addRequestHeader("Content-Type", "application/json"); 123 http.addRequestHeader("User-Agent", "IPfinder dlang-client"); 124 125 this.endpoint = join([this.baseUrl, path]); 126 127 JSONValue data = ["token" : this.token, "foramt" : this.format]; 128 129 auto content = post(this.endpoint, text(data), http); 130 131 auto status = http.statusLine().code; 132 133 if (status != 200) 134 { 135 throw new Exception(""); 136 } 137 else 138 { 139 return parseJSON(content); 140 } 141 142 143 } 144 145 /+ 146 147 +/ 148 public JSONValue Authentication() 149 { 150 return call(); 151 } 152 153 /+ 154 155 +/ 156 public JSONValue getAddressInfo(string path) 157 { 158 Ipvalidation.validate(path); 159 return call(path); 160 } 161 162 /+ 163 164 +/ 165 public JSONValue getAsn(string path) 166 { 167 Asnvalidation.validate(path); 168 return call(path); 169 } 170 171 /+ 172 173 +/ 174 public JSONValue getStatus() 175 { 176 return call(STATUS_PATH); 177 } 178 179 /+ 180 181 +/ 182 public JSONValue getRanges(string path) 183 { 184 return call(join([RANGES_PATH, path.encode])); 185 } 186 187 /+ 188 189 +/ 190 public JSONValue getFirewall(string path, string formats) 191 { 192 Firewallvalidation.validate(path, formats); 193 return call(join([FIREWALL_PATH, path]), formats); 194 } 195 196 /+ 197 198 +/ 199 public JSONValue getDomain(string path) 200 { 201 Domainvalidation.validate(path); 202 return call(join([DOMAIN_PATH, path])); 203 } 204 205 /+ 206 207 +/ 208 public JSONValue getDomainHistory(string path) 209 { 210 Domainvalidation.validate(path); 211 return call(join([DOMAIN_H_PATH, path])); 212 } 213 214 /+ 215 216 +/ 217 public JSONValue getDomainBy(string path) 218 { 219 return call(join([DOMAIN_BY_PATH, path])); 220 } 221 }