Skip to content

Commit

Permalink
修复aksk query参数顺序未按字母排序问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Dot-Liu committed Dec 22, 2024
1 parent 4835b67 commit d7e2d87
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 62 deletions.
36 changes: 25 additions & 11 deletions application/auth/aksk/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,47 @@ import (
"encoding/hex"
"errors"
"fmt"
"net/url"
"strings"

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

const dateHeader = "x-gateway-date"

//buildToSign 构建待加密的签名所需字符串
// buildToSign 构建待加密的签名所需字符串
func buildToSign(ctx http_service.IHttpContext, encType string, signedHeaders []string) string {
toSign := strings.Builder{}
toSign.WriteString(encType + "\n")
dh := ctx.Request().Header().GetHeader(dateHeader)
toSign.WriteString(dh + "\n")

cr := buildHexCanonicalRequest(ctx, signedHeaders)
toSign.WriteString(strings.ToLower(cr))
return toSign.String()
}

//buildHexCanonicalRequest 构建规范消息头
// buildHexCanonicalRequest 构建规范消息头
func buildHexCanonicalRequest(ctx http_service.IHttpContext, signedHeaders []string) string {
cr := strings.Builder{}

cr.WriteString(strings.ToUpper(ctx.Request().Method()) + "\n")
cr.WriteString(buildPath(ctx.Request().URI().Path()) + "\n")
cr.WriteString(ctx.Request().URI().RawQuery() + "\n")

// 对query参数排序
rawQuery := ctx.Request().URI().RawQuery()
query := url.Values{}
queryArgs := strings.Split(strings.TrimSpace(rawQuery), "&")
for i := 0; i < len(queryArgs); i++ {
params := strings.Split(queryArgs[i], "=")
if len(params) != 2 {
//query.Set(params[0], "")
continue
}
query.Set(params[0], params[1])
}
cr.WriteString(query.Encode() + "\n")
//cr.WriteString(ctx.Request().URI().RawQuery() + "\n")

for _, header := range signedHeaders {
if strings.ToLower(header) == "host" {
cr.WriteString(buildHeaders(header, ctx.Request().Header().Host()) + "\n")
Expand All @@ -45,7 +59,7 @@ func buildHexCanonicalRequest(ctx http_service.IHttpContext, signedHeaders []str
cr.WriteString(strings.Join(signedHeaders, ";") + "\n")
body, _ := ctx.Request().Body().RawBody()
cr.WriteString(hexEncode(body))

return hexEncode([]byte(cr.String()))
}

Expand All @@ -72,7 +86,7 @@ func hMaxBySHA256(secretKey, toSign string) string {
}

func parseAuthorization(token string) (encType string, accessKey string, signHeaders []string, signature string, err error) {

infos := strings.Split(token, ",")
if len(infos) < 3 {
err = errors.New("invalid authorization")
Expand All @@ -83,9 +97,9 @@ func parseAuthorization(token string) (encType string, accessKey string, signHea
return
}
signHeaders = parseSignHeaders(infos[1])

signature = parseSignature(infos[2])

return
}

Expand Down
35 changes: 35 additions & 0 deletions application/auth/aksk/sign_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package aksk

import (
"net/url"
"testing"

http_context "github.com/eolinker/apinto/node/http-context"
"github.com/valyala/fasthttp"
)

func TestAksk_Check(t *testing.T) {
req := fasthttp.AcquireRequest()
u := fasthttp.AcquireURI()
uri, _ := url.Parse("https://gateway.hr-soft.cn/api/redapi/tech/redteams/tag/tagUser?userId=8e211885-b622-11ec-8e03-14187749a8c0&tagId=37")
u.SetPath(uri.Path)
u.SetScheme(uri.Scheme)
u.SetHost(uri.Host)
u.SetQueryString(uri.RawQuery)
req.SetURI(u)
req.SetHostBytes(u.Host())
req.Header.Set("x-gateway-date", "20200605T104456Z")
req.Header.Set("content-type", "application/json")
req.Header.SetHostBytes(u.Host())

ctx := &fasthttp.RequestCtx{
Request: *req,
}
origin := buildToSign(http_context.NewContext(ctx, 9000), "HMAC-SHA256", []string{"content-type", "host", "x-gateway-date"})
t.Log("origin", origin)
sign := hMaxBySHA256("8f8154ff07f7153eea59a2ba44b5fcfe443dba1e4c45f87c549e6a05f699145d", origin)
if sign != "523a1d901873fbbc19df355dba6f8bb695a2ef3435206615e7724490054b0529" {
t.Fatalf("sign error %s", sign)
}
t.Logf("sign %s", sign)
}
4 changes: 4 additions & 0 deletions drivers/router/http-router/websocket/dial.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package websocket

import (
"crypto/tls"
"net/http"
"net/url"
"time"
Expand All @@ -14,6 +15,9 @@ import (
var dialer = &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 45 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}

var skipHeaders = []string{
Expand Down
112 changes: 62 additions & 50 deletions node/fasthttp-client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fasthttp_client

import (
"context"
"crypto/tls"
"fmt"
"net"
Expand All @@ -22,7 +23,7 @@ func ProxyTimeout(scheme string, host string, node eocontext.INode, req *fasthtt
return err
}

var defaultClient Client
var defaultClient = NewClient()

const (
DefaultMaxConns = 10240
Expand All @@ -38,9 +39,21 @@ const (
//
// The fields of a Client should not be changed while it is in use.
type Client struct {
mLock sync.Mutex
m map[string]*fasthttp.HostClient
ms map[string]*fasthttp.HostClient
mLock sync.RWMutex
m map[string]*fasthttp.HostClient
ms map[string]*fasthttp.HostClient
ctx context.Context
cancel context.CancelFunc
}

func NewClient() *Client {
ctx, cancel := context.WithCancel(context.Background())
return &Client{
m: make(map[string]*fasthttp.HostClient),
ms: make(map[string]*fasthttp.HostClient),
ctx: ctx,
cancel: cancel,
}
}

func readAddress(addr string) (scheme, host string) {
Expand All @@ -54,31 +67,31 @@ func (c *Client) getHostClient(addr string, rewriteHost string) (*fasthttp.HostC

scheme, nodeAddr := readAddress(addr)
host := nodeAddr
isTLS := false
if strings.EqualFold(scheme, "https") {
isTLS = true
host = fmt.Sprintf("%s-%s", rewriteHost, nodeAddr)
isTLS := strings.EqualFold(scheme, "https")

} else if !strings.EqualFold(scheme, "http") {
if !strings.EqualFold(scheme, "http") && !isTLS {
return nil, "", fmt.Errorf("unsupported protocol %q. http and https are supported", scheme)
}

startCleaner := false

c.mLock.Lock()
c.mLock.RLock()
m := c.m
if isTLS {
m = c.ms
}
if m == nil {
m = make(map[string]*fasthttp.HostClient)
if isTLS {
c.ms = m
} else {
c.m = m
}
}

hc := m[host]
c.mLock.RUnlock()
if hc != nil {
return hc, scheme, nil
}
c.mLock.Lock()
defer c.mLock.Unlock()

if isTLS {
m = c.ms
} else {
m = c.m
}
if hc == nil {
dial := Dial
dialAddr := addMissingPort(nodeAddr, isTLS)
Expand Down Expand Up @@ -109,14 +122,10 @@ func (c *Client) getHostClient(addr string, rewriteHost string) (*fasthttp.HostC
}
m[host] = hc
if len(m) == 1 {
startCleaner = true
go c.startCleaner(m)
}
}
c.mLock.Unlock()

if startCleaner {
go c.mCleaner(m)
}
return hc, scheme, nil
}

Expand Down Expand Up @@ -159,39 +168,42 @@ func (c *Client) ProxyTimeout(addr string, host string, req *fasthttp.Request, r
resp.SetConnectionClose()
}
}()
//var deadline time.Time
//var requestURI string
//redirectCount := 0
//for {

client, scheme, err := c.getHostClient(addr, host)
if err != nil {
return err
}

request.URI().SetScheme(scheme)

//if redirectCount == 0 {
// deadline = time.Now().Add(timeout)
//} else {
// request.SetRequestURI(requestURI)
//}
//log.Info("requestURI:", request.URI().String()+", host:", request.Host(), " body:", string(request.Body()))
return client.DoTimeout(req, resp, timeout)
//if err != nil {
// return err
//}
//if !fasthttp.StatusCodeIsRedirect(resp.StatusCode()) || redirectCount >= DefaultMaxRedirectCount {
// break
//}
//redirectCount++
//location := resp.Header.Peek("Location")
//if len(location) == 0 {
// return fasthttp.ErrMissingLocation
//}
//addr, requestURI = getRedirectURL(req.URI().String(), location)
//}

//return nil
}

func (c *Client) startCleaner(m map[string]*fasthttp.HostClient) {
sleep := time.Second * 10
mustStop := false
for {
select {
case <-c.ctx.Done():
return
case <-time.After(sleep):
c.mLock.Lock()
for k, v := range m {
if v.ConnsCount() == 0 {
v.CloseIdleConnections()
delete(m, k)
}
}

if len(m) == 0 {
mustStop = true
}
c.mLock.Unlock()
if mustStop {
return
}
}
}
}

func (c *Client) mCleaner(m map[string]*fasthttp.HostClient) {
Expand Down
2 changes: 1 addition & 1 deletion node/http-context/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (ur *URIRequest) GetQuery(key string) string {
}

func (ur *URIRequest) RawQuery() string {
return string(ur.uri.QueryArgs().String())
return ur.uri.QueryArgs().String()
}

func (ur *URIRequest) SetPath(s string) {
Expand Down

0 comments on commit d7e2d87

Please sign in to comment.