From fed05b3de300fd0291e0ee1d2db28c2f0d080923 Mon Sep 17 00:00:00 2001 From: "dmy@lab" Date: Wed, 21 Oct 2015 12:04:07 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E4=BA=86=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=9A=84=E6=96=87=E4=BB=B6=202.cache?= =?UTF-8?q?=E5=A4=AA=E5=B0=8F=E4=BC=9A=E6=9C=89=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E8=BF=98=E5=9C=A8=E7=A0=94=E7=A9=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dmy@lab --- main.go | 2 +- main_test.go | 36 +++++++++++++++++++++++++++++++----- tcp.go | 2 +- test/cipher.txt | 9 +++++++++ tscipher/xor.go | 6 ++---- 5 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 test/cipher.txt diff --git a/main.go b/main.go index f9bc607..3a750cc 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,7 @@ func tunnel() { trans := NewTransTCP() log.Info("%s side is encrypted.", cli.EncryptSide) 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 0.0.0.0:%d. Forward %s:%d", cli.ListenPort, cli.DestIP, cli.DestPort) trans.Start(strconv.Itoa(cli.ListenPort), cli.DestIP, strconv.Itoa(cli.DestPort), cli.EncryptSide) } else { trans.Start("1200", "192.168.0.120", "8118", "client") diff --git a/main_test.go b/main_test.go index c350fcf..0bc48fe 100644 --- a/main_test.go +++ b/main_test.go @@ -1,13 +1,17 @@ package main import ( + "bufio" "github.com/TransX/log" "net" + "os" "testing" "time" ) func server(t *testing.T) { + file, _ := os.Open("test/cipher.txt") + reader := bufio.NewReader(file) listener, err := net.Listen("tcp4", "127.0.0.1:1244") if err != nil { t.Fatal(err) @@ -18,8 +22,21 @@ func server(t *testing.T) { if err != nil { t.Fatal(err) } - bytes := make([]byte, 32) + bytes := make([]byte, 1024*32) n, err := conn.Read(bytes) + line, err := reader.ReadString('\n') + if err != nil { + log.Error("client read %", err) + break + } + if string(bytes[:n]) != line { + log.Error("cipher failed %s", string(bytes[:n])) + log.Error("%s %s", len(bytes[:n]), len([]byte(line))) + for i := 0; i < n; i++ { + log.Error("%d", bytes[i]-[]byte(line)[i]) + } + + } log.Info("Test Server Receive %s", string(bytes[:n])) _, err = conn.Write([]byte("OK")) log.Info("Test Server write") @@ -32,15 +49,24 @@ func server(t *testing.T) { } func client(t *testing.T) { + file, _ := os.Open("test/cipher.txt") + reader := bufio.NewReader(file) for { conn, err := net.Dial("tcp4", "127.0.0.1:1200") if err != nil { t.Fatal(err) } - conn.Write([]byte("Client")) + line, err := reader.ReadString('\n') + if err != nil { + log.Error("client read %", err) + + } + bytesLine := []byte(line) + n, _ := conn.Write(bytesLine) + log.Info("Test Client write %s", string(bytesLine[:n])) log.Info("Test Client write") - bytes := make([]byte, 32) - n, err := conn.Read(bytes) + bytes := make([]byte, 1024*32) + n, err = conn.Read(bytes) log.Info("Test Client read") if err != nil { t.Fatal(err) @@ -54,7 +80,7 @@ func client(t *testing.T) { } func TestTunnel(t *testing.T) { - log.LogTo("log.txt", "DEBUG") + log.LogTo("log.txt", "INFO") log.Info("Test Start testing.") go server(t) go client(t) diff --git a/tcp.go b/tcp.go index 3696153..dd7b2d9 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*32) //128kB for { //构建Carrier revCarrier := &tscipher.Carrier{ diff --git a/test/cipher.txt b/test/cipher.txt new file mode 100644 index 0000000..d75a6de --- /dev/null +++ b/test/cipher.txt @@ -0,0 +1,9 @@ +About 4,000 have arrived in Slovenia, the UN refugee agency says. Most aim to travel on to Austria, Germany and other countries. +Slovenia's army has been placed on standby to help police deal with the influx, Prime Minister Miro Cerar said. +He said Slovenia would accept the migrants as long as Austria and Germany kept their borders open. +Hungary said it had closed its border with Croatia at midnight on Friday because European Union leaders had failed to agree a plan to stem the flow of asylum seekers. +Last month it also shut its frontier with Serbia, which was another transit route to Western Europe. + + +On Saturday, hundreds of refugees were bussed across Croatia, from its border with Serbia to its border with Slovenia. +Many had spent weeks walking through Greece, Macedonia and Serbia to reach the Croatian border. \ No newline at end of file diff --git a/tscipher/xor.go b/tscipher/xor.go index 4c2a8b6..0aec4bd 100644 --- a/tscipher/xor.go +++ b/tscipher/xor.go @@ -10,9 +10,8 @@ func (this *XOR) Decrypt(data []byte) (decrypted []byte, err error) { 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] ^ this.key[i%len(this.key)] + // decrypted[i] = data[i] } -// copy(decrypted,data) err = nil return } @@ -21,9 +20,8 @@ func (this *XOR) Encrypt(data []byte) (encryped []byte, err error) { 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] ^ this.key[i%len(this.key)] + // encryped[i] = data[i] } -// copy(encryped,data) err = nil return }