From da0e77d74c8038d555d714c6d23e4207753a50a3 Mon Sep 17 00:00:00 2001 From: "dmy@lab" Date: Fri, 20 Nov 2015 17:31:32 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0web=20=E6=98=BE=E7=A4=BApro?= =?UTF-8?q?file=202.=E9=87=8D=E6=96=B0=E5=AE=89=E6=8E=92=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E6=97=A5=E5=BF=97=E7=9A=84=E7=AD=89=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + benckmark.go | 17 ++++++++++++----- cli/cli.go | 2 ++ client.bat | 2 +- main.go | 15 +++++++++++---- protocol/tcp.go | 2 +- protocol/tunnel.go | 6 +++--- server.bat | 2 +- stat/tunnelstatus.go | 6 +++--- tscipher/cipher.go | 8 ++++---- 10 files changed, 39 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index cb30291..11714e1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ TransX server.log* client.log* applog +profile \ No newline at end of file diff --git a/benckmark.go b/benckmark.go index c14bdbe..761272f 100644 --- a/benckmark.go +++ b/benckmark.go @@ -58,19 +58,26 @@ func randMillionSecond() time.Duration { func attack() { c := make(chan int) - for i := 0; i < 1; i++ { + for i := 0; i < 10; i++ { time.Sleep(time.Millisecond * randMillionSecond()) go doHttp(c) } - for i := 0; i < 1; i++ { + for i := 0; i < 10; i++ { <-c } log.Println("Finish") } -func main() { - llog.LogTo("applog/log.txt", "INFO") - defer profile.Start(profile.CPUProfile).Stop() +func main1() { + cfg := profile.Config{ + MemProfile: true, + ProfilePath: "./profile", // store profiles in current directory + NoShutdownHook: true, // do not hook SIGINT + CPUProfile: true, + BlockProfile: true, + } + llog.LogTo("applog/log.txt", "ERROR") + defer profile.Start(&cfg).Stop() benchSeed = 0 go transClient() go transServer() diff --git a/cli/cli.go b/cli/cli.go index ed07927..f014212 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -9,6 +9,7 @@ var DestPort int var ListenPort int var EncryptSide string var LogTo string +var ProfilePort int func init() { flag.StringVar(&DestIP, "destip", "", "Destination IP") @@ -16,4 +17,5 @@ func init() { flag.StringVar(&LogTo, "log", "stdout", "Log to where") flag.IntVar(&DestPort, "destport", 0, "Destination Port") flag.IntVar(&ListenPort, "listenport", 0, "Listen Port") + flag.IntVar(&ProfilePort, "profileport", 0, "Profile Port") } diff --git a/client.bat b/client.bat index c19093c..9eb9b01 100644 --- a/client.bat +++ b/client.bat @@ -1 +1 @@ -.\TransX -destip 127.0.0.1 -destport 1082 -listenport 1080 -encrypt client -log applog/client.log \ No newline at end of file +.\TransX -destip 127.0.0.1 -destport 1082 -listenport 1080 -encrypt client -log applog/client.log -profileport 6062 \ No newline at end of file diff --git a/main.go b/main.go index e2413c5..fa0b3fe 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,9 @@ import ( "github.com/TransX/cli" "github.com/TransX/log" "github.com/TransX/protocol" - "github.com/davecheney/profile" + // "github.com/davecheney/profile" + "net/http" + _ "net/http/pprof" "strconv" ) @@ -23,10 +25,15 @@ func tunnel() { } -func main1() { - defer profile.Start(profile.CPUProfile).Stop() +func main() { + // defer profile.Start(profile.CPUProfile).Stop() flag.Parse() fmt.Println("Hello World!") - log.LogTo(cli.LogTo, "INFO") + log.LogTo(cli.LogTo, "ERROR") + if cli.ProfilePort != 0 { + go func() { + http.ListenAndServe("0.0.0.0:"+strconv.Itoa(cli.ProfilePort), nil) + }() + } tunnel() } diff --git a/protocol/tcp.go b/protocol/tcp.go index 1309f6b..3af5ea8 100644 --- a/protocol/tcp.go +++ b/protocol/tcp.go @@ -97,7 +97,7 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer }() } else { - log.Info("Failed to accept incoming connection. %s", err) + log.Error("Failed to accept incoming connection. %s", err) } } } diff --git a/protocol/tunnel.go b/protocol/tunnel.go index cce921a..8a6d129 100644 --- a/protocol/tunnel.go +++ b/protocol/tunnel.go @@ -61,7 +61,7 @@ func (this *Tunnel) Run() { //单向的,从src发送到dest cipherDirection := this.cipherDirection id := this.id defer func() { - log.Info("tunnel id %s ends", id) + log.Debug("tunnel id %s ends", id) //注销 this.unregChan <- this if r := recover(); r != nil { @@ -89,7 +89,7 @@ func (this *Tunnel) Run() { //单向的,从src发送到dest if err != nil { log.Panic("Read panic. Tunnel id: %s. Remote Add: %s Local: %s. Err:%s", id, src.RemoteAddr().String(), src.LocalAddr().String(), err.Error()) } - log.Info("Reived %d bytes from %s. Tunnel: id %s", nByte, src.RemoteAddr().String(), id) + log.Debug("Reived %d bytes from %s. Tunnel: id %s", nByte, src.RemoteAddr().String(), id) if cipherDirection != SEND { sendCarrier.Cipher = nil } @@ -97,7 +97,7 @@ func (this *Tunnel) Run() { //单向的,从src发送到dest if err != nil { log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String()) } - log.Info("Write %d bytes from %s to %s. Tunnel: %s . 18 bytes %x", n, dest.LocalAddr(), dest.RemoteAddr().String(), id, sendCarrier.Cache[:18]) + log.Debug("Write %d bytes from %s to %s. Tunnel: %s . 18 bytes %x", n, dest.LocalAddr(), dest.RemoteAddr().String(), id, sendCarrier.Cache[:18]) } } diff --git a/server.bat b/server.bat index 70ce34d..520ae90 100644 --- a/server.bat +++ b/server.bat @@ -1 +1 @@ -.\TransX -destip 127.0.0.1 -destport 1084 -listenport 1082 -encrypt server -log applog/server.log \ No newline at end of file +.\TransX -destip 127.0.0.1 -destport 1084 -listenport 1082 -encrypt server -log applog/server.log -profileport 6060 \ No newline at end of file diff --git a/stat/tunnelstatus.go b/stat/tunnelstatus.go index fae7fea..24f03c9 100644 --- a/stat/tunnelstatus.go +++ b/stat/tunnelstatus.go @@ -54,11 +54,11 @@ func (this *TunnelStatusManager) chanListener() { select { case r := <-this.regChan: this.register(r) - log.Info("A tunnel registered") - log.Info(this.QueryStatString()) + log.Debug("A tunnel registered") + log.Debug(this.QueryStatString()) case ur := <-this.unregChan: this.unregister(ur) - log.Info("A tunnel unregistered") + log.Debug("A tunnel unregistered") } } } diff --git a/tscipher/cipher.go b/tscipher/cipher.go index df3e9c1..e61d29f 100644 --- a/tscipher/cipher.go +++ b/tscipher/cipher.go @@ -155,7 +155,7 @@ func SendData(carrier *Carrier, nByte int) (n int, err error) { //打包 wraped := WrapPackage(encrypedByte[:nByte]) n, err = carrier.Conn.Write(wraped) - log.Info("Ready to write id %s, 18 byte %s", carrier.AttachedTunnelID, string(wraped[:18])) + log.Debug("Ready to write id %s, 18 byte %s", carrier.AttachedTunnelID, string(wraped[:18])) copy(carrier.Cache, encrypedByte[:nByte]) // in case of debugging return } @@ -175,7 +175,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) { // log.Error("ReceiveData err %s", r) // } // }() - log.Info("id %s wrapedPackage := carrier.GetReceiveBuff()", carrier.AttachedTunnelID) + log.Debug("id %s wrapedPackage := carrier.GetReceiveBuff()", carrier.AttachedTunnelID) wrapedPackage := carrier.GetReceiveBuff() //make([]byte, 0, cap(carrier.Cache)) var packageData []byte var _rest []byte @@ -191,11 +191,11 @@ func ReceiveData(carrier *Carrier) (n int, err error) { log.Debug("id %s length of package %d", carrier.AttachedTunnelID, len(packageData)) if err == nil { //够一个完整的包 - log.Info("id %s capBuff := cap(carrier.GetReceiveBuff())", carrier.AttachedTunnelID) + log.Debug("id %s capBuff := cap(carrier.GetReceiveBuff())", carrier.AttachedTunnelID) capBuff := cap(carrier.GetReceiveBuff()) _buff := make([]byte, 0, capBuff) //释放 _buff = append(_buff, _rest...) - log.Info("id %s carrier.SetReceiveBuff(_buff)", carrier.AttachedTunnelID) + log.Debug("id %s carrier.SetReceiveBuff(_buff)", carrier.AttachedTunnelID) carrier.SetReceiveBuff(_buff) break }