app

package
v0.0.0-...-1f38f91 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2019 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	COUNT_MINUTES = 60
)

Variables

View Source
var (
	PT_TCP       = "tcp"
	PT_WEBSOCKET = "websocket"

	/* 默认配置项 */
	DEFAULT_TCP_NODELAY            = true              /* tcp nodelay */
	DEFAULT_TCP_REDIRECT           = false             /* 是否向服务器发送客户端IP */
	DEFAULT_TCP_HEARTBEAT          = time.Second * 30  /* tcp代理设置的心跳时间 */
	DEFAULT_TCP_KEEPALIVE_INTERVAL = time.Second * 600 /* tcp代理设置的 keepalive 时间 */
	DEFAULT_TCP_READ_BUF_LEN       = 1024 * 4          /* tcp 接收缓冲区 */
	DEFAULT_TCP_WRITE_BUF_LEN      = 1024 * 4          /* tcp 发送缓冲区 */
	DEFAULT_TCP_READ_BLOCK_TIME    = time.Second * 35  /* tcp 读数据超时时间 */
	DEFAULT_TCP_WRITE_BLOCK_TIME   = time.Second * 5   /* tcp 写数据超时时间 */
	DEFAULT_TCP_CHECKLINE_INTERVAL = time.Second * 60  /* 线路检测周期 */
	DEFAULT_TCP_CHECKLINE_TIMEOUT  = time.Second * 10  /* 线路检测超时时间 */
)
View Source
var (
	DefaultSocketOpt = &knet.SocketOpt{
		NoDelay:           true,
		Keepalive:         false,
		ReadBufLen:        1024 * 8,
		WriteBufLen:       1024 * 8,
		ReadTimeout:       time.Second * 35,
		ReadHeaderTimeout: time.Second * 10,
		WriteTimeout:      time.Second * 5,
		MaxHeaderBytes:    4096,
	}

	DefaultUpgrader = &websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true }}
)
View Source
var (
	ErrorInvalidAddr = errors.New("Invalid Addr")
)

Functions

func Run

func Run(version string)

func Stop

func Stop()

Types

type ConnMgr

type ConnMgr struct {
	// sync.Mutex
	InNum         int64 /* 当前客户端连接数 */
	OutNum        int64 /* 当前服务端连接数 */
	SuccessNum    int64 /* 启动以来隧道成功总数 */
	FailedNum     int64 /* 启动以来隧道失败总数 */
	ClientInSize  int64 /* 启动以来客户端读总流量 */
	ClientOutSize int64 /* 启动以来客户端写总流量 */
	ServerInSize  int64 /* 启动以来服务端读总流量 */
	ServerOutSize int64 /* 启动以来服务端写总流量 */
}

func (*ConnMgr) GetClientInSize

func (mgr *ConnMgr) GetClientInSize() int64

获取启动以来客户端读总流量

func (*ConnMgr) GetClientOutSize

func (mgr *ConnMgr) GetClientOutSize() int64

获取启动以来客户端写总流量

func (*ConnMgr) GetInNum

func (mgr *ConnMgr) GetInNum() int64

获取当前客户端连接数

func (*ConnMgr) GetOutNum

func (mgr *ConnMgr) GetOutNum() int64

获取当前服务端连接数

func (*ConnMgr) GetServerInSize

func (mgr *ConnMgr) GetServerInSize() int64

获取启动以来服务端读总流量

func (*ConnMgr) GetServerOutSize

func (mgr *ConnMgr) GetServerOutSize() int64

获取启动以来服务端写总流量

func (*ConnMgr) GetSuccessNum

func (mgr *ConnMgr) GetSuccessNum() int64

获取启动以来隧道成功总数

func (*ConnMgr) GetdateFailedNum

func (mgr *ConnMgr) GetdateFailedNum(delta int64)

获取启动以来隧道失败总数

func (*ConnMgr) LogDataFlowRecord

func (mgr *ConnMgr) LogDataFlowRecord()

func (*ConnMgr) StartDataFlowRecord

func (mgr *ConnMgr) StartDataFlowRecord(interval time.Duration)

func (*ConnMgr) UpdateClientInSize

func (mgr *ConnMgr) UpdateClientInSize(delta int64)

更新启动以来客户端读总流量

func (*ConnMgr) UpdateClientOutSize

func (mgr *ConnMgr) UpdateClientOutSize(delta int64)

更新启动以来客户端写总流量

func (*ConnMgr) UpdateFailedNum

func (mgr *ConnMgr) UpdateFailedNum(delta int64)

更新启动以来隧道失败总数

func (*ConnMgr) UpdateInNum

func (mgr *ConnMgr) UpdateInNum(delta int64)

更新当前客户端连接数

func (*ConnMgr) UpdateOutNum

func (mgr *ConnMgr) UpdateOutNum(delta int64)

更新当前服务端连接数

func (*ConnMgr) UpdateServerInSize

func (mgr *ConnMgr) UpdateServerInSize(delta int64)

更新启动以来服务端读总流量

func (*ConnMgr) UpdateServerOutSize

func (mgr *ConnMgr) UpdateServerOutSize(delta int64)

更新启动以来服务端写总流量

func (*ConnMgr) UpdateSuccessNum

func (mgr *ConnMgr) UpdateSuccessNum(delta int64)

更新启动以来隧道成功总数

type FailedInMunite

type FailedInMunite struct {
	Time      time.Time
	FailedNum int64
}

type IProxy

type IProxy interface {
	GetBestLine() *Line
}

type Line

type Line struct {
	sync.RWMutex
	Running  bool          /* 线路检测是否在进行的标志 */
	Born     time.Time     /* 线路出生时间 */
	Remote   string        /* 线路指向的服务器地址 */
	Delay    time.Duration /* 线路延迟 */
	Timeout  time.Duration /* 进行线路检测时的超时时间 */
	Interval time.Duration /* 线路检测的时间周期 */
	Timer    *time.Timer   /* 用于定时进行线路检测的定时器 */
	CurLoad  int64         /* 当前线路负载 */
	MaxLoad  int64         /* 线路最大负载 */

	IsPaused bool /* 线路暂停使用的标志 */
	Redirect bool /* 线路是否需要向服务器发送客户端真实IP的标志 */

	ChUpdateDelay chan time.Duration /* 用于外部更新线路当前延迟的channel,外部进行更新后本线路的线路检测reset计时周期避免浪费 */

	FailedRecord     [COUNT_MINUTES]FailedInMunite /* 环形队列,记录过去COUNT_MINUTES分钟内连接失败次数 */
	FailedRecordHead int                           /* 环形队列头 */
}

线路

func NewLine

func NewLine(remote string, timeout time.Duration, interval time.Duration, maxLoad int64, redirect bool) *Line

创建新线路

func (*Line) CheckLine

func (line *Line) CheckLine(now time.Time)

func (*Line) GetFailedInLastNMinutes

func (line *Line) GetFailedInLastNMinutes(n int) int64

获取近期n分钟内为客户端与服务器建立连接的失败次数

func (*Line) HandleRedirect

func (line *Line) HandleRedirect(conn *net.TCPConn, addr string) error

根据线路配置决定是否向服务器发送重定向包,应在刚建立与服务器的连接时首先进行然后再发送其他包

func (*Line) Pause

func (line *Line) Pause()

暂停在此线路选路和进行代理连接

func (*Line) Score

func (line *Line) Score() int64

线路分数,小于0为线路不可用

func (*Line) Start

func (line *Line) Start(idx int)

启动线路检测

func (*Line) Stop

func (line *Line) Stop()

停止线路检测

func (*Line) UnPause

func (line *Line) UnPause()

恢复在此线路选路和进行代理连接

func (*Line) UpdateDelay

func (line *Line) UpdateDelay(delay time.Duration)

更新线路延迟

func (*Line) UpdateFailedNum

func (line *Line) UpdateFailedNum(delta int64)

更新为客户端与服务器建立连接的失败总次数,以及近期每分钟内的失败次数记录

func (*Line) UpdateLoad

func (line *Line) UpdateLoad(delta int64)

更新负载

type ProxyBase

type ProxyBase struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

每个 ProxyBase 管理一组 Line ,Proxy is a ProxyBase

func (*ProxyBase) AddLine

func (mgr *ProxyBase) AddLine(addr string, timeout time.Duration, interval time.Duration, maxLoad int64, redirect bool)

添加一个 Line

func (*ProxyBase) GetBestLine

func (mgr *ProxyBase) GetBestLine() *Line

当前最适合的线路

func (*ProxyBase) GetBestLineWithoutLock

func (mgr *ProxyBase) GetBestLineWithoutLock() *Line

当前最适合的线路

func (*ProxyBase) StartCheckLines

func (mgr *ProxyBase) StartCheckLines()

开始检查所有 Line 状况

func (*ProxyBase) StopCheckLines

func (mgr *ProxyBase) StopCheckLines()

停止检查所有 Line 状况

type ProxyMgr

type ProxyMgr struct {
	Proxys map[string]IProxy
}

