IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

聊聊rocketmq-client-go的ACLInterceptor

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

本文主要研究一下rocketmq-client-go的ACLInterceptor

ACLInterceptor

rocketmq-client-go-v2.0.0/internal/remote/interceptor.go

func ACLInterceptor(credentials primitive.Credentials) primitive.Interceptor {
    return func(ctx context.Context, req, reply interface{}, next primitive.Invoker) error {
        cmd := req.(*RemotingCommand)
        m := make(map[string]string)
        order := make([]string, 1)
        m[accessKey] = credentials.AccessKey
        order[0] = accessKey
        if credentials.SecurityToken != "" {
            m[securityToken] = credentials.SecurityToken
        }
        for k, v := range cmd.ExtFields {
            m[k] = v
            order = append(order, k)
        }
        sort.Slice(order, func(i, j int) bool {
            return strings.Compare(order[i], order[j]) < 0
        })
        content := ""
        for idx := range order {
            content += m[order[idx]]
        }
        buf := make([]byte, len(content)+len(cmd.Body))
        copy(buf, []byte(content))
        copy(buf[len(content):], cmd.Body)

        cmd.ExtFields[signature] = calculateSignature(buf, []byte(credentials.SecretKey))
        cmd.ExtFields[accessKey] = credentials.AccessKey

        // The SecurityToken value is unnecessary, user can choose this one.
        if credentials.SecurityToken != "" {
            cmd.ExtFields[securityToken] = credentials.SecurityToken
        }
        err := next(ctx, req, reply)
        return err
    }
}

  • ACLInterceptor方法会调用calculateSignature计算签名,然后执行next

calculateSignature

rocketmq-client-go-v2.0.0/internal/remote/interceptor.go

func calculateSignature(data, sk []byte) string {
    mac := hmac.New(func() hash.Hash {
        return sha1.New()
    }, sk)
    mac.Write(data)
    return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}

  • calculateSignature方法通过base64来将mac.Sum的结果编码为string

SetCredentials

rocketmq-client-go-v2.0.0/internal/namesrv.go

func (s *namesrvs) SetCredentials(credentials primitive.Credentials) {
    s.nameSrvClient.RegisterInterceptor(remote.ACLInterceptor(credentials))
}

  • namesrv的SetCredentials方法执行的是s.nameSrvClient.RegisterInterceptor(remote.ACLInterceptor(credentials))

RegisterInterceptor

rocketmq-client-go-v2.0.0/internal/remote/remote_client.go

func (c *remotingClient) RegisterInterceptor(interceptors ...primitive.Interceptor) {
    c.interceptor = primitive.ChainInterceptors(interceptors...)
}

  • RegisterInterceptor方法主要是执行primitive.ChainInterceptors

ChainInterceptors

rocketmq-client-go-v2.0.0/primitive/interceptor.go

func ChainInterceptors(interceptors ...Interceptor) Interceptor {
    if len(interceptors) == 0 {
        return nil
    }
    if len(interceptors) == 1 {
        return interceptors[0]
    }
    return func(ctx context.Context, req, reply interface{}, invoker Invoker) error {
        return interceptors[0](ctx, req, reply, getChainedInterceptor(interceptors, 0, invoker))
    }
}

  • ChainInterceptors方法会执行getChainedInterceptor

getChainedInterceptor

rocketmq-client-go-v2.0.0/primitive/interceptor.go

func getChainedInterceptor(interceptors []Interceptor, cur int, finalInvoker Invoker) Invoker {
    if cur == len(interceptors)-1 {
        return finalInvoker
    }
    return func(ctx context.Context, req, reply interface{}) error {
        return interceptors[cur+1](ctx, req, reply, getChainedInterceptor(interceptors, cur+1, finalInvoker))
    }
}

  • getChainedInterceptor方法递归执行getChainedInterceptor(interceptors, cur+1, finalInvoker)直到cur == len(interceptors)-1

小结

ACLInterceptor方法会调用calculateSignature计算签名,然后执行next;calculateSignature方法通过base64来将mac.Sum的结果编码为string

doc

  • interceptor

文章永久链接:https://tech.souyunku.com/?p=25984


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(97) 打赏



未经允许不得转载:搜云库技术团队 » 聊聊rocketmq-client-go的ACLInterceptor

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367