From 3be197ebbae71aedfebe2dc4ca8e1639927f666b Mon Sep 17 00:00:00 2001 From: "dmy@lab" Date: Sat, 17 Oct 2015 23:14:43 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0cli=202.=E9=87=87=E7=94=A8l?= =?UTF-8?q?og4go=203.=E6=B6=88=E9=99=A4XOR=E7=9A=84=E4=B8=80=E4=B8=AAbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dmy@lab --- .gitignore | 3 ++- cli/cli.go | 15 +++++++++++++++ main.go | 18 ++++++++++++++---- main_test.go | 10 +++++----- tcp.go | 19 ++++++++++--------- tscipher/cipher.go | 2 -- tscipher/xor.go | 6 +++++- 7 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 cli/cli.go diff --git a/.gitignore b/.gitignore index 0fee15a..025f077 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -TransX.exe \ No newline at end of file +TransX.exe +TransX \ No newline at end of file diff --git a/cli/cli.go b/cli/cli.go new file mode 100644 index 0000000..a40b6e1 --- /dev/null +++ b/cli/cli.go @@ -0,0 +1,15 @@ +package cli + +import ( + "flag" +) + +var DestIP string +var DestPort int +var ListenPort int + +func init() { + flag.StringVar(&DestIP, "destip", "", "Destination IP") + flag.IntVar(&DestPort, "desport", 0, "Destination Port") + flag.IntVar(&ListenPort, "listenport", 0, "Listen Port") +} diff --git a/main.go b/main.go index 23a80c7..ade5429 100644 --- a/main.go +++ b/main.go @@ -2,17 +2,27 @@ package main import ( + "flag" "fmt" + "github.com/TransX/cli" "github.com/TransX/log" + "strconv" ) -func Tunnel() { +func tunnel() { trans := NewTransTCP() - trans.Start("1200", "192.168.0.120", "8118") + if cli.DestPort != 0 { + 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)) + } else { + trans.Start("1200", "192.168.0.120", "8118") + } + } func main() { fmt.Println("Hello World!") - log.LogTo("stdout", "FINEST") - Tunnel() + log.LogTo("stdout", "INFO") + flag.Parse() + tunnel() } diff --git a/main_test.go b/main_test.go index 6da5b0b..fb0c830 100644 --- a/main_test.go +++ b/main_test.go @@ -15,7 +15,7 @@ func server(t *testing.T) { } for { conn, err := listener.Accept() - log.Info("Test Server Incoming", conn.RemoteAddr().String()) + log.Info("Test Server Incoming %s", conn.RemoteAddr().String()) if err != nil { t.Fatal(err) } @@ -24,7 +24,7 @@ func server(t *testing.T) { n, err := conn.Read(bytes) decryped, _ := XOR.Decrypt(bytes[:n]) copy(bytes, decryped[:n]) - log.Info("Test Server Receive ", string(bytes[:n])) + log.Info("Test Server Receive %s", string(bytes[:n])) encrypted, _ := XOR.Encrypt([]byte("OK")) _, err = conn.Write(encrypted) log.Info("Test Server write") @@ -45,7 +45,7 @@ func client(t *testing.T) { XOR := tscipher.NewXOR([]byte("fasdfasdf")) encrypted, _ := XOR.Encrypt([]byte("Client")) conn.Write(encrypted) - log.Info("Test Client write") + log.Info("Test Client write %s",string(encrypted)) bytes := make([]byte, 32) n, err := conn.Read(bytes) decryped, _ := XOR.Decrypt(bytes[:n]) @@ -54,7 +54,7 @@ func client(t *testing.T) { if err != nil { t.Fatal(err) } - log.Info("Test Client Receive ", string(bytes[:n])) + log.Info("Test Client Receive") time.Sleep(time.Second * 2) conn.Close() log.Info("Test Client closed") @@ -63,7 +63,7 @@ func client(t *testing.T) { } func TestTunnel(t *testing.T) { - log.LogTo("stdout", "FINEST") + log.LogTo("stdout", "INFO") // t.Log("Start testing.") log.Info("Test Start testing.") go server(t) diff --git a/tcp.go b/tcp.go index 6c13953..184a28f 100644 --- a/tcp.go +++ b/tcp.go @@ -69,18 +69,19 @@ func (this *TransTCP) tunnel(src, dest net.Conn, id string) { } nByte, err := tscipher.ReceiveData(revCarrier) if err != nil { - log.Panic("Read panic", id, err, src.RemoteAddr().String()) + log.Panic("Read panic. Tunnel id: %s. Remote Add: %s. Err:%s", id, src.RemoteAddr().String(), err) } - log.Info("Reived ", nByte, "bytes:", id, string(cache[:nByte])) + log.Info("Reived %d bytes. Tunnel: id %s", nByte,id) + log.Debug("Reived %s",cache[:nByte]) sendCarrier := &tscipher.Carrier{ dest, tscipher.NewCipher("XOR"), cache, //TODO:危险,cache的容量容易被不小心修改 } _, err = tscipher.SendData(sendCarrier, nByte) - log.Info("Write") + log.Info("Write %d bytes. Tunnel: %s",nByte,id) if err != nil { - log.Panic("Write panic", id, err, dest.RemoteAddr().String()) + log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String()) } } @@ -97,23 +98,23 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string) { listener, err := this.createTCPListener("0.0.0.0", listenPort) if err != nil { - log.Panic("Failed to create listener.", err) + log.Panic("Failed to create listener. %s", err) os.Exit(0) } for { if listenerConn, err := listener.Accept(); err == nil { - log.Info("Incoming ", listenerConn.RemoteAddr().String()) + log.Info("Incoming %s", listenerConn.RemoteAddr().String()) //创建到目标的连接 destConn, err := this.createTCPClient(destIP, destPort) if err != nil { - log.Panic("Failed to connect to destination.", err) + log.Panic("Failed to connect to destination. %s", err) os.Exit(0) } - log.Info("Dial", destConn.RemoteAddr().String()) + log.Info("Dial %s", destConn.RemoteAddr().String()) go this.tunnel(listenerConn, destConn, this.tunnelID()) go this.tunnel(destConn, listenerConn, this.tunnelID()) } else { - log.Info("Failed to accept incoming connection.", err) + log.Info("Failed to accept incoming connection. %s", err) } } } diff --git a/tscipher/cipher.go b/tscipher/cipher.go index 3222dbc..68f0978 100644 --- a/tscipher/cipher.go +++ b/tscipher/cipher.go @@ -44,10 +44,8 @@ func ReceiveData(carrier *Carrier) (n int, err error) { n = 0 return } - //TODO:err decrypted, err := carrier.Cipher.Decrypt(carrier.Cache[:n]) copy(carrier.Cache, decrypted[:n]) - if err != nil { n = 0 return diff --git a/tscipher/xor.go b/tscipher/xor.go index e34efab..4c2a8b6 100644 --- a/tscipher/xor.go +++ b/tscipher/xor.go @@ -10,7 +10,9 @@ 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)] } +// copy(decrypted,data) err = nil return } @@ -18,8 +20,10 @@ func (this *XOR) Decrypt(data []byte) (decrypted []byte, err error) { func (this *XOR) Encrypt(data []byte) (encryped []byte, err error) { encryped = make([]byte, len(data)) for i := 0; i < len(data); i++ { - encryped[i] = data[len(data)-i-1] ^ this.key[i%len(this.key)] + encryped[len(data)-i-1] = data[i] ^ this.key[i%len(this.key)] +// encryped[i] = data[i] ^ this.key[i%len(this.key)] } +// copy(encryped,data) err = nil return }