From f0d343cebb5666c8f58b37090e7aa8210abf8671 Mon Sep 17 00:00:00 2001 From: "dmy@lab" Date: Sat, 24 Oct 2015 16:07:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E4=BA=86=E6=9B=B4=E5=A5=BD=E7=9A=84?= =?UTF-8?q?=E8=B0=83=E8=AF=95=EF=BC=8C=E5=87=86=E5=A4=87=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dmy@lab --- cli/cli.go | 2 ++ client.bat | 2 +- main.go | 4 ++-- main_test.go | 30 ++++++++++++++++++++++------ tcp.go | 49 +++++++++++++++++++++++----------------------- tscipher/cipher.go | 12 ++++++++---- tscipher/xor.go | 10 ++++++++-- 7 files changed, 69 insertions(+), 40 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index f81bae8..ed07927 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -8,10 +8,12 @@ var DestIP string var DestPort int var ListenPort int var EncryptSide string +var LogTo string func init() { flag.StringVar(&DestIP, "destip", "", "Destination IP") flag.StringVar(&EncryptSide, "encrypt", "", "Encrypt Side") + flag.StringVar(&LogTo, "log", "stdout", "Log to where") flag.IntVar(&DestPort, "destport", 0, "Destination Port") flag.IntVar(&ListenPort, "listenport", 0, "Listen Port") } diff --git a/client.bat b/client.bat index 19f2e94..85bdaf7 100644 --- a/client.bat +++ b/client.bat @@ -1 +1 @@ -.\TransX -destip 127.0.0.1 -destport 1082 -listenport 1080 -encrypt client \ No newline at end of file +.\TransX -destip 127.0.0.1 -destport 1082 -listenport 1080 -encrypt client \ No newline at end of file diff --git a/main.go b/main.go index 3a750cc..6f33b7f 100644 --- a/main.go +++ b/main.go @@ -22,8 +22,8 @@ func tunnel() { } func main() { - fmt.Println("Hello World!") - log.LogTo("stdout", "INFO") flag.Parse() + fmt.Println("Hello World!") + log.LogTo(cli.LogTo, "INFO") tunnel() } diff --git a/main_test.go b/main_test.go index 4698ff3..5a28c72 100644 --- a/main_test.go +++ b/main_test.go @@ -24,12 +24,29 @@ func serverBin(t *testing.T) { if err != nil { log.Error(err.Error()) } + bytes := make([]byte, 1024*32) - n, err := conn.Read(bytes) - if err != nil { - log.Error("Test Server read %", err.Error()) - break + /////<- 当发送端的cache大于4097时,把这些注释掉 + _bytes := make([]byte, 1024*32) + add := 0 + n := 0 + for { + /////-> + n, err := conn.Read(_bytes) + if err != nil { + log.Error("Test Server read %", err.Error()) + break + } + ////<- + copy(bytes[add:add+n], _bytes[:n]) + add += n + if add == 4097 { + break + n = 4097 + } + log.Info("add %d", add) } + ////-> log.Info("Test Server read %d bytes from %s", n, conn.RemoteAddr().String()) if bytes[0] != nCount { log.Error("package sequence not right. it's %d. should be %d", bytes[0], nCount) @@ -59,6 +76,7 @@ func serverBin(t *testing.T) { } break } + log.Info("Test Server read file %d, it should be %d", nBinByte, n-1) for i := 0; i < nBinByte; i++ { if binBytes[i] != bytes[i+1] { @@ -142,7 +160,7 @@ func clientBin(t *testing.T) { log.Error("client not write enough bytes") } // log.Info("Test Client write %s", string(binBytes[:n])) - log.Info("Test Client write with count %d", nCount) + log.Info("Test Client write %d bytes with count %d", n, nCount) nCount++ bytes := make([]byte, 1024*32) n, err = conn.Read(bytes) @@ -190,7 +208,7 @@ func clientText(t *testing.T) { } func TestTunnel(t *testing.T) { - log.LogTo("log.txt", "INFO") + log.LogTo("log.txt", "DEBUG") log.Info("Test Start testing.") go serverBin(t) go clientBin(t) diff --git a/tcp.go b/tcp.go index 5788d23..9775244 100644 --- a/tcp.go +++ b/tcp.go @@ -62,7 +62,7 @@ func (this *TransTCP) tunnel(src, dest net.Conn, id string, encrypDirection stri } }() - cache := make([]byte, 1024*128) //128kB + cache := make([]byte, 1024*2) //128kB for { //构建Carrier revCarrier := &tscipher.Carrier{ @@ -80,7 +80,6 @@ func (this *TransTCP) tunnel(src, dest net.Conn, id string, encrypDirection stri log.Panic("Read panic. Tunnel id: %s. Remote Add: %s. Err:%s", id, src.RemoteAddr().String(), err) } log.Info("Reived %d bytes from %s. Tunnel: id %s", nByte, src.RemoteAddr().String(), id) - log.Debug("Reived %s %s", id, cache[:nByte]) sendCarrier := &tscipher.Carrier{ dest, tscipher.NewCipher("XOR"), @@ -92,7 +91,6 @@ func (this *TransTCP) tunnel(src, dest net.Conn, id string, encrypDirection stri } n, err := tscipher.SendData(sendCarrier, nByte) log.Info("Write %d bytes from %s to %s. Tunnel: %s", n, dest.LocalAddr(), dest.RemoteAddr().String(), id) - log.Debug("Write %s %s", id, cache[:nByte]) if err != nil { log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String()) } @@ -104,7 +102,6 @@ func (this *TransTCP) tunnelID() string { nowString := time.Now().String() + strconv.Itoa(int(seed)) atomic.AddInt32(&seed, 1) //避免多线程情况下获得的种子相同 md5Byte := md5.Sum(bytes.NewBufferString(nowString).Bytes()) - // log.Info("seed %d %s", seed, hex.EncodeToString(md5Byte[:])) return hex.EncodeToString(md5Byte[:]) } @@ -116,27 +113,29 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer } for { if listenerConn, err := listener.Accept(); err == nil { - log.Info("Incoming %s", listenerConn.RemoteAddr().String()) - //创建到目标的连接 - destConn, err := this.createTCPClient(destIP, destPort) - if err != nil { - log.Panic("Failed to connect to destination. %s", err) - os.Exit(0) - } - log.Info("Dial %s", destConn.RemoteAddr().String()) - //tunnel model : [ -->>server ---- client -->> ](this is a tunnel) - 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") - } + go func() { + log.Info("Incoming %s", listenerConn.RemoteAddr().String()) + //创建到目标的连接 + destConn, err := this.createTCPClient(destIP, destPort) + if err != nil { + log.Panic("Failed to connect to destination. %s", err) + os.Exit(0) + } + log.Info("Dial %s", destConn.RemoteAddr().String()) + //tunnel model : [ -->>server ---- client -->> ](this is a tunnel) + 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("two 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("two tunnel created: %s %s %s %s", tunnelIDA, "receive", tunnelIDB, "send") + } + }() } else { log.Info("Failed to accept incoming connection. %s", err) diff --git a/tscipher/cipher.go b/tscipher/cipher.go index cff0de8..f25baa8 100644 --- a/tscipher/cipher.go +++ b/tscipher/cipher.go @@ -73,6 +73,9 @@ func UnwrapPackage(pacakge []byte) (packageSize int, data []byte, err error) { } func SendData(carrier *Carrier, nByte int) (n int, err error) { + if len(carrier.Cache) < nByte { + log.Panic("Cache of send is too small") + } if carrier.Cipher == nil { n, err = carrier.Conn.Write(carrier.Cache[:nByte]) return @@ -113,7 +116,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) { // log.Info("packageSize %d data size %d", packageSize, len(data)) if err == nil && packageSize == len(data) { //读到的是一个完整的包 realData = data - log.Info("read a complete package") + log.Debug("read a complete package") } else { gotSize := len(data) for { @@ -123,7 +126,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) { return } wrapedPackage = carrier.Cache[:n] - log.Info("got partial package size %d", n) + log.Debug("got partial package size %d from %s", n, carrier.Conn.RemoteAddr().String()) _, data, err = UnwrapPackage(wrapedPackage) if err == nil { n = 0 @@ -133,7 +136,8 @@ func ReceiveData(carrier *Carrier) (n int, err error) { gotSize += len(data) realData = append(realData, data...) if gotSize == packageSize { - log.Info("got enough:. packageSize %d, real size %d", packageSize, gotSize) + log.Debug("got enough:. packageSize %d, real size %d. not include header", packageSize, gotSize) + n = gotSize break } } @@ -144,7 +148,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) { n = 0 return } - n = len(decrypted) + // n = len(decrypted) copy(carrier.Cache, decrypted) return } diff --git a/tscipher/xor.go b/tscipher/xor.go index 0aec4bd..6191cf5 100644 --- a/tscipher/xor.go +++ b/tscipher/xor.go @@ -7,20 +7,26 @@ type XOR struct { } func (this *XOR) Decrypt(data []byte) (decrypted []byte, err error) { + // decrypted = data + // err = nil + // return decrypted = make([]byte, len(data)) for i := 0; i < len(data); i++ { decrypted[i] = data[len(data)-i-1] ^ this.key[i%len(this.key)] - // decrypted[i] = data[i] + // decrypted[i] = data[i] } err = nil return } func (this *XOR) Encrypt(data []byte) (encryped []byte, err error) { + // encryped = data + // err = nil + // return encryped = make([]byte, len(data)) for i := 0; i < len(data); i++ { encryped[len(data)-i-1] = data[i] ^ this.key[i%len(this.key)] - // encryped[i] = data[i] + // encryped[i] = data[i] } err = nil return