tunnel

package module
v0.0.0-...-9513d3d Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2019 License: MIT Imports: 8 Imported by: 7

README

go-tunnel

供Go语言使用的全双工TCP隧道。可以用它对接两个TCP连接,使数据在其间透明流动。

特性

  • 支持流量整形
  • 支持加密
  • 自带对象池

基本使用

var newTunnel = NewTunnel(opts... Option) // NewTunnel会自动从对象池中获取元素
newTunnel.Update(opts... Option)          // 更新Tunnel的属性,如果调用NewTunnel时传入opts,可以省略此步骤
newTunnel.Flow()                          // 数据开始流动,此函数阻塞
newTunnel.Close()                         // 关闭,这会中断数据的流动

流量整形

Tunnel.Update(
    WithSpeedLimiter(limiter go.uber.org/ratelimit.Limiter),
)

加密

以函数指针的方式提供通用的加密解密接口:

Tunnel.Update(
    WithLeftCipher(cipher0),
    WithRightCipher(cipher1),
)

对象池

可通过以下函数重复利用Tunnel:

func PutTunnel(*Tunnel){} // 归还Tunnel,此函数会自动调用 Tunnel.Close()

Documentation

Index

Constants

View Source
const (
	Left direction = iota
	Right
)

隧道的方向

Variables

This section is empty.

Functions

func PutTunnel

func PutTunnel(tunnel *Tunnel)

PutTunnel 将*Tunnel放回TunnelPool

Types

type Option

type Option func(*Tunnel)

Option 选项

func WithLeft

func WithLeft(left net.Conn) Option

WithLeft 带有左连接

func WithLeftCipher

func WithLeftCipher(c mycipher.Cipher) Option

WithLeftCipher 带有左连接的Cipher

func WithRight

func WithRight(right net.Conn) Option

WithRight 带有右连接

func WithRightCipher

func WithRightCipher(c mycipher.Cipher) Option

WithRightCipher 带有右连接的Cipher

func WithSpeedLimiter

func WithSpeedLimiter(l ratelimit.Limiter) Option

WithSpeedLimiter 带有速度限制器

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout 带有超时长度

type Tunnel

type Tunnel struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Tunnel 是一个全双工的双向隧道,内置加密解密、暂停等待的控制器

func NewTunnel

func NewTunnel(opts ...Option) *Tunnel

NewTunnel 构建新的Tunnel

func (*Tunnel) Clear

func (t *Tunnel) Clear()

Clear 清空Tunnel

func (*Tunnel) Close

func (t *Tunnel) Close()

Close 关闭Tunnel,关闭前会停止其双向的流动

func (*Tunnel) Closed

func (t *Tunnel) Closed() bool

Closed Tunnel是否已经关闭

func (*Tunnel) Flow

func (t *Tunnel) Flow()

Flow 开始双向流动 此方法阻塞 同一个Tunnel同时只能执行一次Flow,多次Flow会导致panic

func (*Tunnel) IsNil

func (t *Tunnel) IsNil() bool

IsNil Tunnel的Left和Right都为nil

func (*Tunnel) Left

func (t *Tunnel) Left() net.Conn

Left 左边的连接

func (*Tunnel) ReadLeft

func (t *Tunnel) ReadLeft(buffer []byte) (int, error)

ReadLeft 从左边读取数据

func (*Tunnel) ReadLeftStr

func (t *Tunnel) ReadLeftStr() (text string, err error)

ReadLeftStr 从左边读字符串

func (*Tunnel) ReadRight

func (t *Tunnel) ReadRight(buffer []byte) (int, error)

ReadRight 从右边读取数据

func (*Tunnel) ReadRightStr

func (t *Tunnel) ReadRightStr() (text string, err error)

ReadRightStr 从左边读字符串

func (*Tunnel) Right

func (t *Tunnel) Right() net.Conn

Right 右边的连接

func (*Tunnel) Update

func (t *Tunnel) Update(opts ...Option) *Tunnel

Update 更新Tunnel的属性

func (*Tunnel) WriteLeft

func (t *Tunnel) WriteLeft(buffer []byte) (int, error)

WriteLeft 向左边写数据

func (*Tunnel) WriteRight

func (t *Tunnel) WriteRight(buffer []byte) (int, error)

WriteRight 向右边写数据

Jump to

Keyboard shortcuts

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