proxyclient

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

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

Go to latest
Published: Oct 21, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

README

ProxyClient

Build Status

English | 简体中文

ProxyClient is a Go proxy client with an API modeled after the standard net package.

It supports proxy chaining. SOCKS4, SOCKS4A, SOCKS5, HTTP, HTTPS, and direct connections are implemented natively. shadowsocks, vmess, vless, trojan, wireguard, and hysteria2 are handled by the bundled xray-core protocol engine, with ss, hysteria, and hy2 aliases supported.

TLS certificate verification is enabled by default. Newer Xray versions no longer support allowInsecure. For self-signed certificates, use the pcs / pinnedPeerCertSha256 or vcn / verifyPeerCertByName URL query parameters. An explicitly enabled legacy allowInsecure parameter must be accompanied by one of these secure verification options.

package main

import (
	"fmt"
	"io"

	proxyclient "github.com/GameXG/ProxyClient"
)

func main() {
	p, err := proxyclient.NewProxyClient("socks5://127.0.0.1:5556?upProxy=https://145.2.1.3:8080")
	if err != nil {
		panic("failed to create proxy client")
	}

	c, err := p.Dial("tcp", "www.google.com:80")
	if err != nil {
		panic("connection failed")
	}
	defer c.Close()

	io.WriteString(c, "GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n")
	b, err := io.ReadAll(c)
	if err != nil {
		panic("read failed")
	}
	fmt.Print(string(b))
}

API

The client exposes connection interfaces compatible with net.Conn and supports proxy chaining through SetUpProxy.

type Conn interface {
	net.Conn
}

type TCPConn interface {
	Conn
	SetLinger(sec int) error
	SetNoDelay(noDelay bool) error
	SetReadBuffer(bytes int) error
	SetWriteBuffer(bytes int) error
}

type ProxyTCPConn interface {
	TCPConn
	ProxyClient() ProxyClient
}

type UDPConn interface {
	Conn
}

type ProxyUDPConn interface {
	UDPConn
	ProxyClient() ProxyClient
}

type ProxyClient interface {
	UpProxy() ProxyClient
	SetUpProxy(upProxy ProxyClient) error

	Dial(network, address string) (net.Conn, error)
	DialTimeout(network, address string, timeout time.Duration) (net.Conn, error)
	DialTCP(network string, laddr, raddr *net.TCPAddr) (net.Conn, error)
	DialTCPSAddr(network string, raddr string) (ProxyTCPConn, error)
	DialTCPSAddrTimeout(network string, raddr string, timeout time.Duration) (ProxyTCPConn, error)
	DialUDP(network string, laddr, raddr *net.UDPAddr) (net.Conn, error)
	GetProxyAddrQuery() map[string][]string
}

Create a client with NewProxyClient using one of these URL forms:

http://123.123.123.123:8088
https://123.123.123.123:8088
socks4://123.123.123.123:5050
socks4a://123.123.123.123:5050
socks5://123.123.123.123:5050?upProxy=http://145.2.1.3:8080
shadowsocks://...
vmess://...
vless://...
trojan://...
wireguard://...
hysteria2://...
direct://0.0.0.0:0000/?localAddr=123.123.123.123:0

Aliases: ss://, hysteria://, and hy2://.

Credits

Extension protocol support is provided by the bundled xray-core engine.

Documentation

Index

Constants

