Skip to content

xorcare/tornado

Repository files navigation

tornado

Go codecov Go Report Card Go Reference

Library for easy launch of tor proxy on golang.

Examples

Small example of using single proxy server

package main

import (
	"context"
	"log"
	"net/http"
	"net/http/httputil"
	"time"

	"github.com/xorcare/tornado"
)

func main() {
	const proxyServerStartupTimeout = 15 * time.Second
	ctx, done := context.WithTimeout(context.Background(), proxyServerStartupTimeout)
	defer done()

	prx, err := tornado.NewProxy(ctx)
	if err != nil {
		log.Panicln("failed to create new instance of proxy:", err)
	}
	// After usage Proxy must be closed to prevent memory leak and tor
	// demon process leak.
	defer prx.Close()
	
	httpcli := &http.Client{
		Transport: &http.Transport{
			DialContext: prx.DialContext,
		},
		Timeout: time.Second * 15,
	}

	resp, err := httpcli.Get("https://check.torproject.org/api/ip")
	if err != nil {
		log.Panicln("failed to execute http request to tor project api:", err)
	}

	text, err := httputil.DumpResponse(resp, true)
	if err != nil {
		log.Panicln("failed to dump full response info:", err)
	}

	log.Println(string(text))
}

Output:

2022/04/05 23:50:18 HTTP/1.1 200 OK
Content-Length: 37
Content-Type: application/json
Date: Tue, 05 Apr 2022 20:50:18 GMT
Referrer-Policy: no-referrer
Server: Apache
Strict-Transport-Security: max-age=15768000; preload
X-Content-Type-Options: nosniff
X-Frame-Options: sameorigin
X-Xss-Protection: 1

{"IsTor":true,"IP":"185.220.100.250"}