From 486c455236a2e343fc4e8d62e410d346d6495561 Mon Sep 17 00:00:00 2001 From: nadoo <287492+nadoo@users.noreply.github.com> Date: Sun, 20 Oct 2019 17:52:25 +0800 Subject: [PATCH] log: show caller's info in verbose mode --- common/log/log.go | 8 +++++++- main.go | 3 ++- proxy/http/server.go | 2 +- proxy/tcptun/tcptun.go | 6 +++++- proxy/udptun/udptun.go | 4 ++++ proxy/uottun/uottun.go | 4 ++++ 6 files changed, 23 insertions(+), 4 deletions(-) diff --git a/common/log/log.go b/common/log/log.go index 9bf4e1c..ff34eb9 100644 --- a/common/log/log.go +++ b/common/log/log.go @@ -1,6 +1,12 @@ package log -import stdlog "log" +import ( + stdlog "log" +) + +func init() { + stdlog.SetFlags(stdlog.LstdFlags | stdlog.Lshortfile) +} // Func defines a simple log function type Func func(f string, v ...interface{}) diff --git a/main.go b/main.go index 4a9e7a4..50a9af2 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" stdlog "log" "os" "os/signal" @@ -38,7 +39,7 @@ func main() { // setup a log func log.F = func(f string, v ...interface{}) { if conf.Verbose { - stdlog.Printf(f, v...) + stdlog.Output(2, fmt.Sprintf(f, v...)) } } diff --git a/proxy/http/server.go b/proxy/http/server.go index 0b9c807..98bec17 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -61,7 +61,7 @@ func (s *HTTP) Serve(cc net.Conn) { if s.pretend { fmt.Fprintf(c, "%s 404 Not Found\r\nServer: nginx\r\n\r\n404 Not Found\r\n", req.proto) - log.F("[http] accessed by %s as web server", c.RemoteAddr().String()) + log.F("[http] %s <-> %s,pretend as web server", c.RemoteAddr().String(), s.Addr()) return } diff --git a/proxy/tcptun/tcptun.go b/proxy/tcptun/tcptun.go index 1898863..7da066b 100644 --- a/proxy/tcptun/tcptun.go +++ b/proxy/tcptun/tcptun.go @@ -1,6 +1,7 @@ package tcptun import ( + "errors" "net" "net/url" "strings" @@ -26,12 +27,15 @@ func init() { func NewTCPTun(s string, p proxy.Proxy) (*TCPTun, error) { u, err := url.Parse(s) if err != nil { - log.F("parse err: %s", err) + log.F("[tcptun] parse err: %s", err) return nil, err } addr := u.Host d := strings.Split(addr, "=") + if len(d) < 2 { + return nil, errors.New("error in strings.Split") + } t := &TCPTun{ proxy: p, diff --git a/proxy/udptun/udptun.go b/proxy/udptun/udptun.go index d2d66a6..0bee32a 100644 --- a/proxy/udptun/udptun.go +++ b/proxy/udptun/udptun.go @@ -1,6 +1,7 @@ package udptun import ( + "errors" "net" "net/url" "strings" @@ -34,6 +35,9 @@ func NewUDPTun(s string, p proxy.Proxy) (*UDPTun, error) { addr := u.Host d := strings.Split(addr, "=") + if len(d) < 2 { + return nil, errors.New("error in strings.Split") + } ut := &UDPTun{ proxy: p, diff --git a/proxy/uottun/uottun.go b/proxy/uottun/uottun.go index 1f6be96..3bbe142 100644 --- a/proxy/uottun/uottun.go +++ b/proxy/uottun/uottun.go @@ -1,6 +1,7 @@ package uottun import ( + "errors" "io/ioutil" "net" "net/url" @@ -34,6 +35,9 @@ func NewUoTTun(s string, p proxy.Proxy) (*UoTTun, error) { addr := u.Host d := strings.Split(addr, "=") + if len(d) < 2 { + return nil, errors.New("error in strings.Split") + } ut := &UoTTun{ proxy: p,