transx/protocol/tunnel.go

162 lines
3.9 KiB
Go
Raw Normal View History

2015-11-18 23:16:57 +08:00
package protocol
import (
"bytes"
"crypto/md5"
"encoding/hex"
2015-11-23 13:50:34 +08:00
// "fmt"
2016-01-10 13:44:02 +08:00
"github.com/TransX/cache"
"github.com/TransX/log"
"github.com/TransX/tscipher"
"net"
"strconv"
"sync/atomic"
"time"
)
var seed int32
func init() {
seed = 0
}
type Tunnel struct {
id string
src net.Conn
dest net.Conn
2015-11-13 15:44:17 +08:00
cipherDirection Direction
2015-11-20 14:19:10 +08:00
regChan chan interface{}
unregChan chan interface{}
}
2015-11-13 15:44:17 +08:00
func NewTunnel(src, dest net.Conn, cipherDirection Direction) *Tunnel {
return &Tunnel{
id: tunnelID(),
src: src,
dest: dest,
cipherDirection: cipherDirection,
}
}
func (this *Tunnel) GetID(id string) string {
return this.id
}
func (this *Tunnel) SetID(id string) { //rarely used
this.id = id
}
2015-11-20 14:19:10 +08:00
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)
2015-11-20 14:19:10 +08:00
func (this *Tunnel) Run() { //单向的从src发送到dest
//进行注册
this.regChan <- this
2015-11-23 13:50:34 +08:00
src := this.src
dest := this.dest
// cipherDirection := this.cipherDirection
id := this.id
// cache := make([]byte, 1024*4) //4kB
//构建Carrier
2016-01-10 13:44:02 +08:00
queCache := cache.NewUnblockingQueueCache(1)
msg := cache.NewBlockingQueueCache(1)
revCarrier := tscipher.NewCarrier(src, tscipher.NewCipher("XOR"), queCache, msg, id)
sendCarrier := tscipher.NewCarrier(dest, tscipher.NewCipher("XOR"), queCache, msg, id)
2015-11-23 13:50:34 +08:00
//timer
// for {
// nCh := make(chan int)
go this.receive(revCarrier)
go this.send(sendCarrier)
// log.Info("id %s send %d /receive %d duration %d ms", n, nByte, id, time.Since(srTimer)/1000)
// }
}
func (this *Tunnel) receive(revCarrier *tscipher.Carrier) {
src := this.src
dest := this.dest
cipherDirection := this.cipherDirection
id := this.id
defer func() {
2015-11-23 13:50:34 +08:00
// log.Debug("tunnel id %s ends", id)
2015-11-20 14:19:10 +08:00
//注销
2015-11-23 13:50:34 +08:00
// this.unregChan <- this
if r := recover(); r != nil {
if src != nil {
src.Close()
}
if dest != nil {
dest.Close()
}
}
}()
2015-11-23 13:50:34 +08:00
// srTimer := time.Now() //send receive timer
var n int
var err error
for {
2015-11-21 16:54:04 +08:00
rTimer := time.Now() //receive timer
2015-11-13 15:44:17 +08:00
if cipherDirection != RECEIVE {
revCarrier.Cipher = nil
2015-11-23 13:50:34 +08:00
n, err = tscipher.RowReceiveData(revCarrier)
} else {
2015-11-23 13:50:34 +08:00
n, err = tscipher.ReceiveData(revCarrier)
}
2015-11-23 13:50:34 +08:00
// fmt.Println("receive")
2015-11-21 16:54:04 +08:00
log.Info("id %s time to receive %d", id, time.Since(rTimer)/1000)
if err != nil {
log.Panic("Read panic. Tunnel id: %s. Remote Add: %s Local: %s. Err:%s", id, src.RemoteAddr().String(), src.LocalAddr().String(), err.Error())
}
2016-01-10 13:44:02 +08:00
log.Debug("Reived %d bytes (local add %s) from %s. Tunnel: id %s", n, src.LocalAddr().String(),src.RemoteAddr().String(), id)
2015-11-23 13:50:34 +08:00
// nCh <- n
}
}
func (this *Tunnel) send(sendCarrier *tscipher.Carrier) {
src := this.src
dest := this.dest
cipherDirection := this.cipherDirection
id := this.id
defer func() {
// log.Debug("tunnel id %s ends", id)
//注销
this.unregChan <- this
if r := recover(); r != nil {
if src != nil {
src.Close()
}
if dest != nil {
dest.Close()
}
}
2015-11-23 13:50:34 +08:00
}()
if cipherDirection != SEND {
sendCarrier.Cipher = nil
}
for {
// nByte := <-nCh
// fmt.Println("in send loop\n")
2015-11-21 16:54:04 +08:00
sTimer := time.Now() //send timer
2015-11-23 13:50:34 +08:00
_, err := tscipher.SendData(sendCarrier)
// fmt.Println("send")
2016-01-10 13:44:02 +08:00
log.Info("id %s time to send %d", id, time.Since(sTimer)/1000)
if err != nil {
log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String())
}
2015-11-23 13:50:34 +08:00
// log.Debug("Write %d bytes from %s to %s. Tunnel: %s . 18 bytes %x", n, dest.LocalAddr(), dest.RemoteAddr().String(), id, sendCarrier.Cache[:18])
}
}
func tunnelID() string {
nowString := time.Now().String() + strconv.Itoa(int(seed))
atomic.AddInt32(&seed, 1) //避免多线程情况下获得的种子相同
md5Byte := md5.Sum(bytes.NewBufferString(nowString).Bytes())
return hex.EncodeToString(md5Byte[:])
}