proxyclient

package module
v0.0.0-...-0ebb17d Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2015 License: GPL-3.0 Imports: 18 Imported by: 0

README

ProxyClient

Build Status

golang 代理客户端,和 net 标准库一致的 API 。 支持嵌套代理,支持 socks4、socks4a、socks5、http、https、ss 代理协议。


package main

import (
	"fmt"
	"github.com/gamexg/proxyclient"
	"io"
	"io/ioutil"
)

func main() {
	p, err := proxyclient.NewProxyClient("socks5://127.0.0.1:5556?upProxy=https://145.2.1.3:8080")
	if err != nil {
		panic("创建代理客户端错误")
	}

	c, err := p.Dial("tcp", "www.google.com:80")
	if err != nil {
		panic("连接错误")
	}

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

API


// 连接
type Conn interface {
	net.Conn
}

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

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

// 表示 UDP 连接
type UDPConn interface {
	Conn
}

type ProxyUDPConn interface {
	UDPConn
	ProxyClient() ProxyClient // 获得所属的代理
}
// 仿 net 库接口的代理客户端
// 支持级联代理功能,可以通过 SetUpProxy 设置上级代理。
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
}

// 创建代理客户端
// 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
// ss 代理 ss://method:[email protected]:5050
// 直连 direct://0.0.0.0:0000/?LocalAddr=123.123.123.123:0
func NewProxyClient(addr string) (ProxyClient, error)

感谢

  • ss 协议使用的是 ss-go

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 ss 代理 ss://method:[email protected]:5050 直连 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 NewSsProxyClient

func NewSsProxyClient(proxyAddr, method, password string, upProxy ProxyClient, 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 SsProxyClient

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

func (*SsProxyClient) Dial

func (p *SsProxyClient) Dial(network, address string) (net.Conn, error)

func (*SsProxyClient) DialTCP

func (p *SsProxyClient) DialTCP(network string, laddr, raddr *net.TCPAddr) (net.Conn, error)

func (*SsProxyClient) DialTCPSAddr

func (p *SsProxyClient) DialTCPSAddr(network string, raddr string) (ProxyTCPConn, error)

func (*SsProxyClient) DialTCPSAddrTimeout

func (p *SsProxyClient) DialTCPSAddrTimeout(network string, raddr string, timeout time.Duration) (rconn ProxyTCPConn, rerr error)

func (*SsProxyClient) DialTimeout

func (p *SsProxyClient) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error)

func (*SsProxyClient) DialUDP

func (p *SsProxyClient) DialUDP(network string, laddr, raddr *net.UDPAddr) (conn net.Conn, err error)

func (*SsProxyClient) GetProxyAddrQuery

func (c *SsProxyClient) GetProxyAddrQuery() map[string][]string

func (*SsProxyClient) SetUpProxy

func (p *SsProxyClient) SetUpProxy(upProxy ProxyClient) error

func (*SsProxyClient) UpProxy

func (p *SsProxyClient) UpProxy() ProxyClient

type SsTCPConn

type SsTCPConn struct {
	TCPConn
	// contains filtered or unexported fields
}

func (*SsTCPConn) Close

func (c *SsTCPConn) Close() error

func (*SsTCPConn) ProxyClient

func (c *SsTCPConn) ProxyClient() ProxyClient

func (*SsTCPConn) Read

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

func (*SsTCPConn) Write

func (c *SsTCPConn) Write(b []byte) (n int, err error)

type SsUDPConn

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

func (*SsUDPConn) ProxyClient

func (c *SsUDPConn) 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