mirror of
https://github.com/alibaba/higress.git
synced 2026-06-09 04:37:31 +08:00
Fix log import (#1957)
This commit is contained in:
@@ -19,7 +19,6 @@ import (
|
|||||||
|
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -56,7 +55,7 @@ func writeTraceAttribute(ctx wrapper.HttpContext) {
|
|||||||
ctx.WriteUserAttributeToTrace()
|
ctx.WriteUserAttributeToTrace()
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config CustomLogConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config CustomLogConfig, log wrapper.Log) types.Action {
|
||||||
if rand.Intn(10)%3 == 1 {
|
if rand.Intn(10)%3 == 1 {
|
||||||
writeLog(ctx)
|
writeLog(ctx)
|
||||||
} else if rand.Intn(10)%3 == 2 {
|
} else if rand.Intn(10)%3 == 2 {
|
||||||
|
|||||||
@@ -2,11 +2,9 @@ module github.com/alibaba/higress/plugins/wasm-go/extensions/ai-agent
|
|||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
replace github.com/alibaba/higress/plugins/wasm-go => ../..
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/alibaba/higress/plugins/wasm-go v1.4.2
|
github.com/alibaba/higress/plugins/wasm-go v1.4.2
|
||||||
github.com/higress-group/proxy-wasm-go-sdk v1.0.0
|
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240711023527-ba358c48772f
|
||||||
github.com/tidwall/gjson v1.17.3
|
github.com/tidwall/gjson v1.17.3
|
||||||
gopkg.in/yaml.v2 v2.4.0
|
gopkg.in/yaml.v2 v2.4.0
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
|
github.com/alibaba/higress/plugins/wasm-go v1.4.2 h1:gH7OIGXm4wtW5Vo7L2deMPqF7OVWNESDHv1CaaTGu6s=
|
||||||
|
github.com/alibaba/higress/plugins/wasm-go v1.4.2/go.mod h1:359don/ahMxpfeLMzr29Cjwcu8IywTTDUzWlBPRNLHw=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520 h1:IHDghbGQ2DTIXHBHxWfqCYQW1fKjyJ/I7W1pMyUDeEA=
|
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520 h1:IHDghbGQ2DTIXHBHxWfqCYQW1fKjyJ/I7W1pMyUDeEA=
|
||||||
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520/go.mod h1:Nz8ORLaFiLWotg6GeKlJMhv8cci8mM43uEnLA5t8iew=
|
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520/go.mod h1:Nz8ORLaFiLWotg6GeKlJMhv8cci8mM43uEnLA5t8iew=
|
||||||
github.com/higress-group/proxy-wasm-go-sdk v1.0.0 h1:BZRNf4R7jr9hwRivg/E29nkVaKEak5MWjBDhWjuHijU=
|
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240711023527-ba358c48772f h1:ZIiIBRvIw62gA5MJhuwp1+2wWbqL9IGElQ499rUsYYg=
|
||||||
github.com/higress-group/proxy-wasm-go-sdk v1.0.0/go.mod h1:iiSyFbo+rAtbtGt/bsefv8GU57h9CCLYGJA74/tF5/0=
|
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240711023527-ba358c48772f/go.mod h1:hNFjhrLUIq+kJ9bOcs8QtiplSQ61GZXtd2xHKx4BYRo=
|
||||||
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
|
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
|
||||||
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
|
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-agent/dashscope"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-agent/dashscope"
|
||||||
prompttpl "github.com/alibaba/higress/plugins/wasm-go/extensions/ai-agent/promptTpl"
|
prompttpl "github.com/alibaba/higress/plugins/wasm-go/extensions/ai-agent/promptTpl"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -38,7 +37,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(gjson gjson.Result, c *PluginConfig, log log.Log) error {
|
func parseConfig(gjson gjson.Result, c *PluginConfig, log wrapper.Log) error {
|
||||||
initResponsePromptTpl(gjson, c)
|
initResponsePromptTpl(gjson, c)
|
||||||
|
|
||||||
err := initAPIs(gjson, c)
|
err := initAPIs(gjson, c)
|
||||||
@@ -55,11 +54,11 @@ func parseConfig(gjson gjson.Result, c *PluginConfig, log log.Log) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log) types.Action {
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func firstReq(ctx wrapper.HttpContext, config PluginConfig, prompt string, rawRequest Request, log log.Log) types.Action {
|
func firstReq(ctx wrapper.HttpContext, config PluginConfig, prompt string, rawRequest Request, log wrapper.Log) types.Action {
|
||||||
log.Debugf("[onHttpRequestBody] firstreq:%s", prompt)
|
log.Debugf("[onHttpRequestBody] firstreq:%s", prompt)
|
||||||
|
|
||||||
var userMessage Message
|
var userMessage Message
|
||||||
@@ -89,7 +88,7 @@ func firstReq(ctx wrapper.HttpContext, config PluginConfig, prompt string, rawRe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
log.Debug("onHttpRequestBody start")
|
log.Debug("onHttpRequestBody start")
|
||||||
defer log.Debug("onHttpRequestBody end")
|
defer log.Debug("onHttpRequestBody end")
|
||||||
|
|
||||||
@@ -173,7 +172,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log) types.Action {
|
||||||
log.Debug("onHttpResponseHeaders start")
|
log.Debug("onHttpResponseHeaders start")
|
||||||
defer log.Debug("onHttpResponseHeaders end")
|
defer log.Debug("onHttpResponseHeaders end")
|
||||||
|
|
||||||
@@ -201,7 +200,7 @@ func extractJson(bodyStr string) (string, error) {
|
|||||||
return jsonStr, nil
|
return jsonStr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func jsonFormat(llmClient wrapper.HttpClient, llmInfo LLMInfo, jsonSchema map[string]interface{}, assistantMessage Message, actionInput string, headers [][2]string, streamMode bool, rawResponse Response, log log.Log) string {
|
func jsonFormat(llmClient wrapper.HttpClient, llmInfo LLMInfo, jsonSchema map[string]interface{}, assistantMessage Message, actionInput string, headers [][2]string, streamMode bool, rawResponse Response, log wrapper.Log) string {
|
||||||
prompt := fmt.Sprintf(prompttpl.Json_Resp_Template, jsonSchema, actionInput)
|
prompt := fmt.Sprintf(prompttpl.Json_Resp_Template, jsonSchema, actionInput)
|
||||||
|
|
||||||
messages := []dashscope.Message{{Role: "user", Content: prompt}}
|
messages := []dashscope.Message{{Role: "user", Content: prompt}}
|
||||||
@@ -242,7 +241,7 @@ func jsonFormat(llmClient wrapper.HttpClient, llmInfo LLMInfo, jsonSchema map[st
|
|||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
|
|
||||||
func noneStream(assistantMessage Message, actionInput string, rawResponse Response, log log.Log) {
|
func noneStream(assistantMessage Message, actionInput string, rawResponse Response, log wrapper.Log) {
|
||||||
assistantMessage.Role = "assistant"
|
assistantMessage.Role = "assistant"
|
||||||
assistantMessage.Content = actionInput
|
assistantMessage.Content = actionInput
|
||||||
rawResponse.Choices[0].Message = assistantMessage
|
rawResponse.Choices[0].Message = assistantMessage
|
||||||
@@ -258,7 +257,7 @@ func noneStream(assistantMessage Message, actionInput string, rawResponse Respon
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func stream(actionInput string, rawResponse Response, log log.Log) {
|
func stream(actionInput string, rawResponse Response, log wrapper.Log) {
|
||||||
headers := [][2]string{{"content-type", "text/event-stream; charset=utf-8"}}
|
headers := [][2]string{{"content-type", "text/event-stream; charset=utf-8"}}
|
||||||
proxywasm.ReplaceHttpResponseHeaders(headers)
|
proxywasm.ReplaceHttpResponseHeaders(headers)
|
||||||
// Remove quotes from actionInput
|
// Remove quotes from actionInput
|
||||||
@@ -272,7 +271,7 @@ func stream(actionInput string, rawResponse Response, log log.Log) {
|
|||||||
proxywasm.ResumeHttpResponse()
|
proxywasm.ResumeHttpResponse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func toolsCallResult(ctx wrapper.HttpContext, llmClient wrapper.HttpClient, llmInfo LLMInfo, jsonResp JsonResp, aPIsParam []APIsParam, aPIClient []wrapper.HttpClient, content string, rawResponse Response, log log.Log, statusCode int, responseBody []byte) {
|
func toolsCallResult(ctx wrapper.HttpContext, llmClient wrapper.HttpClient, llmInfo LLMInfo, jsonResp JsonResp, aPIsParam []APIsParam, aPIClient []wrapper.HttpClient, content string, rawResponse Response, log wrapper.Log, statusCode int, responseBody []byte) {
|
||||||
if statusCode != http.StatusOK {
|
if statusCode != http.StatusOK {
|
||||||
log.Debugf("statusCode: %d", statusCode)
|
log.Debugf("statusCode: %d", statusCode)
|
||||||
}
|
}
|
||||||
@@ -333,7 +332,7 @@ func toolsCallResult(ctx wrapper.HttpContext, llmClient wrapper.HttpClient, llmI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func outputParser(response string, log log.Log) (string, string) {
|
func outputParser(response string, log wrapper.Log) (string, string) {
|
||||||
log.Debugf("Raw response:%s", response)
|
log.Debugf("Raw response:%s", response)
|
||||||
|
|
||||||
start := strings.Index(response, "```")
|
start := strings.Index(response, "```")
|
||||||
@@ -380,7 +379,7 @@ func outputParser(response string, log log.Log) (string, string) {
|
|||||||
return "", ""
|
return "", ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func toolsCall(ctx wrapper.HttpContext, llmClient wrapper.HttpClient, llmInfo LLMInfo, jsonResp JsonResp, aPIsParam []APIsParam, aPIClient []wrapper.HttpClient, content string, rawResponse Response, log log.Log) (types.Action, string) {
|
func toolsCall(ctx wrapper.HttpContext, llmClient wrapper.HttpClient, llmInfo LLMInfo, jsonResp JsonResp, aPIsParam []APIsParam, aPIClient []wrapper.HttpClient, content string, rawResponse Response, log wrapper.Log) (types.Action, string) {
|
||||||
dashscope.MessageStore.AddForAssistant(content)
|
dashscope.MessageStore.AddForAssistant(content)
|
||||||
|
|
||||||
action, actionInput := outputParser(content, log)
|
action, actionInput := outputParser(content, log)
|
||||||
@@ -515,7 +514,7 @@ func toolsCall(ctx wrapper.HttpContext, llmClient wrapper.HttpClient, llmInfo LL
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 从response接收到firstreq的大模型返回
|
// 从response接收到firstreq的大模型返回
|
||||||
func onHttpResponseBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log log.Log) types.Action {
|
func onHttpResponseBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
log.Debugf("onHttpResponseBody start")
|
log.Debugf("onHttpResponseBody start")
|
||||||
defer log.Debugf("onHttpResponseBody end")
|
defer log.Debugf("onHttpResponseBody end")
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@@ -16,7 +15,7 @@ const (
|
|||||||
|
|
||||||
type providerInitializer interface {
|
type providerInitializer interface {
|
||||||
ValidateConfig(ProviderConfig) error
|
ValidateConfig(ProviderConfig) error
|
||||||
CreateProvider(ProviderConfig, log.Log) (Provider, error)
|
CreateProvider(ProviderConfig, wrapper.Log) (Provider, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -129,7 +128,7 @@ func (c *ProviderConfig) Validate() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateProvider(pc ProviderConfig, log log.Log) (Provider, error) {
|
func CreateProvider(pc ProviderConfig, log wrapper.Log) (Provider, error) {
|
||||||
initializer, has := providerInitializers[pc.typ]
|
initializer, has := providerInitializers[pc.typ]
|
||||||
if !has {
|
if !has {
|
||||||
return nil, errors.New("unknown provider type: " + pc.typ)
|
return nil, errors.New("unknown provider type: " + pc.typ)
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package cache
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,7 +16,7 @@ func (r *redisProviderInitializer) ValidateConfig(cf ProviderConfig) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *redisProviderInitializer) CreateProvider(cf ProviderConfig, log log.Log) (Provider, error) {
|
func (r *redisProviderInitializer) CreateProvider(cf ProviderConfig, log wrapper.Log) (Provider, error) {
|
||||||
rp := redisProvider{
|
rp := redisProvider{
|
||||||
config: cf,
|
config: cf,
|
||||||
client: wrapper.NewRedisClusterClient(wrapper.FQDNCluster{
|
client: wrapper.NewRedisClusterClient(wrapper.FQDNCluster{
|
||||||
@@ -33,7 +32,7 @@ func (r *redisProviderInitializer) CreateProvider(cf ProviderConfig, log log.Log
|
|||||||
type redisProvider struct {
|
type redisProvider struct {
|
||||||
config ProviderConfig
|
config ProviderConfig
|
||||||
client wrapper.RedisClient
|
client wrapper.RedisClient
|
||||||
log log.Log
|
log wrapper.Log
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *redisProvider) GetProviderType() string {
|
func (rp *redisProvider) GetProviderType() string {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/cache"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/cache"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/embedding"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/embedding"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/vector"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/vector"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ type PluginConfig struct {
|
|||||||
CacheKeyStrategy string
|
CacheKeyStrategy string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PluginConfig) FromJson(json gjson.Result, log log.Log) {
|
func (c *PluginConfig) FromJson(json gjson.Result, log wrapper.Log) {
|
||||||
c.embeddingProviderConfig = &embedding.ProviderConfig{}
|
c.embeddingProviderConfig = &embedding.ProviderConfig{}
|
||||||
c.vectorProviderConfig = &vector.ProviderConfig{}
|
c.vectorProviderConfig = &vector.ProviderConfig{}
|
||||||
c.cacheProviderConfig = &cache.ProviderConfig{}
|
c.cacheProviderConfig = &cache.ProviderConfig{}
|
||||||
@@ -140,7 +140,7 @@ func (c *PluginConfig) Validate() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PluginConfig) Complete(log log.Log) error {
|
func (c *PluginConfig) Complete(log wrapper.Log) error {
|
||||||
var err error
|
var err error
|
||||||
if c.embeddingProviderConfig.GetProviderType() != "" {
|
if c.embeddingProviderConfig.GetProviderType() != "" {
|
||||||
log.Debugf("embedding provider is set to %s", c.embeddingProviderConfig.GetProviderType())
|
log.Debugf("embedding provider is set to %s", c.embeddingProviderConfig.GetProviderType())
|
||||||
@@ -191,7 +191,7 @@ func (c *PluginConfig) GetCacheProvider() cache.Provider {
|
|||||||
return c.cacheProvider
|
return c.cacheProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertLegacyMapFields(c *PluginConfig, json gjson.Result, log log.Log) {
|
func convertLegacyMapFields(c *PluginConfig, json gjson.Result, log wrapper.Log) {
|
||||||
keyMap := map[string]string{
|
keyMap := map[string]string{
|
||||||
"cacheKeyFrom.requestBody": "cacheKeyFrom",
|
"cacheKeyFrom.requestBody": "cacheKeyFrom",
|
||||||
"cacheValueFrom.requestBody": "cacheValueFrom",
|
"cacheValueFrom.requestBody": "cacheValueFrom",
|
||||||
@@ -210,7 +210,7 @@ func convertLegacyMapFields(c *PluginConfig, json gjson.Result, log log.Log) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setField(c *PluginConfig, fieldName string, value string, log log.Log) {
|
func setField(c *PluginConfig, fieldName string, value string, log wrapper.Log) {
|
||||||
switch fieldName {
|
switch fieldName {
|
||||||
case "cacheKeyFrom":
|
case "cacheKeyFrom":
|
||||||
c.CacheKeyFrom = value
|
c.CacheKeyFrom = value
|
||||||
|
|||||||
@@ -8,14 +8,13 @@ import (
|
|||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/config"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/config"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/vector"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/vector"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/tidwall/resp"
|
"github.com/tidwall/resp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckCacheForKey checks if the key is in the cache, or triggers similarity search if not found.
|
// CheckCacheForKey checks if the key is in the cache, or triggers similarity search if not found.
|
||||||
func CheckCacheForKey(key string, ctx wrapper.HttpContext, c config.PluginConfig, log log.Log, stream bool, useSimilaritySearch bool) error {
|
func CheckCacheForKey(key string, ctx wrapper.HttpContext, c config.PluginConfig, log wrapper.Log, stream bool, useSimilaritySearch bool) error {
|
||||||
activeCacheProvider := c.GetCacheProvider()
|
activeCacheProvider := c.GetCacheProvider()
|
||||||
if activeCacheProvider == nil {
|
if activeCacheProvider == nil {
|
||||||
log.Debugf("[%s] [CheckCacheForKey] no cache provider configured, performing similarity search", PLUGIN_NAME)
|
log.Debugf("[%s] [CheckCacheForKey] no cache provider configured, performing similarity search", PLUGIN_NAME)
|
||||||
@@ -38,7 +37,7 @@ func CheckCacheForKey(key string, ctx wrapper.HttpContext, c config.PluginConfig
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handleCacheResponse processes cache response and handles cache hits and misses.
|
// handleCacheResponse processes cache response and handles cache hits and misses.
|
||||||
func handleCacheResponse(key string, response resp.Value, ctx wrapper.HttpContext, log log.Log, stream bool, c config.PluginConfig, useSimilaritySearch bool) {
|
func handleCacheResponse(key string, response resp.Value, ctx wrapper.HttpContext, log wrapper.Log, stream bool, c config.PluginConfig, useSimilaritySearch bool) {
|
||||||
if err := response.Error(); err == nil && !response.IsNull() {
|
if err := response.Error(); err == nil && !response.IsNull() {
|
||||||
log.Infof("[%s] cache hit for key: %s", PLUGIN_NAME, key)
|
log.Infof("[%s] cache hit for key: %s", PLUGIN_NAME, key)
|
||||||
processCacheHit(key, response.String(), stream, ctx, c, log)
|
processCacheHit(key, response.String(), stream, ctx, c, log)
|
||||||
@@ -61,7 +60,7 @@ func handleCacheResponse(key string, response resp.Value, ctx wrapper.HttpContex
|
|||||||
}
|
}
|
||||||
|
|
||||||
// processCacheHit handles a successful cache hit.
|
// processCacheHit handles a successful cache hit.
|
||||||
func processCacheHit(key string, response string, stream bool, ctx wrapper.HttpContext, c config.PluginConfig, log log.Log) {
|
func processCacheHit(key string, response string, stream bool, ctx wrapper.HttpContext, c config.PluginConfig, log wrapper.Log) {
|
||||||
if strings.TrimSpace(response) == "" {
|
if strings.TrimSpace(response) == "" {
|
||||||
log.Warnf("[%s] [processCacheHit] cached response for key %s is empty", PLUGIN_NAME, key)
|
log.Warnf("[%s] [processCacheHit] cached response for key %s is empty", PLUGIN_NAME, key)
|
||||||
proxywasm.ResumeHttpRequest()
|
proxywasm.ResumeHttpRequest()
|
||||||
@@ -86,7 +85,7 @@ func processCacheHit(key string, response string, stream bool, ctx wrapper.HttpC
|
|||||||
}
|
}
|
||||||
|
|
||||||
// performSimilaritySearch determines the appropriate similarity search method to use.
|
// performSimilaritySearch determines the appropriate similarity search method to use.
|
||||||
func performSimilaritySearch(key string, ctx wrapper.HttpContext, c config.PluginConfig, log log.Log, queryString string, stream bool) error {
|
func performSimilaritySearch(key string, ctx wrapper.HttpContext, c config.PluginConfig, log wrapper.Log, queryString string, stream bool) error {
|
||||||
activeVectorProvider := c.GetVectorProvider()
|
activeVectorProvider := c.GetVectorProvider()
|
||||||
if activeVectorProvider == nil {
|
if activeVectorProvider == nil {
|
||||||
return logAndReturnError(log, "[performSimilaritySearch] no vector provider configured for similarity search")
|
return logAndReturnError(log, "[performSimilaritySearch] no vector provider configured for similarity search")
|
||||||
@@ -108,19 +107,19 @@ func performSimilaritySearch(key string, ctx wrapper.HttpContext, c config.Plugi
|
|||||||
}
|
}
|
||||||
|
|
||||||
// performStringQuery executes the string-based similarity search.
|
// performStringQuery executes the string-based similarity search.
|
||||||
func performStringQuery(key string, queryString string, ctx wrapper.HttpContext, c config.PluginConfig, log log.Log, stream bool) error {
|
func performStringQuery(key string, queryString string, ctx wrapper.HttpContext, c config.PluginConfig, log wrapper.Log, stream bool) error {
|
||||||
stringQuerier, ok := c.GetVectorProvider().(vector.StringQuerier)
|
stringQuerier, ok := c.GetVectorProvider().(vector.StringQuerier)
|
||||||
if !ok {
|
if !ok {
|
||||||
return logAndReturnError(log, "[performStringQuery] active vector provider does not implement StringQuerier interface")
|
return logAndReturnError(log, "[performStringQuery] active vector provider does not implement StringQuerier interface")
|
||||||
}
|
}
|
||||||
|
|
||||||
return stringQuerier.QueryString(queryString, ctx, log, func(results []vector.QueryResult, ctx wrapper.HttpContext, log log.Log, err error) {
|
return stringQuerier.QueryString(queryString, ctx, log, func(results []vector.QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error) {
|
||||||
handleQueryResults(key, results, ctx, log, stream, c, err)
|
handleQueryResults(key, results, ctx, log, stream, c, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// performEmbeddingQuery executes the embedding-based similarity search.
|
// performEmbeddingQuery executes the embedding-based similarity search.
|
||||||
func performEmbeddingQuery(key string, ctx wrapper.HttpContext, c config.PluginConfig, log log.Log, stream bool) error {
|
func performEmbeddingQuery(key string, ctx wrapper.HttpContext, c config.PluginConfig, log wrapper.Log, stream bool) error {
|
||||||
embeddingQuerier, ok := c.GetVectorProvider().(vector.EmbeddingQuerier)
|
embeddingQuerier, ok := c.GetVectorProvider().(vector.EmbeddingQuerier)
|
||||||
if !ok {
|
if !ok {
|
||||||
return logAndReturnError(log, fmt.Sprintf("[performEmbeddingQuery] active vector provider does not implement EmbeddingQuerier interface"))
|
return logAndReturnError(log, fmt.Sprintf("[performEmbeddingQuery] active vector provider does not implement EmbeddingQuerier interface"))
|
||||||
@@ -139,7 +138,7 @@ func performEmbeddingQuery(key string, ctx wrapper.HttpContext, c config.PluginC
|
|||||||
}
|
}
|
||||||
ctx.SetContext(CACHE_KEY_EMBEDDING_KEY, textEmbedding)
|
ctx.SetContext(CACHE_KEY_EMBEDDING_KEY, textEmbedding)
|
||||||
|
|
||||||
err = embeddingQuerier.QueryEmbedding(textEmbedding, ctx, log, func(results []vector.QueryResult, ctx wrapper.HttpContext, log log.Log, err error) {
|
err = embeddingQuerier.QueryEmbedding(textEmbedding, ctx, log, func(results []vector.QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error) {
|
||||||
handleQueryResults(key, results, ctx, log, stream, c, err)
|
handleQueryResults(key, results, ctx, log, stream, c, err)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -149,7 +148,7 @@ func performEmbeddingQuery(key string, ctx wrapper.HttpContext, c config.PluginC
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handleQueryResults processes the results of similarity search and determines next actions.
|
// handleQueryResults processes the results of similarity search and determines next actions.
|
||||||
func handleQueryResults(key string, results []vector.QueryResult, ctx wrapper.HttpContext, log log.Log, stream bool, c config.PluginConfig, err error) {
|
func handleQueryResults(key string, results []vector.QueryResult, ctx wrapper.HttpContext, log wrapper.Log, stream bool, c config.PluginConfig, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleInternalError(err, fmt.Sprintf("[%s] [handleQueryResults] error querying vector database for key: %s", PLUGIN_NAME, key), log)
|
handleInternalError(err, fmt.Sprintf("[%s] [handleQueryResults] error querying vector database for key: %s", PLUGIN_NAME, key), log)
|
||||||
return
|
return
|
||||||
@@ -187,14 +186,14 @@ func handleQueryResults(key string, results []vector.QueryResult, ctx wrapper.Ht
|
|||||||
}
|
}
|
||||||
|
|
||||||
// logAndReturnError logs an error and returns it.
|
// logAndReturnError logs an error and returns it.
|
||||||
func logAndReturnError(log log.Log, message string) error {
|
func logAndReturnError(log wrapper.Log, message string) error {
|
||||||
message = fmt.Sprintf("[%s] %s", PLUGIN_NAME, message)
|
message = fmt.Sprintf("[%s] %s", PLUGIN_NAME, message)
|
||||||
log.Errorf(message)
|
log.Errorf(message)
|
||||||
return errors.New(message)
|
return errors.New(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleInternalError logs an error and resumes the HTTP request.
|
// handleInternalError logs an error and resumes the HTTP request.
|
||||||
func handleInternalError(err error, message string, log log.Log) {
|
func handleInternalError(err error, message string, log wrapper.Log) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[%s] [handleInternalError] %s: %v", PLUGIN_NAME, message, err)
|
log.Errorf("[%s] [handleInternalError] %s: %v", PLUGIN_NAME, message, err)
|
||||||
} else {
|
} else {
|
||||||
@@ -205,7 +204,7 @@ func handleInternalError(err error, message string, log log.Log) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Caches the response value
|
// Caches the response value
|
||||||
func cacheResponse(ctx wrapper.HttpContext, c config.PluginConfig, key string, value string, log log.Log) {
|
func cacheResponse(ctx wrapper.HttpContext, c config.PluginConfig, key string, value string, log wrapper.Log) {
|
||||||
if strings.TrimSpace(value) == "" {
|
if strings.TrimSpace(value) == "" {
|
||||||
log.Warnf("[%s] [cacheResponse] cached value for key %s is empty", PLUGIN_NAME, key)
|
log.Warnf("[%s] [cacheResponse] cached value for key %s is empty", PLUGIN_NAME, key)
|
||||||
return
|
return
|
||||||
@@ -220,7 +219,7 @@ func cacheResponse(ctx wrapper.HttpContext, c config.PluginConfig, key string, v
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handles embedding upload if available
|
// Handles embedding upload if available
|
||||||
func uploadEmbeddingAndAnswer(ctx wrapper.HttpContext, c config.PluginConfig, key string, value string, log log.Log) {
|
func uploadEmbeddingAndAnswer(ctx wrapper.HttpContext, c config.PluginConfig, key string, value string, log wrapper.Log) {
|
||||||
embedding := ctx.GetContext(CACHE_KEY_EMBEDDING_KEY)
|
embedding := ctx.GetContext(CACHE_KEY_EMBEDDING_KEY)
|
||||||
if embedding == nil {
|
if embedding == nil {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@@ -80,7 +79,7 @@ type CohereProvider struct {
|
|||||||
func (t *CohereProvider) GetProviderType() string {
|
func (t *CohereProvider) GetProviderType() string {
|
||||||
return PROVIDER_TYPE_COHERE
|
return PROVIDER_TYPE_COHERE
|
||||||
}
|
}
|
||||||
func (t *CohereProvider) constructParameters(texts []string, log log.Log) (string, [][2]string, []byte, error) {
|
func (t *CohereProvider) constructParameters(texts []string, log wrapper.Log) (string, [][2]string, []byte, error) {
|
||||||
model := t.config.model
|
model := t.config.model
|
||||||
|
|
||||||
if model == "" {
|
if model == "" {
|
||||||
@@ -119,7 +118,7 @@ func (t *CohereProvider) parseTextEmbedding(responseBody []byte) (*cohereRespons
|
|||||||
func (t *CohereProvider) GetEmbedding(
|
func (t *CohereProvider) GetEmbedding(
|
||||||
queryString string,
|
queryString string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(emb []float64, err error)) error {
|
callback func(emb []float64, err error)) error {
|
||||||
embUrl, embHeaders, embRequestBody, err := t.constructParameters([]string{queryString}, log)
|
embUrl, embHeaders, embRequestBody, err := t.constructParameters([]string{queryString}, log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@@ -104,7 +103,7 @@ type DSProvider struct {
|
|||||||
client wrapper.HttpClient
|
client wrapper.HttpClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DSProvider) constructParameters(texts []string, log log.Log) (string, [][2]string, []byte, error) {
|
func (d *DSProvider) constructParameters(texts []string, log wrapper.Log) (string, [][2]string, []byte, error) {
|
||||||
|
|
||||||
model := d.config.model
|
model := d.config.model
|
||||||
|
|
||||||
@@ -160,7 +159,7 @@ func (d *DSProvider) parseTextEmbedding(responseBody []byte) (*Response, error)
|
|||||||
func (d *DSProvider) GetEmbedding(
|
func (d *DSProvider) GetEmbedding(
|
||||||
queryString string,
|
queryString string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(emb []float64, err error)) error {
|
callback func(emb []float64, err error)) error {
|
||||||
embUrl, embHeaders, embRequestBody, err := d.constructParameters([]string{queryString}, log)
|
embUrl, embHeaders, embRequestBody, err := d.constructParameters([]string{queryString}, log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -4,13 +4,11 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
|
||||||
"github.com/tidwall/gjson"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -80,7 +78,7 @@ type HuggingFaceEmbeddingRequest struct {
|
|||||||
} `json:"options"`
|
} `json:"options"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HuggingFaceProvider) constructParameters(text string, log log.Log) (string, [][2]string, []byte, error) {
|
func (t *HuggingFaceProvider) constructParameters(text string, log wrapper.Log) (string, [][2]string, []byte, error) {
|
||||||
if text == "" {
|
if text == "" {
|
||||||
err := errors.New("queryString text cannot be empty")
|
err := errors.New("queryString text cannot be empty")
|
||||||
return "", nil, nil, err
|
return "", nil, nil, err
|
||||||
@@ -129,7 +127,7 @@ func (t *HuggingFaceProvider) parseTextEmbedding(responseBody []byte) ([]float64
|
|||||||
func (t *HuggingFaceProvider) GetEmbedding(
|
func (t *HuggingFaceProvider) GetEmbedding(
|
||||||
queryString string,
|
queryString string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(emb []float64, err error)) error {
|
callback func(emb []float64, err error)) error {
|
||||||
embUrl, embHeaders, embRequestBody, err := t.constructParameters(queryString, log)
|
embUrl, embHeaders, embRequestBody, err := t.constructParameters(queryString, log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -4,12 +4,10 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -71,7 +69,7 @@ type ollamaEmbeddingRequest struct {
|
|||||||
Model string `json:"model"`
|
Model string `json:"model"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ollamaProvider) constructParameters(text string, log log.Log) (string, [][2]string, []byte, error) {
|
func (t *ollamaProvider) constructParameters(text string, log wrapper.Log) (string, [][2]string, []byte, error) {
|
||||||
if text == "" {
|
if text == "" {
|
||||||
err := errors.New("queryString text cannot be empty")
|
err := errors.New("queryString text cannot be empty")
|
||||||
return "", nil, nil, err
|
return "", nil, nil, err
|
||||||
@@ -107,7 +105,7 @@ func (t *ollamaProvider) parseTextEmbedding(responseBody []byte) (*ollamaRespons
|
|||||||
func (t *ollamaProvider) GetEmbedding(
|
func (t *ollamaProvider) GetEmbedding(
|
||||||
queryString string,
|
queryString string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(emb []float64, err error)) error {
|
callback func(emb []float64, err error)) error {
|
||||||
embUrl, embHeaders, embRequestBody, err := t.constructParameters(queryString, log)
|
embUrl, embHeaders, embRequestBody, err := t.constructParameters(queryString, log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@@ -94,7 +93,7 @@ type OpenAIProvider struct {
|
|||||||
client wrapper.HttpClient
|
client wrapper.HttpClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *OpenAIProvider) constructParameters(text string, log log.Log) (string, [][2]string, []byte, error) {
|
func (t *OpenAIProvider) constructParameters(text string, log wrapper.Log) (string, [][2]string, []byte, error) {
|
||||||
if text == "" {
|
if text == "" {
|
||||||
err := errors.New("queryString text cannot be empty")
|
err := errors.New("queryString text cannot be empty")
|
||||||
return "", nil, nil, err
|
return "", nil, nil, err
|
||||||
@@ -131,7 +130,7 @@ func (t *OpenAIProvider) parseTextEmbedding(responseBody []byte) (*OpenAIRespons
|
|||||||
func (t *OpenAIProvider) GetEmbedding(
|
func (t *OpenAIProvider) GetEmbedding(
|
||||||
queryString string,
|
queryString string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(emb []float64, err error)) error {
|
callback func(emb []float64, err error)) error {
|
||||||
embUrl, embHeaders, embRequestBody, err := t.constructParameters(queryString, log)
|
embUrl, embHeaders, embRequestBody, err := t.constructParameters(queryString, log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package embedding
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@@ -109,6 +108,6 @@ type Provider interface {
|
|||||||
GetEmbedding(
|
GetEmbedding(
|
||||||
queryString string,
|
queryString string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(emb []float64, err error)) error
|
callback func(emb []float64, err error)) error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@@ -98,7 +97,7 @@ type TIProvider struct {
|
|||||||
client wrapper.HttpClient
|
client wrapper.HttpClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TIProvider) constructParameters(texts []string, log log.Log) (string, [][2]string, []byte, error) {
|
func (t *TIProvider) constructParameters(texts []string, log wrapper.Log) (string, [][2]string, []byte, error) {
|
||||||
|
|
||||||
data := TextInEmbeddingRequest{
|
data := TextInEmbeddingRequest{
|
||||||
Input: texts,
|
Input: texts,
|
||||||
@@ -143,7 +142,7 @@ func (t *TIProvider) parseTextEmbedding(responseBody []byte) (*TextInResponse, e
|
|||||||
func (t *TIProvider) GetEmbedding(
|
func (t *TIProvider) GetEmbedding(
|
||||||
queryString string,
|
queryString string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(emb []float64, err error)) error {
|
callback func(emb []float64, err error)) error {
|
||||||
embUrl, embHeaders, embRequestBody, err := t.constructParameters([]string{queryString}, log)
|
embUrl, embHeaders, embRequestBody, err := t.constructParameters([]string{queryString}, log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/config"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/config"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -39,7 +38,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, c *config.PluginConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, c *config.PluginConfig, log wrapper.Log) error {
|
||||||
// config.EmbeddingProviderConfig.FromJson(json.Get("embeddingProvider"))
|
// config.EmbeddingProviderConfig.FromJson(json.Get("embeddingProvider"))
|
||||||
// config.VectorDatabaseProviderConfig.FromJson(json.Get("vectorBaseProvider"))
|
// config.VectorDatabaseProviderConfig.FromJson(json.Get("vectorBaseProvider"))
|
||||||
// config.RedisConfig.FromJson(json.Get("redis"))
|
// config.RedisConfig.FromJson(json.Get("redis"))
|
||||||
@@ -55,7 +54,7 @@ func parseConfig(json gjson.Result, c *config.PluginConfig, log log.Log) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, c config.PluginConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, c config.PluginConfig, log wrapper.Log) types.Action {
|
||||||
skipCache, _ := proxywasm.GetHttpRequestHeader(SKIP_CACHE_HEADER)
|
skipCache, _ := proxywasm.GetHttpRequestHeader(SKIP_CACHE_HEADER)
|
||||||
if skipCache == "on" {
|
if skipCache == "on" {
|
||||||
ctx.SetContext(SKIP_CACHE_HEADER, struct{}{})
|
ctx.SetContext(SKIP_CACHE_HEADER, struct{}{})
|
||||||
@@ -79,7 +78,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, c config.PluginConfig, log lo
|
|||||||
return types.HeaderStopIteration
|
return types.HeaderStopIteration
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, c config.PluginConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, c config.PluginConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
|
|
||||||
bodyJson := gjson.ParseBytes(body)
|
bodyJson := gjson.ParseBytes(body)
|
||||||
// TODO: It may be necessary to support stream mode determination for different LLM providers.
|
// TODO: It may be necessary to support stream mode determination for different LLM providers.
|
||||||
@@ -129,7 +128,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, c config.PluginConfig, body []by
|
|||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, c config.PluginConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, c config.PluginConfig, log wrapper.Log) types.Action {
|
||||||
skipCache := ctx.GetContext(SKIP_CACHE_HEADER)
|
skipCache := ctx.GetContext(SKIP_CACHE_HEADER)
|
||||||
if skipCache != nil {
|
if skipCache != nil {
|
||||||
ctx.SetUserAttribute("cache_status", "skip")
|
ctx.SetUserAttribute("cache_status", "skip")
|
||||||
@@ -151,7 +150,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, c config.PluginConfig, log l
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseBody(ctx wrapper.HttpContext, c config.PluginConfig, chunk []byte, isLastChunk bool, log log.Log) []byte {
|
func onHttpResponseBody(ctx wrapper.HttpContext, c config.PluginConfig, chunk []byte, isLastChunk bool, log wrapper.Log) []byte {
|
||||||
log.Debugf("[onHttpResponseBody] is last chunk: %v", isLastChunk)
|
log.Debugf("[onHttpResponseBody] is last chunk: %v", isLastChunk)
|
||||||
log.Debugf("[onHttpResponseBody] chunk: %s", string(chunk))
|
log.Debugf("[onHttpResponseBody] chunk: %s", string(chunk))
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/config"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-cache/config"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleNonStreamChunk(ctx wrapper.HttpContext, c config.PluginConfig, chunk []byte, log log.Log) error {
|
func handleNonStreamChunk(ctx wrapper.HttpContext, c config.PluginConfig, chunk []byte, log wrapper.Log) error {
|
||||||
tempContentI := ctx.GetContext(CACHE_CONTENT_CONTEXT_KEY)
|
tempContentI := ctx.GetContext(CACHE_CONTENT_CONTEXT_KEY)
|
||||||
if tempContentI == nil {
|
if tempContentI == nil {
|
||||||
ctx.SetContext(CACHE_CONTENT_CONTEXT_KEY, chunk)
|
ctx.SetContext(CACHE_CONTENT_CONTEXT_KEY, chunk)
|
||||||
@@ -29,7 +28,7 @@ func unifySSEChunk(data []byte) []byte {
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleStreamChunk(ctx wrapper.HttpContext, c config.PluginConfig, chunk []byte, log log.Log) error {
|
func handleStreamChunk(ctx wrapper.HttpContext, c config.PluginConfig, chunk []byte, log wrapper.Log) error {
|
||||||
var partialMessage []byte
|
var partialMessage []byte
|
||||||
partialMessageI := ctx.GetContext(PARTIAL_MESSAGE_CONTEXT_KEY)
|
partialMessageI := ctx.GetContext(PARTIAL_MESSAGE_CONTEXT_KEY)
|
||||||
log.Debugf("[handleStreamChunk] cache content: %v", ctx.GetContext(CACHE_CONTENT_CONTEXT_KEY))
|
log.Debugf("[handleStreamChunk] cache content: %v", ctx.GetContext(CACHE_CONTENT_CONTEXT_KEY))
|
||||||
@@ -55,7 +54,7 @@ func handleStreamChunk(ctx wrapper.HttpContext, c config.PluginConfig, chunk []b
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processNonStreamLastChunk(ctx wrapper.HttpContext, c config.PluginConfig, chunk []byte, log log.Log) (string, error) {
|
func processNonStreamLastChunk(ctx wrapper.HttpContext, c config.PluginConfig, chunk []byte, log wrapper.Log) (string, error) {
|
||||||
var body []byte
|
var body []byte
|
||||||
tempContentI := ctx.GetContext(CACHE_CONTENT_CONTEXT_KEY)
|
tempContentI := ctx.GetContext(CACHE_CONTENT_CONTEXT_KEY)
|
||||||
if tempContentI != nil {
|
if tempContentI != nil {
|
||||||
@@ -71,7 +70,7 @@ func processNonStreamLastChunk(ctx wrapper.HttpContext, c config.PluginConfig, c
|
|||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processStreamLastChunk(ctx wrapper.HttpContext, c config.PluginConfig, chunk []byte, log log.Log) (string, error) {
|
func processStreamLastChunk(ctx wrapper.HttpContext, c config.PluginConfig, chunk []byte, log wrapper.Log) (string, error) {
|
||||||
if len(chunk) > 0 {
|
if len(chunk) > 0 {
|
||||||
var lastMessage []byte
|
var lastMessage []byte
|
||||||
partialMessageI := ctx.GetContext(PARTIAL_MESSAGE_CONTEXT_KEY)
|
partialMessageI := ctx.GetContext(PARTIAL_MESSAGE_CONTEXT_KEY)
|
||||||
@@ -97,7 +96,7 @@ func processStreamLastChunk(ctx wrapper.HttpContext, c config.PluginConfig, chun
|
|||||||
return tempContentI.(string), nil
|
return tempContentI.(string), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processSSEMessage(ctx wrapper.HttpContext, c config.PluginConfig, sseMessage string, log log.Log) (string, error) {
|
func processSSEMessage(ctx wrapper.HttpContext, c config.PluginConfig, sseMessage string, log wrapper.Log) (string, error) {
|
||||||
content := ""
|
content := ""
|
||||||
for _, chunk := range strings.Split(sseMessage, "\n\n") {
|
for _, chunk := range strings.Split(sseMessage, "\n\n") {
|
||||||
log.Debugf("single sse message: %s", chunk)
|
log.Debugf("single sse message: %s", chunk)
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -45,8 +44,8 @@ func (c *ChromaProvider) GetProviderType() string {
|
|||||||
func (d *ChromaProvider) QueryEmbedding(
|
func (d *ChromaProvider) QueryEmbedding(
|
||||||
emb []float64,
|
emb []float64,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
|
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
// 最少需要填写的参数为 collection_id, embeddings 和 ids
|
// 最少需要填写的参数为 collection_id, embeddings 和 ids
|
||||||
// 下面是一个例子
|
// 下面是一个例子
|
||||||
// {
|
// {
|
||||||
@@ -97,8 +96,8 @@ func (d *ChromaProvider) UploadAnswerAndEmbedding(
|
|||||||
queryEmb []float64,
|
queryEmb []float64,
|
||||||
queryAnswer string,
|
queryAnswer string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
|
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
// 最少需要填写的参数为 collection_id, embeddings 和 ids
|
// 最少需要填写的参数为 collection_id, embeddings 和 ids
|
||||||
// 下面是一个例子
|
// 下面是一个例子
|
||||||
// {
|
// {
|
||||||
@@ -178,7 +177,7 @@ type chromaQueryResponse struct {
|
|||||||
Included []string `json:"included"`
|
Included []string `json:"included"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ChromaProvider) parseQueryResponse(responseBody []byte, log log.Log) ([]QueryResult, error) {
|
func (d *ChromaProvider) parseQueryResponse(responseBody []byte, log wrapper.Log) ([]QueryResult, error) {
|
||||||
var queryResp chromaQueryResponse
|
var queryResp chromaQueryResponse
|
||||||
err := json.Unmarshal(responseBody, &queryResp)
|
err := json.Unmarshal(responseBody, &queryResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -120,8 +119,8 @@ func (d *DvProvider) parseQueryResponse(responseBody []byte) (queryResponse, err
|
|||||||
func (d *DvProvider) QueryEmbedding(
|
func (d *DvProvider) QueryEmbedding(
|
||||||
emb []float64,
|
emb []float64,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
|
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
url, body, headers, err := d.constructEmbeddingQueryParameters(emb)
|
url, body, headers, err := d.constructEmbeddingQueryParameters(emb)
|
||||||
log.Debugf("url:%s, body:%s, headers:%v", url, string(body), headers)
|
log.Debugf("url:%s, body:%s, headers:%v", url, string(body), headers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -158,7 +157,7 @@ func getStringValue(fields map[string]interface{}, key string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DvProvider) ParseQueryResponse(responseBody []byte, ctx wrapper.HttpContext, log log.Log) ([]QueryResult, error) {
|
func (d *DvProvider) ParseQueryResponse(responseBody []byte, ctx wrapper.HttpContext, log wrapper.Log) ([]QueryResult, error) {
|
||||||
resp, err := d.parseQueryResponse(responseBody)
|
resp, err := d.parseQueryResponse(responseBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -216,7 +215,7 @@ func (d *DvProvider) constructUploadParameters(emb []float64, queryString string
|
|||||||
return url, requestBody, header, err
|
return url, requestBody, header, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DvProvider) UploadEmbedding(queryString string, queryEmb []float64, ctx wrapper.HttpContext, log log.Log, callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
|
func (d *DvProvider) UploadEmbedding(queryString string, queryEmb []float64, ctx wrapper.HttpContext, log wrapper.Log, callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
url, body, headers, err := d.constructUploadParameters(queryEmb, queryString, "")
|
url, body, headers, err := d.constructUploadParameters(queryEmb, queryString, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -236,7 +235,7 @@ func (d *DvProvider) UploadEmbedding(queryString string, queryEmb []float64, ctx
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DvProvider) UploadAnswerAndEmbedding(queryString string, queryEmb []float64, queryAnswer string, ctx wrapper.HttpContext, log log.Log, callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
|
func (d *DvProvider) UploadAnswerAndEmbedding(queryString string, queryEmb []float64, queryAnswer string, ctx wrapper.HttpContext, log wrapper.Log, callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
url, body, headers, err := d.constructUploadParameters(queryEmb, queryString, queryAnswer)
|
url, body, headers, err := d.constructUploadParameters(queryEmb, queryString, queryAnswer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -46,8 +45,8 @@ func (c *ESProvider) GetProviderType() string {
|
|||||||
func (d *ESProvider) QueryEmbedding(
|
func (d *ESProvider) QueryEmbedding(
|
||||||
emb []float64,
|
emb []float64,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
|
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
|
|
||||||
requestBody, err := json.Marshal(esQueryRequest{
|
requestBody, err := json.Marshal(esQueryRequest{
|
||||||
Source: Source{Excludes: []string{"embedding"}},
|
Source: Source{Excludes: []string{"embedding"}},
|
||||||
@@ -100,8 +99,8 @@ func (d *ESProvider) UploadAnswerAndEmbedding(
|
|||||||
queryEmb []float64,
|
queryEmb []float64,
|
||||||
queryAnswer string,
|
queryAnswer string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
|
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
// 最少需要填写的参数为 index, embeddings 和 question
|
// 最少需要填写的参数为 index, embeddings 和 question
|
||||||
// 下面是一个例子
|
// 下面是一个例子
|
||||||
// POST /<index>/_doc
|
// POST /<index>/_doc
|
||||||
@@ -177,7 +176,7 @@ type esQueryResponse struct {
|
|||||||
} `json:"hits"`
|
} `json:"hits"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ESProvider) parseQueryResponse(responseBody []byte, log log.Log) ([]QueryResult, error) {
|
func (d *ESProvider) parseQueryResponse(responseBody []byte, log wrapper.Log) ([]QueryResult, error) {
|
||||||
log.Infof("[ES] responseBody: %s", string(responseBody))
|
log.Infof("[ES] responseBody: %s", string(responseBody))
|
||||||
var queryResp esQueryResponse
|
var queryResp esQueryResponse
|
||||||
err := json.Unmarshal(responseBody, &queryResp)
|
err := json.Unmarshal(responseBody, &queryResp)
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@@ -59,8 +58,8 @@ func (d *milvusProvider) UploadAnswerAndEmbedding(
|
|||||||
queryEmb []float64,
|
queryEmb []float64,
|
||||||
queryAnswer string,
|
queryAnswer string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
|
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
// 最少需要填写的参数为 collectionName, data 和 Authorization. question, answer 可选
|
// 最少需要填写的参数为 collectionName, data 和 Authorization. question, answer 可选
|
||||||
// 需要填写 id,否则 v2.4.13-hotfix 提示 invalid syntax: invalid parameter[expected=Int64][actual=]
|
// 需要填写 id,否则 v2.4.13-hotfix 提示 invalid syntax: invalid parameter[expected=Int64][actual=]
|
||||||
// 如果不填写 id,要在创建 collection 的时候设置 autoId 为 true
|
// 如果不填写 id,要在创建 collection 的时候设置 autoId 为 true
|
||||||
@@ -121,8 +120,8 @@ type milvusQueryRequest struct {
|
|||||||
func (d *milvusProvider) QueryEmbedding(
|
func (d *milvusProvider) QueryEmbedding(
|
||||||
emb []float64,
|
emb []float64,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
|
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
// 最少需要填写的参数为 collectionName, data, annsField. outputFields 为可选参数
|
// 最少需要填写的参数为 collectionName, data, annsField. outputFields 为可选参数
|
||||||
// 下面是一个例子
|
// 下面是一个例子
|
||||||
// {
|
// {
|
||||||
@@ -176,7 +175,7 @@ func (d *milvusProvider) QueryEmbedding(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *milvusProvider) parseQueryResponse(responseBody []byte, log log.Log) ([]QueryResult, error) {
|
func (d *milvusProvider) parseQueryResponse(responseBody []byte, log wrapper.Log) ([]QueryResult, error) {
|
||||||
if !gjson.GetBytes(responseBody, "data.0.distance").Exists() {
|
if !gjson.GetBytes(responseBody, "data.0.distance").Exists() {
|
||||||
log.Errorf("[Milvus] No distance found in response body: %s", responseBody)
|
log.Errorf("[Milvus] No distance found in response body: %s", responseBody)
|
||||||
return nil, errors.New("[Milvus] No distance found in response body")
|
return nil, errors.New("[Milvus] No distance found in response body")
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@@ -68,8 +67,8 @@ func (d *pineconeProvider) UploadAnswerAndEmbedding(
|
|||||||
queryEmb []float64,
|
queryEmb []float64,
|
||||||
queryAnswer string,
|
queryAnswer string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
|
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
// 最少需要填写的参数为 vector 和 question
|
// 最少需要填写的参数为 vector 和 question
|
||||||
// 下面是一个例子
|
// 下面是一个例子
|
||||||
// {
|
// {
|
||||||
@@ -123,8 +122,8 @@ type pineconeQueryRequest struct {
|
|||||||
func (d *pineconeProvider) QueryEmbedding(
|
func (d *pineconeProvider) QueryEmbedding(
|
||||||
emb []float64,
|
emb []float64,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
|
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
// 最少需要填写的参数为 vector
|
// 最少需要填写的参数为 vector
|
||||||
// 下面是一个例子
|
// 下面是一个例子
|
||||||
// {
|
// {
|
||||||
@@ -164,7 +163,7 @@ func (d *pineconeProvider) QueryEmbedding(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *pineconeProvider) parseQueryResponse(responseBody []byte, log log.Log) ([]QueryResult, error) {
|
func (d *pineconeProvider) parseQueryResponse(responseBody []byte, log wrapper.Log) ([]QueryResult, error) {
|
||||||
if !gjson.GetBytes(responseBody, "matches.0.score").Exists() {
|
if !gjson.GetBytes(responseBody, "matches.0.score").Exists() {
|
||||||
log.Errorf("[Pinecone] No distance found in response body: %s", responseBody)
|
log.Errorf("[Pinecone] No distance found in response body: %s", responseBody)
|
||||||
return nil, errors.New("[Pinecone] No distance found in response body")
|
return nil, errors.New("[Pinecone] No distance found in response body")
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package vector
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@@ -51,8 +50,8 @@ type EmbeddingQuerier interface {
|
|||||||
QueryEmbedding(
|
QueryEmbedding(
|
||||||
emb []float64,
|
emb []float64,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error
|
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type EmbeddingUploader interface {
|
type EmbeddingUploader interface {
|
||||||
@@ -60,8 +59,8 @@ type EmbeddingUploader interface {
|
|||||||
queryString string,
|
queryString string,
|
||||||
queryEmb []float64,
|
queryEmb []float64,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error
|
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type AnswerAndEmbeddingUploader interface {
|
type AnswerAndEmbeddingUploader interface {
|
||||||
@@ -70,16 +69,16 @@ type AnswerAndEmbeddingUploader interface {
|
|||||||
queryEmb []float64,
|
queryEmb []float64,
|
||||||
answer string,
|
answer string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error
|
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type StringQuerier interface {
|
type StringQuerier interface {
|
||||||
QueryString(
|
QueryString(
|
||||||
queryString string,
|
queryString string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error
|
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProviderConfig struct {
|
type ProviderConfig struct {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@@ -64,8 +63,8 @@ func (d *qdrantProvider) UploadAnswerAndEmbedding(
|
|||||||
queryEmb []float64,
|
queryEmb []float64,
|
||||||
queryAnswer string,
|
queryAnswer string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
|
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
// 最少需要填写的参数为 id 和 vector. payload 可选
|
// 最少需要填写的参数为 id 和 vector. payload 可选
|
||||||
// 下面是一个例子
|
// 下面是一个例子
|
||||||
// {
|
// {
|
||||||
@@ -123,8 +122,8 @@ type qdrantQueryRequest struct {
|
|||||||
func (d *qdrantProvider) QueryEmbedding(
|
func (d *qdrantProvider) QueryEmbedding(
|
||||||
emb []float64,
|
emb []float64,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
|
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
// 最少需要填写的参数为 vector 和 limit. with_payload 可选,为了直接得到问题答案,所以这里需要
|
// 最少需要填写的参数为 vector 和 limit. with_payload 可选,为了直接得到问题答案,所以这里需要
|
||||||
// 下面是一个例子
|
// 下面是一个例子
|
||||||
// {
|
// {
|
||||||
@@ -165,7 +164,7 @@ func (d *qdrantProvider) QueryEmbedding(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *qdrantProvider) parseQueryResponse(responseBody []byte, log log.Log) ([]QueryResult, error) {
|
func (d *qdrantProvider) parseQueryResponse(responseBody []byte, log wrapper.Log) ([]QueryResult, error) {
|
||||||
// 返回的内容例子如下
|
// 返回的内容例子如下
|
||||||
// {
|
// {
|
||||||
// "time": 0.002,
|
// "time": 0.002,
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@@ -46,8 +45,8 @@ func (c *WeaviateProvider) GetProviderType() string {
|
|||||||
func (d *WeaviateProvider) QueryEmbedding(
|
func (d *WeaviateProvider) QueryEmbedding(
|
||||||
emb []float64,
|
emb []float64,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
|
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
// 最少需要填写的参数为 class, vector
|
// 最少需要填写的参数为 class, vector
|
||||||
// 下面是一个例子
|
// 下面是一个例子
|
||||||
// {"query": "{ Get { Higress ( limit: 2 nearVector: { vector: [0.1, 0.2, 0.3] } ) { question _additional { distance } } } }"}
|
// {"query": "{ Get { Higress ( limit: 2 nearVector: { vector: [0.1, 0.2, 0.3] } ) { question _additional { distance } } } }"}
|
||||||
@@ -110,8 +109,8 @@ func (d *WeaviateProvider) UploadAnswerAndEmbedding(
|
|||||||
queryEmb []float64,
|
queryEmb []float64,
|
||||||
queryAnswer string,
|
queryAnswer string,
|
||||||
ctx wrapper.HttpContext,
|
ctx wrapper.HttpContext,
|
||||||
log log.Log,
|
log wrapper.Log,
|
||||||
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
|
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
|
||||||
// 最少需要填写的参数为 class, vector 和 question 和 answer
|
// 最少需要填写的参数为 class, vector 和 question 和 answer
|
||||||
// 下面是一个例子
|
// 下面是一个例子
|
||||||
// {"class": "Higress", "vector": [0.1, 0.2, 0.3], "properties": {"question": "这里是问题", "answer": "这里是答案"}}
|
// {"class": "Higress", "vector": [0.1, 0.2, 0.3], "properties": {"question": "这里是问题", "answer": "这里是答案"}}
|
||||||
@@ -156,7 +155,7 @@ type weaviateQueryRequest struct {
|
|||||||
Query string `json:"query"`
|
Query string `json:"query"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *WeaviateProvider) parseQueryResponse(responseBody []byte, log log.Log) ([]QueryResult, error) {
|
func (d *WeaviateProvider) parseQueryResponse(responseBody []byte, log wrapper.Log) ([]QueryResult, error) {
|
||||||
log.Infof("[Weaviate] queryResp: %s", string(responseBody))
|
log.Infof("[Weaviate] queryResp: %s", string(responseBody))
|
||||||
|
|
||||||
if !gjson.GetBytes(responseBody, fmt.Sprintf("data.Get.%s.0._additional.distance", d.config.collectionID)).Exists() {
|
if !gjson.GetBytes(responseBody, fmt.Sprintf("data.Get.%s.0._additional.distance", d.config.collectionID)).Exists() {
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -122,7 +121,7 @@ type ChatHistory struct {
|
|||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, c *PluginConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, c *PluginConfig, log wrapper.Log) error {
|
||||||
c.RedisInfo.ServiceName = json.Get("redis.serviceName").String()
|
c.RedisInfo.ServiceName = json.Get("redis.serviceName").String()
|
||||||
if c.RedisInfo.ServiceName == "" {
|
if c.RedisInfo.ServiceName == "" {
|
||||||
return errors.New("redis service name must not be empty")
|
return errors.New("redis service name must not be empty")
|
||||||
@@ -167,7 +166,7 @@ func parseConfig(json gjson.Result, c *PluginConfig, log log.Log) error {
|
|||||||
return c.redisClient.Init(c.RedisInfo.Username, c.RedisInfo.Password, int64(c.RedisInfo.Timeout), wrapper.WithDataBase(c.RedisInfo.Database))
|
return c.redisClient.Init(c.RedisInfo.Username, c.RedisInfo.Password, int64(c.RedisInfo.Timeout), wrapper.WithDataBase(c.RedisInfo.Database))
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log) types.Action {
|
||||||
contentType, _ := proxywasm.GetHttpRequestHeader("content-type")
|
contentType, _ := proxywasm.GetHttpRequestHeader("content-type")
|
||||||
if !strings.Contains(contentType, "application/json") {
|
if !strings.Contains(contentType, "application/json") {
|
||||||
log.Warnf("content is not json, can't process:%s", contentType)
|
log.Warnf("content is not json, can't process:%s", contentType)
|
||||||
@@ -193,7 +192,7 @@ func TrimQuote(source string) string {
|
|||||||
return strings.Trim(source, `"`)
|
return strings.Trim(source, `"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
bodyJson := gjson.ParseBytes(body)
|
bodyJson := gjson.ParseBytes(body)
|
||||||
if bodyJson.Get("stream").Bool() {
|
if bodyJson.Get("stream").Bool() {
|
||||||
ctx.SetContext(StreamContextKey, struct{}{})
|
ctx.SetContext(StreamContextKey, struct{}{})
|
||||||
@@ -320,7 +319,7 @@ func getIntQueryParameter(name string, path string, defaultValue int) int {
|
|||||||
return num
|
return num
|
||||||
}
|
}
|
||||||
|
|
||||||
func processSSEMessage(ctx wrapper.HttpContext, config PluginConfig, sseMessage string, log log.Log) string {
|
func processSSEMessage(ctx wrapper.HttpContext, config PluginConfig, sseMessage string, log wrapper.Log) string {
|
||||||
content := ""
|
content := ""
|
||||||
for _, chunk := range strings.Split(sseMessage, "\n\n") {
|
for _, chunk := range strings.Split(sseMessage, "\n\n") {
|
||||||
subMessages := strings.Split(chunk, "\n")
|
subMessages := strings.Split(chunk, "\n")
|
||||||
@@ -356,14 +355,14 @@ func processSSEMessage(ctx wrapper.HttpContext, config PluginConfig, sseMessage
|
|||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log) types.Action {
|
||||||
contentType, _ := proxywasm.GetHttpResponseHeader("content-type")
|
contentType, _ := proxywasm.GetHttpResponseHeader("content-type")
|
||||||
if strings.Contains(contentType, "text/event-stream") {
|
if strings.Contains(contentType, "text/event-stream") {
|
||||||
ctx.SetContext(StreamContextKey, struct{}{})
|
ctx.SetContext(StreamContextKey, struct{}{})
|
||||||
}
|
}
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
func onHttpStreamResponseBody(ctx wrapper.HttpContext, config PluginConfig, chunk []byte, isLastChunk bool, log log.Log) []byte {
|
func onHttpStreamResponseBody(ctx wrapper.HttpContext, config PluginConfig, chunk []byte, isLastChunk bool, log wrapper.Log) []byte {
|
||||||
if ctx.GetContext(ToolCallsContextKey) != nil {
|
if ctx.GetContext(ToolCallsContextKey) != nil {
|
||||||
// we should not cache tool call result
|
// we should not cache tool call result
|
||||||
return chunk
|
return chunk
|
||||||
@@ -455,7 +454,7 @@ func onHttpStreamResponseBody(ctx wrapper.HttpContext, config PluginConfig, chun
|
|||||||
return chunk
|
return chunk
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveChatHistory(ctx wrapper.HttpContext, config PluginConfig, questionI any, value string, log log.Log) {
|
func saveChatHistory(ctx wrapper.HttpContext, config PluginConfig, questionI any, value string, log wrapper.Log) {
|
||||||
question := questionI.(string)
|
question := questionI.(string)
|
||||||
identityKey := ctx.GetStringContext(IdentityKey, "")
|
identityKey := ctx.GetStringContext(IdentityKey, "")
|
||||||
var chat []ChatHistory
|
var chat []ChatHistory
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -101,7 +100,7 @@ type KVExtractor struct {
|
|||||||
ResponseBody string `required:"false" yaml:"responseBody" json:"responseBody"`
|
ResponseBody string `required:"false" yaml:"responseBody" json:"responseBody"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, c *PluginConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, c *PluginConfig, log wrapper.Log) error {
|
||||||
log.Infof("config:%s", json.Raw)
|
log.Infof("config:%s", json.Raw)
|
||||||
// init scene
|
// init scene
|
||||||
c.SceneInfo.Category = json.Get("scene.category").String()
|
c.SceneInfo.Category = json.Get("scene.category").String()
|
||||||
@@ -195,14 +194,14 @@ func parseConfig(json gjson.Result, c *PluginConfig, log log.Log) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log) types.Action {
|
||||||
log.Debug("start onHttpRequestHeaders function.")
|
log.Debug("start onHttpRequestHeaders function.")
|
||||||
|
|
||||||
log.Debug("end onHttpRequestHeaders function.")
|
log.Debug("end onHttpRequestHeaders function.")
|
||||||
return types.HeaderStopIteration
|
return types.HeaderStopIteration
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
log.Debug("start onHttpRequestBody function.")
|
log.Debug("start onHttpRequestBody function.")
|
||||||
bodyJson := gjson.ParseBytes(body)
|
bodyJson := gjson.ParseBytes(body)
|
||||||
TempKey := strings.Trim(bodyJson.Get(config.KeyFrom.RequestBody).Raw, `"`)
|
TempKey := strings.Trim(bodyJson.Get(config.KeyFrom.RequestBody).Raw, `"`)
|
||||||
@@ -260,21 +259,21 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte
|
|||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log) types.Action {
|
||||||
log.Debug("start onHttpResponseHeaders function.")
|
log.Debug("start onHttpResponseHeaders function.")
|
||||||
|
|
||||||
log.Debug("end onHttpResponseHeaders function.")
|
log.Debug("end onHttpResponseHeaders function.")
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onStreamingResponseBody(ctx wrapper.HttpContext, config PluginConfig, chunk []byte, isLastChunk bool, log log.Log) []byte {
|
func onStreamingResponseBody(ctx wrapper.HttpContext, config PluginConfig, chunk []byte, isLastChunk bool, log wrapper.Log) []byte {
|
||||||
log.Debug("start onStreamingResponseBody function.")
|
log.Debug("start onStreamingResponseBody function.")
|
||||||
|
|
||||||
log.Debug("end onStreamingResponseBody function.")
|
log.Debug("end onStreamingResponseBody function.")
|
||||||
return chunk
|
return chunk
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log log.Log) types.Action {
|
func onHttpResponseBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
log.Debug("start onHttpResponseBody function.")
|
log.Debug("start onHttpResponseBody function.")
|
||||||
|
|
||||||
log.Debug("end onHttpResponseBody function.")
|
log.Debug("end onHttpResponseBody function.")
|
||||||
@@ -291,7 +290,7 @@ type ProxyRequestMessage struct {
|
|||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateProxyRequest(c *PluginConfig, texts []string, log log.Log) (string, []byte, [][2]string) {
|
func generateProxyRequest(c *PluginConfig, texts []string, log wrapper.Log) (string, []byte, [][2]string) {
|
||||||
url := c.LLMInfo.ProxyPath
|
url := c.LLMInfo.ProxyPath
|
||||||
var userMessage ProxyRequestMessage
|
var userMessage ProxyRequestMessage
|
||||||
userMessage.Role = "user"
|
userMessage.Role = "user"
|
||||||
@@ -339,7 +338,7 @@ type ProxyResponseOutputChoicesMessage struct {
|
|||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func proxyResponseHandler(responseBody []byte, log log.Log) (*ProxyResponse, error) {
|
func proxyResponseHandler(responseBody []byte, log wrapper.Log) (*ProxyResponse, error) {
|
||||||
var response ProxyResponse
|
var response ProxyResponse
|
||||||
err := json.Unmarshal(responseBody, &response)
|
err := json.Unmarshal(responseBody, &response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -349,7 +348,7 @@ func proxyResponseHandler(responseBody []byte, log log.Log) (*ProxyResponse, err
|
|||||||
return &response, nil
|
return &response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getProxyResponseByExtractor(c *PluginConfig, responseBody []byte, log log.Log) string {
|
func getProxyResponseByExtractor(c *PluginConfig, responseBody []byte, log wrapper.Log) string {
|
||||||
bodyJson := gjson.ParseBytes(responseBody)
|
bodyJson := gjson.ParseBytes(responseBody)
|
||||||
responseContent := strings.Trim(bodyJson.Get(c.KeyFrom.ResponseBody).Raw, `"`)
|
responseContent := strings.Trim(bodyJson.Get(c.KeyFrom.ResponseBody).Raw, `"`)
|
||||||
// llm返回的结果
|
// llm返回的结果
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import (
|
|||||||
"github.com/santhosh-tekuri/jsonschema"
|
"github.com/santhosh-tekuri/jsonschema"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -142,7 +141,7 @@ func parseUrl(url string) (string, string) {
|
|||||||
return url[:index], url[index:]
|
return url[:index], url[index:]
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(result gjson.Result, config *PluginConfig, log log.Log) error {
|
func parseConfig(result gjson.Result, config *PluginConfig, log wrapper.Log) error {
|
||||||
config.serviceName = result.Get("serviceName").String()
|
config.serviceName = result.Get("serviceName").String()
|
||||||
config.serviceUrl = result.Get("serviceUrl").String()
|
config.serviceUrl = result.Get("serviceUrl").String()
|
||||||
config.serviceDomain = result.Get("serviceDomain").String()
|
config.serviceDomain = result.Get("serviceDomain").String()
|
||||||
@@ -279,7 +278,7 @@ func (r *RequestContext) assembleReqBody(config PluginConfig) []byte {
|
|||||||
return reqBody
|
return reqBody
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RequestContext) SaveBodyToHistMsg(log log.Log, reqBody []byte, respBody []byte) {
|
func (r *RequestContext) SaveBodyToHistMsg(log wrapper.Log, reqBody []byte, respBody []byte) {
|
||||||
r.RespBody = respBody
|
r.RespBody = respBody
|
||||||
lastUserMessage := ""
|
lastUserMessage := ""
|
||||||
lastSystemMessage := ""
|
lastSystemMessage := ""
|
||||||
@@ -319,7 +318,7 @@ func (r *RequestContext) SaveBodyToHistMsg(log log.Log, reqBody []byte, respBody
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RequestContext) SaveStrToHistMsg(log log.Log, errMsg string) {
|
func (r *RequestContext) SaveStrToHistMsg(log wrapper.Log, errMsg string) {
|
||||||
r.HistoryMessages = append(r.HistoryMessages, chatMessage{
|
r.HistoryMessages = append(r.HistoryMessages, chatMessage{
|
||||||
Role: "system",
|
Role: "system",
|
||||||
Content: errMsg,
|
Content: errMsg,
|
||||||
@@ -341,7 +340,7 @@ func (c *PluginConfig) ValidateBody(body []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PluginConfig) ValidateJson(body []byte, log log.Log) (string, error) {
|
func (c *PluginConfig) ValidateJson(body []byte, log wrapper.Log) (string, error) {
|
||||||
content := gjson.ParseBytes(body).Get(c.contentPath).String()
|
content := gjson.ParseBytes(body).Get(c.contentPath).String()
|
||||||
// first extract json from response body
|
// first extract json from response body
|
||||||
if content == "" {
|
if content == "" {
|
||||||
@@ -400,7 +399,7 @@ func (c *PluginConfig) ExtractJson(bodyStr string) (string, error) {
|
|||||||
return jsonStr, nil
|
return jsonStr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendResponse(ctx wrapper.HttpContext, config PluginConfig, log log.Log, body []byte) {
|
func sendResponse(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log, body []byte) {
|
||||||
log.Infof("Final send: Code %d, Message %s, Body: %s", config.rejectStruct.RejectCode, config.rejectStruct.RejectMsg, string(body))
|
log.Infof("Final send: Code %d, Message %s, Body: %s", config.rejectStruct.RejectCode, config.rejectStruct.RejectMsg, string(body))
|
||||||
header := [][2]string{
|
header := [][2]string{
|
||||||
{"Content-Type", "application/json"},
|
{"Content-Type", "application/json"},
|
||||||
@@ -415,7 +414,7 @@ func sendResponse(ctx wrapper.HttpContext, config PluginConfig, log log.Log, bod
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func recursiveRefineJson(ctx wrapper.HttpContext, config PluginConfig, log log.Log, retryCount int, requestContext *RequestContext) {
|
func recursiveRefineJson(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log, retryCount int, requestContext *RequestContext) {
|
||||||
// if retry count exceeds max retry count, return the response
|
// if retry count exceeds max retry count, return the response
|
||||||
if retryCount >= config.maxRetry {
|
if retryCount >= config.maxRetry {
|
||||||
log.Debugf("retry count exceeds max retry count")
|
log.Debugf("retry count exceeds max retry count")
|
||||||
@@ -446,7 +445,7 @@ func recursiveRefineJson(ctx wrapper.HttpContext, config PluginConfig, log log.L
|
|||||||
}, uint32(config.serviceTimeout))
|
}, uint32(config.serviceTimeout))
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log) types.Action {
|
||||||
if config.rejectStruct.RejectCode != HTTP_STATUS_OK {
|
if config.rejectStruct.RejectCode != HTTP_STATUS_OK {
|
||||||
sendResponse(ctx, config, log, nil)
|
sendResponse(ctx, config, log, nil)
|
||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
@@ -506,7 +505,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
// if the request is from this plugin, continue the request
|
// if the request is from this plugin, continue the request
|
||||||
fromThisPlugin, ok := ctx.GetContext(FROM_THIS_PLUGIN_KEY).(bool)
|
fromThisPlugin, ok := ctx.GetContext(FROM_THIS_PLUGIN_KEY).(bool)
|
||||||
if ok && fromThisPlugin {
|
if ok && fromThisPlugin {
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -32,11 +31,11 @@ type AIPromptDecoratorConfig struct {
|
|||||||
Append []Message `json:"append"`
|
Append []Message `json:"append"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(jsonConfig gjson.Result, config *AIPromptDecoratorConfig, log log.Log) error {
|
func parseConfig(jsonConfig gjson.Result, config *AIPromptDecoratorConfig, log wrapper.Log) error {
|
||||||
return json.Unmarshal([]byte(jsonConfig.Raw), config)
|
return json.Unmarshal([]byte(jsonConfig.Raw), config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIPromptDecoratorConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIPromptDecoratorConfig, log wrapper.Log) types.Action {
|
||||||
proxywasm.RemoveHttpRequestHeader("content-length")
|
proxywasm.RemoveHttpRequestHeader("content-length")
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
@@ -67,7 +66,7 @@ func decorateGeographicPrompt(entry *Message) (*Message, error) {
|
|||||||
return entry, nil
|
return entry, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config AIPromptDecoratorConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config AIPromptDecoratorConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
messageJson := `{"messages":[]}`
|
messageJson := `{"messages":[]}`
|
||||||
|
|
||||||
for _, entry := range config.Prepend {
|
for _, entry := range config.Prepend {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -24,7 +23,7 @@ type AIPromptTemplateConfig struct {
|
|||||||
templates map[string]string
|
templates map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *AIPromptTemplateConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *AIPromptTemplateConfig, log wrapper.Log) error {
|
||||||
config.templates = make(map[string]string)
|
config.templates = make(map[string]string)
|
||||||
for _, v := range json.Get("templates").Array() {
|
for _, v := range json.Get("templates").Array() {
|
||||||
config.templates[v.Get("name").String()] = v.Get("template").Raw
|
config.templates[v.Get("name").String()] = v.Get("template").Raw
|
||||||
@@ -33,7 +32,7 @@ func parseConfig(json gjson.Result, config *AIPromptTemplateConfig, log log.Log)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIPromptTemplateConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIPromptTemplateConfig, log wrapper.Log) types.Action {
|
||||||
templateEnable, _ := proxywasm.GetHttpRequestHeader("template-enable")
|
templateEnable, _ := proxywasm.GetHttpRequestHeader("template-enable")
|
||||||
if templateEnable == "false" {
|
if templateEnable == "false" {
|
||||||
ctx.DontReadRequestBody()
|
ctx.DontReadRequestBody()
|
||||||
@@ -43,7 +42,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIPromptTemplateConfig
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config AIPromptTemplateConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config AIPromptTemplateConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
if gjson.GetBytes(body, "template").Exists() && gjson.GetBytes(body, "properties").Exists() {
|
if gjson.GetBytes(body, "template").Exists() && gjson.GetBytes(body, "properties").Exists() {
|
||||||
name := gjson.GetBytes(body, "template").String()
|
name := gjson.GetBytes(body, "template").String()
|
||||||
template := config.templates[name]
|
template := config.templates[name]
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-quota/util"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-quota/util"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -73,7 +72,7 @@ type RedisInfo struct {
|
|||||||
Database int `required:"false" yaml:"database" json:"database"`
|
Database int `required:"false" yaml:"database" json:"database"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *QuotaConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *QuotaConfig, log wrapper.Log) error {
|
||||||
log.Debugf("parse config()")
|
log.Debugf("parse config()")
|
||||||
// admin
|
// admin
|
||||||
config.AdminPath = json.Get("admin_path").String()
|
config.AdminPath = json.Get("admin_path").String()
|
||||||
@@ -127,7 +126,7 @@ func parseConfig(json gjson.Result, config *QuotaConfig, log log.Log) error {
|
|||||||
return config.redisClient.Init(username, password, int64(timeout), wrapper.WithDataBase(database))
|
return config.redisClient.Init(username, password, int64(timeout), wrapper.WithDataBase(database))
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(context wrapper.HttpContext, config QuotaConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(context wrapper.HttpContext, config QuotaConfig, log wrapper.Log) types.Action {
|
||||||
log.Debugf("onHttpRequestHeaders()")
|
log.Debugf("onHttpRequestHeaders()")
|
||||||
// get tokens
|
// get tokens
|
||||||
consumer, err := proxywasm.GetHttpRequestHeader("x-mse-consumer")
|
consumer, err := proxywasm.GetHttpRequestHeader("x-mse-consumer")
|
||||||
@@ -184,7 +183,7 @@ func onHttpRequestHeaders(context wrapper.HttpContext, config QuotaConfig, log l
|
|||||||
return types.HeaderStopAllIterationAndWatermark
|
return types.HeaderStopAllIterationAndWatermark
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config QuotaConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config QuotaConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
log.Debugf("onHttpRequestBody()")
|
log.Debugf("onHttpRequestBody()")
|
||||||
chatMode, ok := ctx.GetContext("chatMode").(ChatMode)
|
chatMode, ok := ctx.GetContext("chatMode").(ChatMode)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -212,7 +211,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config QuotaConfig, body []byte,
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpStreamingResponseBody(ctx wrapper.HttpContext, config QuotaConfig, data []byte, endOfStream bool, log log.Log) []byte {
|
func onHttpStreamingResponseBody(ctx wrapper.HttpContext, config QuotaConfig, data []byte, endOfStream bool, log wrapper.Log) []byte {
|
||||||
chatMode, ok := ctx.GetContext("chatMode").(ChatMode)
|
chatMode, ok := ctx.GetContext("chatMode").(ChatMode)
|
||||||
if !ok {
|
if !ok {
|
||||||
return data
|
return data
|
||||||
@@ -275,7 +274,7 @@ func deniedUnauthorizedConsumer() types.Action {
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func getOperationMode(path string, adminPath string, log log.Log) (ChatMode, AdminMode) {
|
func getOperationMode(path string, adminPath string, log wrapper.Log) (ChatMode, AdminMode) {
|
||||||
fullAdminPath := "/v1/chat/completions" + adminPath
|
fullAdminPath := "/v1/chat/completions" + adminPath
|
||||||
if strings.HasSuffix(path, fullAdminPath+"/refresh") {
|
if strings.HasSuffix(path, fullAdminPath+"/refresh") {
|
||||||
return ChatModeAdmin, AdminModeRefresh
|
return ChatModeAdmin, AdminModeRefresh
|
||||||
@@ -292,7 +291,7 @@ func getOperationMode(path string, adminPath string, log log.Log) (ChatMode, Adm
|
|||||||
return ChatModeNone, AdminModeNone
|
return ChatModeNone, AdminModeNone
|
||||||
}
|
}
|
||||||
|
|
||||||
func refreshQuota(ctx wrapper.HttpContext, config QuotaConfig, adminConsumer string, body string, log log.Log) types.Action {
|
func refreshQuota(ctx wrapper.HttpContext, config QuotaConfig, adminConsumer string, body string, log wrapper.Log) types.Action {
|
||||||
// check consumer
|
// check consumer
|
||||||
if adminConsumer != config.AdminConsumer {
|
if adminConsumer != config.AdminConsumer {
|
||||||
util.SendResponse(http.StatusForbidden, "ai-quota.unauthorized", "text/plain", "Request denied by ai quota check. Unauthorized admin consumer.")
|
util.SendResponse(http.StatusForbidden, "ai-quota.unauthorized", "text/plain", "Request denied by ai quota check. Unauthorized admin consumer.")
|
||||||
@@ -326,7 +325,7 @@ func refreshQuota(ctx wrapper.HttpContext, config QuotaConfig, adminConsumer str
|
|||||||
|
|
||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
func queryQuota(ctx wrapper.HttpContext, config QuotaConfig, adminConsumer string, url *url.URL, log log.Log) types.Action {
|
func queryQuota(ctx wrapper.HttpContext, config QuotaConfig, adminConsumer string, url *url.URL, log wrapper.Log) types.Action {
|
||||||
// check consumer
|
// check consumer
|
||||||
if adminConsumer != config.AdminConsumer {
|
if adminConsumer != config.AdminConsumer {
|
||||||
util.SendResponse(http.StatusForbidden, "ai-quota.unauthorized", "text/plain", "Request denied by ai quota check. Unauthorized admin consumer.")
|
util.SendResponse(http.StatusForbidden, "ai-quota.unauthorized", "text/plain", "Request denied by ai quota check. Unauthorized admin consumer.")
|
||||||
@@ -369,7 +368,7 @@ func queryQuota(ctx wrapper.HttpContext, config QuotaConfig, adminConsumer strin
|
|||||||
}
|
}
|
||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
func deltaQuota(ctx wrapper.HttpContext, config QuotaConfig, adminConsumer string, body string, log log.Log) types.Action {
|
func deltaQuota(ctx wrapper.HttpContext, config QuotaConfig, adminConsumer string, body string, log wrapper.Log) types.Action {
|
||||||
// check consumer
|
// check consumer
|
||||||
if adminConsumer != config.AdminConsumer {
|
if adminConsumer != config.AdminConsumer {
|
||||||
util.SendResponse(http.StatusForbidden, "ai-quota.unauthorized", "text/plain", "Request denied by ai quota check. Unauthorized admin consumer.")
|
util.SendResponse(http.StatusForbidden, "ai-quota.unauthorized", "text/plain", "Request denied by ai quota check. Unauthorized admin consumer.")
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"ai-rag/dashscope"
|
"ai-rag/dashscope"
|
||||||
"ai-rag/dashvector"
|
"ai-rag/dashvector"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -52,7 +51,7 @@ type Message struct {
|
|||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *AIRagConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *AIRagConfig, log wrapper.Log) error {
|
||||||
checkList := []string{
|
checkList := []string{
|
||||||
"dashscope.apiKey",
|
"dashscope.apiKey",
|
||||||
"dashscope.serviceFQDN",
|
"dashscope.serviceFQDN",
|
||||||
@@ -92,12 +91,12 @@ func parseConfig(json gjson.Result, config *AIRagConfig, log log.Log) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIRagConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIRagConfig, log wrapper.Log) types.Action {
|
||||||
proxywasm.RemoveHttpRequestHeader("content-length")
|
proxywasm.RemoveHttpRequestHeader("content-length")
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config AIRagConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config AIRagConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
var rawRequest Request
|
var rawRequest Request
|
||||||
_ = json.Unmarshal(body, &rawRequest)
|
_ = json.Unmarshal(body, &rawRequest)
|
||||||
messageLength := len(rawRequest.Messages)
|
messageLength := len(rawRequest.Messages)
|
||||||
@@ -166,7 +165,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config AIRagConfig, body []byte,
|
|||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config AIRagConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config AIRagConfig, log wrapper.Log) types.Action {
|
||||||
recall, ok := ctx.GetContext("x-envoy-rag-recall").(bool)
|
recall, ok := ctx.GetContext("x-envoy-rag-recall").(bool)
|
||||||
if ok && recall {
|
if ok && recall {
|
||||||
proxywasm.AddHttpResponseHeader("x-envoy-rag-recall", "true")
|
proxywasm.AddHttpResponseHeader("x-envoy-rag-recall", "true")
|
||||||
|
|||||||
@@ -24,18 +24,19 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
|
"github.com/tidwall/sjson"
|
||||||
|
|
||||||
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-search/engine"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-search/engine"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-search/engine/arxiv"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-search/engine/arxiv"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-search/engine/bing"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-search/engine/bing"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-search/engine/elasticsearch"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-search/engine/elasticsearch"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-search/engine/google"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-search/engine/google"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-search/engine/quark"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-search/engine/quark"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
|
||||||
"github.com/tidwall/gjson"
|
|
||||||
"github.com/tidwall/sjson"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SearchRewrite struct {
|
type SearchRewrite struct {
|
||||||
@@ -91,7 +92,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *Config, log log.Log) error {
|
func parseConfig(json gjson.Result, config *Config, log wrapper.Log) error {
|
||||||
config.defaultEnable = true // Default to true if not specified
|
config.defaultEnable = true // Default to true if not specified
|
||||||
if json.Get("defaultEnable").Exists() {
|
if json.Get("defaultEnable").Exists() {
|
||||||
config.defaultEnable = json.Get("defaultEnable").Bool()
|
config.defaultEnable = json.Get("defaultEnable").Bool()
|
||||||
@@ -275,7 +276,7 @@ func parseConfig(json gjson.Result, config *Config, log log.Log) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Log) types.Action {
|
||||||
contentType, _ := proxywasm.GetHttpRequestHeader("content-type")
|
contentType, _ := proxywasm.GetHttpRequestHeader("content-type")
|
||||||
// The request does not have a body.
|
// The request does not have a body.
|
||||||
if contentType == "" {
|
if contentType == "" {
|
||||||
@@ -291,7 +292,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log log.Log) t
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config Config, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config Config, body []byte, log wrapper.Log) types.Action {
|
||||||
// Check if plugin should be enabled based on config and request
|
// Check if plugin should be enabled based on config and request
|
||||||
webSearchOptions := gjson.GetBytes(body, "web_search_options")
|
webSearchOptions := gjson.GetBytes(body, "web_search_options")
|
||||||
if !config.defaultEnable {
|
if !config.defaultEnable {
|
||||||
@@ -450,7 +451,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config Config, body []byte, log
|
|||||||
}}, log)
|
}}, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeSearch(ctx wrapper.HttpContext, config Config, queryIndex int, body []byte, searchContexts []engine.SearchContext, log log.Log) types.Action {
|
func executeSearch(ctx wrapper.HttpContext, config Config, queryIndex int, body []byte, searchContexts []engine.SearchContext, log wrapper.Log) types.Action {
|
||||||
searchResultGroups := make([][]engine.SearchResult, len(config.engine))
|
searchResultGroups := make([][]engine.SearchResult, len(config.engine))
|
||||||
var finished int
|
var finished int
|
||||||
var searching int
|
var searching int
|
||||||
@@ -553,7 +554,7 @@ func executeSearch(ctx wrapper.HttpContext, config Config, queryIndex int, body
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config Config, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Log) types.Action {
|
||||||
if !config.needReference {
|
if !config.needReference {
|
||||||
ctx.DontReadResponseBody()
|
ctx.DontReadResponseBody()
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
@@ -570,7 +571,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config Config, log log.Log)
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseBody(ctx wrapper.HttpContext, config Config, body []byte, log log.Log) types.Action {
|
func onHttpResponseBody(ctx wrapper.HttpContext, config Config, body []byte, log wrapper.Log) types.Action {
|
||||||
references := ctx.GetStringContext("References", "")
|
references := ctx.GetStringContext("References", "")
|
||||||
if references == "" {
|
if references == "" {
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
@@ -624,7 +625,7 @@ const (
|
|||||||
BUFFER_SIZE = 30
|
BUFFER_SIZE = 30
|
||||||
)
|
)
|
||||||
|
|
||||||
func onStreamingResponseBody(ctx wrapper.HttpContext, config Config, chunk []byte, isLastChunk bool, log log.Log) []byte {
|
func onStreamingResponseBody(ctx wrapper.HttpContext, config Config, chunk []byte, isLastChunk bool, log wrapper.Log) []byte {
|
||||||
if ctx.GetBoolContext("ReferenceAppended", false) {
|
if ctx.GetBoolContext("ReferenceAppended", false) {
|
||||||
return chunk
|
return chunk
|
||||||
}
|
}
|
||||||
@@ -663,7 +664,7 @@ func onStreamingResponseBody(ctx wrapper.HttpContext, config Config, chunk []byt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func processSSEMessage(ctx wrapper.HttpContext, sseMessage string, references string, tailReference bool, log log.Log) string {
|
func processSSEMessage(ctx wrapper.HttpContext, sseMessage string, references string, tailReference bool, log wrapper.Log) string {
|
||||||
log.Debugf("single sse message: %s", sseMessage)
|
log.Debugf("single sse message: %s", sseMessage)
|
||||||
subMessages := strings.Split(sseMessage, "\n")
|
subMessages := strings.Split(sseMessage, "\n")
|
||||||
var message string
|
var message string
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -173,7 +172,7 @@ func generateHexID(length int) (string, error) {
|
|||||||
return hex.EncodeToString(bytes), nil
|
return hex.EncodeToString(bytes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *AISecurityConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *AISecurityConfig, log wrapper.Log) error {
|
||||||
serviceName := json.Get("serviceName").String()
|
serviceName := json.Get("serviceName").String()
|
||||||
servicePort := json.Get("servicePort").Int()
|
servicePort := json.Get("servicePort").Int()
|
||||||
serviceHost := json.Get("serviceHost").String()
|
serviceHost := json.Get("serviceHost").String()
|
||||||
@@ -251,7 +250,7 @@ func generateRandomID() string {
|
|||||||
return "chatcmpl-" + string(b)
|
return "chatcmpl-" + string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config AISecurityConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config AISecurityConfig, log wrapper.Log) types.Action {
|
||||||
if !config.checkRequest {
|
if !config.checkRequest {
|
||||||
log.Debugf("request checking is disabled")
|
log.Debugf("request checking is disabled")
|
||||||
ctx.DontReadRequestBody()
|
ctx.DontReadRequestBody()
|
||||||
@@ -259,7 +258,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config AISecurityConfig, log
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config AISecurityConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config AISecurityConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
log.Debugf("checking request body...")
|
log.Debugf("checking request body...")
|
||||||
startTime := time.Now().UnixMilli()
|
startTime := time.Now().UnixMilli()
|
||||||
content := gjson.GetBytes(body, config.requestContentJsonPath).String()
|
content := gjson.GetBytes(body, config.requestContentJsonPath).String()
|
||||||
@@ -368,7 +367,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config AISecurityConfig, body []
|
|||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config AISecurityConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config AISecurityConfig, log wrapper.Log) types.Action {
|
||||||
if !config.checkResponse {
|
if !config.checkResponse {
|
||||||
log.Debugf("response checking is disabled")
|
log.Debugf("response checking is disabled")
|
||||||
ctx.DontReadResponseBody()
|
ctx.DontReadResponseBody()
|
||||||
@@ -383,7 +382,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config AISecurityConfig, log
|
|||||||
return types.HeaderStopIteration
|
return types.HeaderStopIteration
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseBody(ctx wrapper.HttpContext, config AISecurityConfig, body []byte, log log.Log) types.Action {
|
func onHttpResponseBody(ctx wrapper.HttpContext, config AISecurityConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
log.Debugf("checking response body...")
|
log.Debugf("checking response body...")
|
||||||
startTime := time.Now().UnixMilli()
|
startTime := time.Now().UnixMilli()
|
||||||
contentType, _ := proxywasm.GetHttpResponseHeader("content-type")
|
contentType, _ := proxywasm.GetHttpResponseHeader("content-type")
|
||||||
@@ -508,7 +507,7 @@ func extractMessageFromStreamingBody(data []byte, jsonPath string) string {
|
|||||||
return strings.Join(strChunks, "")
|
return strings.Join(strChunks, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func marshalStr(raw string, log log.Log) string {
|
func marshalStr(raw string, log wrapper.Log) string {
|
||||||
helper := map[string]string{
|
helper := map[string]string{
|
||||||
"placeholder": raw,
|
"placeholder": raw,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -129,7 +128,7 @@ func (config *AIStatisticsConfig) incrementCounter(metricName string, inc uint64
|
|||||||
counter.Increment(inc)
|
counter.Increment(inc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(configJson gjson.Result, config *AIStatisticsConfig, log log.Log) error {
|
func parseConfig(configJson gjson.Result, config *AIStatisticsConfig, log wrapper.Log) error {
|
||||||
// Parse tracing span attributes setting.
|
// Parse tracing span attributes setting.
|
||||||
attributeConfigs := configJson.Get("attributes").Array()
|
attributeConfigs := configJson.Get("attributes").Array()
|
||||||
config.attributes = make([]Attribute, len(attributeConfigs))
|
config.attributes = make([]Attribute, len(attributeConfigs))
|
||||||
@@ -153,7 +152,7 @@ func parseConfig(configJson gjson.Result, config *AIStatisticsConfig, log log.Lo
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIStatisticsConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIStatisticsConfig, log wrapper.Log) types.Action {
|
||||||
route, _ := getRouteName()
|
route, _ := getRouteName()
|
||||||
cluster, _ := getClusterName()
|
cluster, _ := getClusterName()
|
||||||
api, api_error := getAPIName()
|
api, api_error := getAPIName()
|
||||||
@@ -177,7 +176,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIStatisticsConfig, lo
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config AIStatisticsConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config AIStatisticsConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
// Set user defined log & span attributes.
|
// Set user defined log & span attributes.
|
||||||
setAttributeBySource(ctx, config, RequestBody, body, log)
|
setAttributeBySource(ctx, config, RequestBody, body, log)
|
||||||
|
|
||||||
@@ -186,7 +185,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config AIStatisticsConfig, body
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config AIStatisticsConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config AIStatisticsConfig, log wrapper.Log) types.Action {
|
||||||
contentType, _ := proxywasm.GetHttpResponseHeader("content-type")
|
contentType, _ := proxywasm.GetHttpResponseHeader("content-type")
|
||||||
if !strings.Contains(contentType, "text/event-stream") {
|
if !strings.Contains(contentType, "text/event-stream") {
|
||||||
ctx.BufferResponseBody()
|
ctx.BufferResponseBody()
|
||||||
@@ -198,7 +197,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config AIStatisticsConfig, l
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpStreamingBody(ctx wrapper.HttpContext, config AIStatisticsConfig, data []byte, endOfStream bool, log log.Log) []byte {
|
func onHttpStreamingBody(ctx wrapper.HttpContext, config AIStatisticsConfig, data []byte, endOfStream bool, log wrapper.Log) []byte {
|
||||||
// Buffer stream body for record log & span attributes
|
// Buffer stream body for record log & span attributes
|
||||||
if config.shouldBufferStreamingBody {
|
if config.shouldBufferStreamingBody {
|
||||||
var streamingBodyBuffer []byte
|
var streamingBodyBuffer []byte
|
||||||
@@ -256,7 +255,7 @@ func onHttpStreamingBody(ctx wrapper.HttpContext, config AIStatisticsConfig, dat
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseBody(ctx wrapper.HttpContext, config AIStatisticsConfig, body []byte, log log.Log) types.Action {
|
func onHttpResponseBody(ctx wrapper.HttpContext, config AIStatisticsConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
// Get requestStartTime from http context
|
// Get requestStartTime from http context
|
||||||
requestStartTime, _ := ctx.GetContext(StatisticsRequestStartTime).(int64)
|
requestStartTime, _ := ctx.GetContext(StatisticsRequestStartTime).(int64)
|
||||||
|
|
||||||
@@ -314,7 +313,7 @@ func getUsage(data []byte) (model string, inputTokenUsage int64, outputTokenUsag
|
|||||||
}
|
}
|
||||||
|
|
||||||
// fetches the tracing span value from the specified source.
|
// fetches the tracing span value from the specified source.
|
||||||
func setAttributeBySource(ctx wrapper.HttpContext, config AIStatisticsConfig, source string, body []byte, log log.Log) {
|
func setAttributeBySource(ctx wrapper.HttpContext, config AIStatisticsConfig, source string, body []byte, log wrapper.Log) {
|
||||||
for _, attribute := range config.attributes {
|
for _, attribute := range config.attributes {
|
||||||
var key string
|
var key string
|
||||||
var value interface{}
|
var value interface{}
|
||||||
@@ -353,7 +352,7 @@ func setAttributeBySource(ctx wrapper.HttpContext, config AIStatisticsConfig, so
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractStreamingBodyByJsonPath(data []byte, jsonPath string, rule string, log log.Log) interface{} {
|
func extractStreamingBodyByJsonPath(data []byte, jsonPath string, rule string, log wrapper.Log) interface{} {
|
||||||
chunks := bytes.Split(bytes.TrimSpace(data), []byte("\n\n"))
|
chunks := bytes.Split(bytes.TrimSpace(data), []byte("\n\n"))
|
||||||
var value interface{}
|
var value interface{}
|
||||||
if rule == RuleFirst {
|
if rule == RuleFirst {
|
||||||
@@ -388,7 +387,7 @@ func extractStreamingBodyByJsonPath(data []byte, jsonPath string, rule string, l
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set the tracing span with value.
|
// Set the tracing span with value.
|
||||||
func setSpanAttribute(key string, value interface{}, log log.Log) {
|
func setSpanAttribute(key string, value interface{}, log wrapper.Log) {
|
||||||
if value != "" {
|
if value != "" {
|
||||||
traceSpanTag := wrapper.TraceSpanTagPrefix + key
|
traceSpanTag := wrapper.TraceSpanTagPrefix + key
|
||||||
if e := proxywasm.SetProperty([]string{traceSpanTag}, []byte(fmt.Sprint(value))); e != nil {
|
if e := proxywasm.SetProperty([]string{traceSpanTag}, []byte(fmt.Sprint(value))); e != nil {
|
||||||
@@ -399,7 +398,7 @@ func setSpanAttribute(key string, value interface{}, log log.Log) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeMetric(ctx wrapper.HttpContext, config AIStatisticsConfig, log log.Log) {
|
func writeMetric(ctx wrapper.HttpContext, config AIStatisticsConfig, log wrapper.Log) {
|
||||||
// Generate usage metrics
|
// Generate usage metrics
|
||||||
var ok bool
|
var ok bool
|
||||||
var route, cluster, model string
|
var route, cluster, model string
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@@ -85,7 +84,7 @@ type LimitConfigItem struct {
|
|||||||
timeWindow int64 // 时间窗口大小
|
timeWindow int64 // 时间窗口大小
|
||||||
}
|
}
|
||||||
|
|
||||||
func initRedisClusterClient(json gjson.Result, config *ClusterKeyRateLimitConfig, log log.Log) error {
|
func initRedisClusterClient(json gjson.Result, config *ClusterKeyRateLimitConfig, log wrapper.Log) error {
|
||||||
redisConfig := json.Get("redis")
|
redisConfig := json.Get("redis")
|
||||||
if !redisConfig.Exists() {
|
if !redisConfig.Exists() {
|
||||||
return errors.New("missing redis in config")
|
return errors.New("missing redis in config")
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -82,7 +81,7 @@ type LimitRedisContext struct {
|
|||||||
window int64
|
window int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *ClusterKeyRateLimitConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *ClusterKeyRateLimitConfig, log wrapper.Log) error {
|
||||||
err := initRedisClusterClient(json, config, log)
|
err := initRedisClusterClient(json, config, log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -96,7 +95,7 @@ func parseConfig(json gjson.Result, config *ClusterKeyRateLimitConfig, log log.L
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config ClusterKeyRateLimitConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config ClusterKeyRateLimitConfig, log wrapper.Log) types.Action {
|
||||||
// 判断是否命中限流规则
|
// 判断是否命中限流规则
|
||||||
val, ruleItem, configItem := checkRequestAgainstLimitRule(ctx, config.ruleItems, log)
|
val, ruleItem, configItem := checkRequestAgainstLimitRule(ctx, config.ruleItems, log)
|
||||||
if ruleItem == nil || configItem == nil {
|
if ruleItem == nil || configItem == nil {
|
||||||
@@ -144,7 +143,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config ClusterKeyRateLimitCon
|
|||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpStreamingBody(ctx wrapper.HttpContext, config ClusterKeyRateLimitConfig, data []byte, endOfStream bool, log log.Log) []byte {
|
func onHttpStreamingBody(ctx wrapper.HttpContext, config ClusterKeyRateLimitConfig, data []byte, endOfStream bool, log wrapper.Log) []byte {
|
||||||
var inputToken, outputToken int64
|
var inputToken, outputToken int64
|
||||||
if inputToken, outputToken, ok := getUsage(data); ok {
|
if inputToken, outputToken, ok := getUsage(data); ok {
|
||||||
ctx.SetContext("input_token", inputToken)
|
ctx.SetContext("input_token", inputToken)
|
||||||
@@ -190,7 +189,7 @@ func getUsage(data []byte) (inputTokenUsage int64, outputTokenUsage int64, ok bo
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkRequestAgainstLimitRule(ctx wrapper.HttpContext, ruleItems []LimitRuleItem, log log.Log) (string, *LimitRuleItem, *LimitConfigItem) {
|
func checkRequestAgainstLimitRule(ctx wrapper.HttpContext, ruleItems []LimitRuleItem, log wrapper.Log) (string, *LimitRuleItem, *LimitConfigItem) {
|
||||||
for _, rule := range ruleItems {
|
for _, rule := range ruleItems {
|
||||||
val, ruleItem, configItem := hitRateRuleItem(ctx, rule, log)
|
val, ruleItem, configItem := hitRateRuleItem(ctx, rule, log)
|
||||||
if ruleItem != nil && configItem != nil {
|
if ruleItem != nil && configItem != nil {
|
||||||
@@ -200,7 +199,7 @@ func checkRequestAgainstLimitRule(ctx wrapper.HttpContext, ruleItems []LimitRule
|
|||||||
return "", nil, nil
|
return "", nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func hitRateRuleItem(ctx wrapper.HttpContext, rule LimitRuleItem, log log.Log) (string, *LimitRuleItem, *LimitConfigItem) {
|
func hitRateRuleItem(ctx wrapper.HttpContext, rule LimitRuleItem, log wrapper.Log) (string, *LimitRuleItem, *LimitConfigItem) {
|
||||||
switch rule.limitType {
|
switch rule.limitType {
|
||||||
// 根据HTTP请求头限流
|
// 根据HTTP请求头限流
|
||||||
case limitByHeaderType, limitByPerHeaderType:
|
case limitByHeaderType, limitByPerHeaderType:
|
||||||
@@ -259,7 +258,7 @@ func hitRateRuleItem(ctx wrapper.HttpContext, rule LimitRuleItem, log log.Log) (
|
|||||||
return "", nil, nil
|
return "", nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func logDebugAndReturnEmpty(log log.Log, errMsg string, args ...interface{}) (string, *LimitRuleItem, *LimitConfigItem) {
|
func logDebugAndReturnEmpty(log wrapper.Log, errMsg string, args ...interface{}) (string, *LimitRuleItem, *LimitConfigItem) {
|
||||||
log.Debugf(errMsg, args...)
|
log.Debugf(errMsg, args...)
|
||||||
return "", nil, nil
|
return "", nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -53,7 +52,7 @@ const llmRequestTemplate = `{
|
|||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *AITransformerConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *AITransformerConfig, log wrapper.Log) error {
|
||||||
config.requestTransformEnable = json.Get("request.enable").Bool()
|
config.requestTransformEnable = json.Get("request.enable").Bool()
|
||||||
config.requestTransformPrompt = json.Get("request.prompt").String()
|
config.requestTransformPrompt = json.Get("request.prompt").String()
|
||||||
config.responseTransformEnable = json.Get("response.enable").Bool()
|
config.responseTransformEnable = json.Get("response.enable").Bool()
|
||||||
@@ -90,7 +89,7 @@ func extraceHttpFrame(frame string) ([][2]string, []byte, error) {
|
|||||||
return headers, body, nil
|
return headers, body, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config AITransformerConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config AITransformerConfig, log wrapper.Log) types.Action {
|
||||||
log.Info("onHttpRequestHeaders")
|
log.Info("onHttpRequestHeaders")
|
||||||
if !config.requestTransformEnable || config.requestTransformPrompt == "" {
|
if !config.requestTransformEnable || config.requestTransformPrompt == "" {
|
||||||
ctx.DontReadRequestBody()
|
ctx.DontReadRequestBody()
|
||||||
@@ -100,7 +99,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config AITransformerConfig, l
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config AITransformerConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config AITransformerConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
log.Info("onHttpRequestBody")
|
log.Info("onHttpRequestBody")
|
||||||
headers, err := proxywasm.GetHttpRequestHeaders()
|
headers, err := proxywasm.GetHttpRequestHeaders()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -134,7 +133,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config AITransformerConfig, body
|
|||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config AITransformerConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config AITransformerConfig, log wrapper.Log) types.Action {
|
||||||
if !config.responseTransformEnable || config.responseTransformPrompt == "" {
|
if !config.responseTransformEnable || config.responseTransformPrompt == "" {
|
||||||
ctx.DontReadResponseBody()
|
ctx.DontReadResponseBody()
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
@@ -143,7 +142,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config AITransformerConfig,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseBody(ctx wrapper.HttpContext, config AITransformerConfig, body []byte, log log.Log) types.Action {
|
func onHttpResponseBody(ctx wrapper.HttpContext, config AITransformerConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
headers, err := proxywasm.GetHttpResponseHeaders()
|
headers, err := proxywasm.GetHttpResponseHeaders()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to get http response headers.")
|
log.Error("Failed to get http response headers.")
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import (
|
|||||||
"api-workflow/utils"
|
"api-workflow/utils"
|
||||||
. "api-workflow/workflow"
|
. "api-workflow/workflow"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -45,7 +44,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, c *PluginConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, c *PluginConfig, log wrapper.Log) error {
|
||||||
|
|
||||||
edges := make([]Edge, 0)
|
edges := make([]Edge, 0)
|
||||||
nodes := make(map[string]Node)
|
nodes := make(map[string]Node)
|
||||||
@@ -175,7 +174,7 @@ func initWorkflowExecStatus(config *PluginConfig) (map[string]int, error) {
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
|
|
||||||
initHeader := make([][2]string, 0)
|
initHeader := make([][2]string, 0)
|
||||||
// 初始化运行状态
|
// 初始化运行状态
|
||||||
@@ -200,7 +199,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 放入符合条件的edge
|
// 放入符合条件的edge
|
||||||
func recursive(edge Edge, headers [][2]string, body []byte, depth uint32, config PluginConfig, log log.Log, ctx wrapper.HttpContext) error {
|
func recursive(edge Edge, headers [][2]string, body []byte, depth uint32, config PluginConfig, log wrapper.Log, ctx wrapper.HttpContext) error {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
// 防止递归次数太多
|
// 防止递归次数太多
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
|
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
@@ -107,7 +106,7 @@ var (
|
|||||||
protectionSpace = "MSE Gateway" // 认证失败时,返回响应头 WWW-Authenticate: Basic realm=MSE Gateway
|
protectionSpace = "MSE Gateway" // 认证失败时,返回响应头 WWW-Authenticate: Basic realm=MSE Gateway
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseGlobalConfig(json gjson.Result, global *BasicAuthConfig, log log.Log) error {
|
func parseGlobalConfig(json gjson.Result, global *BasicAuthConfig, log wrapper.Log) error {
|
||||||
// log.Debug("global config")
|
// log.Debug("global config")
|
||||||
ruleSet = false
|
ruleSet = false
|
||||||
global.credential2Name = make(map[string]string)
|
global.credential2Name = make(map[string]string)
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bot-detect/config"
|
"bot-detect/config"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -35,7 +34,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, botDetectConfig *config.BotDetectConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, botDetectConfig *config.BotDetectConfig, log wrapper.Log) error {
|
||||||
log.Debug("parseConfig()")
|
log.Debug("parseConfig()")
|
||||||
|
|
||||||
if json.Get("blocked_code").Exists() {
|
if json.Get("blocked_code").Exists() {
|
||||||
@@ -82,7 +81,7 @@ func parseConfig(json gjson.Result, botDetectConfig *config.BotDetectConfig, log
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, botDetectConfig config.BotDetectConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, botDetectConfig config.BotDetectConfig, log wrapper.Log) types.Action {
|
||||||
log.Debug("onHttpRequestHeaders()")
|
log.Debug("onHttpRequestHeaders()")
|
||||||
//// Get user-agent header
|
//// Get user-agent header
|
||||||
ua, err := proxywasm.GetHttpRequestHeader("user-agent")
|
ua, err := proxywasm.GetHttpRequestHeader("user-agent")
|
||||||
|
|||||||
@@ -1,16 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -27,7 +25,7 @@ type CacheControlConfig struct {
|
|||||||
expires string
|
expires string
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *CacheControlConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *CacheControlConfig, log wrapper.Log) error {
|
||||||
suffix := json.Get("suffix").String()
|
suffix := json.Get("suffix").String()
|
||||||
if suffix != "" {
|
if suffix != "" {
|
||||||
parts := strings.Split(suffix, "|")
|
parts := strings.Split(suffix, "|")
|
||||||
@@ -40,7 +38,7 @@ func parseConfig(json gjson.Result, config *CacheControlConfig, log log.Log) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config CacheControlConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config CacheControlConfig, log wrapper.Log) types.Action {
|
||||||
path := ctx.Path()
|
path := ctx.Path()
|
||||||
if strings.Contains(path, "?") {
|
if strings.Contains(path, "?") {
|
||||||
path = strings.Split(path, "?")[0]
|
path = strings.Split(path, "?")[0]
|
||||||
@@ -51,7 +49,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config CacheControlConfig, lo
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config CacheControlConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config CacheControlConfig, log wrapper.Log) types.Action {
|
||||||
hit := false
|
hit := false
|
||||||
if len(config.suffix) == 0 {
|
if len(config.suffix) == 0 {
|
||||||
hit = true
|
hit = true
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -32,7 +31,7 @@ type MyConfig struct {
|
|||||||
client wrapper.HttpClient
|
client wrapper.HttpClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *MyConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *MyConfig, log wrapper.Log) error {
|
||||||
chatgptUri := json.Get("chatgptUri").String()
|
chatgptUri := json.Get("chatgptUri").String()
|
||||||
var chatgptHost string
|
var chatgptHost string
|
||||||
if chatgptUri == "" {
|
if chatgptUri == "" {
|
||||||
@@ -91,7 +90,7 @@ const bodyTemplate string = `
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config MyConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config MyConfig, log wrapper.Log) types.Action {
|
||||||
pairs := strings.SplitN(ctx.Path(), "?", 2)
|
pairs := strings.SplitN(ctx.Path(), "?", 2)
|
||||||
|
|
||||||
if len(pairs) < 2 {
|
if len(pairs) < 2 {
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -65,7 +64,7 @@ type LimitContext struct {
|
|||||||
reset int
|
reset int
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *ClusterKeyRateLimitConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *ClusterKeyRateLimitConfig, log wrapper.Log) error {
|
||||||
err := initRedisClusterClient(json, config)
|
err := initRedisClusterClient(json, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -77,7 +76,7 @@ func parseConfig(json gjson.Result, config *ClusterKeyRateLimitConfig, log log.L
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config ClusterKeyRateLimitConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config ClusterKeyRateLimitConfig, log wrapper.Log) types.Action {
|
||||||
// 判断是否命中限流规则
|
// 判断是否命中限流规则
|
||||||
val, ruleItem, configItem := checkRequestAgainstLimitRule(ctx, config.ruleItems, log)
|
val, ruleItem, configItem := checkRequestAgainstLimitRule(ctx, config.ruleItems, log)
|
||||||
if ruleItem == nil || configItem == nil {
|
if ruleItem == nil || configItem == nil {
|
||||||
@@ -116,7 +115,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config ClusterKeyRateLimitCon
|
|||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config ClusterKeyRateLimitConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config ClusterKeyRateLimitConfig, log wrapper.Log) types.Action {
|
||||||
limitContext, ok := ctx.GetContext(LimitContextKey).(LimitContext)
|
limitContext, ok := ctx.GetContext(LimitContextKey).(LimitContext)
|
||||||
if !ok {
|
if !ok {
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
@@ -128,7 +127,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config ClusterKeyRateLimitCo
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkRequestAgainstLimitRule(ctx wrapper.HttpContext, ruleItems []LimitRuleItem, log log.Log) (string, *LimitRuleItem, *LimitConfigItem) {
|
func checkRequestAgainstLimitRule(ctx wrapper.HttpContext, ruleItems []LimitRuleItem, log wrapper.Log) (string, *LimitRuleItem, *LimitConfigItem) {
|
||||||
for _, rule := range ruleItems {
|
for _, rule := range ruleItems {
|
||||||
val, ruleItem, configItem := hitRateRuleItem(ctx, rule, log)
|
val, ruleItem, configItem := hitRateRuleItem(ctx, rule, log)
|
||||||
if ruleItem != nil && configItem != nil {
|
if ruleItem != nil && configItem != nil {
|
||||||
@@ -138,7 +137,7 @@ func checkRequestAgainstLimitRule(ctx wrapper.HttpContext, ruleItems []LimitRule
|
|||||||
return "", nil, nil
|
return "", nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func hitRateRuleItem(ctx wrapper.HttpContext, rule LimitRuleItem, log log.Log) (string, *LimitRuleItem, *LimitConfigItem) {
|
func hitRateRuleItem(ctx wrapper.HttpContext, rule LimitRuleItem, log wrapper.Log) (string, *LimitRuleItem, *LimitConfigItem) {
|
||||||
switch rule.limitType {
|
switch rule.limitType {
|
||||||
// 根据HTTP请求头限流
|
// 根据HTTP请求头限流
|
||||||
case limitByHeaderType, limitByPerHeaderType:
|
case limitByHeaderType, limitByPerHeaderType:
|
||||||
@@ -197,7 +196,7 @@ func hitRateRuleItem(ctx wrapper.HttpContext, rule LimitRuleItem, log log.Log) (
|
|||||||
return "", nil, nil
|
return "", nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func logDebugAndReturnEmpty(log log.Log, errMsg string, args ...interface{}) (string, *LimitRuleItem, *LimitConfigItem) {
|
func logDebugAndReturnEmpty(log wrapper.Log, errMsg string, args ...interface{}) (string, *LimitRuleItem, *LimitConfigItem) {
|
||||||
log.Debugf(errMsg, args...)
|
log.Debugf(errMsg, args...)
|
||||||
return "", nil, nil
|
return "", nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -35,7 +34,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, corsConfig *config.CorsConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, corsConfig *config.CorsConfig, log wrapper.Log) error {
|
||||||
log.Debug("parseConfig()")
|
log.Debug("parseConfig()")
|
||||||
allowOrigins := json.Get("allow_origins").Array()
|
allowOrigins := json.Get("allow_origins").Array()
|
||||||
for _, origin := range allowOrigins {
|
for _, origin := range allowOrigins {
|
||||||
@@ -72,7 +71,7 @@ func parseConfig(json gjson.Result, corsConfig *config.CorsConfig, log log.Log)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, corsConfig config.CorsConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, corsConfig config.CorsConfig, log wrapper.Log) types.Action {
|
||||||
log.Debug("onHttpRequestHeaders()")
|
log.Debug("onHttpRequestHeaders()")
|
||||||
requestUrl, _ := proxywasm.GetHttpRequestHeader(":path")
|
requestUrl, _ := proxywasm.GetHttpRequestHeader(":path")
|
||||||
method, _ := proxywasm.GetHttpRequestHeader(":method")
|
method, _ := proxywasm.GetHttpRequestHeader(":method")
|
||||||
@@ -110,7 +109,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, corsConfig config.CorsConfig,
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, corsConfig config.CorsConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, corsConfig config.CorsConfig, log wrapper.Log) types.Action {
|
||||||
log.Debug("onHttpResponseHeaders()")
|
log.Debug("onHttpResponseHeaders()")
|
||||||
// Remove trace header if existed
|
// Remove trace header if existed
|
||||||
proxywasm.RemoveHttpResponseHeader(config.HeaderPluginTrace)
|
proxywasm.RemoveHttpResponseHeader(config.HeaderPluginTrace)
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -44,7 +43,7 @@ type CustomResponseConfig struct {
|
|||||||
contentType string
|
contentType string
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(gjson gjson.Result, config *CustomResponseConfig, log log.Log) error {
|
func parseConfig(gjson gjson.Result, config *CustomResponseConfig, log wrapper.Log) error {
|
||||||
headersArray := gjson.Get("headers").Array()
|
headersArray := gjson.Get("headers").Array()
|
||||||
config.headers = make([][2]string, 0, len(headersArray))
|
config.headers = make([][2]string, 0, len(headersArray))
|
||||||
for _, v := range headersArray {
|
for _, v := range headersArray {
|
||||||
@@ -97,7 +96,7 @@ func parseConfig(gjson gjson.Result, config *CustomResponseConfig, log log.Log)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config CustomResponseConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config CustomResponseConfig, log wrapper.Log) types.Action {
|
||||||
if len(config.enableOnStatus) != 0 {
|
if len(config.enableOnStatus) != 0 {
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
@@ -109,7 +108,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config CustomResponseConfig,
|
|||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config CustomResponseConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config CustomResponseConfig, log wrapper.Log) types.Action {
|
||||||
// enableOnStatus is not empty, compare the status code.
|
// enableOnStatus is not empty, compare the status code.
|
||||||
// if match the status code, mock the response.
|
// if match the status code, mock the response.
|
||||||
statusCodeStr, err := proxywasm.GetHttpResponseHeader(":status")
|
statusCodeStr, err := proxywasm.GetHttpResponseHeader(":status")
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import (
|
|||||||
|
|
||||||
"de-graphql/config"
|
"de-graphql/config"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -38,7 +37,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *config.DeGraphQLConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *config.DeGraphQLConfig, log wrapper.Log) error {
|
||||||
log.Debug("parseConfig()")
|
log.Debug("parseConfig()")
|
||||||
gql := json.Get("gql").String()
|
gql := json.Get("gql").String()
|
||||||
endpoint := json.Get("endpoint").String()
|
endpoint := json.Get("endpoint").String()
|
||||||
@@ -58,7 +57,7 @@ func parseConfig(json gjson.Result, config *config.DeGraphQLConfig, log log.Log)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config config.DeGraphQLConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config config.DeGraphQLConfig, log wrapper.Log) types.Action {
|
||||||
log.Debug("onHttpRequestHeaders()")
|
log.Debug("onHttpRequestHeaders()")
|
||||||
log.Debugf("schema:%s host:%s path:%s", ctx.Scheme(), ctx.Host(), ctx.Path())
|
log.Debugf("schema:%s host:%s path:%s", ctx.Scheme(), ctx.Host(), ctx.Path())
|
||||||
requestUrl, _ := proxywasm.GetHttpRequestHeader(":path")
|
requestUrl, _ := proxywasm.GetHttpRequestHeader(":path")
|
||||||
@@ -103,17 +102,17 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config config.DeGraphQLConfig
|
|||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config config.DeGraphQLConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config config.DeGraphQLConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
log.Debug("onHttpRequestBody()")
|
log.Debug("onHttpRequestBody()")
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config config.DeGraphQLConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config config.DeGraphQLConfig, log wrapper.Log) types.Action {
|
||||||
log.Debug("onHttpResponseHeaders()")
|
log.Debug("onHttpResponseHeaders()")
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseBody(ctx wrapper.HttpContext, config config.DeGraphQLConfig, body []byte, log log.Log) types.Action {
|
func onHttpResponseBody(ctx wrapper.HttpContext, config config.DeGraphQLConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
log.Debug("onHttpResponseBody()")
|
log.Debug("onHttpResponseBody()")
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"ext-auth/expr"
|
"ext-auth/expr"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@@ -58,7 +56,7 @@ type AuthorizationResponse struct {
|
|||||||
AllowedClientHeaders expr.Matcher
|
AllowedClientHeaders expr.Matcher
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseConfig(json gjson.Result, config *ExtAuthConfig, log log.Log) error {
|
func ParseConfig(json gjson.Result, config *ExtAuthConfig, log wrapper.Log) error {
|
||||||
httpServiceConfig := json.Get("http_service")
|
httpServiceConfig := json.Get("http_service")
|
||||||
if !httpServiceConfig.Exists() {
|
if !httpServiceConfig.Exists() {
|
||||||
return errors.New("missing http_service in config")
|
return errors.New("missing http_service in config")
|
||||||
@@ -90,7 +88,7 @@ func ParseConfig(json gjson.Result, config *ExtAuthConfig, log log.Log) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseHttpServiceConfig(json gjson.Result, config *ExtAuthConfig, log log.Log) error {
|
func parseHttpServiceConfig(json gjson.Result, config *ExtAuthConfig, log wrapper.Log) error {
|
||||||
var httpService HttpService
|
var httpService HttpService
|
||||||
|
|
||||||
if err := parseEndpointConfig(json, &httpService, log); err != nil {
|
if err := parseEndpointConfig(json, &httpService, log); err != nil {
|
||||||
@@ -116,7 +114,7 @@ func parseHttpServiceConfig(json gjson.Result, config *ExtAuthConfig, log log.Lo
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseEndpointConfig(json gjson.Result, httpService *HttpService, log log.Log) error {
|
func parseEndpointConfig(json gjson.Result, httpService *HttpService, log wrapper.Log) error {
|
||||||
endpointMode := json.Get("endpoint_mode").String()
|
endpointMode := json.Get("endpoint_mode").String()
|
||||||
if endpointMode == "" {
|
if endpointMode == "" {
|
||||||
endpointMode = EndpointModeEnvoy
|
endpointMode = EndpointModeEnvoy
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"ext-auth/expr"
|
"ext-auth/expr"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ import (
|
|||||||
"ext-auth/config"
|
"ext-auth/config"
|
||||||
"ext-auth/util"
|
"ext-auth/util"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -51,7 +50,7 @@ const (
|
|||||||
HeaderXForwardedHost = "x-forwarded-host"
|
HeaderXForwardedHost = "x-forwarded-host"
|
||||||
)
|
)
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config config.ExtAuthConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config config.ExtAuthConfig, log wrapper.Log) types.Action {
|
||||||
// If the request's domain and path match the MatchRules, skip authentication
|
// If the request's domain and path match the MatchRules, skip authentication
|
||||||
if config.MatchRules.IsAllowedByMode(ctx.Host(), ctx.Method(), wrapper.GetRequestPathWithoutQuery()) {
|
if config.MatchRules.IsAllowedByMode(ctx.Host(), ctx.Method(), wrapper.GetRequestPathWithoutQuery()) {
|
||||||
ctx.DontReadRequestBody()
|
ctx.DontReadRequestBody()
|
||||||
@@ -74,14 +73,14 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config config.ExtAuthConfig,
|
|||||||
return checkExtAuth(ctx, config, nil, log, types.HeaderStopAllIterationAndWatermark)
|
return checkExtAuth(ctx, config, nil, log, types.HeaderStopAllIterationAndWatermark)
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config config.ExtAuthConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config config.ExtAuthConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
if config.HttpService.AuthorizationRequest.WithRequestBody {
|
if config.HttpService.AuthorizationRequest.WithRequestBody {
|
||||||
return checkExtAuth(ctx, config, body, log, types.DataStopIterationAndBuffer)
|
return checkExtAuth(ctx, config, body, log, types.DataStopIterationAndBuffer)
|
||||||
}
|
}
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkExtAuth(ctx wrapper.HttpContext, cfg config.ExtAuthConfig, body []byte, log log.Log, pauseAction types.Action) types.Action {
|
func checkExtAuth(ctx wrapper.HttpContext, cfg config.ExtAuthConfig, body []byte, log wrapper.Log, pauseAction types.Action) types.Action {
|
||||||
httpServiceConfig := cfg.HttpService
|
httpServiceConfig := cfg.HttpService
|
||||||
|
|
||||||
extAuthReqHeaders := buildExtAuthRequestHeaders(ctx, cfg)
|
extAuthReqHeaders := buildExtAuthRequestHeaders(ctx, cfg)
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/frontend-gray/config"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/frontend-gray/config"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/extensions/frontend-gray/util"
|
"github.com/alibaba/higress/plugins/wasm-go/extensions/frontend-gray/util"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -27,14 +26,14 @@ func main() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, grayConfig *config.GrayConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, grayConfig *config.GrayConfig, log wrapper.Log) error {
|
||||||
// 解析json 为GrayConfig
|
// 解析json 为GrayConfig
|
||||||
config.JsonToGrayConfig(json, grayConfig)
|
config.JsonToGrayConfig(json, grayConfig)
|
||||||
log.Infof("Rewrite: %v, GrayDeployments: %v", json.Get("rewrite"), json.Get("grayDeployments"))
|
log.Infof("Rewrite: %v, GrayDeployments: %v", json.Get("rewrite"), json.Get("grayDeployments"))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig, log wrapper.Log) types.Action {
|
||||||
requestPath, _ := proxywasm.GetHttpRequestHeader(":path")
|
requestPath, _ := proxywasm.GetHttpRequestHeader(":path")
|
||||||
requestPath = path.Clean(requestPath)
|
requestPath = path.Clean(requestPath)
|
||||||
parsedURL, err := url.Parse(requestPath)
|
parsedURL, err := url.Parse(requestPath)
|
||||||
@@ -130,7 +129,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig, log log.Log) types.Action {
|
func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig, log wrapper.Log) types.Action {
|
||||||
enabledGray, _ := ctx.GetContext(config.EnabledGray).(bool)
|
enabledGray, _ := ctx.GetContext(config.EnabledGray).(bool)
|
||||||
if !enabledGray {
|
if !enabledGray {
|
||||||
ctx.DontReadResponseBody()
|
ctx.DontReadResponseBody()
|
||||||
@@ -214,7 +213,7 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseBody(ctx wrapper.HttpContext, grayConfig config.GrayConfig, body []byte, log log.Log) types.Action {
|
func onHttpResponseBody(ctx wrapper.HttpContext, grayConfig config.GrayConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
enabledGray, _ := ctx.GetContext(config.EnabledGray).(bool)
|
enabledGray, _ := ctx.GetContext(config.EnabledGray).(bool)
|
||||||
if !enabledGray {
|
if !enabledGray {
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
|
|||||||
@@ -2,11 +2,9 @@ module higress/plugins/wasm-go/extensions/geo-ip
|
|||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
replace github.com/alibaba/higress/plugins/wasm-go => ../..
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/alibaba/higress/plugins/wasm-go v1.4.2
|
github.com/alibaba/higress/plugins/wasm-go v1.4.2
|
||||||
github.com/higress-group/proxy-wasm-go-sdk v1.0.0
|
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240711023527-ba358c48772f
|
||||||
github.com/stretchr/testify v1.8.4
|
github.com/stretchr/testify v1.8.4
|
||||||
github.com/tidwall/gjson v1.17.3
|
github.com/tidwall/gjson v1.17.3
|
||||||
github.com/zmap/go-iptree v0.0.0-20210731043055-d4e632617837
|
github.com/zmap/go-iptree v0.0.0-20210731043055-d4e632617837
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
github.com/alibaba/higress/plugins/wasm-go v1.4.2 h1:gH7OIGXm4wtW5Vo7L2deMPqF7OVWNESDHv1CaaTGu6s=
|
||||||
|
github.com/alibaba/higress/plugins/wasm-go v1.4.2/go.mod h1:359don/ahMxpfeLMzr29Cjwcu8IywTTDUzWlBPRNLHw=
|
||||||
github.com/asergeyev/nradix v0.0.0-20170505151046-3872ab85bb56 h1:Wi5Tgn8K+jDcBYL+dIMS1+qXYH2r7tpRAyBgqrWfQtw=
|
github.com/asergeyev/nradix v0.0.0-20170505151046-3872ab85bb56 h1:Wi5Tgn8K+jDcBYL+dIMS1+qXYH2r7tpRAyBgqrWfQtw=
|
||||||
github.com/asergeyev/nradix v0.0.0-20170505151046-3872ab85bb56/go.mod h1:8BhOLuqtSuT5NZtZMwfvEibi09RO3u79uqfHZzfDTR4=
|
github.com/asergeyev/nradix v0.0.0-20170505151046-3872ab85bb56/go.mod h1:8BhOLuqtSuT5NZtZMwfvEibi09RO3u79uqfHZzfDTR4=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
@@ -6,8 +8,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
|||||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520 h1:IHDghbGQ2DTIXHBHxWfqCYQW1fKjyJ/I7W1pMyUDeEA=
|
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520 h1:IHDghbGQ2DTIXHBHxWfqCYQW1fKjyJ/I7W1pMyUDeEA=
|
||||||
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520/go.mod h1:Nz8ORLaFiLWotg6GeKlJMhv8cci8mM43uEnLA5t8iew=
|
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520/go.mod h1:Nz8ORLaFiLWotg6GeKlJMhv8cci8mM43uEnLA5t8iew=
|
||||||
github.com/higress-group/proxy-wasm-go-sdk v1.0.0 h1:BZRNf4R7jr9hwRivg/E29nkVaKEak5MWjBDhWjuHijU=
|
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240711023527-ba358c48772f h1:ZIiIBRvIw62gA5MJhuwp1+2wWbqL9IGElQ499rUsYYg=
|
||||||
github.com/higress-group/proxy-wasm-go-sdk v1.0.0/go.mod h1:iiSyFbo+rAtbtGt/bsefv8GU57h9CCLYGJA74/tF5/0=
|
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240711023527-ba358c48772f/go.mod h1:hNFjhrLUIq+kJ9bOcs8QtiplSQ61GZXtd2xHKx4BYRo=
|
||||||
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
|
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
|
||||||
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
|
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -57,7 +56,7 @@ type GeoIpData struct {
|
|||||||
Isp string `json:"isp"`
|
Isp string `json:"isp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *GeoIpConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *GeoIpConfig, log wrapper.Log) error {
|
||||||
sourceType := json.Get("ip_source_type")
|
sourceType := json.Get("ip_source_type")
|
||||||
if sourceType.Exists() && sourceType.String() != "" {
|
if sourceType.Exists() && sourceType.String() != "" {
|
||||||
switch sourceType.String() {
|
switch sourceType.String() {
|
||||||
@@ -105,7 +104,7 @@ func parseConfig(json gjson.Result, config *GeoIpConfig, log log.Log) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadGeoIpDataToRdxtree(log log.Log) error {
|
func ReadGeoIpDataToRdxtree(log wrapper.Log) error {
|
||||||
GeoIpRdxTree = iptree.New()
|
GeoIpRdxTree = iptree.New()
|
||||||
|
|
||||||
//eg., cidr country province city isp
|
//eg., cidr country province city isp
|
||||||
@@ -142,7 +141,7 @@ func ReadGeoIpDataToRdxtree(log log.Log) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// search geodata using client ip in radixtree.
|
// search geodata using client ip in radixtree.
|
||||||
func SearchGeoIpDataInRdxtree(ip string, log log.Log) (*GeoIpData, error) {
|
func SearchGeoIpDataInRdxtree(ip string, log wrapper.Log) (*GeoIpData, error) {
|
||||||
val, found, err := GeoIpRdxTree.GetByString(ip)
|
val, found, err := GeoIpRdxTree.GetByString(ip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("search geo ip data in raditree failed. %v %s", err, ip)
|
log.Errorf("search geo ip data in raditree failed. %v %s", err, ip)
|
||||||
@@ -197,7 +196,7 @@ func isInternalIp(ip string) (string, error) {
|
|||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config GeoIpConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config GeoIpConfig, log wrapper.Log) types.Action {
|
||||||
var (
|
var (
|
||||||
s string
|
s string
|
||||||
err error
|
err error
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -24,7 +23,7 @@ type MyConfig struct {
|
|||||||
set_header []gjson.Result
|
set_header []gjson.Result
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *MyConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *MyConfig, log wrapper.Log) error {
|
||||||
config.set_header = json.Get("set_header").Array()
|
config.set_header = json.Get("set_header").Array()
|
||||||
config.rules = json.Get("rules").Array()
|
config.rules = json.Get("rules").Array()
|
||||||
for _, item := range config.rules {
|
for _, item := range config.rules {
|
||||||
|
|||||||
@@ -17,10 +17,10 @@ package main
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
|
|
||||||
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -33,7 +33,7 @@ func main() {
|
|||||||
type HelloWorldConfig struct {
|
type HelloWorldConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config HelloWorldConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config HelloWorldConfig, log wrapper.Log) types.Action {
|
||||||
err := proxywasm.AddHttpRequestHeader("hello", "world")
|
err := proxywasm.AddHttpRequestHeader("hello", "world")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Critical("failed to set request header")
|
log.Critical("failed to set request header")
|
||||||
|
|||||||
@@ -19,11 +19,11 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
|
||||||
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -41,7 +41,7 @@ type HttpCallConfig struct {
|
|||||||
tokenHeader string
|
tokenHeader string
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *HttpCallConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *HttpCallConfig, log wrapper.Log) error {
|
||||||
config.bodyHeader = json.Get("bodyHeader").String()
|
config.bodyHeader = json.Get("bodyHeader").String()
|
||||||
if config.bodyHeader == "" {
|
if config.bodyHeader == "" {
|
||||||
return errors.New("missing bodyHeader in config")
|
return errors.New("missing bodyHeader in config")
|
||||||
@@ -96,7 +96,7 @@ func parseConfig(json gjson.Result, config *HttpCallConfig, log log.Log) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config HttpCallConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config HttpCallConfig, log wrapper.Log) types.Action {
|
||||||
config.client.Get(config.requestPath, nil,
|
config.client.Get(config.requestPath, nil,
|
||||||
func(statusCode int, responseHeaders http.Header, responseBody []byte) {
|
func(statusCode int, responseHeaders http.Header, responseBody []byte) {
|
||||||
defer proxywasm.ResumeHttpRequest()
|
defer proxywasm.ResumeHttpRequest()
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -39,7 +38,7 @@ func main() {
|
|||||||
wrapper.ProcessRequestHeadersBy(onHttpRequestHeaders))
|
wrapper.ProcessRequestHeadersBy(onHttpRequestHeaders))
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *RestrictionConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *RestrictionConfig, log wrapper.Log) error {
|
||||||
sourceType := json.Get("ip_source_type")
|
sourceType := json.Get("ip_source_type")
|
||||||
if sourceType.Exists() && sourceType.String() != "" {
|
if sourceType.Exists() && sourceType.String() != "" {
|
||||||
switch sourceType.String() {
|
switch sourceType.String() {
|
||||||
@@ -118,7 +117,7 @@ func getDownStreamIp(config RestrictionConfig) (net.IP, error) {
|
|||||||
return realIP, nil
|
return realIP, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(context wrapper.HttpContext, config RestrictionConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(context wrapper.HttpContext, config RestrictionConfig, log wrapper.Log) types.Action {
|
||||||
realIp, err := getDownStreamIp(config)
|
realIp, err := getDownStreamIp(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return deniedUnauthorized(config, "get_ip_failed")
|
return deniedUnauthorized(config, "get_ip_failed")
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/go-jose/go-jose/v3"
|
"github.com/go-jose/go-jose/v3"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@@ -28,7 +28,7 @@ var RuleSet bool
|
|||||||
|
|
||||||
// ParseGlobalConfig 从wrapper提供的配置中解析并转换到插件运行时需要使用的配置。
|
// ParseGlobalConfig 从wrapper提供的配置中解析并转换到插件运行时需要使用的配置。
|
||||||
// 此处解析的是全局配置,域名和路由级配置由 ParseRuleConfig 负责。
|
// 此处解析的是全局配置,域名和路由级配置由 ParseRuleConfig 负责。
|
||||||
func ParseGlobalConfig(json gjson.Result, config *JWTAuthConfig, log log.Log) error {
|
func ParseGlobalConfig(json gjson.Result, config *JWTAuthConfig, log wrapper.Log) error {
|
||||||
RuleSet = false
|
RuleSet = false
|
||||||
consumers := json.Get("consumers")
|
consumers := json.Get("consumers")
|
||||||
if !consumers.IsArray() {
|
if !consumers.IsArray() {
|
||||||
@@ -53,7 +53,7 @@ func ParseGlobalConfig(json gjson.Result, config *JWTAuthConfig, log log.Log) er
|
|||||||
|
|
||||||
// ParseRuleConfig 从wrapper提供的配置中解析并转换到插件运行时需要使用的配置。
|
// ParseRuleConfig 从wrapper提供的配置中解析并转换到插件运行时需要使用的配置。
|
||||||
// 此处解析的是域名和路由级配置,全局配置由 ParseConfig 负责。
|
// 此处解析的是域名和路由级配置,全局配置由 ParseConfig 负责。
|
||||||
func ParseRuleConfig(json gjson.Result, global JWTAuthConfig, config *JWTAuthConfig, log log.Log) error {
|
func ParseRuleConfig(json gjson.Result, global JWTAuthConfig, config *JWTAuthConfig, log wrapper.Log) error {
|
||||||
// override config via global
|
// override config via global
|
||||||
*config = global
|
*config = global
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
cfg "github.com/alibaba/higress/plugins/wasm-go/extensions/jwt-auth/config"
|
cfg "github.com/alibaba/higress/plugins/wasm-go/extensions/jwt-auth/config"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
)
|
)
|
||||||
@@ -37,7 +36,7 @@ import (
|
|||||||
// - 若有至少一个 domain/route 配置该插件:则遵循 (2*)
|
// - 若有至少一个 domain/route 配置该插件:则遵循 (2*)
|
||||||
//
|
//
|
||||||
// https://github.com/alibaba/higress/blob/e09edff827b94fa5bcc149bbeadc905361100c2a/plugins/wasm-go/extensions/basic-auth/main.go#L191
|
// https://github.com/alibaba/higress/blob/e09edff827b94fa5bcc149bbeadc905361100c2a/plugins/wasm-go/extensions/basic-auth/main.go#L191
|
||||||
func OnHTTPRequestHeaders(ctx wrapper.HttpContext, config cfg.JWTAuthConfig, log log.Log) types.Action {
|
func OnHTTPRequestHeaders(ctx wrapper.HttpContext, config cfg.JWTAuthConfig, log wrapper.Log) types.Action {
|
||||||
var (
|
var (
|
||||||
noAllow = len(config.Allow) == 0 // 未配置 allow 列表,表示插件在该 domain/route 未生效
|
noAllow = len(config.Allow) == 0 // 未配置 allow 列表,表示插件在该 domain/route 未生效
|
||||||
globalAuthNoSet = config.GlobalAuthCheck() == cfg.GlobalAuthNoSet
|
globalAuthNoSet = config.GlobalAuthCheck() == cfg.GlobalAuthNoSet
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -128,7 +127,7 @@ type KeyAuthConfig struct {
|
|||||||
credential2Name map[string]string `yaml:"-"`
|
credential2Name map[string]string `yaml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseGlobalConfig(json gjson.Result, global *KeyAuthConfig, log log.Log) error {
|
func parseGlobalConfig(json gjson.Result, global *KeyAuthConfig, log wrapper.Log) error {
|
||||||
log.Debug("global config")
|
log.Debug("global config")
|
||||||
|
|
||||||
// init
|
// init
|
||||||
@@ -201,7 +200,7 @@ func parseGlobalConfig(json gjson.Result, global *KeyAuthConfig, log log.Log) er
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseOverrideRuleConfig(json gjson.Result, global KeyAuthConfig, config *KeyAuthConfig, log log.Log) error {
|
func parseOverrideRuleConfig(json gjson.Result, global KeyAuthConfig, config *KeyAuthConfig, log wrapper.Log) error {
|
||||||
log.Debug("domain/route config")
|
log.Debug("domain/route config")
|
||||||
|
|
||||||
*config = global
|
*config = global
|
||||||
@@ -234,7 +233,7 @@ func parseOverrideRuleConfig(json gjson.Result, global KeyAuthConfig, config *Ke
|
|||||||
// - global_auth 未设置:
|
// - global_auth 未设置:
|
||||||
// - 若没有一个 domain/route 配置该插件:则遵循 (1*)
|
// - 若没有一个 domain/route 配置该插件:则遵循 (1*)
|
||||||
// - 若有至少一个 domain/route 配置该插件:则遵循 (2*)
|
// - 若有至少一个 domain/route 配置该插件:则遵循 (2*)
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config KeyAuthConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config KeyAuthConfig, log wrapper.Log) types.Action {
|
||||||
var (
|
var (
|
||||||
noAllow = len(config.allow) == 0 // 未配置 allow 列表,表示插件在该 domain/route 未生效
|
noAllow = len(config.allow) == 0 // 未配置 allow 列表,表示插件在该 domain/route 未生效
|
||||||
globalAuthNoSet = config.globalAuth == nil
|
globalAuthNoSet = config.globalAuth == nil
|
||||||
|
|||||||
@@ -6,11 +6,11 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
|
||||||
oidc "github.com/higress-group/oauth2-proxy"
|
oidc "github.com/higress-group/oauth2-proxy"
|
||||||
"github.com/higress-group/oauth2-proxy/pkg/apis/options"
|
"github.com/higress-group/oauth2-proxy/pkg/apis/options"
|
||||||
"github.com/higress-group/oauth2-proxy/pkg/util"
|
"github.com/higress-group/oauth2-proxy/pkg/util"
|
||||||
|
|
||||||
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@@ -35,7 +35,7 @@ type PluginConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 在控制台插件配置中填写的yaml配置会自动转换为json,此处直接从json这个参数里解析配置即可
|
// 在控制台插件配置中填写的yaml配置会自动转换为json,此处直接从json这个参数里解析配置即可
|
||||||
func parseConfig(json gjson.Result, config *PluginConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *PluginConfig, log wrapper.Log) error {
|
||||||
oidc.SetLogger(log)
|
oidc.SetLogger(log)
|
||||||
opts, err := oidc.LoadOptions(json)
|
opts, err := oidc.LoadOptions(json)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -55,7 +55,7 @@ func parseConfig(json gjson.Result, config *PluginConfig, log log.Log) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log) types.Action {
|
||||||
config.oidcHandler.SetContext(ctx)
|
config.oidcHandler.SetContext(ctx)
|
||||||
req := getHttpRequest()
|
req := getHttpRequest()
|
||||||
rw := util.NewRecorder()
|
rw := util.NewRecorder()
|
||||||
@@ -77,7 +77,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.
|
|||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config PluginConfig, log wrapper.Log) types.Action {
|
||||||
value := ctx.GetContext(oidc.SetCookieHeader)
|
value := ctx.GetContext(oidc.SetCookieHeader)
|
||||||
if value != nil {
|
if value != nil {
|
||||||
proxywasm.AddHttpResponseHeader(oidc.SetCookieHeader, value.(string))
|
proxywasm.AddHttpResponseHeader(oidc.SetCookieHeader, value.(string))
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@@ -25,7 +25,7 @@ import (
|
|||||||
func TestConfig(t *testing.T) {
|
func TestConfig(t *testing.T) {
|
||||||
json := gjson.Result{Type: gjson.JSON, Raw: `{"serviceSource": "k8s","serviceName": "opa","servicePort": 8181,"namespace": "example1","policy": "example1","timeout": "5s"}`}
|
json := gjson.Result{Type: gjson.JSON, Raw: `{"serviceSource": "k8s","serviceName": "opa","servicePort": 8181,"namespace": "example1","policy": "example1","timeout": "5s"}`}
|
||||||
config := &OpaConfig{}
|
config := &OpaConfig{}
|
||||||
assert.NoError(t, parseConfig(json, config, log.Log{}))
|
assert.NoError(t, parseConfig(json, config, wrapper.Log{}))
|
||||||
assert.Equal(t, config.policy, "example1")
|
assert.Equal(t, config.policy, "example1")
|
||||||
assert.Equal(t, config.timeout, uint32(5000))
|
assert.Equal(t, config.timeout, uint32(5000))
|
||||||
assert.NotNil(t, config.client)
|
assert.NotNil(t, config.client)
|
||||||
@@ -45,6 +45,6 @@ func TestConfig(t *testing.T) {
|
|||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
json = gjson.Result{Type: gjson.JSON, Raw: test.raw}
|
json = gjson.Result{Type: gjson.JSON, Raw: test.raw}
|
||||||
assert.Equal(t, parseConfig(json, config, log.Log{}) == nil, test.result)
|
assert.Equal(t, parseConfig(json, config, wrapper.Log{}) == nil, test.result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -43,7 +42,7 @@ type Metadata struct {
|
|||||||
Input map[string]interface{} `json:"input"`
|
Input map[string]interface{} `json:"input"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *OpaConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *OpaConfig, log wrapper.Log) error {
|
||||||
policy := json.Get("policy").String()
|
policy := json.Get("policy").String()
|
||||||
if strings.TrimSpace(policy) == "" {
|
if strings.TrimSpace(policy) == "" {
|
||||||
return errors.New("policy not allow empty")
|
return errors.New("policy not allow empty")
|
||||||
@@ -77,15 +76,15 @@ func parseConfig(json gjson.Result, config *OpaConfig, log log.Log) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config OpaConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config OpaConfig, log wrapper.Log) types.Action {
|
||||||
return opaCall(ctx, config, nil, log)
|
return opaCall(ctx, config, nil, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config OpaConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config OpaConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
return opaCall(ctx, config, body, log)
|
return opaCall(ctx, config, body, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
func opaCall(ctx wrapper.HttpContext, config OpaConfig, body []byte, log log.Log) types.Action {
|
func opaCall(ctx wrapper.HttpContext, config OpaConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
request := make(map[string]interface{}, 6)
|
request := make(map[string]interface{}, 6)
|
||||||
headers, _ := proxywasm.GetHttpRequestHeaders()
|
headers, _ := proxywasm.GetHttpRequestHeaders()
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@@ -26,7 +25,7 @@ type RedisConfig struct {
|
|||||||
KeyPrefix string
|
KeyPrefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseConfig(json gjson.Result, config *ReplayProtectionConfig, log log.Log) error {
|
func ParseConfig(json gjson.Result, config *ReplayProtectionConfig, log wrapper.Log) error {
|
||||||
// Parse Redis configuration
|
// Parse Redis configuration
|
||||||
redisConfig := json.Get("redis")
|
redisConfig := json.Get("redis")
|
||||||
if !redisConfig.Exists() {
|
if !redisConfig.Exists() {
|
||||||
|
|||||||
@@ -3,14 +3,12 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"replay-protection/config"
|
|
||||||
"replay-protection/util"
|
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
"github.com/tidwall/resp"
|
"github.com/tidwall/resp"
|
||||||
|
"replay-protection/config"
|
||||||
|
"replay-protection/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -21,7 +19,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, cfg config.ReplayProtectionConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, cfg config.ReplayProtectionConfig, log wrapper.Log) types.Action {
|
||||||
nonce, _ := proxywasm.GetHttpRequestHeader(cfg.NonceHeader)
|
nonce, _ := proxywasm.GetHttpRequestHeader(cfg.NonceHeader)
|
||||||
if cfg.ForceNonce && nonce == "" {
|
if cfg.ForceNonce && nonce == "" {
|
||||||
// In force mode, reject the request if a required header is missing.
|
// In force mode, reject the request if a required header is missing.
|
||||||
|
|||||||
@@ -19,12 +19,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
regexp "github.com/wasilibs/go-re2"
|
regexp "github.com/wasilibs/go-re2"
|
||||||
|
|
||||||
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -47,7 +47,7 @@ type RequestBlockConfig struct {
|
|||||||
blockRegExpArray []*regexp.Regexp
|
blockRegExpArray []*regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *RequestBlockConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *RequestBlockConfig, log wrapper.Log) error {
|
||||||
code := json.Get("blocked_code").Int()
|
code := json.Get("blocked_code").Int()
|
||||||
if code != 0 && code > 100 && code < 600 {
|
if code != 0 && code > 100 && code < 600 {
|
||||||
config.blockedCode = uint32(code)
|
config.blockedCode = uint32(code)
|
||||||
@@ -120,7 +120,7 @@ func parseConfig(json gjson.Result, config *RequestBlockConfig, log log.Log) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config RequestBlockConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config RequestBlockConfig, log wrapper.Log) types.Action {
|
||||||
if len(config.blockUrls) > 0 {
|
if len(config.blockUrls) > 0 {
|
||||||
requestUrl, err := proxywasm.GetHttpRequestHeader(":path")
|
requestUrl, err := proxywasm.GetHttpRequestHeader(":path")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -176,7 +176,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config RequestBlockConfig, lo
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config RequestBlockConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config RequestBlockConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
log.Infof("My request-block body: %s\n", string(body))
|
log.Infof("My request-block body: %s\n", string(body))
|
||||||
bodyStr := string(body)
|
bodyStr := string(body)
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -58,7 +57,7 @@ type Config struct {
|
|||||||
enableHeaderSchema bool
|
enableHeaderSchema bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(result gjson.Result, config *Config, log log.Log) error {
|
func parseConfig(result gjson.Result, config *Config, log wrapper.Log) error {
|
||||||
headerSchema := result.Get("header_schema").String()
|
headerSchema := result.Get("header_schema").String()
|
||||||
bodySchema := result.Get("body_schema").String()
|
bodySchema := result.Get("body_schema").String()
|
||||||
enableSwagger := result.Get("enable_swagger").Bool()
|
enableSwagger := result.Get("enable_swagger").Bool()
|
||||||
@@ -120,7 +119,7 @@ func parseConfig(result gjson.Result, config *Config, log log.Log) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Log) types.Action {
|
||||||
if !config.enableHeaderSchema {
|
if !config.enableHeaderSchema {
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
@@ -162,7 +161,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log log.Log) t
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config Config, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config Config, body []byte, log wrapper.Log) types.Action {
|
||||||
if !config.enableBodySchema {
|
if !config.enableBodySchema {
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
jwt "github.com/dgrijalva/jwt-go"
|
jwt "github.com/dgrijalva/jwt-go"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
@@ -32,14 +31,14 @@ type Res struct {
|
|||||||
Msg string `json:"msg"` // 返回信息
|
Msg string `json:"msg"` // 返回信息
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *Config, log log.Log) error {
|
func parseConfig(json gjson.Result, config *Config, log wrapper.Log) error {
|
||||||
// 解析出配置,更新到config中
|
// 解析出配置,更新到config中
|
||||||
config.TokenSecretKey = json.Get("token_secret_key").String()
|
config.TokenSecretKey = json.Get("token_secret_key").String()
|
||||||
config.TokenHeaders = json.Get("token_headers").String()
|
config.TokenHeaders = json.Get("token_headers").String()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Log) types.Action {
|
||||||
var res Res
|
var res Res
|
||||||
if config.TokenHeaders == "" || config.TokenSecretKey == "" {
|
if config.TokenHeaders == "" || config.TokenSecretKey == "" {
|
||||||
res.Code = http.StatusBadRequest
|
res.Code = http.StatusBadRequest
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -20,7 +19,7 @@ func main() {
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Log) types.Action {
|
||||||
// no need to check HTTP/1.0 and HTTP/1.1
|
// no need to check HTTP/1.0 and HTTP/1.1
|
||||||
protocol, err := proxywasm.GetProperty([]string{"request", "protocol"})
|
protocol, err := proxywasm.GetProperty([]string{"request", "protocol"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import (
|
|||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,22 +34,22 @@ func main() {
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Log) types.Action {
|
||||||
proxywasm.RemoveHttpRequestHeader("content-length")
|
proxywasm.RemoveHttpRequestHeader("content-length")
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config Config, chunk []byte, isLastChunk bool, log log.Log) []byte {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config Config, chunk []byte, isLastChunk bool, log wrapper.Log) []byte {
|
||||||
log.Infof("receive request body chunk:%s, isLastChunk:%v", chunk, isLastChunk)
|
log.Infof("receive request body chunk:%s, isLastChunk:%v", chunk, isLastChunk)
|
||||||
return []byte("test\n")
|
return []byte("test\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config Config, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Log) types.Action {
|
||||||
proxywasm.RemoveHttpResponseHeader("content-length")
|
proxywasm.RemoveHttpResponseHeader("content-length")
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseBody(ctx wrapper.HttpContext, config Config, chunk []byte, isLastChunk bool, log log.Log) []byte {
|
func onHttpResponseBody(ctx wrapper.HttpContext, config Config, chunk []byte, isLastChunk bool, log wrapper.Log) []byte {
|
||||||
log.Infof("receive response body chunk:%s, isLastChunk:%v", chunk, isLastChunk)
|
log.Infof("receive response body chunk:%s, isLastChunk:%v", chunk, isLastChunk)
|
||||||
return []byte("test\n")
|
return []byte("test\n")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,11 +21,11 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func onContentRequestHeaders(conditionGroups []ConditionGroup, log log.Log) bool {
|
func onContentRequestHeaders(conditionGroups []ConditionGroup, log wrapper.Log) bool {
|
||||||
for _, cg := range conditionGroups {
|
for _, cg := range conditionGroups {
|
||||||
if matchCondition(&cg, log) {
|
if matchCondition(&cg, log) {
|
||||||
addTagHeader(cg.HeaderName, cg.HeaderValue, log)
|
addTagHeader(cg.HeaderName, cg.HeaderValue, log)
|
||||||
@@ -37,7 +37,7 @@ func onContentRequestHeaders(conditionGroups []ConditionGroup, log log.Log) bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
// matchCondition matches the single condition group
|
// matchCondition matches the single condition group
|
||||||
func matchCondition(conditionGroup *ConditionGroup, log log.Log) bool {
|
func matchCondition(conditionGroup *ConditionGroup, log wrapper.Log) bool {
|
||||||
for _, condition := range conditionGroup.Conditions {
|
for _, condition := range conditionGroup.Conditions {
|
||||||
conditionKeyValue, err := getConditionValue(condition, log)
|
conditionKeyValue, err := getConditionValue(condition, log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -143,7 +143,7 @@ func matchCondition(conditionGroup *ConditionGroup, log log.Log) bool {
|
|||||||
return len(conditionGroup.Conditions) > 0 && conditionGroup.Logic == "and" // all conditions are matched
|
return len(conditionGroup.Conditions) > 0 && conditionGroup.Logic == "and" // all conditions are matched
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConditionValue(condition ConditionRule, log log.Log) (string, error) {
|
func getConditionValue(condition ConditionRule, log wrapper.Log) (string, error) {
|
||||||
// log.Debugf("conditionType: %s, key: %s", condition.ConditionType, condition.Key)
|
// log.Debugf("conditionType: %s, key: %s", condition.ConditionType, condition.Key)
|
||||||
|
|
||||||
switch condition.ConditionType {
|
switch condition.ConditionType {
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@@ -93,7 +92,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *TrafficTagConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *TrafficTagConfig, log wrapper.Log) error {
|
||||||
|
|
||||||
jsonStr := strings.TrimSpace(json.Raw)
|
jsonStr := strings.TrimSpace(json.Raw)
|
||||||
if jsonStr == "{}" || jsonStr == "" {
|
if jsonStr == "{}" || jsonStr == "" {
|
||||||
|
|||||||
@@ -23,13 +23,13 @@ import (
|
|||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
regexp "github.com/wasilibs/go-re2"
|
regexp "github.com/wasilibs/go-re2"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
var regexCache = map[string]*regexp.Regexp{}
|
var regexCache = map[string]*regexp.Regexp{}
|
||||||
|
|
||||||
func parseContentConfig(json gjson.Result, config *TrafficTagConfig, log log.Log) error {
|
func parseContentConfig(json gjson.Result, config *TrafficTagConfig, log wrapper.Log) error {
|
||||||
var parseError error
|
var parseError error
|
||||||
config.ConditionGroups = []ConditionGroup{}
|
config.ConditionGroups = []ConditionGroup{}
|
||||||
|
|
||||||
@@ -79,7 +79,7 @@ func parseContentConfig(json gjson.Result, config *TrafficTagConfig, log log.Log
|
|||||||
return parseError
|
return parseError
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseWeightConfig(json gjson.Result, config *TrafficTagConfig, log log.Log) error {
|
func parseWeightConfig(json gjson.Result, config *TrafficTagConfig, log wrapper.Log) error {
|
||||||
var parseError error
|
var parseError error
|
||||||
var accumulatedWeight int64
|
var accumulatedWeight int64
|
||||||
config.WeightGroups = []WeightGroup{}
|
config.WeightGroups = []WeightGroup{}
|
||||||
|
|||||||
@@ -19,11 +19,11 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func setDefaultTag(k string, v string, log log.Log) {
|
func setDefaultTag(k string, v string, log wrapper.Log) {
|
||||||
if k == "" || v == "" {
|
if k == "" || v == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -61,7 +61,7 @@ func getQueryParameter(urlStr, paramKey string) (string, error) {
|
|||||||
return values[0], nil
|
return values[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func addTagHeader(key string, value string, log log.Log) {
|
func addTagHeader(key string, value string, log wrapper.Log) {
|
||||||
existValue, _ := proxywasm.GetHttpRequestHeader(key)
|
existValue, _ := proxywasm.GetHttpRequestHeader(key)
|
||||||
if existValue != "" {
|
if existValue != "" {
|
||||||
log.Infof("ADD HEADER failed: %s already exists, value: %s", key, existValue)
|
log.Infof("ADD HEADER failed: %s already exists, value: %s", key, existValue)
|
||||||
|
|||||||
@@ -15,10 +15,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func onWeightRequestHeaders(weightGroups []WeightGroup, randomNum uint64, log log.Log) bool {
|
func onWeightRequestHeaders(weightGroups []WeightGroup, randomNum uint64, log wrapper.Log) bool {
|
||||||
randomValue := randomNum % TotalWeight
|
randomValue := randomNum % TotalWeight
|
||||||
log.Debugf("random value for weighted headers : %d", randomValue)
|
log.Debugf("random value for weighted headers : %d", randomValue)
|
||||||
// CDF
|
// CDF
|
||||||
|
|||||||
@@ -17,14 +17,14 @@ package main
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
"github.com/tidwall/sjson"
|
"github.com/tidwall/sjson"
|
||||||
regexp "github.com/wasilibs/go-re2"
|
regexp "github.com/wasilibs/go-re2"
|
||||||
|
|
||||||
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -214,7 +214,7 @@ type Param struct {
|
|||||||
pathPattern string `yaml:"path_pattern"`
|
pathPattern string `yaml:"path_pattern"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *TransformerConfig, log log.Log) (err error) {
|
func parseConfig(json gjson.Result, config *TransformerConfig, log wrapper.Log) (err error) {
|
||||||
reqRulesInJson := json.Get("reqRules")
|
reqRulesInJson := json.Get("reqRules")
|
||||||
respRulesInJson := json.Get("respRules")
|
respRulesInJson := json.Get("respRules")
|
||||||
|
|
||||||
@@ -284,7 +284,7 @@ func constructParam(item gjson.Result, op, valueType string) Param {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config TransformerConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config TransformerConfig, log wrapper.Log) types.Action {
|
||||||
// because it may be a response transformer, so the setting of host and path have to advance
|
// because it may be a response transformer, so the setting of host and path have to advance
|
||||||
host, path := ctx.Host(), ctx.Path()
|
host, path := ctx.Host(), ctx.Path()
|
||||||
ctx.SetContext("host", host)
|
ctx.SetContext("host", host)
|
||||||
@@ -393,7 +393,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config TransformerConfig, log
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config TransformerConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config TransformerConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
if config.reqTrans == nil {
|
if config.reqTrans == nil {
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
@@ -514,7 +514,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config TransformerConfig, body [
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config TransformerConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config TransformerConfig, log wrapper.Log) types.Action {
|
||||||
if config.respTrans == nil {
|
if config.respTrans == nil {
|
||||||
ctx.DontReadResponseBody()
|
ctx.DontReadResponseBody()
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
@@ -582,7 +582,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config TransformerConfig, lo
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseBody(ctx wrapper.HttpContext, config TransformerConfig, body []byte, log log.Log) types.Action {
|
func onHttpResponseBody(ctx wrapper.HttpContext, config TransformerConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
if config.respTrans == nil {
|
if config.respTrans == nil {
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/corazawaf/coraza/v3"
|
"github.com/corazawaf/coraza/v3"
|
||||||
"github.com/corazawaf/coraza/v3/debuglog"
|
"github.com/corazawaf/coraza/v3/debuglog"
|
||||||
@@ -32,7 +31,7 @@ type WafConfig struct {
|
|||||||
//tx ctypes.Transaction
|
//tx ctypes.Transaction
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *WafConfig, log log.Log) error {
|
func parseConfig(json gjson.Result, config *WafConfig, log wrapper.Log) error {
|
||||||
var secRules []string
|
var secRules []string
|
||||||
var value gjson.Result
|
var value gjson.Result
|
||||||
value = json.Get("useCRS")
|
value = json.Get("useCRS")
|
||||||
@@ -69,7 +68,7 @@ func parseConfig(json gjson.Result, config *WafConfig, log log.Log) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config WafConfig, log log.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config WafConfig, log wrapper.Log) types.Action {
|
||||||
ctx.SetContext("skipwaf", false)
|
ctx.SetContext("skipwaf", false)
|
||||||
|
|
||||||
if ignoreBody() {
|
if ignoreBody() {
|
||||||
@@ -149,7 +148,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config WafConfig, log log.Log
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpRequestBody(ctx wrapper.HttpContext, config WafConfig, body []byte, log log.Log) types.Action {
|
func onHttpRequestBody(ctx wrapper.HttpContext, config WafConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
if ctx.GetContext("interruptionHandled").(bool) {
|
if ctx.GetContext("interruptionHandled").(bool) {
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
@@ -201,7 +200,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config WafConfig, body []byte, l
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseHeaders(ctx wrapper.HttpContext, config WafConfig, log log.Log) types.Action {
|
func onHttpResponseHeaders(ctx wrapper.HttpContext, config WafConfig, log wrapper.Log) types.Action {
|
||||||
if ctx.GetContext("skipwaf").(bool) {
|
if ctx.GetContext("skipwaf").(bool) {
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
@@ -258,7 +257,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config WafConfig, log log.Lo
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpResponseBody(ctx wrapper.HttpContext, config WafConfig, body []byte, log log.Log) types.Action {
|
func onHttpResponseBody(ctx wrapper.HttpContext, config WafConfig, body []byte, log wrapper.Log) types.Action {
|
||||||
if ctx.GetContext("interruptionHandled").(bool) {
|
if ctx.GetContext("interruptionHandled").(bool) {
|
||||||
// At response body phase, proxy-wasm currently relies on emptying the response body as a way of
|
// At response body phase, proxy-wasm currently relies on emptying the response body as a way of
|
||||||
// interruption the response. See https://github.com/corazawaf/coraza-proxy-wasm/issues/26.
|
// interruption the response. See https://github.com/corazawaf/coraza-proxy-wasm/issues/26.
|
||||||
@@ -319,7 +318,7 @@ func onHttpResponseBody(ctx wrapper.HttpContext, config WafConfig, body []byte,
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func onHttpStreamDone(ctx wrapper.HttpContext, config WafConfig, log log.Log) {
|
func onHttpStreamDone(ctx wrapper.HttpContext, config WafConfig, log wrapper.Log) {
|
||||||
if ctx.GetContext("skipwaf").(bool) {
|
if ctx.GetContext("skipwaf").(bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
ctypes "github.com/corazawaf/coraza/v3/types"
|
ctypes "github.com/corazawaf/coraza/v3/types"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
@@ -22,7 +21,7 @@ const replaceResponseBody int = 10
|
|||||||
// retrieveAddressInfo retrieves address properties from the proxy
|
// retrieveAddressInfo retrieves address properties from the proxy
|
||||||
// Expected targets are "source" or "destination"
|
// Expected targets are "source" or "destination"
|
||||||
// Envoy ref: https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/attributes#connection-attributes
|
// Envoy ref: https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/attributes#connection-attributes
|
||||||
func retrieveAddressInfo(logger log.Log, target string) (string, int) {
|
func retrieveAddressInfo(logger wrapper.Log, target string) (string, int) {
|
||||||
var targetIP, targetPortStr string
|
var targetIP, targetPortStr string
|
||||||
var targetPort int
|
var targetPort int
|
||||||
targetAddressRaw, err := proxywasm.GetProperty([]string{target, "address"})
|
targetAddressRaw, err := proxywasm.GetProperty([]string{target, "address"})
|
||||||
@@ -69,7 +68,7 @@ func parsePort(b []byte) (int, error) {
|
|||||||
|
|
||||||
// parseServerName parses :authority pseudo-header in order to retrieve the
|
// parseServerName parses :authority pseudo-header in order to retrieve the
|
||||||
// virtual host.
|
// virtual host.
|
||||||
func parseServerName(logger log.Log, authority string) string {
|
func parseServerName(logger wrapper.Log, authority string) string {
|
||||||
host, _, err := net.SplitHostPort(authority)
|
host, _, err := net.SplitHostPort(authority)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// missing port or bad format
|
// missing port or bad format
|
||||||
@@ -79,7 +78,7 @@ func parseServerName(logger log.Log, authority string) string {
|
|||||||
return host
|
return host
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleInterruption(ctx wrapper.HttpContext, phase string, interruption *ctypes.Interruption, log log.Log) types.Action {
|
func handleInterruption(ctx wrapper.HttpContext, phase string, interruption *ctypes.Interruption, log wrapper.Log) types.Action {
|
||||||
if ctx.GetContext("interruptionHandled").(bool) {
|
if ctx.GetContext("interruptionHandled").(bool) {
|
||||||
// handleInterruption should never be called more than once
|
// handleInterruption should never be called more than once
|
||||||
panic("Interruption already handled")
|
panic("Interruption already handled")
|
||||||
@@ -106,7 +105,7 @@ func handleInterruption(ctx wrapper.HttpContext, phase string, interruption *cty
|
|||||||
// replaceResponseBodyWhenInterrupted address an interruption raised during phase 4.
|
// replaceResponseBodyWhenInterrupted address an interruption raised during phase 4.
|
||||||
// At this phase, response headers are already sent downstream, therefore an interruption
|
// At this phase, response headers are already sent downstream, therefore an interruption
|
||||||
// can not change anymore the status code, but only tweak the response body
|
// can not change anymore the status code, but only tweak the response body
|
||||||
func replaceResponseBodyWhenInterrupted(logger log.Log, bodySize int) types.Action {
|
func replaceResponseBodyWhenInterrupted(logger wrapper.Log, bodySize int) types.Action {
|
||||||
// TODO(M4tteoP): Update response body interruption logic after https://github.com/corazawaf/coraza-proxy-wasm/issues/26
|
// TODO(M4tteoP): Update response body interruption logic after https://github.com/corazawaf/coraza-proxy-wasm/issues/26
|
||||||
// Currently returns a body filled with null bytes that replaces the sensitive data potentially leaked
|
// Currently returns a body filled with null bytes that replaces the sensitive data potentially leaked
|
||||||
err := proxywasm.ReplaceHttpResponseBody(bytes.Repeat([]byte("\x00"), bodySize))
|
err := proxywasm.ReplaceHttpResponseBody(bytes.Repeat([]byte("\x00"), bodySize))
|
||||||
|
|||||||
@@ -21,13 +21,16 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/matcher"
|
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
|
||||||
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
|
||||||
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/matcher"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Log log.Log
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CustomLogKey = "custom_log"
|
CustomLogKey = "custom_log"
|
||||||
AILogKey = "ai_log"
|
AILogKey = "ai_log"
|
||||||
@@ -72,12 +75,12 @@ type HttpContext interface {
|
|||||||
SetResponseBodyBufferLimit(byteSize uint32)
|
SetResponseBodyBufferLimit(byteSize uint32)
|
||||||
}
|
}
|
||||||
|
|
||||||
type oldParseConfigFunc[PluginConfig any] func(json gjson.Result, config *PluginConfig, log log.Log) error
|
type oldParseConfigFunc[PluginConfig any] func(json gjson.Result, config *PluginConfig, log Log) error
|
||||||
type oldParseRuleConfigFunc[PluginConfig any] func(json gjson.Result, global PluginConfig, config *PluginConfig, log log.Log) error
|
type oldParseRuleConfigFunc[PluginConfig any] func(json gjson.Result, global PluginConfig, config *PluginConfig, log Log) error
|
||||||
type oldOnHttpHeadersFunc[PluginConfig any] func(context HttpContext, config PluginConfig, log log.Log) types.Action
|
type oldOnHttpHeadersFunc[PluginConfig any] func(context HttpContext, config PluginConfig, log Log) types.Action
|
||||||
type oldOnHttpBodyFunc[PluginConfig any] func(context HttpContext, config PluginConfig, body []byte, log log.Log) types.Action
|
type oldOnHttpBodyFunc[PluginConfig any] func(context HttpContext, config PluginConfig, body []byte, log Log) types.Action
|
||||||
type oldOnHttpStreamingBodyFunc[PluginConfig any] func(context HttpContext, config PluginConfig, chunk []byte, isLastChunk bool, log log.Log) []byte
|
type oldOnHttpStreamingBodyFunc[PluginConfig any] func(context HttpContext, config PluginConfig, chunk []byte, isLastChunk bool, log Log) []byte
|
||||||
type oldOnHttpStreamDoneFunc[PluginConfig any] func(context HttpContext, config PluginConfig, log log.Log)
|
type oldOnHttpStreamDoneFunc[PluginConfig any] func(context HttpContext, config PluginConfig, log Log)
|
||||||
|
|
||||||
type ParseConfigFunc[PluginConfig any] func(json gjson.Result, config *PluginConfig) error
|
type ParseConfigFunc[PluginConfig any] func(json gjson.Result, config *PluginConfig) error
|
||||||
type ParseRuleConfigFunc[PluginConfig any] func(json gjson.Result, global PluginConfig, config *PluginConfig) error
|
type ParseRuleConfigFunc[PluginConfig any] func(json gjson.Result, global PluginConfig, config *PluginConfig) error
|
||||||
@@ -89,7 +92,7 @@ type onHttpStreamDoneFunc[PluginConfig any] func(context HttpContext, config Plu
|
|||||||
type CommonVmCtx[PluginConfig any] struct {
|
type CommonVmCtx[PluginConfig any] struct {
|
||||||
types.DefaultVMContext
|
types.DefaultVMContext
|
||||||
pluginName string
|
pluginName string
|
||||||
log log.Log
|
log Log
|
||||||
hasCustomConfig bool
|
hasCustomConfig bool
|
||||||
parseConfig ParseConfigFunc[PluginConfig]
|
parseConfig ParseConfigFunc[PluginConfig]
|
||||||
parseRuleConfig ParseRuleConfigFunc[PluginConfig]
|
parseRuleConfig ParseRuleConfigFunc[PluginConfig]
|
||||||
@@ -116,7 +119,7 @@ var globalOnTickFuncs []TickFuncEntry = []TickFuncEntry{}
|
|||||||
//
|
//
|
||||||
// You should call this function in parseConfig phase, for example:
|
// You should call this function in parseConfig phase, for example:
|
||||||
//
|
//
|
||||||
// func parseConfig(json gjson.Result, config *HelloWorldConfig, log log.Log) error {
|
// func parseConfig(json gjson.Result, config *HelloWorldConfig, log wrapper.Log) error {
|
||||||
// wrapper.RegisteTickFunc(1000, func() { proxywasm.LogInfo("onTick 1s") })
|
// wrapper.RegisteTickFunc(1000, func() { proxywasm.LogInfo("onTick 1s") })
|
||||||
// wrapper.RegisteTickFunc(3000, func() { proxywasm.LogInfo("onTick 3s") })
|
// wrapper.RegisteTickFunc(3000, func() { proxywasm.LogInfo("onTick 3s") })
|
||||||
// return nil
|
// return nil
|
||||||
@@ -363,7 +366,7 @@ func ProcessStreamDone[PluginConfig any](f onHttpStreamDoneFunc[PluginConfig]) C
|
|||||||
}
|
}
|
||||||
|
|
||||||
type logOption[PluginConfig any] struct {
|
type logOption[PluginConfig any] struct {
|
||||||
logger log.Log
|
logger Log
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *logOption[PluginConfig]) Apply(ctx *CommonVmCtx[PluginConfig]) {
|
func (o *logOption[PluginConfig]) Apply(ctx *CommonVmCtx[PluginConfig]) {
|
||||||
@@ -371,7 +374,7 @@ func (o *logOption[PluginConfig]) Apply(ctx *CommonVmCtx[PluginConfig]) {
|
|||||||
ctx.log = o.logger
|
ctx.log = o.logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithLogger[PluginConfig any](logger log.Log) CtxOption[PluginConfig] {
|
func WithLogger[PluginConfig any](logger Log) CtxOption[PluginConfig] {
|
||||||
return &logOption[PluginConfig]{logger}
|
return &logOption[PluginConfig]{logger}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user