parent
b284b401dc
commit
8599c87f0c
|
|
@ -11,32 +11,6 @@ type QueueCache struct { //FIFO
|
||||||
|
|
||||||
type BlockingQueueCache QueueCache
|
type BlockingQueueCache QueueCache
|
||||||
|
|
||||||
type UnblockingQueueCache QueueCache
|
|
||||||
|
|
||||||
func NewUnblockingQueueCache(quN int) *UnblockingQueueCache {
|
|
||||||
t := &UnblockingQueueCache{
|
|
||||||
queue: make(chan *Cache, quN),
|
|
||||||
}
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *UnblockingQueueCache) Put(bits []byte, l int) {
|
|
||||||
this.queue <- &Cache{
|
|
||||||
cache: bits,
|
|
||||||
l: l,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *UnblockingQueueCache) Get() ([]byte, int) {
|
|
||||||
var t *Cache
|
|
||||||
// select {
|
|
||||||
t = <-this.queue
|
|
||||||
// default:
|
|
||||||
// return make([]byte, 1024*4), 1024 * 4
|
|
||||||
// }
|
|
||||||
return t.cache, t.l
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBlockingQueueCache(quN int) *BlockingQueueCache {
|
func NewBlockingQueueCache(quN int) *BlockingQueueCache {
|
||||||
t := &BlockingQueueCache{
|
t := &BlockingQueueCache{
|
||||||
queue: make(chan *Cache, quN),
|
queue: make(chan *Cache, quN),
|
||||||
|
|
@ -52,6 +26,6 @@ func (this *BlockingQueueCache) Put(bits []byte, l int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *BlockingQueueCache) Get() ([]byte, int) {
|
func (this *BlockingQueueCache) Get() ([]byte, int) {
|
||||||
t := <-this.queue
|
t := <-this.queue
|
||||||
return t.cache, t.l
|
return t.cache, t.l
|
||||||
}
|
}
|
||||||
|
|
@ -1,25 +1,28 @@
|
||||||
package cache
|
package cache
|
||||||
|
|
||||||
import(
|
import (
|
||||||
"testing"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GetAndPut(name string, get *BlockingQueueCache, put *BlockingQueueCache) {
|
||||||
func GetAndPut(name string,get *BlockingQueueCache,put *BlockingQueueCache){
|
r, _ := get.Get()
|
||||||
for{
|
fmt.Printf("%s Get\n", name)
|
||||||
r,_:=get.Get()
|
put.Put(r, 1)
|
||||||
fmt.Printf("%s Get\n",name)
|
fmt.Printf("%s Put\n", name)
|
||||||
put.Put(r,1)
|
|
||||||
fmt.Printf("%s Put\n",name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
func TestAsynchronousCache(t *testing.T) {
|
||||||
|
a := NewBlockingQueueCache(1)
|
||||||
|
b := NewBlockingQueueCache(1)
|
||||||
|
a.Put(make([]byte, 1), 1)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
GetAndPut("A", a, b)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
GetAndPut("B", b, a)
|
||||||
|
}
|
||||||
|
|
||||||
func TestAsynchronousCache(t *testing.T){
|
|
||||||
a:=NewBlockingQueueCache(1)
|
|
||||||
b:=NewBlockingQueueCache(1)
|
|
||||||
a.Put(make([]byte,1),1)
|
|
||||||
go GetAndPut("A",a,b)
|
|
||||||
GetAndPut("B",b,a)
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package protocol
|
package constant
|
||||||
|
|
||||||
type Direction int
|
type Direction int
|
||||||
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
package protocol
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
||||||
// "fmt"
|
// "fmt"
|
||||||
"github.com/TransX/cache"
|
"github.com/TransX/cache"
|
||||||
|
"github.com/TransX/constant"
|
||||||
"github.com/TransX/log"
|
"github.com/TransX/log"
|
||||||
"github.com/TransX/tscipher"
|
"github.com/TransX/tscipher"
|
||||||
"net"
|
"net"
|
||||||
|
|
@ -14,12 +15,12 @@ type Tunnel struct {
|
||||||
id string
|
id string
|
||||||
src net.Conn
|
src net.Conn
|
||||||
dest net.Conn
|
dest net.Conn
|
||||||
cipherDirection Direction
|
cipherDirection constant.Direction
|
||||||
regChan chan interface{}
|
regChan chan interface{}
|
||||||
unregChan chan interface{}
|
unregChan chan interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTunnel(id string, src, dest net.Conn, cipherDirection Direction) *Tunnel {
|
func NewTunnel(id string, src, dest net.Conn, cipherDirection constant.Direction) *Tunnel {
|
||||||
return &Tunnel{
|
return &Tunnel{
|
||||||
id: id,
|
id: id,
|
||||||
src: src,
|
src: src,
|
||||||
|
|
@ -28,7 +29,7 @@ func NewTunnel(id string, src, dest net.Conn, cipherDirection Direction) *Tunnel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Tunnel) GetID(id string) string {
|
func (this *Tunnel) GetID() string {
|
||||||
return this.id
|
return this.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,7 +55,7 @@ func (this *Tunnel) Run() { //单向的,从src发送到dest
|
||||||
id := this.id
|
id := this.id
|
||||||
// cache := make([]byte, 1024*4) //4kB
|
// cache := make([]byte, 1024*4) //4kB
|
||||||
//构建Carrier
|
//构建Carrier
|
||||||
queCache := cache.NewUnblockingQueueCache(1)
|
queCache := cache.NewBlockingQueueCache(1)
|
||||||
for i := 0; i < 1; i++ {
|
for i := 0; i < 1; i++ {
|
||||||
queCache.Put(make([]byte, 1024*4), 0)
|
queCache.Put(make([]byte, 1024*4), 0)
|
||||||
}
|
}
|
||||||
|
|
@ -88,7 +89,7 @@ func (this *Tunnel) receive(revCarrier *tscipher.Carrier) {
|
||||||
var err error
|
var err error
|
||||||
for {
|
for {
|
||||||
rTimer := time.Now() //receive timer
|
rTimer := time.Now() //receive timer
|
||||||
if cipherDirection != RECEIVE {
|
if cipherDirection != constant.RECEIVE {
|
||||||
revCarrier.Cipher = nil
|
revCarrier.Cipher = nil
|
||||||
n, err = tscipher.RowReceiveData(revCarrier)
|
n, err = tscipher.RowReceiveData(revCarrier)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -119,7 +120,7 @@ func (this *Tunnel) send(sendCarrier *tscipher.Carrier) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if cipherDirection != SEND {
|
if cipherDirection != constant.SEND {
|
||||||
sendCarrier.Cipher = nil
|
sendCarrier.Cipher = nil
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
|
|
@ -3,7 +3,9 @@ package protocol
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/TransX/constant"
|
||||||
"github.com/TransX/log"
|
"github.com/TransX/log"
|
||||||
|
"github.com/TransX/model"
|
||||||
"github.com/TransX/stat"
|
"github.com/TransX/stat"
|
||||||
"github.com/TransX/utils"
|
"github.com/TransX/utils"
|
||||||
"net"
|
"net"
|
||||||
|
|
@ -77,11 +79,11 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer
|
||||||
//tunnel model : [ -->>server ---- client -->> ](this is a tunnel)
|
//tunnel model : [ -->>server ---- client -->> ](this is a tunnel)
|
||||||
if clientOrServer == "client" {
|
if clientOrServer == "client" {
|
||||||
sendID := utils.TunnelID()
|
sendID := utils.TunnelID()
|
||||||
ntSend := NewTunnel(sendID, listenerConn, destConn, SEND)
|
ntSend := model.NewTunnel(sendID, listenerConn, destConn, constant.SEND)
|
||||||
ntSend.SetRegChan(tunMng.GetRegChan())
|
ntSend.SetRegChan(tunMng.GetRegChan())
|
||||||
ntSend.SetUnRegChan(tunMng.GetUnregChan())
|
ntSend.SetUnRegChan(tunMng.GetUnregChan())
|
||||||
receiveID := utils.TunnelID()
|
receiveID := utils.TunnelID()
|
||||||
ntRev := NewTunnel(receiveID, destConn, listenerConn, RECEIVE)
|
ntRev := model.NewTunnel(receiveID, destConn, listenerConn, constant.RECEIVE)
|
||||||
ntRev.SetRegChan(tunMng.GetRegChan())
|
ntRev.SetRegChan(tunMng.GetRegChan())
|
||||||
ntRev.SetUnRegChan(tunMng.GetUnregChan())
|
ntRev.SetUnRegChan(tunMng.GetUnregChan())
|
||||||
printModelDetail(sendID, receiveID)
|
printModelDetail(sendID, receiveID)
|
||||||
|
|
@ -90,11 +92,11 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer
|
||||||
}
|
}
|
||||||
if clientOrServer == "server" {
|
if clientOrServer == "server" {
|
||||||
receiveID := utils.TunnelID()
|
receiveID := utils.TunnelID()
|
||||||
ntRev := NewTunnel(receiveID, listenerConn, destConn, RECEIVE)
|
ntRev := model.NewTunnel(receiveID, listenerConn, destConn, constant.RECEIVE)
|
||||||
ntRev.SetRegChan(tunMng.GetRegChan())
|
ntRev.SetRegChan(tunMng.GetRegChan())
|
||||||
ntRev.SetUnRegChan(tunMng.GetUnregChan())
|
ntRev.SetUnRegChan(tunMng.GetUnregChan())
|
||||||
sendID := utils.TunnelID()
|
sendID := utils.TunnelID()
|
||||||
ntSend := NewTunnel(sendID, destConn, listenerConn, SEND)
|
ntSend := model.NewTunnel(sendID, destConn, listenerConn, constant.SEND)
|
||||||
ntSend.SetRegChan(tunMng.GetRegChan())
|
ntSend.SetRegChan(tunMng.GetRegChan())
|
||||||
ntSend.SetUnRegChan(tunMng.GetUnregChan())
|
ntSend.SetUnRegChan(tunMng.GetUnregChan())
|
||||||
printModelDetail(sendID, receiveID)
|
printModelDetail(sendID, receiveID)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"container/list"
|
"container/list"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TransX/log"
|
"github.com/TransX/log"
|
||||||
|
"github.com/TransX/model"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -40,12 +41,25 @@ func (this *TunnelStatusManager) unregister(t interface{}) {
|
||||||
this.mux.Lock()
|
this.mux.Lock()
|
||||||
defer this.mux.Unlock()
|
defer this.mux.Unlock()
|
||||||
l := this.tunnelList
|
l := this.tunnelList
|
||||||
|
log.Debug("%d tunnels before remove.", this.tunnelList.Len())
|
||||||
|
n := 0
|
||||||
|
nl := new(list.List)
|
||||||
for e := l.Front(); e != nil; e = e.Next() {
|
for e := l.Front(); e != nil; e = e.Next() {
|
||||||
if e == t {
|
if e.Value != t {
|
||||||
l.Remove(e)
|
// log.Debug("Found")
|
||||||
break
|
// l.Remove(e)
|
||||||
|
// break
|
||||||
|
n++
|
||||||
|
nl.PushBack(e.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
if n == this.tunnelList.Len() {
|
||||||
|
m := t.(*model.Tunnel)
|
||||||
|
log.Debug("check tunntel %s", m.GetID())
|
||||||
|
}
|
||||||
|
this.tunnelList = *nl
|
||||||
|
log.Debug("%d tunnels after remove.", this.tunnelList.Len())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,13 +39,13 @@ type Cipher interface {
|
||||||
type Carrier struct {
|
type Carrier struct {
|
||||||
Conn net.Conn
|
Conn net.Conn
|
||||||
Cipher Cipher
|
Cipher Cipher
|
||||||
Cache *cache.UnblockingQueueCache
|
Cache *cache.BlockingQueueCache
|
||||||
Msg *cache.BlockingQueueCache
|
Msg *cache.BlockingQueueCache
|
||||||
AttachedTunnelID string
|
AttachedTunnelID string
|
||||||
receiveBuff []byte
|
receiveBuff []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCarrier(conn net.Conn, cipher Cipher, queCache *cache.UnblockingQueueCache, msg *cache.BlockingQueueCache, id string) *Carrier {
|
func NewCarrier(conn net.Conn, cipher Cipher, queCache *cache.BlockingQueueCache, msg *cache.BlockingQueueCache, id string) *Carrier {
|
||||||
t := new(Carrier)
|
t := new(Carrier)
|
||||||
t.Conn = conn
|
t.Conn = conn
|
||||||
t.Cipher = cipher
|
t.Cipher = cipher
|
||||||
|
|
@ -137,8 +137,6 @@ func UnwrapPackage(pacakge []byte) (data []byte, rest []byte, err error) {
|
||||||
|
|
||||||
func SendData(carrier *Carrier) (n int, err error) {
|
func SendData(carrier *Carrier) (n int, err error) {
|
||||||
msg, nByte := carrier.Msg.Get()
|
msg, nByte := carrier.Msg.Get()
|
||||||
id := carrier.AttachedTunnelID
|
|
||||||
log.Info("id %s Get Msg", id)
|
|
||||||
if len(msg) < nByte {
|
if len(msg) < nByte {
|
||||||
log.Panic("Cache of send is too small")
|
log.Panic("Cache of send is too small")
|
||||||
}
|
}
|
||||||
|
|
@ -147,7 +145,6 @@ func SendData(carrier *Carrier) (n int, err error) {
|
||||||
carrier.Cache.Put(make([]byte, 1024*4), 1024*4)
|
carrier.Cache.Put(make([]byte, 1024*4), 1024*4)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Info("id %s AAAAAAAaaa", id)
|
|
||||||
encrypedByte, err := carrier.Cipher.Encrypt(msg[:nByte])
|
encrypedByte, err := carrier.Cipher.Encrypt(msg[:nByte])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
n = 0
|
n = 0
|
||||||
|
|
@ -157,35 +154,24 @@ func SendData(carrier *Carrier) (n int, err error) {
|
||||||
wraped := WrapPackage(encrypedByte[:nByte])
|
wraped := WrapPackage(encrypedByte[:nByte])
|
||||||
n, err = carrier.Conn.Write(wraped)
|
n, err = carrier.Conn.Write(wraped)
|
||||||
carrier.Cache.Put(make([]byte, 1024*4), 1024*4)
|
carrier.Cache.Put(make([]byte, 1024*4), 1024*4)
|
||||||
log.Info("id %s give back cache", id)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func RowReceiveData(carrier *Carrier) (n int, err error) {
|
func RowReceiveData(carrier *Carrier) (n int, err error) {
|
||||||
cache, _ := carrier.Cache.Get()
|
cache, _ := carrier.Cache.Get()
|
||||||
log.Info("id %s get Cache", carrier.AttachedTunnelID)
|
|
||||||
n, err = carrier.Conn.Read(cache)
|
n, err = carrier.Conn.Read(cache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
n = 0
|
n = 0
|
||||||
}
|
}
|
||||||
carrier.Msg.Put(cache, n)
|
carrier.Msg.Put(cache, n)
|
||||||
id := carrier.AttachedTunnelID
|
|
||||||
log.Info("id %s put Msg", id)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReceiveData(carrier *Carrier) (n int, err error) {
|
func ReceiveData(carrier *Carrier) (n int, err error) {
|
||||||
// defer func() {
|
|
||||||
// if r := recover(); r != nil {
|
|
||||||
// log.Error("ReceiveData err %s", r)
|
|
||||||
// }
|
|
||||||
// }()
|
|
||||||
// log.Debug("id %s wrapedPackage := carrier.GetReceiveBuff()", carrier.AttachedTunnelID)
|
|
||||||
wrapedPackage := carrier.GetReceiveBuff() //make([]byte, 0, cap(carrier.Cache))
|
wrapedPackage := carrier.GetReceiveBuff() //make([]byte, 0, cap(carrier.Cache))
|
||||||
var packageData []byte
|
var packageData []byte
|
||||||
var _rest []byte
|
var _rest []byte
|
||||||
cache, _ := carrier.Cache.Get()
|
cache, _ := carrier.Cache.Get()
|
||||||
log.Info("id %s get Cache", carrier.AttachedTunnelID)
|
|
||||||
for {
|
for {
|
||||||
//首先检查这个是不是完整的包,是就返回好了,免得被阻塞
|
//首先检查这个是不是完整的包,是就返回好了,免得被阻塞
|
||||||
data, rest, err := UnwrapPackage(wrapedPackage)
|
data, rest, err := UnwrapPackage(wrapedPackage)
|
||||||
|
|
@ -226,7 +212,5 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
|
||||||
}
|
}
|
||||||
n = len(decrypted)
|
n = len(decrypted)
|
||||||
carrier.Msg.Put(decrypted, n)
|
carrier.Msg.Put(decrypted, n)
|
||||||
id := carrier.AttachedTunnelID
|
|
||||||
log.Info("id %s put Msg", id)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue