Google Safe Browsing v2 library for Scala / Java
APACHE-2.0 License
This project implements the Google Safebrowsing API v2 and the Google Safebrowsing Lookup API in Scala. It also works with Yandex Safebrowsing API.
See Zscaler blog post for other implementations and notes
val apikey = "123456"
val dburl = "jdbc:mysql://localhost:3306/googlesafebrowsing2"
val tablePrefix = "gsb2_"
val storage = new MySQL(LiteDataSource.driverManager(dburl, "user", "pass"), tablePrefix)
val sb2 = new SafeBrowsing2(apikey, storage)
// uses Google database by default, you can use Yandex like this:
// val sb2 = new SafeBrowsing(apikey, storage, "yandex")
// update database
val forceUpdate = false
val useMac = false
val secondsToWaitBeforeNextUpdate = sb2.update("", forceUpdate, useMac)
// lookup url in database
val list = "" // lookup in all lists
val listMatch = sb2.lookup("http://ianfette.org", list, useMac)
listMatch match {
case Some(list) => println("Match found in list: " + list)
case None => println("No match found")
}
val resp = new Lookup(apikey).lookup(Array("http://www.google.com/", "http://ianfette.org/"))
resp.foreach(a => {
println(a._1 + " -> " + a._2)
})
Outputs:
http://www.google.com/ -> ok
http://ianfette.org/ -> malware
String apikey = "123456";
String url = "jdbc:mysql://localhost:3306/googlesafebrowsing2";
String tablePrefix = "gsb2_";
Storage storage = JavaHelper.buildStorageMySQL(url, "user", "password", tablePrefix);
SafeBrowsing2 sb2 = new SafeBrowsing2(apikey, storage);
// update the database
boolean forceUpdate = false;
boolean useMac = false;
String[] updateLists = null; // update all lists
int wait = sb2.update(updateLists, forceUpdate, useMac);
System.out.println("Seconds to wait before next update: " + wait);
// lookup url in database
String[] lookupLists = null; // lookup in all lists
String match = sb2.jlookup("http://ianfette.org", lookupLists, useMac);
if (match != null)
System.out.println("Match found in list: " + match);
Lookup lookup = new Lookup(apikey, "appname");
Map<String, String> r = lookup.jlookup(new String[]{"http://ianfette.org"}, 0);
for (String key : r.keySet()) {
System.out.println(key + " -> " + r.get(key));
}
The follow URLs are used to test Safe Browsing API:
The following databases are currently supported:
New Storage classes can be added by extending the net.google.safebrowsing2.db.DBI class or the net.google.safebrowsing2.db.Storage trait.