Standard library · Network

net/proxy

Connecting through an HTTP or SOCKS5 proxy, alone or in a chain.

import net.proxy

A proxy is a middleman that makes the connection on behalf of the program. This package builds the tunnel and gives back an ordinary net/tcp connection, read and written exactly as a direct one is.

Types

Proxy

struct Proxy
One proxy.
NameTypeDescription
kindint = 0The kind; one of the Type constants.
hoststr = ""The host.
portint = 0The port.
usernamestr = ""The user name, when one is needed.
passwordstr = ""The password.
net-proxy-Proxy.salam
import net.proxy

func main:
    a := proxy.NewHTTPProxy("10.0.0.1:3128")
    println a.kind == proxy.TypeHTTP, a.host, a.port
    b := proxy.NewHTTPProxyAuth("proxy.local:8080", "ali", "secret")
    println b.username, b.password
    c := proxy.NewSOCKS5Proxy("127.0.0.1:1080")
    d := proxy.NewSOCKS5ProxyAuth("socks.local:1080", "u", "p")
    println c.kind == proxy.TypeSOCKS5, d.port
    mut chain := proxy.NewChain()
    proxy.Add(chain, c)
    proxy.Add(chain, a)
    println chain.items.len()
end
Outputtrue 10.0.0.1 3128 ali secret true 1080 2

Chain

struct Chain
A chain of proxies used one after another.
NameTypeDescription
itemsVectorThe proxies in order.

The example for this item is shown with the ones above: net-proxy-Proxy.salam

Constants and variables

TypeHTTP

const TypeHTTP: i32 = 0
An HTTP proxy, through the CONNECT method.

The example for this item is shown with the ones above: net-proxy-Proxy.salam

TypeSOCKS5

const TypeSOCKS5: i32 = 1
A SOCKS5 proxy.

The example for this item is shown with the ones above: net-proxy-Proxy.salam

Functions

NewHTTPProxy

func NewHTTPProxy(addr: str): Proxy
An HTTP proxy from a host:port address.

The example for this item is shown with the ones above: net-proxy-Proxy.salam

NewHTTPProxyAuth

func NewHTTPProxyAuth(addr: str, username: str, password: str): Proxy
An HTTP proxy with a user name and password.

The example for this item is shown with the ones above: net-proxy-Proxy.salam

NewSOCKS5Proxy

func NewSOCKS5Proxy(addr: str): Proxy
A SOCKS5 proxy.

The example for this item is shown with the ones above: net-proxy-Proxy.salam

NewSOCKS5ProxyAuth

func NewSOCKS5ProxyAuth(addr: str, username: str, password: str): Proxy
A SOCKS5 proxy with a user name and password.

The example for this item is shown with the ones above: net-proxy-Proxy.salam

Dial

func Dial(p: Proxy, target_host: str, target_port: int): tcp.Conn
Connects to the destination through the proxy and gives back a ready TCP connection.
net-proxy-Dial.salam
import net.proxy
import net.tcp

func main:
    p := proxy.NewSOCKS5Proxy("127.0.0.1:1080")
    conn := proxy.Dial(p, "example.com", 80)
    mut chain := proxy.NewChain()
    proxy.Add(chain, p)
    conn2 := proxy.DialChain(chain, "example.com", 80)
    println tcp.ConnOk(conn), tcp.ConnOk(conn2)
end

NewChain

func NewChain(): Chain
An empty chain.

The example for this item is shown with the ones above: net-proxy-Proxy.salam

Add

func Add(c &: Chain, p: Proxy)
Adds a proxy to the end of the chain.

The example for this item is shown with the ones above: net-proxy-Proxy.salam

DialChain

func DialChain(c: Chain, target_host: str, target_port: int): tcp.Conn
Connects to the destination through every proxy in the chain, in order.

The example for this item is shown with the ones above: net-proxy-Dial.salam