Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Contributions have been made to Surf by the following awesome developers:
* [nicot](https://github.com/nicot)
* [Joseph Watson](https://github.com/jtwatson)
* [lxt2](https://github.com/lxt2)
* [shavit](https://github.com/shavit)

The idea to create Surf was born in [this Reddit thread](http://www.reddit.com/r/golang/comments/2efw1q/mechanize_in_go/cjz4lze).

Expand Down
20 changes: 20 additions & 0 deletions browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strings"
"time"

"golang.org/x/net/proxy"

"github.com/PuerkitoBio/goquery"
"github.com/headzoo/surf/errors"
"github.com/headzoo/surf/jar"
Expand Down Expand Up @@ -93,6 +95,9 @@ type Browsable interface {
// SetTransport sets the http library transport mechanism for each request.
SetTransport(rt http.RoundTripper)

// Set a proxy URL. You can use it with Tor
SetProxy(u string) (err error)

// AddRequestHeader adds a header the browser sends with each request.
AddRequestHeader(name, value string)

Expand Down Expand Up @@ -547,6 +552,21 @@ func (bow *Browser) SetTransport(rt http.RoundTripper) {
bow.client.Transport = rt
}

// Set a proxy url for Tor
func (bow *Browser) SetProxy(u string) (err error) {
_url, err := url.Parse(u)
if err != nil {
return err
}
dialer, err := proxy.FromURL(_url, proxy.Direct)
if err != nil {
return err
}
bow.SetTransport(&http.Transport{Dial: dialer.Dial})

return err
}

// AddRequestHeader sets a header the browser sends with each request.
func (bow *Browser) AddRequestHeader(name, value string) {
bow.headers.Set(name, value)
Expand Down
8 changes: 8 additions & 0 deletions browser/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ func TestCookieHeader(t *testing.T) {
}
}

// Test proxy
// https://github.com/headzoo/surf/pull/56
func TestSetProxyWillSetTransport(t *testing.T){
b := newDefaultTestBrowser()
b.SetProxy("socks5://127.0.0.1:9050")
if b.client.Transport == nil {
t.Errorf("no transport method")

// Should inherit the configuration into a new instance
func TestTabInheritance(t *testing.T){
bow1 := newDefaultTestBrowser()
Expand Down