2019-01-09 12:33:00 +00:00
|
|
|
|
package bridge
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2019-02-03 09:25:00 +00:00
|
|
|
|
"github.com/cnlh/nps/lib"
|
2019-01-09 12:33:00 +00:00
|
|
|
|
"net"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2019-01-31 18:06:30 +00:00
|
|
|
|
type Client struct {
|
2019-02-03 09:25:00 +00:00
|
|
|
|
tunnel *lib.Conn
|
|
|
|
|
signal *lib.Conn
|
|
|
|
|
linkMap map[int]*lib.Link
|
2019-01-31 18:06:30 +00:00
|
|
|
|
linkStatusMap map[int]bool
|
|
|
|
|
stop chan bool
|
|
|
|
|
sync.RWMutex
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 09:27:28 +00:00
|
|
|
|
type Bridge struct {
|
2019-01-31 18:06:30 +00:00
|
|
|
|
TunnelPort int //通信隧道端口
|
|
|
|
|
listener *net.TCPListener //server端监听
|
|
|
|
|
Client map[int]*Client
|
2019-01-25 04:10:12 +00:00
|
|
|
|
RunList map[int]interface{} //运行中的任务
|
2019-01-09 12:33:00 +00:00
|
|
|
|
lock sync.Mutex
|
|
|
|
|
tunnelLock sync.Mutex
|
2019-01-31 18:06:30 +00:00
|
|
|
|
clientLock sync.Mutex
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 09:27:28 +00:00
|
|
|
|
func NewTunnel(tunnelPort int, runList map[int]interface{}) *Bridge {
|
|
|
|
|
t := new(Bridge)
|
2019-01-09 12:33:00 +00:00
|
|
|
|
t.TunnelPort = tunnelPort
|
2019-01-31 18:06:30 +00:00
|
|
|
|
t.Client = make(map[int]*Client)
|
2019-01-09 12:33:00 +00:00
|
|
|
|
t.RunList = runList
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 09:27:28 +00:00
|
|
|
|
func (s *Bridge) StartTunnel() error {
|
2019-01-09 12:33:00 +00:00
|
|
|
|
var err error
|
|
|
|
|
s.listener, err = net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP("0.0.0.0"), s.TunnelPort, ""})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
go s.tunnelProcess()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//tcp server
|
2019-01-26 09:27:28 +00:00
|
|
|
|
func (s *Bridge) tunnelProcess() error {
|
2019-01-09 12:33:00 +00:00
|
|
|
|
var err error
|
|
|
|
|
for {
|
|
|
|
|
conn, err := s.listener.Accept()
|
|
|
|
|
if err != nil {
|
2019-02-03 09:25:00 +00:00
|
|
|
|
lib.Println(err)
|
2019-01-09 12:33:00 +00:00
|
|
|
|
continue
|
|
|
|
|
}
|
2019-02-03 09:25:00 +00:00
|
|
|
|
go s.cliProcess(lib.NewConn(conn))
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//验证失败,返回错误验证flag,并且关闭连接
|
2019-02-03 09:25:00 +00:00
|
|
|
|
func (s *Bridge) verifyError(c *lib.Conn) {
|
|
|
|
|
c.Write([]byte(lib.VERIFY_EER))
|
2019-01-09 12:33:00 +00:00
|
|
|
|
c.Conn.Close()
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 09:25:00 +00:00
|
|
|
|
func (s *Bridge) cliProcess(c *lib.Conn) {
|
2019-01-28 06:45:55 +00:00
|
|
|
|
c.SetReadDeadline(5)
|
|
|
|
|
var buf []byte
|
|
|
|
|
var err error
|
|
|
|
|
if buf, err = c.ReadLen(32); err != nil {
|
|
|
|
|
c.Close()
|
|
|
|
|
return
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
2019-01-28 06:45:55 +00:00
|
|
|
|
//验证
|
2019-02-03 09:25:00 +00:00
|
|
|
|
id, err := lib.GetCsvDb().GetIdByVerifyKey(string(buf), c.Conn.RemoteAddr().String())
|
2019-01-25 04:10:12 +00:00
|
|
|
|
if err != nil {
|
2019-02-03 09:25:00 +00:00
|
|
|
|
lib.Println("当前客户端连接校验错误,关闭此客户端:", c.Conn.RemoteAddr())
|
2019-01-09 12:33:00 +00:00
|
|
|
|
s.verifyError(c)
|
2019-01-28 06:45:55 +00:00
|
|
|
|
return
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
//做一个判断 添加到对应的channel里面以供使用
|
2019-01-28 06:45:55 +00:00
|
|
|
|
if flag, err := c.ReadFlag(); err == nil {
|
|
|
|
|
s.typeDeal(flag, c, id)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Bridge) closeClient(id int) {
|
2019-01-31 18:06:30 +00:00
|
|
|
|
s.clientLock.Lock()
|
|
|
|
|
defer s.clientLock.Unlock()
|
|
|
|
|
if v, ok := s.Client[id]; ok {
|
|
|
|
|
v.signal.WriteClose()
|
|
|
|
|
delete(s.Client, id)
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//tcp连接类型区分
|
2019-02-03 09:25:00 +00:00
|
|
|
|
func (s *Bridge) typeDeal(typeVal string, c *lib.Conn, id int) {
|
2019-01-09 12:33:00 +00:00
|
|
|
|
switch typeVal {
|
2019-02-03 09:25:00 +00:00
|
|
|
|
case lib.WORK_MAIN:
|
2019-01-28 06:45:55 +00:00
|
|
|
|
//客户端已经存在,下线
|
2019-01-31 18:06:30 +00:00
|
|
|
|
s.clientLock.Lock()
|
|
|
|
|
if _, ok := s.Client[id]; ok {
|
|
|
|
|
s.clientLock.Unlock()
|
2019-01-28 06:45:55 +00:00
|
|
|
|
s.closeClient(id)
|
2019-01-31 18:06:30 +00:00
|
|
|
|
} else {
|
|
|
|
|
s.clientLock.Unlock()
|
|
|
|
|
}
|
|
|
|
|
s.clientLock.Lock()
|
|
|
|
|
|
|
|
|
|
s.Client[id] = &Client{
|
2019-02-03 09:25:00 +00:00
|
|
|
|
linkMap: make(map[int]*lib.Link),
|
2019-01-31 18:06:30 +00:00
|
|
|
|
stop: make(chan bool),
|
|
|
|
|
linkStatusMap: make(map[int]bool),
|
2019-01-28 06:45:55 +00:00
|
|
|
|
}
|
2019-02-03 09:25:00 +00:00
|
|
|
|
lib.Printf("客户端%d连接成功,地址为:%s", id, c.Conn.RemoteAddr())
|
2019-01-31 18:06:30 +00:00
|
|
|
|
s.Client[id].signal = c
|
|
|
|
|
s.clientLock.Unlock()
|
|
|
|
|
go s.GetStatus(id)
|
2019-02-03 09:25:00 +00:00
|
|
|
|
case lib.WORK_CHAN:
|
2019-01-31 18:06:30 +00:00
|
|
|
|
s.clientLock.Lock()
|
|
|
|
|
if v, ok := s.Client[id]; ok {
|
|
|
|
|
s.clientLock.Unlock()
|
|
|
|
|
v.tunnel = c
|
|
|
|
|
} else {
|
|
|
|
|
s.clientLock.Unlock()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
go s.clientCopy(id)
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
c.SetAlive()
|
2019-01-28 06:45:55 +00:00
|
|
|
|
return
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 18:06:30 +00:00
|
|
|
|
//等待
|
|
|
|
|
func (s *Bridge) waitStatus(clientId, id int) (bool) {
|
|
|
|
|
ticker := time.NewTicker(time.Millisecond * 100)
|
|
|
|
|
stop := time.After(time.Second * 10)
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ticker.C:
|
|
|
|
|
s.clientLock.Lock()
|
|
|
|
|
if v, ok := s.Client[clientId]; ok {
|
|
|
|
|
s.clientLock.Unlock()
|
|
|
|
|
v.Lock()
|
|
|
|
|
if vv, ok := v.linkStatusMap[id]; ok {
|
|
|
|
|
ticker.Stop()
|
|
|
|
|
v.Unlock()
|
|
|
|
|
return vv
|
|
|
|
|
}
|
|
|
|
|
v.Unlock()
|
|
|
|
|
} else {
|
|
|
|
|
s.clientLock.Unlock()
|
|
|
|
|
}
|
|
|
|
|
case <-stop:
|
|
|
|
|
return false
|
|
|
|
|
}
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
2019-01-31 18:06:30 +00:00
|
|
|
|
return false
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 09:25:00 +00:00
|
|
|
|
func (s *Bridge) SendLinkInfo(clientId int, link *lib.Link) (tunnel *lib.Conn, err error) {
|
2019-01-31 18:06:30 +00:00
|
|
|
|
s.clientLock.Lock()
|
|
|
|
|
if v, ok := s.Client[clientId]; ok {
|
|
|
|
|
s.clientLock.Unlock()
|
|
|
|
|
v.signal.SendLinkInfo(link)
|
|
|
|
|
if err != nil {
|
2019-02-03 09:25:00 +00:00
|
|
|
|
lib.Println("send error:", err, link.Id)
|
2019-01-31 18:06:30 +00:00
|
|
|
|
s.DelClient(clientId)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if v.tunnel == nil {
|
|
|
|
|
err = errors.New("tunnel获取错误")
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
tunnel = v.tunnel
|
|
|
|
|
}
|
|
|
|
|
v.Lock()
|
|
|
|
|
v.linkMap[link.Id] = link
|
|
|
|
|
v.Unlock()
|
|
|
|
|
if !s.waitStatus(clientId, link.Id) {
|
|
|
|
|
err = errors.New("连接失败")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
s.clientLock.Unlock()
|
|
|
|
|
err = errors.New("客户端未连接")
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 18:06:30 +00:00
|
|
|
|
//得到一个tcp隧道
|
2019-02-03 09:25:00 +00:00
|
|
|
|
func (s *Bridge) GetTunnel(id int, en, de int, crypt, mux bool) (conn *lib.Conn, err error) {
|
2019-01-31 18:06:30 +00:00
|
|
|
|
s.clientLock.Lock()
|
|
|
|
|
defer s.clientLock.Unlock()
|
|
|
|
|
if v, ok := s.Client[id]; !ok {
|
2019-01-09 12:33:00 +00:00
|
|
|
|
err = errors.New("客户端未连接")
|
2019-01-31 18:06:30 +00:00
|
|
|
|
} else {
|
|
|
|
|
conn = v.tunnel
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 18:06:30 +00:00
|
|
|
|
//得到一个通信通道
|
2019-02-03 09:25:00 +00:00
|
|
|
|
func (s *Bridge) GetSignal(id int) (conn *lib.Conn, err error) {
|
2019-01-31 18:06:30 +00:00
|
|
|
|
s.clientLock.Lock()
|
|
|
|
|
defer s.clientLock.Unlock()
|
|
|
|
|
if v, ok := s.Client[id]; !ok {
|
|
|
|
|
err = errors.New("客户端未连接")
|
|
|
|
|
} else {
|
|
|
|
|
conn = v.signal
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
2019-01-31 18:06:30 +00:00
|
|
|
|
return
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//删除通信通道
|
2019-01-31 18:06:30 +00:00
|
|
|
|
func (s *Bridge) DelClient(id int) {
|
|
|
|
|
s.closeClient(id)
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 18:06:30 +00:00
|
|
|
|
func (s *Bridge) verify(id int) bool {
|
|
|
|
|
for k := range s.RunList {
|
|
|
|
|
if k == id {
|
|
|
|
|
return true
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-31 18:06:30 +00:00
|
|
|
|
return false
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
2019-01-31 18:06:30 +00:00
|
|
|
|
func (s *Bridge) GetStatus(clientId int) {
|
|
|
|
|
s.clientLock.Lock()
|
|
|
|
|
client := s.Client[clientId]
|
|
|
|
|
s.clientLock.Unlock()
|
2019-01-09 12:33:00 +00:00
|
|
|
|
|
2019-01-31 18:06:30 +00:00
|
|
|
|
if client == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-01-09 12:33:00 +00:00
|
|
|
|
for {
|
2019-01-31 18:06:30 +00:00
|
|
|
|
if id, status, err := client.signal.GetConnStatus(); err != nil {
|
|
|
|
|
s.closeClient(clientId)
|
2019-01-12 16:09:12 +00:00
|
|
|
|
return
|
2019-01-31 18:06:30 +00:00
|
|
|
|
} else {
|
|
|
|
|
client.Lock()
|
|
|
|
|
client.linkStatusMap[id] = status
|
|
|
|
|
client.Unlock()
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 18:06:30 +00:00
|
|
|
|
func (s *Bridge) clientCopy(clientId int) {
|
|
|
|
|
|
|
|
|
|
s.clientLock.Lock()
|
|
|
|
|
client := s.Client[clientId]
|
|
|
|
|
s.clientLock.Unlock()
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
if id, err := client.tunnel.GetLen(); err != nil {
|
|
|
|
|
s.closeClient(clientId)
|
2019-02-03 09:25:00 +00:00
|
|
|
|
lib.Println("读取msg id 错误", err, id)
|
2019-01-31 18:06:30 +00:00
|
|
|
|
break
|
|
|
|
|
} else {
|
|
|
|
|
client.Lock()
|
|
|
|
|
if link, ok := client.linkMap[id]; ok {
|
|
|
|
|
client.Unlock()
|
|
|
|
|
if content, err := client.tunnel.GetMsgContent(link); err != nil {
|
2019-02-03 09:25:00 +00:00
|
|
|
|
lib.PutBufPoolCopy(content)
|
2019-01-31 18:06:30 +00:00
|
|
|
|
s.closeClient(clientId)
|
2019-02-03 09:25:00 +00:00
|
|
|
|
lib.Println("read msg content error", err, "close client")
|
2019-01-31 18:06:30 +00:00
|
|
|
|
break
|
|
|
|
|
} else {
|
2019-02-03 09:25:00 +00:00
|
|
|
|
if len(content) == len(lib.IO_EOF) && string(content) == lib.IO_EOF {
|
2019-01-31 18:06:30 +00:00
|
|
|
|
if link.Conn != nil {
|
|
|
|
|
link.Conn.Close()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if link.UdpListener != nil && link.UdpRemoteAddr != nil {
|
|
|
|
|
link.UdpListener.WriteToUDP(content, link.UdpRemoteAddr)
|
|
|
|
|
} else {
|
|
|
|
|
link.Conn.Write(content)
|
|
|
|
|
}
|
|
|
|
|
link.Flow.Add(0, len(content))
|
|
|
|
|
}
|
2019-02-03 09:25:00 +00:00
|
|
|
|
lib.PutBufPoolCopy(content)
|
2019-01-31 18:06:30 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
client.Unlock()
|
|
|
|
|
continue
|
|
|
|
|
}
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-31 18:06:30 +00:00
|
|
|
|
|
2019-01-09 12:33:00 +00:00
|
|
|
|
}
|