View Source
const (
	SocksCmdConect       = 0x01
	SocksCmdBind         = 0x02
	SocksCmdUdpAssociate = 0x03
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn interface {
	net.Conn
}

连接

type DirectTCPConn

type DirectTCPConn struct {
	net.TCPConn
	// contains filtered or unexported fields
}

func (*DirectTCPConn) ProxyClient

func (c *DirectTCPConn) ProxyClient() ProxyClient

type DirectUDPConn

type DirectUDPConn struct {
	net.UDPConn
	// contains filtered or unexported fields
}

func (*DirectUDPConn) ProxyClient

func (c *DirectUDPConn) ProxyClient() ProxyClient

type HttpTCPConn

type HttpTCPConn struct {
	Conn //http 协议时是原始链接、https协议时是tls.Conn

	LocalPort uint16
	// contains filtered or unexported fields
}

func (*HttpTCPConn) Close

func (c *HttpTCPConn) Close() error

重写了 Read 接口 由于 http 协议问题,解析响应需要读缓冲,所以必须重写 Read 来兼容读缓冲功能。

func (*HttpTCPConn) ProxyClient

func (c *HttpTCPConn) ProxyClient() ProxyClient

func (*HttpTCPConn) Read

func (c *HttpTCPConn) Read(b []byte) (n int, err error)

重写了 Read 接口 由于 http 协议问题,解析响应需要读缓冲,所以必须重写 Read 来兼容读缓冲功能。

func (*HttpTCPConn) SetLinger

func (c *HttpTCPConn) SetLinger(sec int) error

func (*HttpTCPConn) SetNoDelay

func (c *HttpTCPConn) SetNoDelay(noDelay bool) error

func (*HttpTCPConn) SetReadBuffer

func (c *HttpTCPConn) SetReadBuffer(bytes int) error

func (*HttpTCPConn) SetWriteBuffer

func (c *HttpTCPConn) SetWriteBuffer(bytes int) error

type ProxyClient

type ProxyClient interface {
	// 返回本代理的上层级联代理
	UpProxy() ProxyClient
	// 设置本代理的上层代理
	SetUpProxy(upProxy ProxyClient) error

	// Dial 在网络network上连接地址address,并返回一个Conn接口。可用的网络类型有:
	// "tcp"、"tcp4"、"tcp6"、"udp"、"udp4"、"udp6"
	// 对TCP和UDP网络,地址格式是host:port或[host]:port,参见函数JoinHostPort和SplitHostPort。
	// 如果代理服务器支持远端DNS解析,那么会使用远端DNS解析。
	Dial(network, address string) (net.Conn, error)
	DialTimeout(network, address string, timeout time.Duration) (net.Conn, error)
	// DialTCP在网络协议net上连接本地地址laddr和远端地址raddr。net必须是"tcp"、"tcp4"、"tcp6";如果laddr不是nil,将使用它作为本地地址,否则自动选择一个本地地址。
	// 由于 net.TCPAddr 内部保存的是IP地址及端口,所以使用本函数无法使用远端DNS解析,要想使用远端DNS解析,请使用 Dial 或 DialTCPSAddr 函数。
	DialTCP(net string, laddr, raddr *net.TCPAddr) (net.Conn, error)
	// DialTCPSAddr 同 DialTCP 函数,主要区别是如果代理支持远端dns解析,那么会使用远端dns解析。
	DialTCPSAddr(network string, raddr string) (ProxyTCPConn, error)
	// DialTCPSAddrTimeout 同 DialTCPSAddr 函数,增加了超时功能
	DialTCPSAddrTimeout(network string, raddr string, timeour time.Duration) (ProxyTCPConn, error)
	//ListenTCP在本地TCP地址laddr上声明并返回一个*TCPListener,net参数必须是"tcp"、"tcp4"、"tcp6",如果laddr的端口字段为0,函数将选择一个当前可用的端口,可以用Listener的Addr方法获得该端口。
	//ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error)
	//DialTCP在网络协议net上连接本地地址laddr和远端地址raddr。net必须是"udp"、"udp4"、"udp6";如果laddr不是nil,将使用它作为本地地址,否则自动选择一个本地地址。
	DialUDP(net string, laddr, raddr *net.UDPAddr) (net.Conn, error)

	// 获得 Proxy 代理地址的 Query
	// 为了大小写兼容,key全部是转换成小写的。
	GetProxyAddrQuery() map[string][]string
}

仿 net 库接口的代理客户端 支持级联代理功能,可以通过 SetUpProxy 设置上级代理。

func NewDriectProxyClient

func NewDriectProxyClient(localAddr string, query map[string][]string) (ProxyClient, error)

创建代理客户端 直连 direct://0.0.0.0:0000/?LocalAddr=123.123.123.123:0

func NewHttpProxyClient

func NewHttpProxyClient(proxyType string, proxyAddr string, proxyDomain string, auth string, insecureSkipVerify bool, upProxy ProxyClient, query map[string][]string) (ProxyClient, error)

创建代理客户端 ProxyType http https ProxyAddr 127.0.0.1:5555 proxyDomain ssl 验证域名,"" 则使用 proxyAddr 部分的域名 insecureSkipVerify 使用https代理时是否忽略证书检查 UpProxy

func NewProxyClient

func NewProxyClient(addr string) (ProxyClient, error)

创建代理客户端 http 代理 http://123.123.123.123:8088 https 代理 https://123.123.123.123:8088 socks4 代理 socks4://123.123.123.123:5050 socks4 协议不支持远端 dns 解析 socks4a 代理 socks4a://123.123.123.123:5050 socks5 代理 socks5://123.123.123.123:5050?upProxy=http://145.2.1.3:8080 扩展协议 shadowsocks://... vmess://... vless://... trojan://... wireguard://... hysteria2://... 兼容别名:ss://、hysteria://、hy2:// 直连 direct://0.0.0.0:0000/?LocalAddr=123.123.123.123:0

func NewSocksProxyClient

func NewSocksProxyClient(proxyType string, proxyAddr string, upProxy ProxyClient, query map[string][]string) (ProxyClient, error)

创建代理客户端 ProxyType socks4 socks5 ProxyAddr 127.0.0.1:5555 UpProxy

func NewXrayProxyClient

func NewXrayProxyClient(u *url.URL, query map[string][]string) (ProxyClient, error)

type ProxyTCPConn

type ProxyTCPConn interface {
	TCPConn
	ProxyClient() ProxyClient // 获得所属的代理
}

type ProxyUDPConn

type ProxyUDPConn interface {
	UDPConn
	ProxyClient() ProxyClient // 获得所属的代理
}

type SocksTCPConn

type SocksTCPConn struct {
	ProxyTCPConn

	LocalPort uint16
	// contains filtered or unexported fields
}

func (*SocksTCPConn) ProxyClient

func (c *SocksTCPConn) ProxyClient() ProxyClient

type SocksUDPConn

type SocksUDPConn struct {
	net.UDPConn
	// contains filtered or unexported fields
}

func (*SocksUDPConn) ProxyClient

func (c *SocksUDPConn) ProxyClient() ProxyClient

type TCPConn

type TCPConn interface {
	Conn

	/*
		SetLinger设定当连接中仍有数据等待发送或接受时的Close方法的行为。
		如果sec < 0(默认),Close方法立即返回,操作系统停止后台数据发送;如果 sec == 0,Close立刻返回,操作系统丢弃任何未发送或未接收的数据;如果sec > 0,Close方法阻塞最多sec秒,等待数据发送或者接收,在一些操作系统中,在超时后,任何未发送的数据会被丢弃。
	*/
	SetLinger(sec int) error

	// SetNoDelay设定操作系统是否应该延迟数据包传递,以便发送更少的数据包(Nagle's算法)。默认为真,即数据应该在Write方法后立刻发送。
	SetNoDelay(noDelay bool) error

	//SetReadBuffer设置该连接的系统接收缓冲
	SetReadBuffer(bytes int) error

	//SetWriteBuffer设置该连接的系统发送缓冲
	SetWriteBuffer(bytes int) error
}

表示 TCP 连接 提供 net.TcpConn 全部的方法,但是部分方法由于代理协议的限制可能不能获得正确的结果。例如:LocalAddr 、RemoteAddr 方法不被很多代理协议支持。

type UDPConn

type UDPConn interface {
	Conn
}

表示 UDP 连接

Directories

Path Synopsis
example
Ss command
http command
socks5 command

Jump to

Keyboard shortcuts

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