加入了Tunnel统计功能。

This commit is contained in:
dmy@lab
2015-11-20 14:19:10 +08:00
parent 6f8bda54e4
commit fff90f7057
5 changed files with 120 additions and 18 deletions

View File

@@ -4,10 +4,17 @@ import (
"errors"
"fmt"
"github.com/TransX/log"
"github.com/TransX/stat"
"net"
"os"
)
var tunMng *stat.TunnelStatusManager
func init() {
tunMng = stat.NewTunnelStatusManager()
}
type TransTCP struct {
}
@@ -68,12 +75,24 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer
log.Info("Dial %s", destConn.RemoteAddr().String())
//tunnel model : [ -->>server ---- client -->> ](this is a tunnel)
if clientOrServer == "client" {
go NewTunnel(listenerConn, destConn, SEND).run()
go NewTunnel(destConn, listenerConn, RECEIVE).run()
ntSend := NewTunnel(listenerConn, destConn, SEND)
ntSend.SetRegChan(tunMng.GetRegChan())
ntSend.SetUnRegChan(tunMng.GetUnregChan())
ntRev := NewTunnel(destConn, listenerConn, RECEIVE)
ntRev.SetRegChan(tunMng.GetRegChan())
ntRev.SetUnRegChan(tunMng.GetUnregChan())
go ntSend.Run()
go ntRev.Run()
}
if clientOrServer == "server" {
go NewTunnel(listenerConn, destConn, RECEIVE).run()
go NewTunnel(destConn, listenerConn, SEND).run()
ntRev := NewTunnel(listenerConn, destConn, RECEIVE)
ntRev.SetRegChan(tunMng.GetRegChan())
ntRev.SetUnRegChan(tunMng.GetUnregChan())
ntSend := NewTunnel(destConn, listenerConn, SEND)
ntSend.SetRegChan(tunMng.GetRegChan())
ntSend.SetUnRegChan(tunMng.GetUnregChan())
go ntRev.Run()
go ntSend.Run()
}
}()

View File

@@ -23,6 +23,8 @@ type Tunnel struct {
src net.Conn
dest net.Conn
cipherDirection Direction
regChan chan interface{}
unregChan chan interface{}
}
func NewTunnel(src, dest net.Conn, cipherDirection Direction) *Tunnel {
@@ -42,14 +44,26 @@ func (this *Tunnel) SetID(id string) { //rarely used
this.id = id
}
func (this *Tunnel) SetRegChan(c chan interface{}) {
this.regChan = c
}
func (this *Tunnel) SetUnRegChan(c chan interface{}) {
this.unregChan = c
}
//tunnel model : [ -->>server ---- client -->> ](this is a tunnel)
func (this *Tunnel) run() { //单向的从src发送到dest
func (this *Tunnel) Run() { //单向的从src发送到dest
//进行注册
this.regChan <- this
src := this.src
dest := this.dest
cipherDirection := this.cipherDirection
id := this.id
defer func() {
log.Info("tunnel id %s ends", id)
//注销
this.unregChan <- this
if r := recover(); r != nil {
if src != nil {
src.Close()