Skip to content

Instantly share code, notes, and snippets.

@HugoGuiroux
Last active June 14, 2022 14:50
Show Gist options
  • Select an option

  • Save HugoGuiroux/2e5acbbe15288d3d3ace to your computer and use it in GitHub Desktop.

Select an option

Save HugoGuiroux/2e5acbbe15288d3d3ace to your computer and use it in GitHub Desktop.
Carry http request through tor in go
// Author: Hugo Guiroux: http://hugoguiroux.blogspot.fr/
package main
import (
"code.google.com/p/go.net/proxy"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
)
var torAddr = flag.String("host", "localhost", "host for tor proxy")
var torPort = flag.Int("port", 9050, "port for tor proxy")
const ipWebSite = "http://checkip.dyndns.org/"
// main usage : ./tor_poc [-host tor_address] [-port tor_port] [-help]
func main() {
// Parse command line arguments
flag.Parse()
log.Printf("Connecting using tor (%s:%d) to (%s)\n", *torAddr, *torPort, ipWebSite)
// Try to retrieve IP address from ipWebSite
tr := http.Transport{
Dial: socks5Dial, // To test without tor, replace socks5Dial by net.Dial
}
client := http.Client{Transport: &tr}
resp, err := client.Get(ipWebSite)
if err != nil {
log.Fatal(err)
}
// Read content of http request
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(content))
}
// Create specific dial function for http lib
func socks5Dial(network, addr string) (net.Conn, error) {
socks5Proxy, err := proxy.SOCKS5("tcp", fmt.Sprintf("%s:%d", *torAddr, *torPort), nil, proxy.Direct)
if err != nil {
return nil, err
}
return socks5Proxy.Dial(network, addr)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment