2019-02-09 09:07:47 +00:00
|
|
|
package conn
|
|
|
|
|
|
|
|
import (
|
2019-02-17 11:36:48 +00:00
|
|
|
"github.com/cnlh/nps/lib/common"
|
2019-02-09 09:07:47 +00:00
|
|
|
"github.com/cnlh/nps/lib/file"
|
2019-02-17 11:36:48 +00:00
|
|
|
"github.com/cnlh/nps/lib/pool"
|
2019-02-09 09:07:47 +00:00
|
|
|
"github.com/cnlh/nps/lib/rate"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Link struct {
|
|
|
|
Id int //id
|
|
|
|
ConnType string //连接类型
|
|
|
|
Host string //目标
|
|
|
|
En int //加密
|
|
|
|
De int //解密
|
|
|
|
Crypt bool //加密
|
|
|
|
Conn *Conn
|
|
|
|
Flow *file.Flow
|
|
|
|
UdpListener *net.UDPConn
|
|
|
|
Rate *rate.Rate
|
|
|
|
UdpRemoteAddr *net.UDPAddr
|
2019-02-17 11:36:48 +00:00
|
|
|
MsgCh chan []byte
|
|
|
|
MsgConn *Conn
|
|
|
|
StatusCh chan bool
|
2019-02-09 09:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLink(id int, connType string, host string, en, de int, crypt bool, c *Conn, flow *file.Flow, udpListener *net.UDPConn, rate *rate.Rate, UdpRemoteAddr *net.UDPAddr) *Link {
|
|
|
|
return &Link{
|
|
|
|
Id: id,
|
|
|
|
ConnType: connType,
|
|
|
|
Host: host,
|
|
|
|
En: en,
|
|
|
|
De: de,
|
|
|
|
Crypt: crypt,
|
|
|
|
Conn: c,
|
|
|
|
Flow: flow,
|
|
|
|
UdpListener: udpListener,
|
|
|
|
Rate: rate,
|
|
|
|
UdpRemoteAddr: UdpRemoteAddr,
|
2019-02-17 11:36:48 +00:00
|
|
|
MsgCh: make(chan []byte),
|
|
|
|
StatusCh: make(chan bool),
|
2019-02-09 09:07:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-17 11:36:48 +00:00
|
|
|
|
|
|
|
func (s *Link) Run(flow bool) {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case content := <-s.MsgCh:
|
|
|
|
if len(content) == len(common.IO_EOF) && string(content) == common.IO_EOF {
|
|
|
|
if s.Conn != nil {
|
|
|
|
s.Conn.Close()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else {
|
2019-02-17 11:55:24 +00:00
|
|
|
if s.Conn == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-17 11:36:48 +00:00
|
|
|
if s.UdpListener != nil && s.UdpRemoteAddr != nil {
|
|
|
|
s.UdpListener.WriteToUDP(content, s.UdpRemoteAddr)
|
|
|
|
} else {
|
|
|
|
s.Conn.Write(content)
|
|
|
|
}
|
|
|
|
if flow {
|
|
|
|
s.Flow.Add(0, len(content))
|
|
|
|
}
|
|
|
|
if s.ConnType == common.CONN_UDP {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.MsgConn.WriteWriteSuccess(s.Id)
|
|
|
|
pool.PutBufPoolCopy(content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|