From 804836b7cbfefdc622ca23dee7851d5248ab93a6 Mon Sep 17 00:00:00 2001 From: dingjun Date: Mon, 15 Apr 2019 12:10:51 +0800 Subject: [PATCH] change log library --- conf_test.go | 7 ++++--- main.go | 31 +++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/conf_test.go b/conf_test.go index 70cf334..7cf5547 100644 --- a/conf_test.go +++ b/conf_test.go @@ -2,9 +2,10 @@ package main import ( "fmt" - "github.com/go-yaml/yaml" "io/ioutil" "testing" + + yaml "gopkg.in/yaml.v2" ) func TestConf(t *testing.T) { @@ -18,8 +19,8 @@ func TestConf(t *testing.T) { } fmt.Printf("%+v\n", c) var testdata = map[string]string{ - "www.example.com": "127.0.0.1:8443", - "b.example.com": "127.0.0.1:8541", + "www.example.com": "127.0.0.1:8443 proxy-v2", + "b.example.com": "127.0.0.1:8542", } r := forwardRules(c.ForwardRules) for k, v := range testdata { diff --git a/main.go b/main.go index 5f188db..7722469 100644 --- a/main.go +++ b/main.go @@ -1,19 +1,16 @@ package main import ( + "flag" "fmt" "io" "io/ioutil" + "net" "strings" - "github.com/go-yaml/yaml" - "github.com/golang/glog" + glog "github.com/fangdingjun/go-log" proxyproto "github.com/pires/go-proxyproto" - - //"crypto/tls" - "flag" - //"log" - "net" + yaml "gopkg.in/yaml.v2" ) func getSNIServerName(buf []byte) string { @@ -99,6 +96,7 @@ func forward(c net.Conn, data []byte, dst string) { defer c1.Close() if proxyProto != 0 { + glog.Debugf("send proxy proto v%d to %s", proxyProto, addr) hdr.WriteTo(c1) } @@ -143,12 +141,14 @@ func serve(c net.Conn) { } servername := getSNIServerName(buf[:n]) if servername == "" { + glog.Debugf("no sni, send to default") forward(c, buf[:n], getDefaultDST()) return } dst := getDST(c, servername) if dst == "" { dst = getDefaultDST() + glog.Debugf("use default dst %s for sni %s", dst, servername) } forward(c, buf[:n], dst) } @@ -157,8 +157,11 @@ var cfg conf func main() { var cfgfile string + var logfile string + var loglevel string flag.StringVar(&cfgfile, "c", "config.yaml", "config file") - flag.Set("logtostderr", "true") + flag.StringVar(&logfile, "log_file", "", "log file") + flag.StringVar(&loglevel, "log_level", "INFO", "log level") flag.Parse() data, err := ioutil.ReadFile(cfgfile) @@ -169,6 +172,18 @@ func main() { glog.Fatal(err) } + if logfile != "" { + glog.Default.Out = &glog.FixedSizeFileWriter{ + MaxCount: 4, + Name: logfile, + MaxSize: 10 * 1024 * 1024, + } + } + + if lv, err := glog.ParseLevel(loglevel); err == nil { + glog.Default.Level = lv + } + for _, d := range cfg.Listen { glog.Infof("listen on :%d", d) l, err := net.Listen("tcp", fmt.Sprintf(":%d", d))