Skip to content
Closed
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
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Surf
[![Documentation](https://img.shields.io/badge/documentation-readthedocs-blue.svg?style=flat-square)](http://surf.readthedocs.io/)
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://raw.githubusercontent.com/headzoo/surf/master/LICENSE.md)

Surf is a Go (golang) library that implements a virtual web browser that you control pragmatically.
Surf is a Go (golang) library that implements a virtual web browser that you control programmatically.
Surf isn't just another Go solution for downloading content from the web. Surf is designed to behave
like web browser, and includes: cookie management, history, bookmarking, user agent spoofing
(with a nifty user agent builder), submitting forms, DOM selection and traversal via jQuery style
Expand Down Expand Up @@ -53,17 +53,13 @@ Complete documentation is available on [Read the Docs](http://surf.readthedocs.i


### Credits
Surf was started by [Sean Hickey](https://github.com/headzoo) (headzoo) to learn more about the Go programming language.
The idea to create Surf was born in [this Reddit thread](http://www.reddit.com/r/golang/comments/2efw1q/mechanize_in_go/cjz4lze).

[![Twitter](https://img.shields.io/badge/follow-%40WebSeanHickey-orange.svg?style=flat-square)](https://twitter.com/WebSeanHickey)

Surf uses the awesome [goquery](https://github.com/PuerkitoBio/goquery) by Martin Angers, and
was written using [Intellij](http://www.jetbrains.com/idea/) and
the [golang plugin](http://plugins.jetbrains.com/plugin/5047).

Contributions have been made to Surf by the following awesome developers:

* [Sean Hickey](https://github.com/headzoo)
* [Haruyama Seigo](https://github.com/haruyama)
* [Tatsushi Demachi](https://github.com/tatsushid)
* [Charl Matthee](https://github.com/charl)
Expand All @@ -80,12 +76,11 @@ Contributions have been made to Surf by the following awesome developers:
* [Joseph Watson](https://github.com/jtwatson)
* [lxt2](https://github.com/lxt2)

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


### Contributing
Issues and pull requests are _always_ welcome. Code changes are made to the
[`dev`](https://github.com/headzoo/surf/tree/dev) branch. Once a milestone has been reached the branch will
be merged in with `master`, and a new version tag created. **Do not** make your changes against the
`master` branch, or they will _may_ be ignored.
Issues and pull requests are _always_ welcome.

See [CONTRIBUTING.md](https://raw.githubusercontent.com/headzoo/surf/master/CONTRIBUTING.md) for more information.

Expand Down
17 changes: 17 additions & 0 deletions agent/agent_appengine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build appengine

package agent

import (
"runtime"
)

// osName returns the name of the OS.
func osName() string {
return runtime.GOOS
}

// osVersion returns the OS version.
func osVersion() string {
return "0.0"
}
1 change: 1 addition & 0 deletions agent/agent_bsd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build darwin dragonfly freebsd netbsd openbsd
// +build !appengine

package agent

Expand Down
2 changes: 2 additions & 0 deletions agent/agent_linux.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// +build linux
// +build !appengine
// +build !arm

package agent

Expand Down
41 changes: 41 additions & 0 deletions agent/agent_linux_arm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// +build linux,arm

package agent

import (
"runtime"
"syscall"
)

// osName returns the name of the OS.
func osName() string {
buf := &syscall.Utsname{}
err := syscall.Uname(buf)
if err != nil {
return runtime.GOOS
}
return charsToString(buf.Sysname)
}

// osVersion returns the OS version.
func osVersion() string {
buf := &syscall.Utsname{}
err := syscall.Uname(buf)
if err != nil {
return "0.0"
}
return charsToString(buf.Release)
}

// charsToString converts a [65]uint8 byte array into a string.
func charsToString(ca [65]uint8) string {
s := make([]byte, len(ca))
var lens int
for ; lens < len(ca); lens++ {
if ca[lens] == 0 {
break
}
s[lens] = uint8(ca[lens])
}
return string(s[0:lens])
}
1 change: 1 addition & 0 deletions agent/agent_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build windows
// +build !appengine

package agent

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 @@ -78,6 +80,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 @@ -496,6 +501,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
10 changes: 10 additions & 0 deletions browser/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,13 @@ func TestCookieHeader(t *testing.T) {
t.Errorf("got %d calls, want %d", calls, want)
}
}

// 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")
}
}