func (*ProxyMgr) AddProxy

func (mgr *ProxyMgr) AddProxy(name string, proxy IProxy)

func (*ProxyMgr) InitPorxy

func (mgr *ProxyMgr) InitPorxy()

type ProxyTcp

type ProxyTcp struct {
	*ProxyBase
	Running       bool
	Listener      *net.TCPListener
	Heartbeat     time.Duration
	AliveTime     time.Duration
	RecvBlockTime time.Duration
	RecvBufLen    int
	SendBlockTime time.Duration
	SendBufLen    int
	Nodelay       bool

	ConnCount uint64
}

tcp 代理

func NewTcpProxy

func NewTcpProxy(name string, local string) *ProxyTcp

func (*ProxyTcp) InitConn

func (ptcp *ProxyTcp) InitConn(conn *net.TCPConn) bool

func (*ProxyTcp) OnNew

func (ptcp *ProxyTcp) OnNew(clientConn *net.TCPConn)

func (*ProxyTcp) Start

func (ptcp *ProxyTcp) Start()

func (*ProxyTcp) Stop

func (ptcp *ProxyTcp) Stop()

type ProxyWebsocket

type ProxyWebsocket struct {
	*ProxyBase
	Running       bool
	EnableTls     bool
	Listener      *net.TCPListener
	Heartbeat     time.Duration
	AliveTime     time.Duration
	RecvBlockTime time.Duration
	RecvBufLen    int
	SendBlockTime time.Duration
	SendBufLen    int
	Nodelay       bool
	ConnCount     uint64
	Certs         []XMLCert
	Routes        map[string]func(w http.ResponseWriter, r *http.Request)
}

websocket 代理

func NewWebsocketProxy

func NewWebsocketProxy(name string, local string, paths []string, tls bool, certs []XMLCert) *ProxyWebsocket

func (*ProxyWebsocket) InitConn

func (pws *ProxyWebsocket) InitConn(conn *net.TCPConn) bool

func (*ProxyWebsocket) OnNew

func (pws *ProxyWebsocket) OnNew(w http.ResponseWriter, r *http.Request)

func (*ProxyWebsocket) ServeHTTP

func (pws *ProxyWebsocket) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*ProxyWebsocket) Start

func (pws *ProxyWebsocket) Start()

func (*ProxyWebsocket) Stop

func (pws *ProxyWebsocket) Stop()

type XMLCert

type XMLCert struct {
	XMLName  xml.Name `xml:"cert"`
	Certfile string   `xml:"certfile,attr"`
	Keyfile  string   `xml:"keyfile,attr"`
}

type XMLConfig

type XMLConfig struct {
	XMLName xml.Name   `xml:"setting"`
	Options XMLOptions `xml:"options"`
	Proxy   XMLProxy   `xml:"proxy"`
}

type XMLHeartbeat

type XMLHeartbeat struct {
	XMLName  xml.Name `xml:"heartbeat"`
	Interval int      `xml:"interval,attr"`
	Timeout  int      `xml:"timeout,attr"`
}

type XMLLine

type XMLLine struct {
	XMLName  xml.Name `xml:"line"`
	Name     string   `xml:"name,attr"`
	Addr     string   `xml:"addr,attr"`
	Type     string   `xml:"type,attr"`
	Redirect string   `xml:"redirect,attr"`
	// Maxload  int64     `xml:"maxload,attr"`
	TLS    bool       `xml:"tls,attr"`
	Routes []XMLRoute `xml:"route"`
	Certs  []XMLCert  `xml:"cert"`
	Nodes  []XMLNode  `xml:"node"`
}

type XMLNode

type XMLNode struct {
	XMLName xml.Name `xml:"node"`
	Addr    string   `xml:"addr,attr"`
	Maxload int64    `xml:"maxload,attr"`
	// contains filtered or unexported fields
}

type XMLOptions

type XMLOptions struct {
	XMLName   xml.Name     `xml:"options"`
	Debug     bool         `xml:"debug,attr"`
	LogDir    string       `xml:"logdir,attr"`
	Redirect  bool         `xml:"redirect,attr"`
	Heartbeat XMLHeartbeat `xml:"heartbeat"`
}

type XMLProxy

type XMLProxy struct {
	XMLName xml.Name  `xml:"proxy"`
	Lines   []XMLLine `xml:"line"`
}

type XMLRoute

type XMLRoute struct {
	XMLName xml.Name `xml:"route"`
	Path    string   `xml:"path,attr"`
}

Jump to

Keyboard shortcuts

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