diff --git a/cmd/kone/main.go b/cmd/kone/main.go index 609fb7f..0d1dbb8 100644 --- a/cmd/kone/main.go +++ b/cmd/kone/main.go @@ -10,11 +10,27 @@ import ( "fmt" "os" + "github.com/op/go-logging" "github.com/xjdrew/kone" - . "github.com/xjdrew/kone/internal" ) -var VERSION = "0.2-dev" +var VERSION = "0.3-dev" + +var logger = logging.MustGetLogger("kone") + +func InitLogger(debug bool) { + format := logging.MustStringFormatter( + `%{color}%{time:06-01-02 15:04:05.000} %{level:.4s} @%{shortfile}%{color:reset} %{message}`, + ) + logging.SetFormatter(format) + logging.SetBackend(logging.NewLogBackend(os.Stdout, "", 0)) + + if debug { + logging.SetLevel(logging.DEBUG, "kone") + } else { + logging.SetLevel(logging.INFO, "kone") + } +} func main() { version := flag.Bool("version", false, "Get version info") @@ -28,7 +44,6 @@ func main() { } InitLogger(*debug) - logger := GetLogger() configFile := *config if configFile == "" { diff --git a/internal/logger.go b/internal/logger.go deleted file mode 100644 index ece8154..0000000 --- a/internal/logger.go +++ /dev/null @@ -1,32 +0,0 @@ -// -// date : 2016-05-13 -// author: xjdrew -// - -package kone - -import ( - "os" - - "github.com/op/go-logging" -) - -var logger = logging.MustGetLogger("kone") - -func InitLogger(debug bool) { - format := logging.MustStringFormatter( - `%{color}%{time:06-01-02 15:04:05.000} %{level:.4s} @%{shortfile}%{color:reset} %{message}`, - ) - logging.SetFormatter(format) - logging.SetBackend(logging.NewLogBackend(os.Stdout, "", 0)) - - if debug { - logging.SetLevel(logging.DEBUG, "kone") - } else { - logging.SetLevel(logging.INFO, "kone") - } -} - -func GetLogger() *logging.Logger { - return logger -} diff --git a/one.go b/one.go index 4a9cf09..cbcbbd2 100644 --- a/one.go +++ b/one.go @@ -9,11 +9,11 @@ import ( "net" "sync" - . "github.com/xjdrew/kone/internal" + "github.com/op/go-logging" "github.com/xjdrew/kone/tcpip" ) -var logger = GetLogger() +var logger = logging.MustGetLogger("kone") type One struct { // tun ip diff --git a/pattern.go b/pattern.go index 00d7fdf..eb00b69 100644 --- a/pattern.go +++ b/pattern.go @@ -176,6 +176,7 @@ func CreatePattern(rc RuleConfig) Pattern { proxy := rc.Proxy pattern := rc.Pattern schema := strings.ToUpper(rc.Scheme) + switch schema { case "DOMAIN": return NewDomainPattern(proxy, pattern) @@ -187,18 +188,18 @@ func CreatePattern(rc RuleConfig) Pattern { fallthrough case "IP-CIDR6": if proxy == "DIRECT" { // all IPNet default proxy is DIRECT + logger.Debugf("skip DIRECT rule: %s,%s,%s", rc.Scheme, rc.Pattern, rc.Proxy) return nil } _, ipNet, err := net.ParseCIDR(pattern) if err == nil { return NewIPCIDRPattern(proxy, ipNet) - } else { - return nil } case "GEOIP": return NewGEOIPPattern(proxy, pattern) case "FINAL": return NewFinalPattern(proxy) } + logger.Errorf("invalid rule: %s,%s,%s", rc.Scheme, rc.Pattern, rc.Proxy) return nil } diff --git a/rule.go b/rule.go index 468c654..1c2997b 100644 --- a/rule.go +++ b/rule.go @@ -43,8 +43,6 @@ func NewRule(rcs []RuleConfig) *Rule { for _, rc := range rcs { if pattern := CreatePattern(rc); pattern != nil { rule.patterns = append(rule.patterns, pattern) - } else { - logger.Errorf("invalid rule: %s,%s,%s", rc.Scheme, rc.Pattern, rc.Proxy) } } return rule