Skip to content

Instantly share code, notes, and snippets.

@pr0n00gler
Last active January 31, 2016 13:33
Show Gist options
  • Select an option

  • Save pr0n00gler/999101471276f10b2843 to your computer and use it in GitHub Desktop.

Select an option

Save pr0n00gler/999101471276f10b2843 to your computer and use it in GitHub Desktop.
package main
import (
// Things needed by the actual interface.
"golang.org/x/net/proxy"
"net/http"
"net/url"
// Things needed by the example code.
"fmt"
"io/ioutil"
//"os"
)
func handler(w http.ResponseWriter, r *http.Request) {
app_url := r.FormValue("url")
tbProxyURL, err := url.Parse("socks5://127.0.0.1:9050")
if err != nil {
fmt.Fprintf(w, "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 {
fmt.Fprintf(w, "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(app_url)
if err != nil {
fmt.Fprintf(w, "Failed to issue GET request: %v\n", err)
}
defer resp.Body.Close()
//fmt.Printf("GET returned: %v\n", resp.Status)
if resp.StatusCode != 200 {
fmt.Fprintf(w, resp.Status)
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(w, "Failed to read the body: %v\n", err)
}
fmt.Fprintf(w, string(body[:]))
}
func main() {
http.HandleFunc("/proxy", handler)
http.ListenAndServe(":9999", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment