Fix log import (#1957)

This commit is contained in:
澄潭
2025-03-26 20:27:53 +08:00
committed by GitHub
parent f83e66c23b
commit ea0143829d
80 changed files with 339 additions and 402 deletions

View File

@@ -19,7 +19,6 @@ import (
"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"
)
@@ -56,7 +55,7 @@ func writeTraceAttribute(ctx wrapper.HttpContext) {
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 {
writeLog(ctx)
} else if rand.Intn(10)%3 == 2 {

View File

@@ -2,11 +2,9 @@ module github.com/alibaba/higress/plugins/wasm-go/extensions/ai-agent
go 1.19
replace github.com/alibaba/higress/plugins/wasm-go => ../..
require (
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
gopkg.in/yaml.v2 v2.4.0
)

View File

@@ -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/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
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/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 v1.0.0/go.mod h1:iiSyFbo+rAtbtGt/bsefv8GU57h9CCLYGJA74/tF5/0=
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240711023527-ba358c48772f h1:ZIiIBRvIw62gA5MJhuwp1+2wWbqL9IGElQ499rUsYYg=
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/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

View File

@@ -11,7 +11,6 @@ import (
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-agent/dashscope"
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/higress-group/proxy-wasm-go-sdk/proxywasm"
"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)
err := initAPIs(gjson, c)
@@ -55,11 +54,11 @@ func parseConfig(gjson gjson.Result, c *PluginConfig, log log.Log) error {
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
}
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)
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")
defer log.Debug("onHttpRequestBody end")
@@ -173,7 +172,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte
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")
defer log.Debug("onHttpResponseHeaders end")
@@ -201,7 +200,7 @@ func extractJson(bodyStr string) (string, error) {
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)
messages := []dashscope.Message{{Role: "user", Content: prompt}}
@@ -242,7 +241,7 @@ func jsonFormat(llmClient wrapper.HttpClient, llmInfo LLMInfo, jsonSchema map[st
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.Content = actionInput
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"}}
proxywasm.ReplaceHttpResponseHeaders(headers)
// Remove quotes from actionInput
@@ -272,7 +271,7 @@ func stream(actionInput string, rawResponse Response, log log.Log) {
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 {
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)
start := strings.Index(response, "```")
@@ -380,7 +379,7 @@ func outputParser(response string, log log.Log) (string, string) {
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)
action, actionInput := outputParser(content, log)
@@ -515,7 +514,7 @@ func toolsCall(ctx wrapper.HttpContext, llmClient wrapper.HttpClient, llmInfo LL
}
// 从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")
defer log.Debugf("onHttpResponseBody end")

View File

@@ -4,7 +4,6 @@ import (
"errors"
"strings"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -16,7 +15,7 @@ const (
type providerInitializer interface {
ValidateConfig(ProviderConfig) error
CreateProvider(ProviderConfig, log.Log) (Provider, error)
CreateProvider(ProviderConfig, wrapper.Log) (Provider, error)
}
var (
@@ -129,7 +128,7 @@ func (c *ProviderConfig) Validate() error {
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]
if !has {
return nil, errors.New("unknown provider type: " + pc.typ)

View File

@@ -3,7 +3,6 @@ package cache
import (
"errors"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
@@ -17,7 +16,7 @@ func (r *redisProviderInitializer) ValidateConfig(cf ProviderConfig) error {
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{
config: cf,
client: wrapper.NewRedisClusterClient(wrapper.FQDNCluster{
@@ -33,7 +32,7 @@ func (r *redisProviderInitializer) CreateProvider(cf ProviderConfig, log log.Log
type redisProvider struct {
config ProviderConfig
client wrapper.RedisClient
log log.Log
log wrapper.Log
}
func (rp *redisProvider) GetProviderType() string {

View File

@@ -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/embedding"
"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"
)
@@ -46,7 +46,7 @@ type PluginConfig struct {
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.vectorProviderConfig = &vector.ProviderConfig{}
c.cacheProviderConfig = &cache.ProviderConfig{}
@@ -140,7 +140,7 @@ func (c *PluginConfig) Validate() error {
return nil
}
func (c *PluginConfig) Complete(log log.Log) error {
func (c *PluginConfig) Complete(log wrapper.Log) error {
var err error
if 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
}
func convertLegacyMapFields(c *PluginConfig, json gjson.Result, log log.Log) {
func convertLegacyMapFields(c *PluginConfig, json gjson.Result, log wrapper.Log) {
keyMap := map[string]string{
"cacheKeyFrom.requestBody": "cacheKeyFrom",
"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 {
case "cacheKeyFrom":
c.CacheKeyFrom = value

View File

@@ -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/vector"
"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/tidwall/resp"
)
// 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()
if activeCacheProvider == nil {
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.
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() {
log.Infof("[%s] cache hit for key: %s", PLUGIN_NAME, key)
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.
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) == "" {
log.Warnf("[%s] [processCacheHit] cached response for key %s is empty", PLUGIN_NAME, key)
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.
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()
if activeVectorProvider == nil {
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.
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)
if !ok {
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)
})
}
// 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)
if !ok {
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)
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)
})
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.
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 {
handleInternalError(err, fmt.Sprintf("[%s] [handleQueryResults] error querying vector database for key: %s", PLUGIN_NAME, key), log)
return
@@ -187,14 +186,14 @@ func handleQueryResults(key string, results []vector.QueryResult, ctx wrapper.Ht
}
// 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)
log.Errorf(message)
return errors.New(message)
}
// 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 {
log.Errorf("[%s] [handleInternalError] %s: %v", PLUGIN_NAME, message, err)
} else {
@@ -205,7 +204,7 @@ func handleInternalError(err error, message string, log log.Log) {
}
// 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) == "" {
log.Warnf("[%s] [cacheResponse] cached value for key %s is empty", PLUGIN_NAME, key)
return
@@ -220,7 +219,7 @@ func cacheResponse(ctx wrapper.HttpContext, c config.PluginConfig, key string, v
}
// 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)
if embedding == nil {
return

View File

@@ -7,7 +7,6 @@ import (
"net/http"
"strconv"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -80,7 +79,7 @@ type CohereProvider struct {
func (t *CohereProvider) GetProviderType() string {
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
if model == "" {
@@ -119,7 +118,7 @@ func (t *CohereProvider) parseTextEmbedding(responseBody []byte) (*cohereRespons
func (t *CohereProvider) GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log log.Log,
log wrapper.Log,
callback func(emb []float64, err error)) error {
embUrl, embHeaders, embRequestBody, err := t.constructParameters([]string{queryString}, log)
if err != nil {

View File

@@ -7,7 +7,6 @@ import (
"net/http"
"strconv"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -104,7 +103,7 @@ type DSProvider struct {
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
@@ -160,7 +159,7 @@ func (d *DSProvider) parseTextEmbedding(responseBody []byte) (*Response, error)
func (d *DSProvider) GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log log.Log,
log wrapper.Log,
callback func(emb []float64, err error)) error {
embUrl, embHeaders, embRequestBody, err := d.constructParameters([]string{queryString}, log)
if err != nil {

View File

@@ -4,13 +4,11 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
"net/http"
"strconv"
"strings"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
const (
@@ -80,7 +78,7 @@ type HuggingFaceEmbeddingRequest struct {
} `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 == "" {
err := errors.New("queryString text cannot be empty")
return "", nil, nil, err
@@ -129,7 +127,7 @@ func (t *HuggingFaceProvider) parseTextEmbedding(responseBody []byte) ([]float64
func (t *HuggingFaceProvider) GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log log.Log,
log wrapper.Log,
callback func(emb []float64, err error)) error {
embUrl, embHeaders, embRequestBody, err := t.constructParameters(queryString, log)
if err != nil {

View File

@@ -4,12 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
"net/http"
"strconv"
)
const (
@@ -71,7 +69,7 @@ type ollamaEmbeddingRequest struct {
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 == "" {
err := errors.New("queryString text cannot be empty")
return "", nil, nil, err
@@ -107,7 +105,7 @@ func (t *ollamaProvider) parseTextEmbedding(responseBody []byte) (*ollamaRespons
func (t *ollamaProvider) GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log log.Log,
log wrapper.Log,
callback func(emb []float64, err error)) error {
embUrl, embHeaders, embRequestBody, err := t.constructParameters(queryString, log)
if err != nil {

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"net/http"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -94,7 +93,7 @@ type OpenAIProvider struct {
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 == "" {
err := errors.New("queryString text cannot be empty")
return "", nil, nil, err
@@ -131,7 +130,7 @@ func (t *OpenAIProvider) parseTextEmbedding(responseBody []byte) (*OpenAIRespons
func (t *OpenAIProvider) GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log log.Log,
log wrapper.Log,
callback func(emb []float64, err error)) error {
embUrl, embHeaders, embRequestBody, err := t.constructParameters(queryString, log)
if err != nil {

View File

@@ -3,7 +3,6 @@ package embedding
import (
"errors"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -109,6 +108,6 @@ type Provider interface {
GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log log.Log,
log wrapper.Log,
callback func(emb []float64, err error)) error
}

View File

@@ -7,7 +7,6 @@ import (
"net/http"
"strconv"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -98,7 +97,7 @@ type TIProvider struct {
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{
Input: texts,
@@ -143,7 +142,7 @@ func (t *TIProvider) parseTextEmbedding(responseBody []byte) (*TextInResponse, e
func (t *TIProvider) GetEmbedding(
queryString string,
ctx wrapper.HttpContext,
log log.Log,
log wrapper.Log,
callback func(emb []float64, err error)) error {
embUrl, embHeaders, embRequestBody, err := t.constructParameters([]string{queryString}, log)
if err != nil {

View File

@@ -6,7 +6,6 @@ import (
"strings"
"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/higress-group/proxy-wasm-go-sdk/proxywasm"
"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.VectorDatabaseProviderConfig.FromJson(json.Get("vectorBaseProvider"))
// config.RedisConfig.FromJson(json.Get("redis"))
@@ -55,7 +54,7 @@ func parseConfig(json gjson.Result, c *config.PluginConfig, log log.Log) error {
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)
if skipCache == "on" {
ctx.SetContext(SKIP_CACHE_HEADER, struct{}{})
@@ -79,7 +78,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, c config.PluginConfig, log lo
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)
// 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
}
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)
if skipCache != nil {
ctx.SetUserAttribute("cache_status", "skip")
@@ -151,7 +150,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, c config.PluginConfig, log l
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] chunk: %s", string(chunk))

View File

@@ -6,12 +6,11 @@ import (
"strings"
"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/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)
if tempContentI == nil {
ctx.SetContext(CACHE_CONTENT_CONTEXT_KEY, chunk)
@@ -29,7 +28,7 @@ func unifySSEChunk(data []byte) []byte {
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
partialMessageI := ctx.GetContext(PARTIAL_MESSAGE_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
}
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
tempContentI := ctx.GetContext(CACHE_CONTENT_CONTEXT_KEY)
if tempContentI != nil {
@@ -71,7 +70,7 @@ func processNonStreamLastChunk(ctx wrapper.HttpContext, c config.PluginConfig, c
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 {
var lastMessage []byte
partialMessageI := ctx.GetContext(PARTIAL_MESSAGE_CONTEXT_KEY)
@@ -97,7 +96,7 @@ func processStreamLastChunk(ctx wrapper.HttpContext, c config.PluginConfig, chun
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 := ""
for _, chunk := range strings.Split(sseMessage, "\n\n") {
log.Debugf("single sse message: %s", chunk)

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"net/http"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
@@ -45,8 +44,8 @@ func (c *ChromaProvider) GetProviderType() string {
func (d *ChromaProvider) QueryEmbedding(
emb []float64,
ctx wrapper.HttpContext,
log log.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
log wrapper.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
// 最少需要填写的参数为 collection_id, embeddings 和 ids
// 下面是一个例子
// {
@@ -97,8 +96,8 @@ func (d *ChromaProvider) UploadAnswerAndEmbedding(
queryEmb []float64,
queryAnswer string,
ctx wrapper.HttpContext,
log log.Log,
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
log wrapper.Log,
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
// 最少需要填写的参数为 collection_id, embeddings 和 ids
// 下面是一个例子
// {
@@ -178,7 +177,7 @@ type chromaQueryResponse struct {
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
err := json.Unmarshal(responseBody, &queryResp)
if err != nil {

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"net/http"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"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(
emb []float64,
ctx wrapper.HttpContext,
log log.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
log wrapper.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
url, body, headers, err := d.constructEmbeddingQueryParameters(emb)
log.Debugf("url:%s, body:%s, headers:%v", url, string(body), headers)
if err != nil {
@@ -158,7 +157,7 @@ func getStringValue(fields map[string]interface{}, key string) string {
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)
if err != nil {
return nil, err
@@ -216,7 +215,7 @@ func (d *DvProvider) constructUploadParameters(emb []float64, queryString string
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, "")
if err != nil {
return err
@@ -236,7 +235,7 @@ func (d *DvProvider) UploadEmbedding(queryString string, queryEmb []float64, ctx
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)
if err != nil {
return err

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"net/http"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
@@ -46,8 +45,8 @@ func (c *ESProvider) GetProviderType() string {
func (d *ESProvider) QueryEmbedding(
emb []float64,
ctx wrapper.HttpContext,
log log.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
log wrapper.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
requestBody, err := json.Marshal(esQueryRequest{
Source: Source{Excludes: []string{"embedding"}},
@@ -100,8 +99,8 @@ func (d *ESProvider) UploadAnswerAndEmbedding(
queryEmb []float64,
queryAnswer string,
ctx wrapper.HttpContext,
log log.Log,
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
log wrapper.Log,
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
// 最少需要填写的参数为 index, embeddings 和 question
// 下面是一个例子
// POST /<index>/_doc
@@ -177,7 +176,7 @@ type esQueryResponse struct {
} `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))
var queryResp esQueryResponse
err := json.Unmarshal(responseBody, &queryResp)

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"net/http"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -59,8 +58,8 @@ func (d *milvusProvider) UploadAnswerAndEmbedding(
queryEmb []float64,
queryAnswer string,
ctx wrapper.HttpContext,
log log.Log,
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
log wrapper.Log,
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
// 最少需要填写的参数为 collectionName, data 和 Authorization. question, answer 可选
// 需要填写 id否则 v2.4.13-hotfix 提示 invalid syntax: invalid parameter[expected=Int64][actual=]
// 如果不填写 id要在创建 collection 的时候设置 autoId 为 true
@@ -121,8 +120,8 @@ type milvusQueryRequest struct {
func (d *milvusProvider) QueryEmbedding(
emb []float64,
ctx wrapper.HttpContext,
log log.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
log wrapper.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
// 最少需要填写的参数为 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() {
log.Errorf("[Milvus] No distance found in response body: %s", responseBody)
return nil, errors.New("[Milvus] No distance found in response body")

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"net/http"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/google/uuid"
"github.com/tidwall/gjson"
@@ -68,8 +67,8 @@ func (d *pineconeProvider) UploadAnswerAndEmbedding(
queryEmb []float64,
queryAnswer string,
ctx wrapper.HttpContext,
log log.Log,
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
log wrapper.Log,
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
// 最少需要填写的参数为 vector 和 question
// 下面是一个例子
// {
@@ -123,8 +122,8 @@ type pineconeQueryRequest struct {
func (d *pineconeProvider) QueryEmbedding(
emb []float64,
ctx wrapper.HttpContext,
log log.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
log wrapper.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
// 最少需要填写的参数为 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() {
log.Errorf("[Pinecone] No distance found in response body: %s", responseBody)
return nil, errors.New("[Pinecone] No distance found in response body")

View File

@@ -3,7 +3,6 @@ package vector
import (
"errors"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -51,8 +50,8 @@ type EmbeddingQuerier interface {
QueryEmbedding(
emb []float64,
ctx wrapper.HttpContext,
log log.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error
log wrapper.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error
}
type EmbeddingUploader interface {
@@ -60,8 +59,8 @@ type EmbeddingUploader interface {
queryString string,
queryEmb []float64,
ctx wrapper.HttpContext,
log log.Log,
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error
log wrapper.Log,
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error
}
type AnswerAndEmbeddingUploader interface {
@@ -70,16 +69,16 @@ type AnswerAndEmbeddingUploader interface {
queryEmb []float64,
answer string,
ctx wrapper.HttpContext,
log log.Log,
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error
log wrapper.Log,
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error
}
type StringQuerier interface {
QueryString(
queryString string,
ctx wrapper.HttpContext,
log log.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error
log wrapper.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error
}
type ProviderConfig struct {

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"net/http"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/google/uuid"
"github.com/tidwall/gjson"
@@ -64,8 +63,8 @@ func (d *qdrantProvider) UploadAnswerAndEmbedding(
queryEmb []float64,
queryAnswer string,
ctx wrapper.HttpContext,
log log.Log,
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
log wrapper.Log,
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
// 最少需要填写的参数为 id 和 vector. payload 可选
// 下面是一个例子
// {
@@ -123,8 +122,8 @@ type qdrantQueryRequest struct {
func (d *qdrantProvider) QueryEmbedding(
emb []float64,
ctx wrapper.HttpContext,
log log.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
log wrapper.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
// 最少需要填写的参数为 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,

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"net/http"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -46,8 +45,8 @@ func (c *WeaviateProvider) GetProviderType() string {
func (d *WeaviateProvider) QueryEmbedding(
emb []float64,
ctx wrapper.HttpContext,
log log.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log log.Log, err error)) error {
log wrapper.Log,
callback func(results []QueryResult, ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
// 最少需要填写的参数为 class, vector
// 下面是一个例子
// {"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,
queryAnswer string,
ctx wrapper.HttpContext,
log log.Log,
callback func(ctx wrapper.HttpContext, log log.Log, err error)) error {
log wrapper.Log,
callback func(ctx wrapper.HttpContext, log wrapper.Log, err error)) error {
// 最少需要填写的参数为 class, vector 和 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"`
}
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))
if !gjson.GetBytes(responseBody, fmt.Sprintf("data.Get.%s.0._additional.distance", d.config.collectionID)).Exists() {

View File

@@ -11,7 +11,6 @@ import (
"strconv"
"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/types"
@@ -122,7 +121,7 @@ type ChatHistory struct {
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()
if c.RedisInfo.ServiceName == "" {
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))
}
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")
if !strings.Contains(contentType, "application/json") {
log.Warnf("content is not json, can't process:%s", contentType)
@@ -193,7 +192,7 @@ func TrimQuote(source string) string {
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)
if bodyJson.Get("stream").Bool() {
ctx.SetContext(StreamContextKey, struct{}{})
@@ -320,7 +319,7 @@ func getIntQueryParameter(name string, path string, defaultValue int) int {
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 := ""
for _, chunk := range strings.Split(sseMessage, "\n\n") {
subMessages := strings.Split(chunk, "\n")
@@ -356,14 +355,14 @@ func processSSEMessage(ctx wrapper.HttpContext, config PluginConfig, sseMessage
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")
if strings.Contains(contentType, "text/event-stream") {
ctx.SetContext(StreamContextKey, struct{}{})
}
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 {
// we should not cache tool call result
return chunk
@@ -455,7 +454,7 @@ func onHttpStreamResponseBody(ctx wrapper.HttpContext, config PluginConfig, chun
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)
identityKey := ctx.GetStringContext(IdentityKey, "")
var chat []ChatHistory

View File

@@ -12,7 +12,6 @@ import (
"strconv"
"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/types"
@@ -101,7 +100,7 @@ type KVExtractor struct {
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)
// init scene
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
}
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("end onHttpRequestHeaders function.")
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.")
bodyJson := gjson.ParseBytes(body)
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
}
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("end onHttpResponseHeaders function.")
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("end onStreamingResponseBody function.")
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("end onHttpResponseBody function.")
@@ -291,7 +290,7 @@ type ProxyRequestMessage struct {
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
var userMessage ProxyRequestMessage
userMessage.Role = "user"
@@ -339,7 +338,7 @@ type ProxyResponseOutputChoicesMessage struct {
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
err := json.Unmarshal(responseBody, &response)
if err != nil {
@@ -349,7 +348,7 @@ func proxyResponseHandler(responseBody []byte, log log.Log) (*ProxyResponse, err
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)
responseContent := strings.Trim(bodyJson.Get(c.KeyFrom.ResponseBody).Raw, `"`)
// llm返回的结果

View File

@@ -26,7 +26,6 @@ import (
"github.com/santhosh-tekuri/jsonschema"
"github.com/tidwall/gjson"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
@@ -142,7 +141,7 @@ func parseUrl(url string) (string, string) {
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.serviceUrl = result.Get("serviceUrl").String()
config.serviceDomain = result.Get("serviceDomain").String()
@@ -279,7 +278,7 @@ func (r *RequestContext) assembleReqBody(config PluginConfig) []byte {
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
lastUserMessage := ""
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{
Role: "system",
Content: errMsg,
@@ -341,7 +340,7 @@ func (c *PluginConfig) ValidateBody(body []byte) error {
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()
// first extract json from response body
if content == "" {
@@ -400,7 +399,7 @@ func (c *PluginConfig) ExtractJson(bodyStr string) (string, error) {
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))
header := [][2]string{
{"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 retryCount >= config.maxRetry {
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))
}
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 {
sendResponse(ctx, config, log, nil)
return types.ActionPause
@@ -506,7 +505,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.
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
fromThisPlugin, ok := ctx.GetContext(FROM_THIS_PLUGIN_KEY).(bool)
if ok && fromThisPlugin {

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"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/types"
@@ -32,11 +31,11 @@ type AIPromptDecoratorConfig struct {
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)
}
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")
return types.ActionContinue
}
@@ -67,7 +66,7 @@ func decorateGeographicPrompt(entry *Message) (*Message, error) {
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":[]}`
for _, entry := range config.Prepend {

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"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/types"
@@ -24,7 +23,7 @@ type AIPromptTemplateConfig struct {
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)
for _, v := range json.Get("templates").Array() {
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
}
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")
if templateEnable == "false" {
ctx.DontReadRequestBody()
@@ -43,7 +42,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIPromptTemplateConfig
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() {
name := gjson.GetBytes(body, "template").String()
template := config.templates[name]

View File

@@ -11,7 +11,6 @@ import (
"strings"
"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/higress-group/proxy-wasm-go-sdk/proxywasm"
"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"`
}
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()")
// admin
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))
}
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()")
// get tokens
consumer, err := proxywasm.GetHttpRequestHeader("x-mse-consumer")
@@ -184,7 +183,7 @@ func onHttpRequestHeaders(context wrapper.HttpContext, config QuotaConfig, log l
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()")
chatMode, ok := ctx.GetContext("chatMode").(ChatMode)
if !ok {
@@ -212,7 +211,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config QuotaConfig, body []byte,
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)
if !ok {
return data
@@ -275,7 +274,7 @@ func deniedUnauthorizedConsumer() types.Action {
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
if strings.HasSuffix(path, fullAdminPath+"/refresh") {
return ChatModeAdmin, AdminModeRefresh
@@ -292,7 +291,7 @@ func getOperationMode(path string, adminPath string, log log.Log) (ChatMode, Adm
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
if adminConsumer != config.AdminConsumer {
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
}
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
if adminConsumer != config.AdminConsumer {
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
}
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
if adminConsumer != config.AdminConsumer {
util.SendResponse(http.StatusForbidden, "ai-quota.unauthorized", "text/plain", "Request denied by ai quota check. Unauthorized admin consumer.")

View File

@@ -9,7 +9,6 @@ import (
"ai-rag/dashscope"
"ai-rag/dashvector"
"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"
@@ -52,7 +51,7 @@ type Message struct {
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{
"dashscope.apiKey",
"dashscope.serviceFQDN",
@@ -92,12 +91,12 @@ func parseConfig(json gjson.Result, config *AIRagConfig, log log.Log) error {
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")
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
_ = json.Unmarshal(body, &rawRequest)
messageLength := len(rawRequest.Messages)
@@ -166,7 +165,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config AIRagConfig, body []byte,
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)
if ok && recall {
proxywasm.AddHttpResponseHeader("x-envoy-rag-recall", "true")

View File

@@ -24,18 +24,19 @@ import (
"time"
"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/arxiv"
"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/google"
"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 {
@@ -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
if json.Get("defaultEnable").Exists() {
config.defaultEnable = json.Get("defaultEnable").Bool()
@@ -275,7 +276,7 @@ func parseConfig(json gjson.Result, config *Config, log log.Log) error {
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")
// The request does not have a body.
if contentType == "" {
@@ -291,7 +292,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log log.Log) t
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
webSearchOptions := gjson.GetBytes(body, "web_search_options")
if !config.defaultEnable {
@@ -450,7 +451,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config Config, body []byte, 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))
var finished int
var searching int
@@ -553,7 +554,7 @@ func executeSearch(ctx wrapper.HttpContext, config Config, queryIndex int, body
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 {
ctx.DontReadResponseBody()
return types.ActionContinue
@@ -570,7 +571,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config Config, log log.Log)
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", "")
if references == "" {
return types.ActionContinue
@@ -624,7 +625,7 @@ const (
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) {
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)
subMessages := strings.Split(sseMessage, "\n")
var message string

View File

@@ -17,7 +17,6 @@ import (
"strings"
"time"
"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"
@@ -173,7 +172,7 @@ func generateHexID(length int) (string, error) {
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()
servicePort := json.Get("servicePort").Int()
serviceHost := json.Get("serviceHost").String()
@@ -251,7 +250,7 @@ func generateRandomID() string {
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 {
log.Debugf("request checking is disabled")
ctx.DontReadRequestBody()
@@ -259,7 +258,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config AISecurityConfig, log
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...")
startTime := time.Now().UnixMilli()
content := gjson.GetBytes(body, config.requestContentJsonPath).String()
@@ -368,7 +367,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config AISecurityConfig, body []
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 {
log.Debugf("response checking is disabled")
ctx.DontReadResponseBody()
@@ -383,7 +382,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config AISecurityConfig, log
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...")
startTime := time.Now().UnixMilli()
contentType, _ := proxywasm.GetHttpResponseHeader("content-type")
@@ -508,7 +507,7 @@ func extractMessageFromStreamingBody(data []byte, jsonPath string) string {
return strings.Join(strChunks, "")
}
func marshalStr(raw string, log log.Log) string {
func marshalStr(raw string, log wrapper.Log) string {
helper := map[string]string{
"placeholder": raw,
}

View File

@@ -8,7 +8,6 @@ import (
"strings"
"time"
"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"
@@ -129,7 +128,7 @@ func (config *AIStatisticsConfig) incrementCounter(metricName string, inc uint64
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.
attributeConfigs := configJson.Get("attributes").Array()
config.attributes = make([]Attribute, len(attributeConfigs))
@@ -153,7 +152,7 @@ func parseConfig(configJson gjson.Result, config *AIStatisticsConfig, log log.Lo
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()
cluster, _ := getClusterName()
api, api_error := getAPIName()
@@ -177,7 +176,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIStatisticsConfig, lo
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.
setAttributeBySource(ctx, config, RequestBody, body, log)
@@ -186,7 +185,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config AIStatisticsConfig, body
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")
if !strings.Contains(contentType, "text/event-stream") {
ctx.BufferResponseBody()
@@ -198,7 +197,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config AIStatisticsConfig, l
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
if config.shouldBufferStreamingBody {
var streamingBodyBuffer []byte
@@ -256,7 +255,7 @@ func onHttpStreamingBody(ctx wrapper.HttpContext, config AIStatisticsConfig, dat
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
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.
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 {
var key string
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"))
var value interface{}
if rule == RuleFirst {
@@ -388,7 +387,7 @@ func extractStreamingBodyByJsonPath(data []byte, jsonPath string, rule string, l
}
// 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 != "" {
traceSpanTag := wrapper.TraceSpanTagPrefix + key
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
var ok bool
var route, cluster, model string

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"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/tidwall/gjson"
@@ -85,7 +84,7 @@ type LimitConfigItem struct {
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")
if !redisConfig.Exists() {
return errors.New("missing redis in config")

View File

@@ -22,7 +22,6 @@ import (
"strconv"
"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/types"
@@ -82,7 +81,7 @@ type LimitRedisContext struct {
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)
if err != nil {
return err
@@ -96,7 +95,7 @@ func parseConfig(json gjson.Result, config *ClusterKeyRateLimitConfig, log log.L
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)
if ruleItem == nil || configItem == nil {
@@ -144,7 +143,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config ClusterKeyRateLimitCon
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
if inputToken, outputToken, ok := getUsage(data); ok {
ctx.SetContext("input_token", inputToken)
@@ -190,7 +189,7 @@ func getUsage(data []byte) (inputTokenUsage int64, outputTokenUsage int64, ok bo
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 {
val, ruleItem, configItem := hitRateRuleItem(ctx, rule, log)
if ruleItem != nil && configItem != nil {
@@ -200,7 +199,7 @@ func checkRequestAgainstLimitRule(ctx wrapper.HttpContext, ruleItems []LimitRule
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 {
// 根据HTTP请求头限流
case limitByHeaderType, limitByPerHeaderType:
@@ -259,7 +258,7 @@ func hitRateRuleItem(ctx wrapper.HttpContext, rule LimitRuleItem, log log.Log) (
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...)
return "", nil, nil
}

View File

@@ -5,7 +5,6 @@ import (
"net/http"
"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/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.requestTransformPrompt = json.Get("request.prompt").String()
config.responseTransformEnable = json.Get("response.enable").Bool()
@@ -90,7 +89,7 @@ func extraceHttpFrame(frame string) ([][2]string, []byte, error) {
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")
if !config.requestTransformEnable || config.requestTransformPrompt == "" {
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")
headers, err := proxywasm.GetHttpRequestHeaders()
if err != nil {
@@ -134,7 +133,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config AITransformerConfig, body
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 == "" {
ctx.DontReadResponseBody()
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()
if err != nil {
log.Error("Failed to get http response headers.")

View File

@@ -24,7 +24,6 @@ import (
"api-workflow/utils"
. "api-workflow/workflow"
"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"
@@ -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)
nodes := make(map[string]Node)
@@ -175,7 +174,7 @@ func initWorkflowExecStatus(config *PluginConfig) (map[string]int, error) {
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)
// 初始化运行状态
@@ -200,7 +199,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config PluginConfig, body []byte
}
// 放入符合条件的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
// 防止递归次数太多

View File

@@ -22,7 +22,6 @@ import (
"net/http"
"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"
@@ -107,7 +106,7 @@ var (
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")
ruleSet = false
global.credential2Name = make(map[string]string)

View File

@@ -19,7 +19,6 @@ package main
import (
"bot-detect/config"
"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"
@@ -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()")
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()")
//// Get user-agent header
ua, err := proxywasm.GetHttpRequestHeader("user-agent")

View File

@@ -1,16 +1,14 @@
package main
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/higress-group/proxy-wasm-go-sdk/proxywasm"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
"github.com/tidwall/gjson"
"net/http"
"strconv"
"strings"
"time"
)
func main() {
@@ -27,7 +25,7 @@ type CacheControlConfig struct {
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()
if suffix != "" {
parts := strings.Split(suffix, "|")
@@ -40,7 +38,7 @@ func parseConfig(json gjson.Result, config *CacheControlConfig, log log.Log) err
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()
if strings.Contains(path, "?") {
path = strings.Split(path, "?")[0]
@@ -51,7 +49,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config CacheControlConfig, lo
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
if len(config.suffix) == 0 {
hit = true

View File

@@ -7,7 +7,6 @@ import (
"net/url"
"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/types"
@@ -32,7 +31,7 @@ type MyConfig struct {
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()
var chatgptHost string
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)
if len(pairs) < 2 {

View File

@@ -21,7 +21,6 @@ import (
"strconv"
"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/types"
@@ -65,7 +64,7 @@ type LimitContext struct {
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)
if err != nil {
return err
@@ -77,7 +76,7 @@ func parseConfig(json gjson.Result, config *ClusterKeyRateLimitConfig, log log.L
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)
if ruleItem == nil || configItem == nil {
@@ -116,7 +115,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config ClusterKeyRateLimitCon
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)
if !ok {
return types.ActionContinue
@@ -128,7 +127,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config ClusterKeyRateLimitCo
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 {
val, ruleItem, configItem := hitRateRuleItem(ctx, rule, log)
if ruleItem != nil && configItem != nil {
@@ -138,7 +137,7 @@ func checkRequestAgainstLimitRule(ctx wrapper.HttpContext, ruleItems []LimitRule
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 {
// 根据HTTP请求头限流
case limitByHeaderType, limitByPerHeaderType:
@@ -197,7 +196,7 @@ func hitRateRuleItem(ctx wrapper.HttpContext, rule LimitRuleItem, log log.Log) (
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...)
return "", nil, nil
}

View File

@@ -19,7 +19,6 @@ import (
"fmt"
"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/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()")
allowOrigins := json.Get("allow_origins").Array()
for _, origin := range allowOrigins {
@@ -72,7 +71,7 @@ func parseConfig(json gjson.Result, corsConfig *config.CorsConfig, log log.Log)
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()")
requestUrl, _ := proxywasm.GetHttpRequestHeader(":path")
method, _ := proxywasm.GetHttpRequestHeader(":method")
@@ -110,7 +109,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, corsConfig config.CorsConfig,
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()")
// Remove trace header if existed
proxywasm.RemoveHttpResponseHeader(config.HeaderPluginTrace)

View File

@@ -20,7 +20,6 @@ import (
"strconv"
"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/types"
@@ -44,7 +43,7 @@ type CustomResponseConfig struct {
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()
config.headers = make([][2]string, 0, len(headersArray))
for _, v := range headersArray {
@@ -97,7 +96,7 @@ func parseConfig(gjson gjson.Result, config *CustomResponseConfig, log log.Log)
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 {
return types.ActionContinue
}
@@ -109,7 +108,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config CustomResponseConfig,
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.
// if match the status code, mock the response.
statusCodeStr, err := proxywasm.GetHttpResponseHeader(":status")

View File

@@ -20,7 +20,6 @@ import (
"de-graphql/config"
"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"
@@ -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()")
gql := json.Get("gql").String()
endpoint := json.Get("endpoint").String()
@@ -58,7 +57,7 @@ func parseConfig(json gjson.Result, config *config.DeGraphQLConfig, log log.Log)
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.Debugf("schema:%s host:%s path:%s", ctx.Scheme(), ctx.Host(), ctx.Path())
requestUrl, _ := proxywasm.GetHttpRequestHeader(":path")
@@ -103,17 +102,17 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config config.DeGraphQLConfig
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()")
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()")
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()")
return types.ActionContinue
}

View File

@@ -7,8 +7,6 @@ import (
"strings"
"ext-auth/expr"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -58,7 +56,7 @@ type AuthorizationResponse struct {
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")
if !httpServiceConfig.Exists() {
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
}
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
if err := parseEndpointConfig(json, &httpService, log); err != nil {
@@ -116,7 +114,7 @@ func parseHttpServiceConfig(json gjson.Result, config *ExtAuthConfig, log log.Lo
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()
if endpointMode == "" {
endpointMode = EndpointModeEnvoy

View File

@@ -4,7 +4,6 @@ import (
"testing"
"ext-auth/expr"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"

View File

@@ -21,7 +21,6 @@ import (
"ext-auth/config"
"ext-auth/util"
"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"
@@ -51,7 +50,7 @@ const (
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 config.MatchRules.IsAllowedByMode(ctx.Host(), ctx.Method(), wrapper.GetRequestPathWithoutQuery()) {
ctx.DontReadRequestBody()
@@ -74,14 +73,14 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config config.ExtAuthConfig,
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 {
return checkExtAuth(ctx, config, body, log, types.DataStopIterationAndBuffer)
}
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
extAuthReqHeaders := buildExtAuthRequestHeaders(ctx, cfg)

View File

@@ -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/util"
"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"
@@ -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
config.JsonToGrayConfig(json, grayConfig)
log.Infof("Rewrite: %v, GrayDeployments: %v", json.Get("rewrite"), json.Get("grayDeployments"))
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 = path.Clean(requestPath)
parsedURL, err := url.Parse(requestPath)
@@ -130,7 +129,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
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)
if !enabledGray {
ctx.DontReadResponseBody()
@@ -214,7 +213,7 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
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)
if !enabledGray {
return types.ActionContinue

View File

@@ -2,11 +2,9 @@ module higress/plugins/wasm-go/extensions/geo-ip
go 1.19
replace github.com/alibaba/higress/plugins/wasm-go => ../..
require (
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/tidwall/gjson v1.17.3
github.com/zmap/go-iptree v0.0.0-20210731043055-d4e632617837

View File

@@ -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/go.mod h1:8BhOLuqtSuT5NZtZMwfvEibi09RO3u79uqfHZzfDTR4=
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/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/proxy-wasm-go-sdk v1.0.0 h1:BZRNf4R7jr9hwRivg/E29nkVaKEak5MWjBDhWjuHijU=
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 h1:ZIiIBRvIw62gA5MJhuwp1+2wWbqL9IGElQ499rUsYYg=
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/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

View File

@@ -8,7 +8,6 @@ import (
_ "embed"
"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"
@@ -57,7 +56,7 @@ type GeoIpData struct {
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")
if sourceType.Exists() && sourceType.String() != "" {
switch sourceType.String() {
@@ -105,7 +104,7 @@ func parseConfig(json gjson.Result, config *GeoIpConfig, log log.Log) error {
return nil
}
func ReadGeoIpDataToRdxtree(log log.Log) error {
func ReadGeoIpDataToRdxtree(log wrapper.Log) error {
GeoIpRdxTree = iptree.New()
//eg., cidr country province city isp
@@ -142,7 +141,7 @@ func ReadGeoIpDataToRdxtree(log log.Log) error {
}
// 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)
if err != nil {
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
}
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 (
s string
err error

View File

@@ -3,7 +3,6 @@ package main
import (
"errors"
"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"
@@ -24,7 +23,7 @@ type MyConfig struct {
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.rules = json.Get("rules").Array()
for _, item := range config.rules {

View File

@@ -17,10 +17,10 @@ package main
import (
"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/types"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
func main() {
@@ -33,7 +33,7 @@ func main() {
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")
if err != nil {
log.Critical("failed to set request header")

View File

@@ -19,11 +19,11 @@ import (
"net/http"
"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/types"
"github.com/tidwall/gjson"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
func main() {
@@ -41,7 +41,7 @@ type HttpCallConfig struct {
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()
if config.bodyHeader == "" {
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,
func(statusCode int, responseHeaders http.Header, responseBody []byte) {
defer proxywasm.ResumeHttpRequest()

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"net"
"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"
@@ -39,7 +38,7 @@ func main() {
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")
if sourceType.Exists() && sourceType.String() != "" {
switch sourceType.String() {
@@ -118,7 +117,7 @@ func getDownStreamIp(config RestrictionConfig) (net.IP, error) {
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)
if err != nil {
return deniedUnauthorized(config, "get_ip_failed")

View File

@@ -18,7 +18,7 @@ import (
"encoding/json"
"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/tidwall/gjson"
)
@@ -28,7 +28,7 @@ var RuleSet bool
// ParseGlobalConfig 从wrapper提供的配置中解析并转换到插件运行时需要使用的配置。
// 此处解析的是全局配置,域名和路由级配置由 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
consumers := json.Get("consumers")
if !consumers.IsArray() {
@@ -53,7 +53,7 @@ func ParseGlobalConfig(json gjson.Result, config *JWTAuthConfig, log log.Log) er
// ParseRuleConfig 从wrapper提供的配置中解析并转换到插件运行时需要使用的配置。
// 此处解析的是域名和路由级配置,全局配置由 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
*config = global

View File

@@ -18,7 +18,6 @@ import (
"time"
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/higress-group/proxy-wasm-go-sdk/proxywasm/types"
)
@@ -37,7 +36,7 @@ import (
// - 若有至少一个 domain/route 配置该插件:则遵循 (2*)
//
// 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 (
noAllow = len(config.Allow) == 0 // 未配置 allow 列表,表示插件在该 domain/route 未生效
globalAuthNoSet = config.GlobalAuthCheck() == cfg.GlobalAuthNoSet

View File

@@ -20,7 +20,6 @@ import (
"net/http"
"net/url"
"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"
@@ -128,7 +127,7 @@ type KeyAuthConfig struct {
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")
// init
@@ -201,7 +200,7 @@ func parseGlobalConfig(json gjson.Result, global *KeyAuthConfig, log log.Log) er
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")
*config = global
@@ -234,7 +233,7 @@ func parseOverrideRuleConfig(json gjson.Result, global KeyAuthConfig, config *Ke
// - global_auth 未设置:
// - 若没有一个 domain/route 配置该插件:则遵循 (1*)
// - 若有至少一个 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 (
noAllow = len(config.allow) == 0 // 未配置 allow 列表,表示插件在该 domain/route 未生效
globalAuthNoSet = config.globalAuth == nil

View File

@@ -6,11 +6,11 @@ import (
"net/url"
"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"
"github.com/higress-group/oauth2-proxy/pkg/apis/options"
"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/types"
"github.com/tidwall/gjson"
@@ -35,7 +35,7 @@ type PluginConfig struct {
}
// 在控制台插件配置中填写的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)
opts, err := oidc.LoadOptions(json)
if err != nil {
@@ -55,7 +55,7 @@ func parseConfig(json gjson.Result, config *PluginConfig, log log.Log) error {
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)
req := getHttpRequest()
rw := util.NewRecorder()
@@ -77,7 +77,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config PluginConfig, log log.
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)
if value != nil {
proxywasm.AddHttpResponseHeader(oidc.SetCookieHeader, value.(string))

View File

@@ -17,7 +17,7 @@ package main
import (
"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/tidwall/gjson"
)
@@ -25,7 +25,7 @@ import (
func TestConfig(t *testing.T) {
json := gjson.Result{Type: gjson.JSON, Raw: `{"serviceSource": "k8s","serviceName": "opa","servicePort": 8181,"namespace": "example1","policy": "example1","timeout": "5s"}`}
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.timeout, uint32(5000))
assert.NotNil(t, config.client)
@@ -45,6 +45,6 @@ func TestConfig(t *testing.T) {
for _, test := range tests {
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)
}
}

View File

@@ -23,7 +23,6 @@ import (
"strings"
"time"
"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"
@@ -43,7 +42,7 @@ type Metadata struct {
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()
if strings.TrimSpace(policy) == "" {
return errors.New("policy not allow empty")
@@ -77,15 +76,15 @@ func parseConfig(json gjson.Result, config *OpaConfig, log log.Log) error {
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)
}
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)
}
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)
headers, _ := proxywasm.GetHttpRequestHeaders()

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"strings"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
)
@@ -26,7 +25,7 @@ type RedisConfig struct {
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
redisConfig := json.Get("redis")
if !redisConfig.Exists() {

View File

@@ -3,14 +3,12 @@ package main
import (
"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/higress-group/proxy-wasm-go-sdk/proxywasm"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
"github.com/tidwall/resp"
"replay-protection/config"
"replay-protection/util"
)
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)
if cfg.ForceNonce && nonce == "" {
// In force mode, reject the request if a required header is missing.

View File

@@ -19,12 +19,12 @@ import (
"fmt"
"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/types"
"github.com/tidwall/gjson"
regexp "github.com/wasilibs/go-re2"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
func main() {
@@ -47,7 +47,7 @@ type RequestBlockConfig struct {
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()
if code != 0 && code > 100 && code < 600 {
config.blockedCode = uint32(code)
@@ -120,7 +120,7 @@ func parseConfig(json gjson.Result, config *RequestBlockConfig, log log.Log) err
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 {
requestUrl, err := proxywasm.GetHttpRequestHeader(":path")
if err != nil {
@@ -176,7 +176,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config RequestBlockConfig, lo
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))
bodyStr := string(body)

View File

@@ -19,7 +19,6 @@ import (
"fmt"
"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/types"
@@ -58,7 +57,7 @@ type Config struct {
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()
bodySchema := result.Get("body_schema").String()
enableSwagger := result.Get("enable_swagger").Bool()
@@ -120,7 +119,7 @@ func parseConfig(result gjson.Result, config *Config, log log.Log) error {
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 {
return types.ActionContinue
}
@@ -162,7 +161,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log log.Log) t
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 {
return types.ActionContinue
}

View File

@@ -4,7 +4,6 @@ import (
"encoding/json"
"net/http"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
jwt "github.com/dgrijalva/jwt-go"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
@@ -32,14 +31,14 @@ type Res struct {
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.TokenSecretKey = json.Get("token_secret_key").String()
config.TokenHeaders = json.Get("token_headers").String()
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
if config.TokenHeaders == "" || config.TokenSecretKey == "" {
res.Code = http.StatusBadRequest

View File

@@ -4,7 +4,6 @@ import (
"net/http"
"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/types"
@@ -20,7 +19,7 @@ func main() {
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
protocol, err := proxywasm.GetProperty([]string{"request", "protocol"})
if err != nil {

View File

@@ -18,7 +18,6 @@ import (
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
"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"
)
@@ -35,22 +34,22 @@ func main() {
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")
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)
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")
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)
return []byte("test\n")
}

View File

@@ -21,11 +21,11 @@ import (
"strconv"
"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"
)
func onContentRequestHeaders(conditionGroups []ConditionGroup, log log.Log) bool {
func onContentRequestHeaders(conditionGroups []ConditionGroup, log wrapper.Log) bool {
for _, cg := range conditionGroups {
if matchCondition(&cg, 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
func matchCondition(conditionGroup *ConditionGroup, log log.Log) bool {
func matchCondition(conditionGroup *ConditionGroup, log wrapper.Log) bool {
for _, condition := range conditionGroup.Conditions {
conditionKeyValue, err := getConditionValue(condition, log)
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
}
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)
switch condition.ConditionType {

View File

@@ -18,7 +18,6 @@ import (
"math/rand"
"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/types"
"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)
if jsonStr == "{}" || jsonStr == "" {

View File

@@ -23,13 +23,13 @@ import (
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
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"
)
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
config.ConditionGroups = []ConditionGroup{}
@@ -79,7 +79,7 @@ func parseContentConfig(json gjson.Result, config *TrafficTagConfig, log log.Log
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 accumulatedWeight int64
config.WeightGroups = []WeightGroup{}

View File

@@ -19,11 +19,11 @@ import (
"net/url"
"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"
)
func setDefaultTag(k string, v string, log log.Log) {
func setDefaultTag(k string, v string, log wrapper.Log) {
if k == "" || v == "" {
return
}
@@ -61,7 +61,7 @@ func getQueryParameter(urlStr, paramKey string) (string, error) {
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)
if existValue != "" {
log.Infof("ADD HEADER failed: %s already exists, value: %s", key, existValue)

View File

@@ -15,10 +15,10 @@
package main
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
log.Debugf("random value for weighted headers : %d", randomValue)
// CDF

View File

@@ -17,14 +17,14 @@ package main
import (
"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/types"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
regexp "github.com/wasilibs/go-re2"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
)
func main() {
@@ -214,7 +214,7 @@ type Param struct {
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")
respRulesInJson := json.Get("respRules")
@@ -284,7 +284,7 @@ func constructParam(item gjson.Result, op, valueType string) Param {
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
host, path := ctx.Host(), ctx.Path()
ctx.SetContext("host", host)
@@ -393,7 +393,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config TransformerConfig, log
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 {
return types.ActionContinue
}
@@ -514,7 +514,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config TransformerConfig, body [
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 {
ctx.DontReadResponseBody()
return types.ActionContinue
@@ -582,7 +582,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config TransformerConfig, lo
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 {
return types.ActionContinue
}

View File

@@ -5,7 +5,6 @@ import (
"strconv"
"strings"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/corazawaf/coraza/v3"
"github.com/corazawaf/coraza/v3/debuglog"
@@ -32,7 +31,7 @@ type WafConfig struct {
//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 value gjson.Result
value = json.Get("useCRS")
@@ -69,7 +68,7 @@ func parseConfig(json gjson.Result, config *WafConfig, log log.Log) error {
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)
if ignoreBody() {
@@ -149,7 +148,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config WafConfig, log log.Log
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) {
return types.ActionContinue
}
@@ -201,7 +200,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config WafConfig, body []byte, l
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) {
return types.ActionContinue
}
@@ -258,7 +257,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, config WafConfig, log log.Lo
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) {
// 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.
@@ -319,7 +318,7 @@ func onHttpResponseBody(ctx wrapper.HttpContext, config WafConfig, body []byte,
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) {
return
}

View File

@@ -9,7 +9,6 @@ import (
"net"
"strconv"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
ctypes "github.com/corazawaf/coraza/v3/types"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
@@ -22,7 +21,7 @@ const replaceResponseBody int = 10
// retrieveAddressInfo retrieves address properties from the proxy
// Expected targets are "source" or "destination"
// 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 targetPort int
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
// virtual host.
func parseServerName(logger log.Log, authority string) string {
func parseServerName(logger wrapper.Log, authority string) string {
host, _, err := net.SplitHostPort(authority)
if err != nil {
// missing port or bad format
@@ -79,7 +78,7 @@ func parseServerName(logger log.Log, authority string) string {
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) {
// handleInterruption should never be called more than once
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.
// 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
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
// Currently returns a body filled with null bytes that replaces the sensitive data potentially leaked
err := proxywasm.ReplaceHttpResponseBody(bytes.Repeat([]byte("\x00"), bodySize))

View File

@@ -21,13 +21,16 @@ import (
"time"
"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/types"
"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 (
CustomLogKey = "custom_log"
AILogKey = "ai_log"
@@ -72,12 +75,12 @@ type HttpContext interface {
SetResponseBodyBufferLimit(byteSize uint32)
}
type oldParseConfigFunc[PluginConfig any] func(json gjson.Result, config *PluginConfig, log log.Log) error
type oldParseRuleConfigFunc[PluginConfig any] func(json gjson.Result, global PluginConfig, config *PluginConfig, log log.Log) error
type oldOnHttpHeadersFunc[PluginConfig any] func(context HttpContext, config PluginConfig, log log.Log) types.Action
type oldOnHttpBodyFunc[PluginConfig any] func(context HttpContext, config PluginConfig, body []byte, log log.Log) types.Action
type oldOnHttpStreamingBodyFunc[PluginConfig any] func(context HttpContext, config PluginConfig, chunk []byte, isLastChunk bool, log log.Log) []byte
type oldOnHttpStreamDoneFunc[PluginConfig any] func(context HttpContext, config PluginConfig, log log.Log)
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) error
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) types.Action
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)
type ParseConfigFunc[PluginConfig any] func(json gjson.Result, 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 {
types.DefaultVMContext
pluginName string
log log.Log
log Log
hasCustomConfig bool
parseConfig ParseConfigFunc[PluginConfig]
parseRuleConfig ParseRuleConfigFunc[PluginConfig]
@@ -116,7 +119,7 @@ var globalOnTickFuncs []TickFuncEntry = []TickFuncEntry{}
//
// 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(3000, func() { proxywasm.LogInfo("onTick 3s") })
// return nil
@@ -363,7 +366,7 @@ func ProcessStreamDone[PluginConfig any](f onHttpStreamDoneFunc[PluginConfig]) C
}
type logOption[PluginConfig any] struct {
logger log.Log
logger Log
}
func (o *logOption[PluginConfig]) Apply(ctx *CommonVmCtx[PluginConfig]) {
@@ -371,7 +374,7 @@ func (o *logOption[PluginConfig]) Apply(ctx *CommonVmCtx[PluginConfig]) {
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}
}