From 537605a86c1b7248f6775e40a53fa06426287606 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 16 May 2016 20:36:56 +0800 Subject: [PATCH 1/3] Add hideToken func. Signed-off-by: Bo-Yi Wu --- gorush/log.go | 16 +++++++++++++++- gorush/log_test.go | 5 +++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/gorush/log.go b/gorush/log.go index 42486f9..e22a76a 100644 --- a/gorush/log.go +++ b/gorush/log.go @@ -7,7 +7,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/gin-gonic/gin" "os" - // "time" + "strings" ) var ( @@ -181,6 +181,20 @@ func typeForPlatForm(platform int) string { } } +func hideToken(token string, starLen int) string { + if len(token) == 0 { + return "" + } + + start := token[len(token)-starLen:] + end := token[0:starLen] + + result := strings.Replace(token, start, strings.Repeat("*", starLen), -1) + result = strings.Replace(result, end, strings.Repeat("*", starLen), -1) + + return result +} + // LogPush record user push request and server response. func LogPush(status, token string, req PushNotification, errPush error) { var plat, platColor, output string diff --git a/gorush/log_test.go b/gorush/log_test.go index abbb698..8542d25 100644 --- a/gorush/log_test.go +++ b/gorush/log_test.go @@ -88,3 +88,8 @@ func TestPlatFormColor(t *testing.T) { assert.Equal(t, yellow, colorForPlatForm(PlatFormAndroid)) assert.Equal(t, reset, colorForPlatForm(1000000)) } + +func TestHideToken(t *testing.T) { + assert.Equal(t, "", hideToken("", 2)) + assert.Equal(t, "**345678**", hideToken("1234567890", 2)) +} From af63356e566e92cc7dad1d0eced9ffba0011a296 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 16 May 2016 21:04:05 +0800 Subject: [PATCH 2/3] add hide token config. Signed-off-by: Bo-Yi Wu --- config/config.go | 2 ++ config/config.yml | 1 + gorush/log.go | 18 +++++++++++++----- gorush/log_test.go | 1 + 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index d28e616..c3f768f 100644 --- a/config/config.go +++ b/config/config.go @@ -57,6 +57,7 @@ type SectionLog struct { AccessLevel string `yaml:"access_level"` ErrorLog string `yaml:"error_log"` ErrorLevel string `yaml:"error_level"` + HideToken bool `yaml:"hide_token"` } // SectionStat is sub seciont of config. @@ -115,6 +116,7 @@ func BuildDefaultPushConf() ConfYaml { conf.Log.AccessLevel = "debug" conf.Log.ErrorLog = "stderr" conf.Log.ErrorLevel = "error" + conf.Log.HideToken = true conf.Stat.Engine = "memory" conf.Stat.Redis.Addr = "localhost:6379" diff --git a/config/config.yml b/config/config.yml index 5fb8558..0d4fe6a 100644 --- a/config/config.yml +++ b/config/config.yml @@ -30,6 +30,7 @@ log: access_level: "debug" error_log: "stderr" error_level: "error" + hide_token: true stat: engine: "memory" diff --git a/gorush/log.go b/gorush/log.go index e22a76a..6f6601a 100644 --- a/gorush/log.go +++ b/gorush/log.go @@ -181,16 +181,20 @@ func typeForPlatForm(platform int) string { } } -func hideToken(token string, starLen int) string { +func hideToken(token string, markLen int) string { if len(token) == 0 { return "" } - start := token[len(token)-starLen:] - end := token[0:starLen] + if len(token) < markLen*2 { + return strings.Repeat("*", len(token)) + } - result := strings.Replace(token, start, strings.Repeat("*", starLen), -1) - result = strings.Replace(result, end, strings.Repeat("*", starLen), -1) + start := token[len(token)-markLen:] + end := token[0:markLen] + + result := strings.Replace(token, start, strings.Repeat("*", markLen), -1) + result = strings.Replace(result, end, strings.Repeat("*", markLen), -1) return result } @@ -207,6 +211,10 @@ func LogPush(status, token string, req PushNotification, errPush error) { errMsg = errPush.Error() } + if PushConf.Log.HideToken == true { + token = hideToken(token, 10) + } + log := &LogPushEntry{ Type: status, Platform: plat, diff --git a/gorush/log_test.go b/gorush/log_test.go index 8542d25..e637231 100644 --- a/gorush/log_test.go +++ b/gorush/log_test.go @@ -92,4 +92,5 @@ func TestPlatFormColor(t *testing.T) { func TestHideToken(t *testing.T) { assert.Equal(t, "", hideToken("", 2)) assert.Equal(t, "**345678**", hideToken("1234567890", 2)) + assert.Equal(t, "*****", hideToken("12345", 10)) } From b40bfbead5ca313633ce44acf3b6ef2f1e7e9744 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 16 May 2016 22:02:24 +0800 Subject: [PATCH 3/3] update readme. Signed-off-by: Bo-Yi Wu --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index af52952..6db4086 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ log: access_level: "debug" error_log: "stderr" # stderr: output to console, or define log path like "log/error_log" error_level: "error" + hide_token: true stat: engine: "memory" # support memory, redis or boltdb