Giter VIP home page Giter VIP logo

go-safe-browsing-api's Introduction

Google Safe Browsing API

Build Status Coverage Status

This library provides client functionality for version 2 of the Google safe browsing API as per: https://developers.google.com/safe-browsing/developers_guide_v2

Installation

First you'll need hat-trie: https://github.com/dcjones/hat-trie

Once installed, this should do the trick:

go get github.com/rjohnsondev/go-safe-browsing-api

Usage

The library requires at least your Safe Browsing API key and a writable directory to store the list data.

It it recommended you also set the Client and AppVersion globals to something appropriate:

safebrowsing.Client := "api"
safebrowsing.AppVersion := "1.0"

Calling NewSafeBrowsing immediately attempts to contact the google servers and perform an update/inital download. If this succeeds, it returns a SafeBrowsing instance after spawning a new goroutine which will update itself at the interval requested by google.

package main

import (
	safebrowsing "github.com/rjohnsondev/go-safe-browsing-api"
    log          "github.com/rjohnsondev/log4go-raven"
    "os"
)

func main() {
    key := "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA_BBBBBBBBB"
    dataDir := "/var/lib/safebrowsing/"
	ss, err = safebrowsing.NewSafeBrowsing(key, dataDir)
	if err != nil {
		log.Error(err)
        os.Exit(1)
	}
}

Looking up a URL

There are two methods for looking up URLs, IsListed and MightBeListed. Both of these return either an empty string in the case of an unlisted URL, or the name of the list on which the URL is listed. If there was an error requesting confirmation from Google for a listed URL, or if the last update request was over 45 mins ago, it will be returned along with an empty string.

IsListed(string) is the recommended method to use if displaying a message to a user. It may however make a blocking request to Google's servers for pages that have partial hash matches to perform a full hash match (if it has not already done so for that URL) which can be slow.

response, err := sb.IsListed(url)
if err != nil {
    fmt.Printf("Error quering URL: %s", err)
}
if response == "" {
    fmt.Printf("not listed")
} else {
    fmt.Printf("URL listed on: %s", response)
}

If a quick return time is required, it may be worth using the MightBeListed(string) method. This will not contact Google for confirmation, so it can only be used to display a message to the user if the fullHashMatch return value is True AND the last successful update from Google was in the last 45 mins:

response, fullHashMatch, err := sb.MightBeListed(url)
if err != nil {
    fmt.Printf("Error quering URL: %s", err)
}
if response == "" {
    fmt.Printf("not listed")
} else {
    if fullHashMatch && sb.IsUpToDate() {
        fmt.Printf("URL listed on: %s", response)
    } else {
        fmt.Printf("URL may be listed on: %s", response)
    }
}

It is recommended you combine the two calls when a non-blocking response is required, so a full hash can be requested and used for future queries about the same url:

response, fullHashMatch, err := sb.MightBeListed(url)
if err != nil {
    fmt.Printf("Error quering URL: %s", err)
}
if response != "" {
    if fullHashMatch && sb.IsUpToDate() {
        fmt.Printf("URL listed on: %s", response)
    } else {
        fmt.Printf("URL may be listed on: %s", response)
        // Requesting full hash in background...
        go ss.IsListed(url)
    }
}

Logging Injection

The library includes a safebrowsing.logger interface which can be used to attach logging facilities to the library. The interface matches the log4go Logger, so you can drop that in pretty easily:

package main

import (
	safebrowsing "github.com/rjohnsondev/go-safe-browsing-api"
    log          "github.com/rjohnsondev/log4go-raven"
)

func main() {
    safebrowsing.Logger = log.NewDefaultLogger(log.DEBUG)
}

Offline Mode

The library can work in "offline" mode, where it will not attempt to contact Google's servers and work purely from local files. This can be activated by setting the OfflineMode global variable:

package main

import (
	safebrowsing "github.com/rjohnsondev/go-safe-browsing-api"
)

func main() {
    key := "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA_BBBBBBBBB"
    dataDir := "/var/lib/safebrowsing/"

    // only work from local files.
	safebrowsing.OfflineMode = true

	ss, err = safebrowsing.NewSafeBrowsing(key, dataDir)
	...
}

In this mode IsListed will always return an error complaining that the list has not been updated within the last 45 mins and no warnings may be shown to users.

Other Notes

Memory Usage

The current implementation stores hashes in a reasonably effecient hat-trie data structure. This results in a memory footprint of approximately 35MB.

File Format

The files stored by the library are gob streams of Chunks. They should be portable between identical versions of the library.

go-safe-browsing-api's People

Contributors

apokalyptik avatar rjohnsondev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.