2015-11-18 23:16:57 +08:00
|
|
|
|
package protocol
|
2015-10-24 16:30:10 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"crypto/md5"
|
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
|
"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-10-24 16:30:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-13 15:44:17 +08:00
|
|
|
|
func NewTunnel(src, dest net.Conn, cipherDirection Direction) *Tunnel {
|
2015-10-24 16:30:10 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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-10-24 16:30:10 +08:00
|
|
|
|
src := this.src
|
|
|
|
|
|
dest := this.dest
|
|
|
|
|
|
cipherDirection := this.cipherDirection
|
|
|
|
|
|
id := this.id
|
|
|
|
|
|
defer func() {
|
2015-11-19 21:50:06 +08:00
|
|
|
|
log.Info("tunnel id %s ends", id)
|
2015-11-20 14:19:10 +08:00
|
|
|
|
//注销
|
|
|
|
|
|
this.unregChan <- this
|
2015-10-24 16:30:10 +08:00
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
|
if src != nil {
|
|
|
|
|
|
src.Close()
|
|
|
|
|
|
}
|
|
|
|
|
|
if dest != nil {
|
|
|
|
|
|
dest.Close()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
2015-11-05 15:22:22 +08:00
|
|
|
|
cache := make([]byte, 1024*4) //4kB
|
2015-10-26 23:07:08 +08:00
|
|
|
|
//构建Carrier
|
2015-10-31 22:51:29 +08:00
|
|
|
|
revCarrier := tscipher.NewCarrier(src, tscipher.NewCipher("XOR"), cache, this.id)
|
|
|
|
|
|
sendCarrier := tscipher.NewCarrier(dest, tscipher.NewCipher("XOR"), cache, this.id)
|
2015-10-24 16:30:10 +08:00
|
|
|
|
for {
|
2015-10-26 23:07:08 +08:00
|
|
|
|
var nByte int
|
|
|
|
|
|
var err error
|
2015-11-13 15:44:17 +08:00
|
|
|
|
if cipherDirection != RECEIVE {
|
2015-10-24 16:30:10 +08:00
|
|
|
|
revCarrier.Cipher = nil
|
2015-10-26 23:07:08 +08:00
|
|
|
|
nByte, err = tscipher.RowReceiveData(revCarrier)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
nByte, err = tscipher.ReceiveData(revCarrier)
|
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
|
|
|
|
}
|
|
|
|
|
|
log.Info("Reived %d bytes from %s. Tunnel: id %s", nByte, src.RemoteAddr().String(), id)
|
2015-11-13 15:44:17 +08:00
|
|
|
|
if cipherDirection != SEND {
|
2015-10-24 16:30:10 +08:00
|
|
|
|
sendCarrier.Cipher = nil
|
|
|
|
|
|
}
|
|
|
|
|
|
n, err := tscipher.SendData(sendCarrier, nByte)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String())
|
|
|
|
|
|
}
|
2015-11-18 20:24:39 +08:00
|
|
|
|
log.Info("Write %d bytes from %s to %s. Tunnel: %s . 18 bytes %x", n, dest.LocalAddr(), dest.RemoteAddr().String(), id, sendCarrier.Cache[:18])
|
2015-10-24 16:30:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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[:])
|
|
|
|
|
|
}
|