Skip to content

Instantly share code, notes, and snippets.

Created April 29, 2015 14:41
  • Star 87 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
How to dispatch HTTP requests via Tor in Go.
// To the extent possible under law, the Yawning Angel has waived all copyright
// and related or neighboring rights to orhttp_example, using the creative
// commons "cc0" public domain dedication. See LICENSE or
// <> for full details.
package main
import (
// Things needed by the actual interface.
// Things needed by the example code.
func fatalf(fmtStr string, args interface{}) {
fmt.Fprintf(os.Stderr, fmtStr, args)
func main() {
// Create a transport that uses Tor Browser's SocksPort. If
// talking to a system tor, this may be an AF_UNIX socket, or
// instead.
tbProxyURL, err := url.Parse("socks5://")
if err != nil {
fatalf("Failed to parse proxy URL: %v\n", err)
// Get a proxy Dialer that will create the connection on our
// behalf via the SOCKS5 proxy. Specify the authentication
// and re-create the dialer/transport/client if tor's
// IsolateSOCKSAuth is needed.
tbDialer, err := proxy.FromURL(tbProxyURL, proxy.Direct)
if err != nil {
fatalf("Failed to obtain proxy dialer: %v\n", err)
// Make a http.Transport that uses the proxy dialer, and a
// http.Client that uses the transport.
tbTransport := &http.Transport{Dial: tbDialer.Dial}
client := &http.Client{Transport: tbTransport}
// Example: Fetch something. Real code will probably want to use
// client.Do() so they can change the User-Agent.
resp, err := client.Get("")
if err != nil {
fatalf("Failed to issue GET request: %v\n", err)
defer resp.Body.Close()
fmt.Printf("GET returned: %v\n", resp.Status)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fatalf("Failed to read the body: %v\n", err)
fmt.Printf("----- Body -----\n%s\n----- Body -----", body)
Copy link

abdel-aziz commented May 12, 2016

great, it works perfectly thx :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment