tordam

A library for peer discovery inside the Tor network
git clone https://git.parazyd.org/tordam
Log | Files | Refs | README | LICENSE

commit ae10736455de6b4c6bd821d13791b1be69d9a95a
parent 96ac81d45ba535705abe5d169d6c833a17326f5e
Author: parazyd <parazyd@dyne.org>
Date:   Thu,  7 Dec 2017 18:12:45 +0100

Implement wrapping around HTTP requests for sending socksified POST

Diffstat:
Mgo/lib/helpers.go | 40++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+), 0 deletions(-)

diff --git a/go/lib/helpers.go b/go/lib/helpers.go @@ -1,14 +1,23 @@ package lib +// See LICENSE file for copyright and license details. + import ( "bytes" "log" + "net/http" + "net/url" "os/exec" "regexp" "strings" "time" + + "golang.org/x/net/proxy" ) +// ProxyAddr is the address of our Tor SOCKS port. +const ProxyAddr = "127.0.0.1:9050" + // CheckError is a handler for errors. func CheckError(err error) { if err != nil { @@ -76,3 +85,34 @@ func ValidateReq(req map[string]string) bool { return true } + +// HTTPPost sends an HTTP POST request to the given host. It sends data as +// application/json. +func HTTPPost(host string, data []byte) *http.Response { + socksify := false + + parsedHost, err := url.Parse(host) + CheckError(err) + hostname := parsedHost.Hostname() + if strings.HasSuffix(hostname, ".onion") { + socksify = true + } + + httpTransp := &http.Transport{} + httpClient := &http.Client{Transport: httpTransp} + if socksify { + log.Println("Detected a .onion request. Using SOCKS proxy.") + dialer, err := proxy.SOCKS5("tcp", ProxyAddr, nil, proxy.Direct) + CheckError(err) + httpTransp.Dial = dialer.Dial + } + + request, err := http.NewRequest("POST", host, bytes.NewBuffer(data)) + CheckError(err) + request.Header.Set("Content-Type", "application/json") + + resp, err := httpClient.Do(request) + CheckError(err) + + return resp +}