-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for IP2Proxy Web Service
- Loading branch information
1 parent
cf032ad
commit ad4a1e7
Showing
4 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import std.uri; | ||
import std.regex; | ||
import std.stdio; | ||
import std.net.curl; | ||
import std.json; | ||
|
||
class ip2proxywebservice { | ||
private string _apikey = ""; | ||
private string _apipackage = ""; | ||
private bool _usessl = true; | ||
private const string baseurl = "api.ip2proxy.com/"; | ||
|
||
// constructor | ||
public this() { | ||
} | ||
|
||
// initialize with API key and package | ||
public void open(const string apikey, const string apipackage, bool usessl = true) { | ||
_apikey = apikey; | ||
_apipackage = apipackage; | ||
_usessl = usessl; | ||
checkparams(); | ||
} | ||
|
||
// check params | ||
public void checkparams() { | ||
auto apikeypattern = ctRegex!(`^[\dA-Z]{10}$`); | ||
auto apipackagepattern = ctRegex!(`^PX\d+$`); | ||
|
||
if (!matchFirst(_apikey, apikeypattern) && _apikey != "demo") { | ||
throw new Exception("Invalid API key."); | ||
} | ||
if (!matchFirst(_apipackage, apipackagepattern)) { | ||
throw new Exception("Invalid package name."); | ||
} | ||
} | ||
|
||
// query web service for proxy information | ||
public auto lookup(const string ipaddress) { | ||
checkparams(); // check here in case user haven't called open yet | ||
auto protocol = (_usessl) ? "https" : "http"; | ||
auto url = protocol ~ "://" ~ baseurl ~ "?key=" ~ _apikey ~ "&package=" ~ _apipackage ~ "&ip=" ~ ipaddress.encode; | ||
auto content = get(url); | ||
JSONValue j = parseJSON(content); | ||
return j; | ||
} | ||
|
||
// check web service credit balance | ||
public auto get_credit() { | ||
checkparams(); // check here in case user haven't called open yet | ||
auto protocol = (_usessl) ? "https" : "http"; | ||
auto url = protocol ~ "://" ~ baseurl ~ "?key=" ~ _apikey ~ "&check=true"; | ||
auto content = get(url); | ||
JSONValue j = parseJSON(content); | ||
return j; | ||
} | ||
} |