tImplement wrapping around HTTP requests for sending socksified POST - tordam - A library for peer discovery inside the Tor network
 (HTM) git clone https://git.parazyd.org/tordam
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit ae10736455de6b4c6bd821d13791b1be69d9a95a
 (DIR) parent 96ac81d45ba535705abe5d169d6c833a17326f5e
 (HTM) Author: parazyd <parazyd@dyne.org>
       Date:   Thu,  7 Dec 2017 18:12:45 +0100
       
       Implement wrapping around HTTP requests for sending socksified POST
       
       Diffstat:
         M go/lib/helpers.go                   |      40 +++++++++++++++++++++++++++++++
       
       1 file changed, 40 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/go/lib/helpers.go b/go/lib/helpers.go
       t@@ -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 {
       t@@ -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
       +}