diff --git a/TransX.exe b/TransX.exe deleted file mode 100644 index e576389..0000000 Binary files a/TransX.exe and /dev/null differ diff --git a/log/logger.go b/log/logger.go index be8da39..2e36396 100644 --- a/log/logger.go +++ b/log/logger.go @@ -1,8 +1,8 @@ package log import ( - log "github.com/alecthomas/log4go" "fmt" + log "github.com/alecthomas/log4go" ) var root log.Logger = make(log.Logger) @@ -54,6 +54,7 @@ type Logger interface { Info(string, ...interface{}) Warn(string, ...interface{}) error Error(string, ...interface{}) error + Panic(string, ...interface{}) } type PrefixLogger struct { @@ -91,6 +92,10 @@ func (pl *PrefixLogger) Error(arg0 string, args ...interface{}) error { return pl.Logger.Error(pl.pfx(arg0), args...) } +func (pl *PrefixLogger) Panic(arg0 string, args ...interface{}) { + panic(pl.Error(arg0, args)) +} + func (pl *PrefixLogger) AddLogPrefix(prefix string) { if len(pl.prefix) > 0 { pl.prefix += " " @@ -118,4 +123,8 @@ func Warn(arg0 string, args ...interface{}) error { func Error(arg0 string, args ...interface{}) error { return root.Error(arg0, args...) -} \ No newline at end of file +} + +func Panic(arg0 string, args ...interface{}) error { + panic(root.Error(arg0, args...)) +} diff --git a/main.go b/main.go index 1f207a4..23a80c7 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "fmt" - // "github.com/TransX/log" + "github.com/TransX/log" ) func Tunnel() { @@ -13,5 +13,6 @@ func Tunnel() { func main() { fmt.Println("Hello World!") + log.LogTo("stdout", "FINEST") Tunnel() } diff --git a/main_test.go b/main_test.go index 12ffe24..6da5b0b 100644 --- a/main_test.go +++ b/main_test.go @@ -1,10 +1,9 @@ package main import ( + "github.com/TransX/log" "github.com/TransX/tscipher" - "log" "net" - "os" "testing" "time" ) @@ -16,7 +15,7 @@ func server(t *testing.T) { } for { conn, err := listener.Accept() - log.Println("Test Server Incoming", conn.RemoteAddr().String()) + log.Info("Test Server Incoming", conn.RemoteAddr().String()) if err != nil { t.Fatal(err) } @@ -25,15 +24,15 @@ func server(t *testing.T) { n, err := conn.Read(bytes) decryped, _ := XOR.Decrypt(bytes[:n]) copy(bytes, decryped[:n]) - log.Println("Test Server Receive ", string(bytes[:n])) + log.Info("Test Server Receive ", string(bytes[:n])) encrypted, _ := XOR.Encrypt([]byte("OK")) _, err = conn.Write(encrypted) - log.Println("Test Server write") + log.Info("Test Server write") if err != nil { t.Fatal(err) } conn.Close() - log.Println("Test Server closed") + log.Info("Test Server closed") } } @@ -46,28 +45,27 @@ func client(t *testing.T) { XOR := tscipher.NewXOR([]byte("fasdfasdf")) encrypted, _ := XOR.Encrypt([]byte("Client")) conn.Write(encrypted) - log.Println("Test Client write") + log.Info("Test Client write") bytes := make([]byte, 32) n, err := conn.Read(bytes) decryped, _ := XOR.Decrypt(bytes[:n]) copy(bytes, decryped[:n]) - log.Println("Test Client read") + log.Info("Test Client read") if err != nil { t.Fatal(err) } - log.Println("Test Client Receive ", string(bytes[:n])) + log.Info("Test Client Receive ", string(bytes[:n])) time.Sleep(time.Second * 2) conn.Close() - log.Println("Test Client closed") + log.Info("Test Client closed") } } func TestTunnel(t *testing.T) { - file, _ := os.Create("log.txt") - log.SetOutput(file) + log.LogTo("stdout", "FINEST") // t.Log("Start testing.") - log.Println("Test Start testing.") + log.Info("Test Start testing.") go server(t) go client(t) trans := NewTransTCP() diff --git a/tcp.go b/tcp.go index 3ab7dad..6c13953 100644 --- a/tcp.go +++ b/tcp.go @@ -4,9 +4,10 @@ import ( "bytes" "crypto/md5" "encoding/hex" + "github.com/TransX/log" "github.com/TransX/tscipher" - "log" "net" + "os" "strconv" "sync/atomic" "time" @@ -68,18 +69,18 @@ func (this *TransTCP) tunnel(src, dest net.Conn, id string) { } nByte, err := tscipher.ReceiveData(revCarrier) if err != nil { - log.Panicln("Read panic", id, err, src.RemoteAddr().String()) + log.Panic("Read panic", id, err, src.RemoteAddr().String()) } - log.Println("Reived ", nByte, "bytes:", id, string(cache[:nByte])) + log.Info("Reived ", nByte, "bytes:", id, string(cache[:nByte])) sendCarrier := &tscipher.Carrier{ dest, tscipher.NewCipher("XOR"), cache, //TODO:危险,cache的容量容易被不小心修改 } _, err = tscipher.SendData(sendCarrier, nByte) - log.Println("Write") + log.Info("Write") if err != nil { - log.Panicln("Write panic", id, err, dest.RemoteAddr().String()) + log.Panic("Write panic", id, err, dest.RemoteAddr().String()) } } @@ -96,21 +97,23 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string) { listener, err := this.createTCPListener("0.0.0.0", listenPort) if err != nil { - log.Fatalln("Failed to create listener.", err) + log.Panic("Failed to create listener.", err) + os.Exit(0) } for { if listenerConn, err := listener.Accept(); err == nil { - log.Println("Incoming ", listenerConn.RemoteAddr().String()) + log.Info("Incoming ", listenerConn.RemoteAddr().String()) //创建到目标的连接 destConn, err := this.createTCPClient(destIP, destPort) if err != nil { - log.Fatalln("Failed to connect to destination.", err) + log.Panic("Failed to connect to destination.", err) + os.Exit(0) } - log.Println("Dial", destConn.RemoteAddr().String()) + log.Info("Dial", destConn.RemoteAddr().String()) go this.tunnel(listenerConn, destConn, this.tunnelID()) go this.tunnel(destConn, listenerConn, this.tunnelID()) } else { - log.Println("Failed to accept incoming connection.", err) + log.Info("Failed to accept incoming connection.", err) } } }