Merge pull request #86 from appleboy/hidetoken

Fixed #85 Add hideToken func.
This commit is contained in:
Bo-Yi Wu 2016-05-16 22:03:58 +08:00
commit a918e55b6a
5 changed files with 33 additions and 1 deletions

View File

@ -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

View File

@ -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"

View File

@ -30,6 +30,7 @@ log:
access_level: "debug"
error_log: "stderr"
error_level: "error"
hide_token: true
stat:
engine: "memory"

View File

@ -7,7 +7,7 @@ import (
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"os"
// "time"
"strings"
)
var (
@ -181,6 +181,24 @@ func typeForPlatForm(platform int) string {
}
}
func hideToken(token string, markLen int) string {
if len(token) == 0 {
return ""
}
if len(token) < markLen*2 {
return strings.Repeat("*", len(token))
}
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
}
// LogPush record user push request and server response.
func LogPush(status, token string, req PushNotification, errPush error) {
var plat, platColor, output string
@ -193,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,

View File

@ -88,3 +88,9 @@ 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))
assert.Equal(t, "*****", hideToken("12345", 10))
}