1.增加web 显示profile

2.重新安排了一些日志的等级
This commit is contained in:
dmy@lab 2015-11-20 17:31:32 +08:00
parent fff90f7057
commit da0e77d74c
10 changed files with 39 additions and 22 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ TransX
server.log* server.log*
client.log* client.log*
applog applog
profile

View File

@ -58,19 +58,26 @@ func randMillionSecond() time.Duration {
func attack() { func attack() {
c := make(chan int) c := make(chan int)
for i := 0; i < 1; i++ { for i := 0; i < 10; i++ {
time.Sleep(time.Millisecond * randMillionSecond()) time.Sleep(time.Millisecond * randMillionSecond())
go doHttp(c) go doHttp(c)
} }
for i := 0; i < 1; i++ { for i := 0; i < 10; i++ {
<-c <-c
} }
log.Println("Finish") log.Println("Finish")
} }
func main() { func main1() {
llog.LogTo("applog/log.txt", "INFO") cfg := profile.Config{
defer profile.Start(profile.CPUProfile).Stop() 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 benchSeed = 0
go transClient() go transClient()
go transServer() go transServer()

View File

@ -9,6 +9,7 @@ var DestPort int
var ListenPort int var ListenPort int
var EncryptSide string var EncryptSide string
var LogTo string var LogTo string
var ProfilePort int
func init() { func init() {
flag.StringVar(&DestIP, "destip", "", "Destination IP") flag.StringVar(&DestIP, "destip", "", "Destination IP")
@ -16,4 +17,5 @@ func init() {
flag.StringVar(&LogTo, "log", "stdout", "Log to where") flag.StringVar(&LogTo, "log", "stdout", "Log to where")
flag.IntVar(&DestPort, "destport", 0, "Destination Port") flag.IntVar(&DestPort, "destport", 0, "Destination Port")
flag.IntVar(&ListenPort, "listenport", 0, "Listen Port") flag.IntVar(&ListenPort, "listenport", 0, "Listen Port")
flag.IntVar(&ProfilePort, "profileport", 0, "Profile Port")
} }

View File

@ -1 +1 @@
.\TransX -destip 127.0.0.1 -destport 1082 -listenport 1080 -encrypt client -log applog/client.log .\TransX -destip 127.0.0.1 -destport 1082 -listenport 1080 -encrypt client -log applog/client.log -profileport 6062

15
main.go
View File

@ -7,7 +7,9 @@ import (
"github.com/TransX/cli" "github.com/TransX/cli"
"github.com/TransX/log" "github.com/TransX/log"
"github.com/TransX/protocol" "github.com/TransX/protocol"
"github.com/davecheney/profile" // "github.com/davecheney/profile"
"net/http"
_ "net/http/pprof"
"strconv" "strconv"
) )
@ -23,10 +25,15 @@ func tunnel() {
} }
func main1() { func main() {
defer profile.Start(profile.CPUProfile).Stop() // defer profile.Start(profile.CPUProfile).Stop()
flag.Parse() flag.Parse()
fmt.Println("Hello World!") 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() tunnel()
} }

View File

@ -97,7 +97,7 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer
}() }()
} else { } else {
log.Info("Failed to accept incoming connection. %s", err) log.Error("Failed to accept incoming connection. %s", err)
} }
} }
} }

View File

@ -61,7 +61,7 @@ func (this *Tunnel) Run() { //单向的从src发送到dest
cipherDirection := this.cipherDirection cipherDirection := this.cipherDirection
id := this.id id := this.id
defer func() { defer func() {
log.Info("tunnel id %s ends", id) log.Debug("tunnel id %s ends", id)
//注销 //注销
this.unregChan <- this this.unregChan <- this
if r := recover(); r != nil { if r := recover(); r != nil {
@ -89,7 +89,7 @@ func (this *Tunnel) Run() { //单向的从src发送到dest
if err != nil { 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.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 { if cipherDirection != SEND {
sendCarrier.Cipher = nil sendCarrier.Cipher = nil
} }
@ -97,7 +97,7 @@ func (this *Tunnel) Run() { //单向的从src发送到dest
if err != nil { if err != nil {
log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String()) 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])
} }
} }

View File

@ -1 +1 @@
.\TransX -destip 127.0.0.1 -destport 1084 -listenport 1082 -encrypt server -log applog/server.log .\TransX -destip 127.0.0.1 -destport 1084 -listenport 1082 -encrypt server -log applog/server.log -profileport 6060

View File

@ -54,11 +54,11 @@ func (this *TunnelStatusManager) chanListener() {
select { select {
case r := <-this.regChan: case r := <-this.regChan:
this.register(r) this.register(r)
log.Info("A tunnel registered") log.Debug("A tunnel registered")
log.Info(this.QueryStatString()) log.Debug(this.QueryStatString())
case ur := <-this.unregChan: case ur := <-this.unregChan:
this.unregister(ur) this.unregister(ur)
log.Info("A tunnel unregistered") log.Debug("A tunnel unregistered")
} }
} }
} }

View File

@ -155,7 +155,7 @@ func SendData(carrier *Carrier, nByte int) (n int, err error) {
//打包 //打包
wraped := WrapPackage(encrypedByte[:nByte]) wraped := WrapPackage(encrypedByte[:nByte])
n, err = carrier.Conn.Write(wraped) 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 copy(carrier.Cache, encrypedByte[:nByte]) // in case of debugging
return return
} }
@ -175,7 +175,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
// log.Error("ReceiveData err %s", r) // 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)) wrapedPackage := carrier.GetReceiveBuff() //make([]byte, 0, cap(carrier.Cache))
var packageData []byte var packageData []byte
var _rest []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)) log.Debug("id %s length of package %d", carrier.AttachedTunnelID, len(packageData))
if err == nil { 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()) capBuff := cap(carrier.GetReceiveBuff())
_buff := make([]byte, 0, capBuff) //释放 _buff := make([]byte, 0, capBuff) //释放
_buff = append(_buff, _rest...) _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) carrier.SetReceiveBuff(_buff)
break break
} }