2016-01-11 21:29:48 +08:00
|
|
|
|
package model
|
2015-10-24 16:30:10 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2016-01-11 00:00:30 +08:00
|
|
|
|
|
2015-11-23 13:50:34 +08:00
|
|
|
|
// "fmt"
|
2016-01-10 13:44:02 +08:00
|
|
|
|
"github.com/TransX/cache"
|
2016-01-11 21:29:48 +08:00
|
|
|
|
"github.com/TransX/constant"
|
2015-10-24 16:30:10 +08:00
|
|
|
|
"github.com/TransX/log"
|
|
|
|
|
|
"github.com/TransX/tscipher"
|
2016-01-17 22:34:26 +08:00
|
|
|
|
"github.com/spf13/viper"
|
2015-10-24 16:30:10 +08:00
|
|
|
|
"net"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Tunnel struct {
|
|
|
|
|
|
id string
|
|
|
|
|
|
src net.Conn
|
|
|
|
|
|
dest net.Conn
|
2016-01-11 21:29:48 +08:00
|
|
|
|
cipherDirection constant.Direction
|
2015-11-20 14:19:10 +08:00
|
|
|
|
regChan chan interface{}
|
|
|
|
|
|
unregChan chan interface{}
|
2016-01-14 21:58:41 +08:00
|
|
|
|
unregistered bool
|
2015-10-24 16:30:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-01-11 21:29:48 +08:00
|
|
|
|
func NewTunnel(id string, src, dest net.Conn, cipherDirection constant.Direction) *Tunnel {
|
2015-10-24 16:30:10 +08:00
|
|
|
|
return &Tunnel{
|
2016-01-11 00:00:30 +08:00
|
|
|
|
id: id,
|
2015-10-24 16:30:10 +08:00
|
|
|
|
src: src,
|
|
|
|
|
|
dest: dest,
|
|
|
|
|
|
cipherDirection: cipherDirection,
|
2016-01-14 21:58:41 +08:00
|
|
|
|
unregistered: false,
|
2015-10-24 16:30:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-01-11 21:29:48 +08:00
|
|
|
|
func (this *Tunnel) GetID() string {
|
2015-10-24 16:30:10 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-24 16:30:10 +08:00
|
|
|
|
//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
|
|
|
|
|
|
id := this.id
|
|
|
|
|
|
//构建Carrier
|
2016-01-17 22:34:26 +08:00
|
|
|
|
queLength := viper.GetInt("queueLength")
|
|
|
|
|
|
queCache := cache.NewBlockingQueueCache(queLength)
|
|
|
|
|
|
cacheSize := viper.GetInt("cacheSize")
|
|
|
|
|
|
for i := 0; i < queLength; i++ {
|
|
|
|
|
|
queCache.Put(make([]byte, cacheSize), 0)
|
2016-01-11 00:00:30 +08:00
|
|
|
|
}
|
2016-01-17 22:34:26 +08:00
|
|
|
|
msg := cache.NewBlockingQueueCache(queLength)
|
2016-01-10 13:44:02 +08:00
|
|
|
|
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
|
|
|
|
go this.receive(revCarrier)
|
|
|
|
|
|
go this.send(sendCarrier)
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-01-14 22:03:54 +08:00
|
|
|
|
func (this *Tunnel) onError() {
|
2015-10-24 16:30:10 +08:00
|
|
|
|
src := this.src
|
|
|
|
|
|
dest := this.dest
|
2016-01-14 22:03:54 +08:00
|
|
|
|
//注销
|
|
|
|
|
|
if !this.unregistered { // 应该不存在异步问题
|
|
|
|
|
|
this.unregChan <- this
|
|
|
|
|
|
this.unregistered = true
|
|
|
|
|
|
}
|
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
|
if src != nil {
|
|
|
|
|
|
src.Close()
|
2016-01-14 21:58:41 +08:00
|
|
|
|
}
|
2016-01-14 22:03:54 +08:00
|
|
|
|
if dest != nil {
|
|
|
|
|
|
dest.Close()
|
2015-10-24 16:30:10 +08:00
|
|
|
|
}
|
2016-01-14 22:03:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *Tunnel) receive(revCarrier *tscipher.Carrier) {
|
|
|
|
|
|
src := this.src
|
|
|
|
|
|
cipherDirection := this.cipherDirection
|
|
|
|
|
|
id := this.id
|
|
|
|
|
|
defer this.onError()
|
2015-11-23 13:50:34 +08:00
|
|
|
|
var n int
|
|
|
|
|
|
var err error
|
2015-10-24 16:30:10 +08:00
|
|
|
|
for {
|
2015-11-21 16:54:04 +08:00
|
|
|
|
rTimer := time.Now() //receive timer
|
2016-01-11 21:29:48 +08:00
|
|
|
|
if cipherDirection != constant.RECEIVE {
|
2015-10-24 16:30:10 +08:00
|
|
|
|
revCarrier.Cipher = nil
|
2015-11-23 13:50:34 +08:00
|
|
|
|
n, err = tscipher.RowReceiveData(revCarrier)
|
2015-10-26 23:07:08 +08:00
|
|
|
|
} else {
|
2015-11-23 13:50:34 +08:00
|
|
|
|
n, err = tscipher.ReceiveData(revCarrier)
|
2015-10-24 16:30:10 +08:00
|
|
|
|
}
|
2015-11-21 16:54:04 +08:00
|
|
|
|
log.Info("id %s time to receive %d", id, time.Since(rTimer)/1000)
|
2015-10-24 16:30:10 +08:00
|
|
|
|
if err != nil {
|
2015-10-31 22:51:29 +08:00
|
|
|
|
log.Panic("Read panic. Tunnel id: %s. Remote Add: %s Local: %s. Err:%s", id, src.RemoteAddr().String(), src.LocalAddr().String(), err.Error())
|
2015-10-24 16:30:10 +08:00
|
|
|
|
}
|
2016-01-11 00:00:30 +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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *Tunnel) send(sendCarrier *tscipher.Carrier) {
|
|
|
|
|
|
dest := this.dest
|
|
|
|
|
|
cipherDirection := this.cipherDirection
|
|
|
|
|
|
id := this.id
|
2016-01-14 22:03:54 +08:00
|
|
|
|
defer this.onError()
|
2016-01-11 21:29:48 +08:00
|
|
|
|
if cipherDirection != constant.SEND {
|
2015-11-23 13:50:34 +08:00
|
|
|
|
sendCarrier.Cipher = nil
|
|
|
|
|
|
}
|
|
|
|
|
|
for {
|
2015-11-21 16:54:04 +08:00
|
|
|
|
sTimer := time.Now() //send timer
|
2016-01-11 00:00:30 +08:00
|
|
|
|
n, err := tscipher.SendData(sendCarrier)
|
2016-01-10 13:44:02 +08:00
|
|
|
|
log.Info("id %s time to send %d", id, time.Since(sTimer)/1000)
|
2015-10-24 16:30:10 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String())
|
|
|
|
|
|
}
|
2016-01-11 00:00:30 +08:00
|
|
|
|
log.Info("id %s Send %d bytes (local add %s),to %s", id, n, dest.LocalAddr().String(), dest.RemoteAddr().String())
|
2015-10-24 16:30:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|