feat: 🎸 add frontend gray plugin (#1120)

Co-authored-by: Kent Dong <ch3cho@qq.com>
This commit is contained in:
mamba
2024-07-15 15:47:04 +08:00
committed by GitHub
parent c00c8827f9
commit 5041277be3
9 changed files with 560 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
package util
import (
"net/url"
"strings"
"github.com/alibaba/higress/plugins/wasm-go/extensions/frontend-gray/config"
"github.com/tidwall/gjson"
)
// GetValueByCookie 根据 cookieStr 和 cookieName 获取 cookie 值
func GetValueByCookie(cookieStr string, cookieName string) string {
if cookieStr == "" {
return ""
}
cookies := strings.Split(cookieStr, ";")
curCookieName := cookieName + "="
var foundCookieValue string
var found bool
// 遍历找到 cookie 对并处理
for _, cookie := range cookies {
cookie = strings.TrimSpace(cookie) // 清理空白符
if strings.HasPrefix(cookie, curCookieName) {
foundCookieValue = cookie[len(curCookieName):]
found = true
break
}
}
if !found {
return ""
}
return foundCookieValue
}
// contains 检查切片 slice 中是否含有元素 value。
func Contains(slice []interface{}, value string) bool {
for _, item := range slice {
if item == value {
return true
}
}
return false
}
func GetRule(rules []*config.GrayRule, name string) *config.GrayRule {
for _, rule := range rules {
if rule.Name == name {
return rule
}
}
return nil
}
func GetBySubKey(grayInfoStr string, graySubKey string) string {
// 首先对 URL 编码的字符串进行解码
jsonStr, err := url.QueryUnescape(grayInfoStr)
if err != nil {
return ""
}
// 使用 gjson 从 JSON 字符串中提取 graySubKey 对应的值
value := gjson.Get(jsonStr, graySubKey)
// 检查所提取的值是否存在
if !value.Exists() {
return ""
}
// 返回字符串形式的值
return value.String()
}

View File

@@ -0,0 +1,42 @@
package util
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetValueByCookie(t *testing.T) {
var tests = []struct {
cookie, cookieKey, output string
}{
{"", "uid", ""},
{`cna=pf_9be76347560439f3b87daede1b485e37; uid=111`, "uid", "111"},
{`cna=pf_9be76347560439f3b87daede1b485e37; userid=222`, "userid", "222"},
{`uid=333`, "uid", "333"},
{`cna=pf_9be76347560439f3b87daede1b485e37;`, "uid", ""},
}
for _, test := range tests {
testName := test.cookie
t.Run(testName, func(t *testing.T) {
output := GetValueByCookie(test.cookie, test.cookieKey)
assert.Equal(t, test.output, output)
})
}
}
func TestDecodeJsonCookie(t *testing.T) {
var tests = []struct {
userInfoStr, grayJsonKey, output string
}{
{"{%22password%22:%22$2a$10$YAvYjA6783YeCi44/M395udIZ4Ll2iyKkQCzePaYx5NNG/aIWgICG%22%2C%22username%22:%22%E8%B0%A2%E6%99%AE%E8%80%80%22%2C%22authorities%22:[]%2C%22accountNonExpired%22:true%2C%22accountNonLocked%22:true%2C%22credentialsNonExpired%22:true%2C%22enabledd%22:true%2C%22id%22:838925798835720200%2C%22mobile%22:%22%22%2C%22userCode%22:%22noah%22%2C%22userName%22:%22%E8%B0%A2%E6%99%AE%E8%80%80%22%2C%22orgId%22:10%2C%22ocId%22:87%2C%22userType%22:%22OWN%22%2C%22firstLogin%22:false%2C%22ownOrgId%22:null%2C%22clientCode%22:%22%22%2C%22clientType%22:null%2C%22country%22:%22UAE%22%2C%22isGuide%22:null%2C%22acctId%22:null%2C%22userToken%22:null%2C%22deviceId%22:%223a47fec00a59d140%22%2C%22ocCode%22:%2299990002%22%2C%22secondType%22:%22dtl%22%2C%22vendorCode%22:%2210000001%22%2C%22status%22:%22ACTIVE%22%2C%22isDelete%22:false%2C%22email%22:%22%22%2C%22deleteStatus%22:null%2C%22deleteRequestDate%22:null%2C%22wechatId%22:null%2C%22userMfaInfoDTO%22:{%22checkMfa%22:false%2C%22checkSuccess%22:false%2C%22mobile%22:null%2C%22email%22:null%2C%22wechatId%22:null%2C%22totpSecret%22:null}}",
"userCode", "noah"},
}
for _, test := range tests {
testName := test.userInfoStr
t.Run(testName, func(t *testing.T) {
output := GetBySubKey(test.userInfoStr, test.grayJsonKey)
assert.Equal(t, test.output, output)
})
}
}