Skip to content

Commit

Permalink
新增请求拦截插件,额外参数插件新增hmac-sha256算法
Browse files Browse the repository at this point in the history
  • Loading branch information
Dot-Liu committed Oct 20, 2023
1 parent baef2a0 commit 423b077
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/apinto/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/eolinker/apinto/drivers/plugins/gzip"
params_check "github.com/eolinker/apinto/drivers/plugins/params-check"
"github.com/eolinker/apinto/drivers/plugins/prometheus"
request_interception "github.com/eolinker/apinto/drivers/plugins/request-interception"
response_rewrite_v2 "github.com/eolinker/apinto/drivers/plugins/response-rewrite_v2"

access_log "github.com/eolinker/apinto/drivers/plugins/access-log"
Expand Down Expand Up @@ -68,6 +69,7 @@ func pluginRegister(extenderRegister eosc.IExtenderDriverRegister) {
http_mocking.Register(extenderRegister)
params_check.Register(extenderRegister)
data_transform.Register(extenderRegister)
request_interception.Register(extenderRegister)

// 响应处理插件
response_rewrite.Register(extenderRegister)
Expand Down
2 changes: 1 addition & 1 deletion drivers/plugins/extra-params_v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *Config) doCheck() error {

type ExtraParam struct {
Name string `json:"name" label:"参数名"`
Type string `json:"type" label:"参数类型" enum:"string,int,float,bool,$datetime,$md5,$timestamp,$concat"`
Type string `json:"type" label:"参数类型" enum:"string,int,float,bool,$datetime,$md5,$timestamp,$concat,$hmac-sha256"`
Position string `json:"position" enum:"header,query,body" label:"参数位置"`
Value []string `json:"value" label:"参数值列表"`
Conflict string `json:"conflict" label:"参数冲突时的处理方式" enum:"origin,convert,error"`
Expand Down
3 changes: 3 additions & 0 deletions drivers/plugins/extra-params_v2/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
"sync"

hmac_sha256 "github.com/eolinker/apinto/drivers/plugins/extra-params_v2/dynamic-params/hmac-sha256"

"github.com/eolinker/apinto/drivers/plugins/extra-params_v2/dynamic-params/concat"

"github.com/eolinker/apinto/drivers/plugins/extra-params_v2/dynamic-params/datetime"
Expand Down Expand Up @@ -52,6 +54,7 @@ func Create(id, name string, conf *Config, workers map[eosc.RequireId]eosc.IWork
md5.Register()
timestamp.Register()
concat.Register()
hmac_sha256.Register()
})
ep := &executor{
WorkerBase: drivers.Worker(id, name),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package hmac_sha256

import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"strings"

"github.com/eolinker/eosc/log"

dynamic_params "github.com/eolinker/apinto/drivers/plugins/extra-params_v2/dynamic-params"

http_service "github.com/eolinker/eosc/eocontext/http-context"
)

type executor struct {
*dynamic_params.Param
secretKey string
}

func NewExecutor(name string, value []string) *executor {
secretKey := ""
v := value
if len(value) >= 1 {
secretKey = value[0]
v = v[1:]
}
return &executor{
Param: dynamic_params.NewParam(name, v),
secretKey: secretKey,
}
}

func (m *executor) Generate(ctx http_service.IHttpContext, contentType string, args ...interface{}) (interface{}, error) {
result, err := m.Param.Generate(ctx, contentType, args...)
if err != nil {
return nil, err
}
v, ok := result.(string)
if !ok {
return nil, errors.New("hmac-sha256 value is not string")
}
value := hMacBySHA256(m.secretKey, v)
if !strings.HasPrefix(m.Name(), "__") {
value = strings.ToUpper(value)
}
log.DebugF("hmac-sha256 value before: %s,after: %s", v, value)
return value, nil
}

func hMacBySHA256(secretKey, toSign string) string {
// 创建对应的sha256哈希加密算法
hm := hmac.New(sha256.New, []byte(secretKey))
//写入加密数据
hm.Write([]byte(toSign))
return hex.EncodeToString(hm.Sum(nil))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package hmac_sha256

import (
"context"
"net"
"testing"
"time"

"github.com/eolinker/eosc"
"github.com/eolinker/eosc/eocontext"
http_service "github.com/eolinker/eosc/eocontext/http-context"
)

func TestExecutor(t *testing.T) {
e := NewExecutor("sign", []string{
"111111",
"appKey",
"1111111",
"format",
"JSON",
"idcard",
"111111111111111111",
"method",
"realid.idcard.verify",
"nonce",
"1111111",
"realname",
"张三",
"signMethod",
"HMAC-SHA256",
"signVersion",
"1",
"timestamp",
"2018-02-07 02:50:21",
"version",
"1",
})
ctx := &Context{}
v, err := e.Generate(ctx, "")
t.Log(v, err)
}

type Context struct {
}

func (c *Context) RequestId() string {
//TODO implement me
panic("implement me")
}

func (c *Context) AcceptTime() time.Time {
//TODO implement me
panic("implement me")
}

func (c *Context) Context() context.Context {
//TODO implement me
panic("implement me")
}

func (c *Context) Value(key interface{}) interface{} {
//TODO implement me
panic("implement me")
}

func (c *Context) WithValue(key, val interface{}) {
//TODO implement me
panic("implement me")
}

func (c *Context) Scheme() string {
//TODO implement me
panic("implement me")
}

func (c *Context) Assert(i interface{}) error {
//TODO implement me
panic("implement me")
}

func (c *Context) SetLabel(name, value string) {
//TODO implement me
panic("implement me")
}

func (c *Context) GetLabel(name string) string {
//TODO implement me
panic("implement me")
}

func (c *Context) Labels() map[string]string {
//TODO implement me
panic("implement me")
}

func (c *Context) GetComplete() eocontext.CompleteHandler {
//TODO implement me
panic("implement me")
}

func (c *Context) SetCompleteHandler(handler eocontext.CompleteHandler) {
//TODO implement me
panic("implement me")
}

func (c *Context) GetFinish() eocontext.FinishHandler {
//TODO implement me
panic("implement me")
}

func (c *Context) SetFinish(handler eocontext.FinishHandler) {
//TODO implement me
panic("implement me")
}

func (c *Context) GetBalance() eocontext.BalanceHandler {
//TODO implement me
panic("implement me")
}

func (c *Context) SetBalance(handler eocontext.BalanceHandler) {
//TODO implement me
panic("implement me")
}

func (c *Context) GetUpstreamHostHandler() eocontext.UpstreamHostHandler {
//TODO implement me
panic("implement me")
}

func (c *Context) SetUpstreamHostHandler(handler eocontext.UpstreamHostHandler) {
//TODO implement me
panic("implement me")
}

func (c *Context) RealIP() string {
//TODO implement me
panic("implement me")
}

func (c *Context) LocalIP() net.IP {
//TODO implement me
panic("implement me")
}

func (c *Context) LocalAddr() net.Addr {
//TODO implement me
panic("implement me")
}

func (c *Context) LocalPort() int {
//TODO implement me
panic("implement me")
}

func (c *Context) IsCloneable() bool {
//TODO implement me
panic("implement me")
}

func (c *Context) Clone() (eocontext.EoContext, error) {
//TODO implement me
panic("implement me")
}

func (c *Context) Request() http_service.IRequestReader {
//TODO implement me
panic("implement me")
}

func (c *Context) Proxy() http_service.IRequest {
//TODO implement me
panic("implement me")
}

func (c *Context) Response() http_service.IResponse {
//TODO implement me
panic("implement me")
}

func (c *Context) SendTo(scheme string, node eocontext.INode, timeout time.Duration) error {
//TODO implement me
panic("implement me")
}

func (c *Context) Proxies() []http_service.IProxy {
//TODO implement me
panic("implement me")
}

func (c *Context) FastFinish() {
//TODO implement me
panic("implement me")
}

func (c *Context) GetEntry() eosc.IEntry {
//TODO implement me
panic("implement me")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package hmac_sha256

import dynamic_params "github.com/eolinker/apinto/drivers/plugins/extra-params_v2/dynamic-params"

const name = "$hmac-sha256"

func Register() {
dynamic_params.Register(name, NewFactory())
}

func NewFactory() *Factory {
return &Factory{}
}

type Factory struct {
}

func (f *Factory) Create(name string, value []string) (dynamic_params.IDynamicDriver, error) {
return NewExecutor(name, value), nil
}
32 changes: 32 additions & 0 deletions drivers/plugins/request-interception/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package request_interception

import (
"github.com/eolinker/apinto/drivers"
"github.com/eolinker/eosc"
)

type Config struct {
Status int `json:"status" label:"响应状态码" minimum:"100" default:"200" description:"最小值:100"`
Body string `json:"body" label:"响应体"`
ContentType string `json:"content_type" label:"响应体类型" default:"application/json" enum:"text/plain,text/html,application/json"`
Headers []*Header `json:"headers" label:"响应头"`
}

type Header struct {
Key string `json:"key" label:"响应头Key"`
Value []string `json:"value" label:"响应头Value"`
}

func Create(id, name string, conf *Config, workers map[eosc.RequireId]eosc.IWorker) (eosc.IWorker, error) {
contentType := conf.ContentType
if contentType == "" {
contentType = "application/json"
}
return &executor{
WorkerBase: drivers.Worker(id, name),
status: conf.Status,
body: conf.Body,
headers: conf.Headers,
contentType: conf.ContentType,
}, nil
}

0 comments on commit 423b077

Please sign in to comment.