This commit is contained in:
dmy@lab
2015-11-23 13:50:34 +08:00
parent 67a00e7902
commit 825d0f2501
2 changed files with 127 additions and 46 deletions

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"crypto/md5"
"encoding/hex"
// "fmt"
"github.com/TransX/log"
"github.com/TransX/tscipher"
"net"
@@ -56,12 +57,72 @@ func (this *Tunnel) SetUnRegChan(c chan interface{}) {
func (this *Tunnel) Run() { //单向的从src发送到dest
//进行注册
this.regChan <- this
src := this.src
dest := this.dest
// cipherDirection := this.cipherDirection
id := this.id
// cache := make([]byte, 1024*4) //4kB
//构建Carrier
queCache := tscipher.NewQueueCache(1)
revCarrier := tscipher.NewCarrier(src, tscipher.NewCipher("XOR"), queCache, id)
sendCarrier := tscipher.NewCarrier(dest, tscipher.NewCipher("XOR"), queCache, id)
//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() {
log.Debug("tunnel id %s ends", id)
// 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()
}
}
}()
// srTimer := time.Now() //send receive timer
var n int
var err error
for {
rTimer := time.Now() //receive timer
if cipherDirection != RECEIVE {
revCarrier.Cipher = nil
n, err = tscipher.RowReceiveData(revCarrier)
} else {
n, err = tscipher.ReceiveData(revCarrier)
}
// fmt.Println("receive")
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())
}
log.Debug("Reived %d bytes from %s. Tunnel: id %s", n, src.RemoteAddr().String(), id)
// 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 {
@@ -73,41 +134,21 @@ func (this *Tunnel) Run() { //单向的从src发送到dest
}
}
}()
cache := make([]byte, 1024*4) //4kB
//构建Carrier
revCarrier := tscipher.NewCarrier(src, tscipher.NewCipher("XOR"), cache, id)
sendCarrier := tscipher.NewCarrier(dest, tscipher.NewCipher("XOR"), cache, id)
//timer
if cipherDirection != SEND {
sendCarrier.Cipher = nil
}
for {
srTimer := time.Now() //send receive timer
var nByte int
var err error
rTimer := time.Now() //receive timer
if cipherDirection != RECEIVE {
revCarrier.Cipher = nil
nByte, err = tscipher.RowReceiveData(revCarrier)
} else {
nByte, err = tscipher.ReceiveData(revCarrier)
}
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())
}
log.Debug("Reived %d bytes from %s. Tunnel: id %s", nByte, src.RemoteAddr().String(), id)
if cipherDirection != SEND {
sendCarrier.Cipher = nil
}
// nByte := <-nCh
// fmt.Println("in send loop\n")
sTimer := time.Now() //send timer
n, err := tscipher.SendData(sendCarrier, nByte)
_, err := tscipher.SendData(sendCarrier)
// fmt.Println("send")
log.Info("id %s time to sned %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())
}
log.Debug("Write %d bytes from %s to %s. Tunnel: %s . 18 bytes %x", n, dest.LocalAddr(), dest.RemoteAddr().String(), id, sendCarrier.Cache[:18])
log.Info("id %s send %d /receive %d duration %d ms", n, nByte, id, time.Since(srTimer)/1000)
// 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 {