Skip to content

Commit

Permalink
Added proxy support (#76)
Browse files Browse the repository at this point in the history
* Added proxy support

A proxy can now be used by using the -proxy parameter in both sblookup and sbserver. This overrides the default use of the environment variable HTTP_PROXY (or http_proxy). Please refer to https://golang.org/pkg/net/http/#RoundTripper for more info

* Removed explicit default proxy value

Setting the default proxy value to HTTP_PROXY induced confusion, and non-conformity with the previous behavior of the library.
HTTP_PROXY and NO_PROXY are still used though, through "net/http" and "Transport".
  • Loading branch information
3kt authored and colonelxc committed Nov 28, 2017
1 parent b349c85 commit fe6951d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
15 changes: 13 additions & 2 deletions api.go
Expand Up @@ -47,8 +47,9 @@ type netAPI struct {

// newNetAPI creates a new netAPI object pointed at the provided root URL.
// For every request, it will use the provided API key.
// If a proxy URL is given, it will be used in place of the default $HTTP_PROXY.
// If the protocol is not specified in root, then this defaults to using HTTPS.
func newNetAPI(root string, key string) (*netAPI, error) {
func newNetAPI(root string, key string, proxy string) (*netAPI, error) {
if !strings.Contains(root, "://") {
root = "https://" + root
}
Expand All @@ -57,11 +58,21 @@ func newNetAPI(root string, key string) (*netAPI, error) {
return nil, err
}

httpClient := &http.Client{}

if proxy != "" {
proxyUrl, err := url.Parse(proxy)
if err != nil {
return nil, err
}
httpClient = &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
}

q := u.Query()
q.Set("key", key)
q.Set("alt", "proto")
u.RawQuery = q.Encode()
return &netAPI{url: u, client: &http.Client{}}, nil
return &netAPI{url: u, client: httpClient}, nil
}

// doRequests performs a POST to requestPath. It uses the marshaled form of req
Expand Down
2 changes: 1 addition & 1 deletion api_test.go
Expand Up @@ -61,7 +61,7 @@ func TestNetAPI(t *testing.T) {
}))
defer ts.Close()

api, err := newNetAPI(ts.URL, "fizzbuzz")
api, err := newNetAPI(ts.URL, "fizzbuzz", "")
if err != nil {
t.Errorf("unexpected newNetAPI error: %v", err)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/sblookup/main.go
Expand Up @@ -42,6 +42,7 @@ var (
apiKeyFlag = flag.String("apikey", "", "specify your Safe Browsing API key")
databaseFlag = flag.String("db", "", "path to the Safe Browsing database. By default persistent storage is disabled (not recommended).")
serverURLFlag = flag.String("server", safebrowsing.DefaultServerURL, "Safebrowsing API server address.")
proxyFlag = flag.String("proxy", "", "proxy to use to connect to the HTTP server")
)

const usage = `sblookup: command-line tool to lookup URLs with Safe Browsing.
Expand Down Expand Up @@ -82,6 +83,7 @@ func main() {
DBPath: *databaseFlag,
Logger: os.Stderr,
ServerURL: *serverURLFlag,
ProxyURL: *proxyFlag,
})
if err != nil {
fmt.Fprintln(os.Stderr, "Unable to initialize Safe Browsing client: ", err)
Expand Down
8 changes: 5 additions & 3 deletions cmd/sbserver/main.go
Expand Up @@ -222,6 +222,7 @@ const (
var (
apiKeyFlag = flag.String("apikey", "", "specify your Safe Browsing API key")
srvAddrFlag = flag.String("srvaddr", "localhost:8080", "TCP network address the HTTP server should use")
proxyFlag = flag.String("proxy", "", "proxy to use to connect to the HTTP server")
databaseFlag = flag.String("db", "", "path to the Safe Browsing database.")
)

Expand Down Expand Up @@ -504,9 +505,10 @@ func main() {
os.Exit(1)
}
conf := safebrowsing.Config{
APIKey: *apiKeyFlag,
DBPath: *databaseFlag,
Logger: os.Stderr,
APIKey: *apiKeyFlag,
ProxyURL: *proxyFlag,
DBPath: *databaseFlag,
Logger: os.Stderr,
}
sb, err := safebrowsing.NewSafeBrowser(conf)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion safebrowser.go
Expand Up @@ -185,6 +185,10 @@ type Config struct {
// If empty, it defaults to DefaultServerURL.
ServerURL string

// ProxyURL is the URL of the proxy to use for all requests.
// If empty, the underlying library uses $HTTP_PROXY environment variable.
ProxyURL string

// APIKey is the key used to authenticate with the Safe Browsing API
// service. This field is required.
APIKey string
Expand Down Expand Up @@ -298,7 +302,7 @@ func NewSafeBrowser(conf Config) (*SafeBrowser, error) {
// Create the SafeBrowsing object.
if conf.api == nil {
var err error
conf.api, err = newNetAPI(conf.ServerURL, conf.APIKey)
conf.api, err = newNetAPI(conf.ServerURL, conf.APIKey, conf.ProxyURL)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions safebrowser_system_test.go
Expand Up @@ -34,7 +34,7 @@ func TestNetworkAPIUpdate(t *testing.T) {
t.Skip()
}

nm, err := newNetAPI(DefaultServerURL, *apiKeyFlag)
nm, err := newNetAPI(DefaultServerURL, *apiKeyFlag, "")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestNetworkAPILookup(t *testing.T) {
t.Skip()
}

nm, err := newNetAPI(DefaultServerURL, *apiKeyFlag)
nm, err := newNetAPI(DefaultServerURL, *apiKeyFlag, "")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand Down

0 comments on commit fe6951d

Please sign in to comment.