消除一个线程引起的bug

Signed-off-by: dmy@lab <dmy@lab.com>
This commit is contained in:
dmy@lab 2015-10-18 17:44:56 +08:00
parent 3be197ebba
commit b97df11243
5 changed files with 64 additions and 36 deletions

View File

@ -7,9 +7,11 @@ import (
var DestIP string var DestIP string
var DestPort int var DestPort int
var ListenPort int var ListenPort int
var EncryptSide string
func init() { func init() {
flag.StringVar(&DestIP, "destip", "", "Destination IP") flag.StringVar(&DestIP, "destip", "", "Destination IP")
flag.IntVar(&DestPort, "desport", 0, "Destination Port") flag.StringVar(&EncryptSide, "encrypt", "", "Encrypt Side")
flag.IntVar(&DestPort, "destport", 0, "Destination Port")
flag.IntVar(&ListenPort, "listenport", 0, "Listen Port") flag.IntVar(&ListenPort, "listenport", 0, "Listen Port")
} }

View File

@ -11,11 +11,12 @@ import (
func tunnel() { func tunnel() {
trans := NewTransTCP() trans := NewTransTCP()
log.Info("%s side is encrypted.", cli.EncryptSide)
if cli.DestPort != 0 { if cli.DestPort != 0 {
log.Info("Listening on 127.0.0.1:%d. Forward %s:%d", cli.ListenPort, cli.DestIP, cli.DestPort) log.Info("Listening on 127.0.0.1:%d. Forward %s:%d", cli.ListenPort, cli.DestIP, cli.DestPort)
trans.Start(strconv.Itoa(cli.ListenPort), cli.DestIP, strconv.Itoa(cli.DestPort)) trans.Start(strconv.Itoa(cli.ListenPort), cli.DestIP, strconv.Itoa(cli.DestPort), cli.EncryptSide)
} else { } else {
trans.Start("1200", "192.168.0.120", "8118") trans.Start("1200", "192.168.0.120", "8118", "client")
} }
} }

View File

@ -2,7 +2,6 @@ package main
import ( import (
"github.com/TransX/log" "github.com/TransX/log"
"github.com/TransX/tscipher"
"net" "net"
"testing" "testing"
"time" "time"
@ -20,13 +19,9 @@ func server(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
bytes := make([]byte, 32) bytes := make([]byte, 32)
XOR := tscipher.NewXOR([]byte("fasdfasdf"))
n, err := conn.Read(bytes) n, err := conn.Read(bytes)
decryped, _ := XOR.Decrypt(bytes[:n])
copy(bytes, decryped[:n])
log.Info("Test Server Receive %s", string(bytes[:n])) log.Info("Test Server Receive %s", string(bytes[:n]))
encrypted, _ := XOR.Encrypt([]byte("OK")) _, err = conn.Write([]byte("OK"))
_, err = conn.Write(encrypted)
log.Info("Test Server write") log.Info("Test Server write")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -42,19 +37,15 @@ func client(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
XOR := tscipher.NewXOR([]byte("fasdfasdf")) conn.Write([]byte("Client"))
encrypted, _ := XOR.Encrypt([]byte("Client")) log.Info("Test Client write")
conn.Write(encrypted)
log.Info("Test Client write %s",string(encrypted))
bytes := make([]byte, 32) bytes := make([]byte, 32)
n, err := conn.Read(bytes) n, err := conn.Read(bytes)
decryped, _ := XOR.Decrypt(bytes[:n])
copy(bytes, decryped[:n])
log.Info("Test Client read") log.Info("Test Client read")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
log.Info("Test Client Receive") log.Info("Test Client Receive %s", bytes[:n])
time.Sleep(time.Second * 2) time.Sleep(time.Second * 2)
conn.Close() conn.Close()
log.Info("Test Client closed") log.Info("Test Client closed")
@ -63,11 +54,12 @@ func client(t *testing.T) {
} }
func TestTunnel(t *testing.T) { func TestTunnel(t *testing.T) {
log.LogTo("stdout", "INFO") log.LogTo("log.txt", "DEBUG")
// t.Log("Start testing.")
log.Info("Test Start testing.") log.Info("Test Start testing.")
go server(t) go server(t)
go client(t) go client(t)
trans := NewTransTCP() trans1 := NewTransTCP()
trans.Start("1200", "127.0.0.1", "1244") go trans1.Start("1200", "127.0.0.1", "1201", "client")
trans2 := NewTransTCP()
trans2.Start("1201", "127.0.0.1", "1244", "server")
} }

49
tcp.go
View File

@ -13,15 +13,18 @@ import (
"time" "time"
) )
var seed int32
func init() {
seed = 0
}
type TransTCP struct { type TransTCP struct {
seed int32
} }
func NewTransTCP() *TransTCP { func NewTransTCP() *TransTCP {
return &TransTCP{ return &TransTCP{}
0,
}
} }
func (this *TransTCP) createTCPClient(ip, port string) (conn net.Conn, err error) { func (this *TransTCP) createTCPClient(ip, port string) (conn net.Conn, err error) {
@ -47,7 +50,7 @@ func (this *TransTCP) createTCPListener(ip, port string) (listen net.Listener, e
return return
} }
func (this *TransTCP) tunnel(src, dest net.Conn, id string) { func (this *TransTCP) tunnel(src, dest net.Conn, id string, encrypDirection string) { //单向的从src发送到dest
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
if src != nil { if src != nil {
@ -67,19 +70,29 @@ func (this *TransTCP) tunnel(src, dest net.Conn, id string) {
tscipher.NewCipher("XOR"), tscipher.NewCipher("XOR"),
cache, cache,
} }
log.Debug("Encrypt Direction %s ID %s", encrypDirection, id)
if encrypDirection != "receive" {
revCarrier.Cipher = nil
log.Debug("Read not crypted. Tunnel: %s", id)
}
nByte, err := tscipher.ReceiveData(revCarrier) nByte, err := tscipher.ReceiveData(revCarrier)
if err != nil { if err != nil {
log.Panic("Read panic. Tunnel id: %s. Remote Add: %s. Err:%s", id, src.RemoteAddr().String(), err) log.Panic("Read panic. Tunnel id: %s. Remote Add: %s. Err:%s", id, src.RemoteAddr().String(), err)
} }
log.Info("Reived %d bytes. Tunnel: id %s", nByte, id) log.Info("Reived %d bytes. Tunnel: id %s", nByte, id)
log.Debug("Reived %s",cache[:nByte]) log.Debug("Reived %s %s", id, cache[:nByte])
sendCarrier := &tscipher.Carrier{ sendCarrier := &tscipher.Carrier{
dest, dest,
tscipher.NewCipher("XOR"), tscipher.NewCipher("XOR"),
cache, //TODO:危险cache的容量容易被不小心修改 cache, //TODO:危险cache的容量容易被不小心修改
} }
if encrypDirection != "send" {
sendCarrier.Cipher = nil
log.Debug("Write not crypted. Tunnel: %s", id)
}
_, err = tscipher.SendData(sendCarrier, nByte) _, err = tscipher.SendData(sendCarrier, nByte)
log.Info("Write %d bytes. Tunnel: %s", nByte, id) log.Info("Write %d bytes. Tunnel: %s", nByte, id)
log.Debug("Write %s %s", id, cache[:nByte])
if err != nil { if err != nil {
log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String()) log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String())
} }
@ -88,14 +101,14 @@ func (this *TransTCP) tunnel(src, dest net.Conn, id string) {
} }
func (this *TransTCP) tunnelID() string { func (this *TransTCP) tunnelID() string {
nowString := time.Now().String() + strconv.Itoa(int(this.seed)) nowString := time.Now().String() + strconv.Itoa(int(seed))
atomic.AddInt32(&this.seed, 1) //避免多线程情况下获得的种子相同 atomic.AddInt32(&seed, 1) //避免多线程情况下获得的种子相同
md5Byte := md5.Sum(bytes.NewBufferString(nowString).Bytes()) md5Byte := md5.Sum(bytes.NewBufferString(nowString).Bytes())
// log.Info("seed %d %s", seed, hex.EncodeToString(md5Byte[:]))
return hex.EncodeToString(md5Byte[:]) return hex.EncodeToString(md5Byte[:])
} }
func (this *TransTCP) Start(listenPort, destIP, destPort string) { func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer string) {
listener, err := this.createTCPListener("0.0.0.0", listenPort) listener, err := this.createTCPListener("0.0.0.0", listenPort)
if err != nil { if err != nil {
log.Panic("Failed to create listener. %s", err) log.Panic("Failed to create listener. %s", err)
@ -111,8 +124,20 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string) {
os.Exit(0) os.Exit(0)
} }
log.Info("Dial %s", destConn.RemoteAddr().String()) log.Info("Dial %s", destConn.RemoteAddr().String())
go this.tunnel(listenerConn, destConn, this.tunnelID()) //tunnel model : [ -->>server ---- client -->> ](this is a tunnel)
go this.tunnel(destConn, listenerConn, this.tunnelID()) tunnelIDA := this.tunnelID()
tunnelIDB := this.tunnelID()
if clientOrServer == "client" {
go this.tunnel(listenerConn, destConn, tunnelIDA, "send")
go this.tunnel(destConn, listenerConn, tunnelIDB, "receive")
log.Debug("tow tunnel created: %s %s %s %s", tunnelIDA, "send", tunnelIDB, "receive")
}
if clientOrServer == "server" {
go this.tunnel(listenerConn, destConn, tunnelIDA, "receive")
go this.tunnel(destConn, listenerConn, tunnelIDB, "send")
log.Debug("tow tunnel created: %s %s %s %s", tunnelIDA, "receive", tunnelIDB, "send")
}
} else { } else {
log.Info("Failed to accept incoming connection. %s", err) log.Info("Failed to accept incoming connection. %s", err)
} }

View File

@ -23,18 +23,23 @@ func NewCipher(cipherName string) (cipher Cipher) {
return NewAES() return NewAES()
} }
if cipherName == "XOR" { if cipherName == "XOR" {
return NewXOR([]byte("fasdfasdf")) return NewXOR([]byte("fasdfasdf!3297!jfsl12*&!HHHFds"))
} }
return nil //TODO:临时这样处理 return nil //TODO:临时这样处理
} }
func SendData(carrier *Carrier, nByte int) (n int, err error) { func SendData(carrier *Carrier, nByte int) (n int, err error) {
if carrier.Cipher == nil {
n, err = carrier.Conn.Write(carrier.Cache[:nByte])
return
}
encrypedByte, err := carrier.Cipher.Encrypt(carrier.Cache[:nByte]) encrypedByte, err := carrier.Cipher.Encrypt(carrier.Cache[:nByte])
if err != nil { if err != nil {
n = 0 n = 0
return return
} }
n, err = carrier.Conn.Write(encrypedByte[:nByte]) n, err = carrier.Conn.Write(encrypedByte[:nByte])
copy(carrier.Cache, encrypedByte[:nByte]) // in case of debugging
return return
} }
@ -44,6 +49,9 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
n = 0 n = 0
return return
} }
if carrier.Cipher == nil {
return
}
decrypted, err := carrier.Cipher.Decrypt(carrier.Cache[:n]) decrypted, err := carrier.Cipher.Decrypt(carrier.Cache[:n])
copy(carrier.Cache, decrypted[:n]) copy(carrier.Cache, decrypted[:n])
if err != nil { if err != nil {