metadialer

package module
v0.0.0-...-b550471 Latest Latest
Warning

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

Go to latest
Published: May 27, 2025 License: MIT Imports: 13 Imported by: 2

README

go-meta-dialer

A dialer which will reach clearnet, onion, and I2P sites, and an HTTP Client which has specific TLS behavior for Onion and I2P domains.

Installation

go get github.com/go-i2p/go-meta-dialer

Features

  • Multi-network dialer supporting regular internet, Tor (.onion), and I2P (.i2p) connections
  • Automatic routing based on destination address
  • Optional anonymity mode to route all non-I2P connections through Tor
  • HTTP client with special TLS handling for darknet services

Usage

Basic Dialer
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    
    metadialer "github.com/go-i2p/go-meta-dialer"
)

func main() {
    // IMPORTANT: Close the dialers when done to prevent SAM/Tor session leaks
    defer metadialer.Garlic.Close()
    defer metadialer.Onion.Close()
    
    // Use the dialer directly
    conn, err := metadialer.Dial("tcp", "example.i2p:80")
    if err != nil {
        panic(err)
    }
    defer conn.Close()
    
    // Do something with the connection...
}
HTTP Client
package main

import (
    "fmt"
    "io/ioutil"
    
    metadialer "github.com/go-i2p/go-meta-dialer"
)

func main() {
    // IMPORTANT: Close the dialers when done
    defer metadialer.Garlic.Close()
    defer metadialer.Onion.Close()
    
    // Create a new HTTP client
    client := metadialer.NewMetaHTTPClient(nil)
    
    // Make requests to any network
    resp, err := client.Get("https://example.com")           // Normal website
    // resp, err := client.Get("http://example.onion")       // Tor onion service
    // resp, err := client.Get("http://example.i2p")         // I2P site
    
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }
    
    fmt.Println(string(body))
}
Make it the Default HTTP Client
// Set the MetaHTTPClient as the default for all HTTP requests
client := metadialer.NewMetaHTTPClient(nil)
http.DefaultClient = client.HTTPClient()

Configuration

  • metadialer.ANON = true: (Default) Routes all non-I2P connections through Tor
  • metadialer.ANON = false: Only routes .onion domains through Tor, direct connection for regular domains

Notes

  • TLS verification is skipped for .i2p and .onion domains but performs standard verification for all other domains
  • The dialer automatically routes traffic to the appropriate network based on the TLD
  • Requires a running SAM bridge (default: 127.0.0.1:7656) for I2P connections
  • Requires a running Tor SOCKS proxy for .onion connections

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Garlic and Onion are the dialers for I2P and onion connections respectively.
	// Garlic is used for I2P connections and Onion is used for onion connections.
	// GarlicErr and OnionErr are the errors returned by the dialers.
	// It is important to `defer close` the dialers when you include them in your code.
	// Otherwise your SAMv3 or Tor sessions may leak. onramp tries to fix it for you but do it anyway.
	// in your `main` function, do:
	// defer Garlic.Close()
	// defer Onion.Close()
	Garlic, GarlicErr = onramp.NewGarlic(fmt.Sprintf("metadialer-%s", randomString()), "127.0.0.1:7656", onramp.OPT_DEFAULTS)
	Onion, OnionErr   = onramp.NewOnion(fmt.Sprintf("metadialer-%s", randomString()))
)
View Source
var ANON = true

ANON is a flag to indicate whether to use the onion dialer for all non-I2P connections. If true, all non-I2P connections will be routed through the onion dialer. If false, regular connection will be made directly. Default is true.

View Source
var Dial = func(network, addr string) (net.Conn, error) {
	return dialHelper(network, addr)
}

Dial is a custom dialer that handles .i2p and .onion domains differently. It uses the garlic dialer for .i2p domains and the onion dialer for .onion domains. For all other domains, it uses the default dialer. If ANON is true, it will use the onion dialer for all non-I2P connections. It returns a net.Conn interface for the connection. If the address is invalid or the connection fails, it returns an error. The network parameter is ignored for onion connections.

View Source
var DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
	return dialHelper(network, addr)
}

DialContext is a custom dialer that handles .i2p and .onion domains differently. It uses the garlic dialer for .i2p domains and the onion dialer for .onion domains. For all other domains, it uses the default dialer. If ANON is true, it will use the onion dialer for all non-I2P connections. It returns a net.Conn interface for the connection. If the address is invalid or the connection fails, it returns an error. The network parameter is ignored for onion connections. It accepts a context.Context parameter for cancellation and timeout. The context is ignored.

Functions

func GetTLD

func GetTLD(addr string) (string, error)

GetTLD is a helper function that returns the top-level domain of the given address. It takes a string address as input, which can be a fully qualified domain name or a URL. If the address does not include a scheme, "http://" is added by default. It returns the top-level domain as a string or an error if the address is invalid. The function also checks if the domain is an IP address and returns "ip" in that case. If there is no top-level domain found, it returns the entire domain as the TLD. The function is useful for determining the type of domain (I2P, onion, or regular) for routing purposes. It uses the net package to parse the address and extract the hostname.

Types

type MetaHTTPClient

type MetaHTTPClient struct {
	*http.Client
}

MetaHTTPClient is an HTTP client that skips TLS verification for .i2p and .onion domains but performs standard verification for all other domains.

func NewMetaHTTPClient

func NewMetaHTTPClient(rootCAs *x509.CertPool) *MetaHTTPClient

NewMetaHTTPClient creates a new client with special handling for .i2p and .onion domains. It accepts an optional root CA pool for custom certificate authorities.

func (*MetaHTTPClient) CloseIdleConnections

func (c *MetaHTTPClient) CloseIdleConnections()

CloseIdleConnections closes any idle connections in the transport

func (*MetaHTTPClient) Do

func (c *MetaHTTPClient) Do(req *http.Request) (*http.Response, error)

Do is a convenience method for making arbitrary HTTP requests

func (*MetaHTTPClient) Get

func (c *MetaHTTPClient) Get(url string) (*http.Response, error)

Get is a convenience method for making GET requests

func (*MetaHTTPClient) HTTPClient

func (c *MetaHTTPClient) HTTPClient() *http.Client

HTTPClient returns the underlying http.Client. This is useful for accessing the raw client if needed. It is anticipated that this will be necessary for some use cases. For example, to configure the default dialer for the application:

http.DefaultClient = client.HTTPClient()

func (*MetaHTTPClient) Head

func (c *MetaHTTPClient) Head(url string) (*http.Response, error)

Head is a convenience method for making HEAD requests

func (*MetaHTTPClient) Post

func (c *MetaHTTPClient) Post(url, contentType string, body interface{}) (*http.Response, error)

Post is a convenience method for making POST requests

func (*MetaHTTPClient) PostForm

func (c *MetaHTTPClient) PostForm(url string, data url.Values) (*http.Response, error)

PostForm is a convenience method for making POST requests with form data

Directories

Path Synopsis
example
httpproxy command

Jump to

Keyboard shortcuts

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