change log library

master
dingjun 6 years ago
parent a1cfdbc1d6
commit 804836b7cb

@ -2,9 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"github.com/go-yaml/yaml"
"io/ioutil" "io/ioutil"
"testing" "testing"
yaml "gopkg.in/yaml.v2"
) )
func TestConf(t *testing.T) { func TestConf(t *testing.T) {
@ -18,8 +19,8 @@ func TestConf(t *testing.T) {
} }
fmt.Printf("%+v\n", c) fmt.Printf("%+v\n", c)
var testdata = map[string]string{ var testdata = map[string]string{
"www.example.com": "127.0.0.1:8443", "www.example.com": "127.0.0.1:8443 proxy-v2",
"b.example.com": "127.0.0.1:8541", "b.example.com": "127.0.0.1:8542",
} }
r := forwardRules(c.ForwardRules) r := forwardRules(c.ForwardRules)
for k, v := range testdata { for k, v := range testdata {

@ -1,19 +1,16 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"net"
"strings" "strings"
"github.com/go-yaml/yaml" glog "github.com/fangdingjun/go-log"
"github.com/golang/glog"
proxyproto "github.com/pires/go-proxyproto" proxyproto "github.com/pires/go-proxyproto"
yaml "gopkg.in/yaml.v2"
//"crypto/tls"
"flag"
//"log"
"net"
) )
func getSNIServerName(buf []byte) string { func getSNIServerName(buf []byte) string {
@ -99,6 +96,7 @@ func forward(c net.Conn, data []byte, dst string) {
defer c1.Close() defer c1.Close()
if proxyProto != 0 { if proxyProto != 0 {
glog.Debugf("send proxy proto v%d to %s", proxyProto, addr)
hdr.WriteTo(c1) hdr.WriteTo(c1)
} }
@ -143,12 +141,14 @@ func serve(c net.Conn) {
} }
servername := getSNIServerName(buf[:n]) servername := getSNIServerName(buf[:n])
if servername == "" { if servername == "" {
glog.Debugf("no sni, send to default")
forward(c, buf[:n], getDefaultDST()) forward(c, buf[:n], getDefaultDST())
return return
} }
dst := getDST(c, servername) dst := getDST(c, servername)
if dst == "" { if dst == "" {
dst = getDefaultDST() dst = getDefaultDST()
glog.Debugf("use default dst %s for sni %s", dst, servername)
} }
forward(c, buf[:n], dst) forward(c, buf[:n], dst)
} }
@ -157,8 +157,11 @@ var cfg conf
func main() { func main() {
var cfgfile string var cfgfile string
var logfile string
var loglevel string
flag.StringVar(&cfgfile, "c", "config.yaml", "config file") 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() flag.Parse()
data, err := ioutil.ReadFile(cfgfile) data, err := ioutil.ReadFile(cfgfile)
@ -169,6 +172,18 @@ func main() {
glog.Fatal(err) 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 { for _, d := range cfg.Listen {
glog.Infof("listen on :%d", d) glog.Infof("listen on :%d", d)
l, err := net.Listen("tcp", fmt.Sprintf(":%d", d)) l, err := net.Listen("tcp", fmt.Sprintf(":%d", d))

Loading…
Cancel
Save