tornado

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

README

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"}

Documentation

Overview

Package tornado provides support for an easy launch of tor proxy on golang.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextDialer

type ContextDialer interface {
	// DialContext connects to the address on the named network using
	// the provided context.
	DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

A ContextDialer dials using a context.

type FloatingProxy

type FloatingProxy struct {
	// contains filtered or unexported fields
}

A FloatingProxy is an abstraction for making it easy to create connections through different proxies but within the same instance of the dial function.

But do not forget that different implementations of clients for network interaction can keep the connection open for different requests while using one common Proxy chain, for example http.Client with http.DefaultTransport.

func NewFloatingProxy

func NewFloatingProxy(pool *Pool) *FloatingProxy

NewFloatingProxy creates new instance of FloatingProxy.

func (*FloatingProxy) Dial

func (p *FloatingProxy) Dial(network, address string) (c net.Conn, err error)

Dial connects to the address on the named network.

Dial uses context.Background internally; to specify the context, use DialContext.

See func Dial of the net package of standard library for a description of the network and address parameters.

func (*FloatingProxy) DialContext

func (p *FloatingProxy) DialContext(ctx context.Context, network, address string) (net.Conn, error)

DialContext connects to the address on the named network using the provided context.

See func Dial of the net package of standard library for a description of the network and address parameters.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is an abstraction on the options.

func WithForwardContextDialer

func WithForwardContextDialer(dialer ContextDialer) Option

WithForwardContextDialer allows to specify the optional dial function for establishing the transport connection.

func WithTorrcOption

func WithTorrcOption(ops ...string) Option

WithTorrcOption allows adding arbitrary parameters to torrc.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

A Pool is an abstraction for creating multiple proxy instances using a single tor process to reduce resource usage.

Also, Pool provides a minimal interface for managing a set of proxies and their reuse.

func NewPool

func NewPool(ctx context.Context, size int, ops ...Option) (*Pool, error)

NewPool creates new instance of proxy Pool.

Pool must be closed wia using Close method after end usage to prevent memory leak and tor demon process leak, but keep in mind that all proxies will stop working immediately after the Pool is closed.

func (*Pool) Close

func (p *Pool) Close() (err error)

Close stops the tor demon running in the background.

This operation will not wait for active connections to close, they will be aborted.

func (*Pool) Get

func (p *Pool) Get() *Proxy

Get gets a proxy instance from the pool.

This operation can block the goroutine until a new proxy instance appears in the pool.

func (*Pool) Put

func (p *Pool) Put(prx *Proxy)

Put puts the proxy instance back in the pool.

Do not try to manually fill the Pool in excess of the size specified when creating the pool, this can lead to blocking of the goroutine when pool overflows.

type Proxy

type Proxy struct {
	// contains filtered or unexported fields
}

Proxy returns a ContextDialer that makes connections to the given address over tor network.

func NewProxy

func NewProxy(ctx context.Context, ops ...Option) (*Proxy, error)

NewProxy creates new instance of Proxy.

If the Proxy was created using NewProxy, it must be closed wia using Close method after end usage to prevent memory leak and tor demon process leak.

Example
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:

func (*Proxy) Close

func (p *Proxy) Close() (err error)

Close stops the tor demon running in the background. If the Proxy was created using NewPool directly instead of NewProxy, Close has no effect.

This operation will not wait for active connections to close, they will be aborted.

func (*Proxy) Dial

func (p *Proxy) Dial(network, address string) (c net.Conn, err error)

Dial connects to the address on the named network over tor network.

See func Dial of the net package of standard library for a description of the network and address parameters.

Dial uses context.Background internally; to specify the context, use DialContext.

func (*Proxy) DialContext

func (p *Proxy) DialContext(ctx context.Context, network, address string) (net.Conn, error)

DialContext connects to the address on the named network over tor network using the provided context.

The provided Context must be non-nil. If the context expires before the connection is complete, an error is returned. Once successfully connected, any expiration of the context will not affect the connection.

See func Dial of the net package of standard library for a description of the network and address parameters.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL