找回密码
 立即注册
查看: 163|回复: 0

抖音直播间数据采集,ProtoBuf + gRPC 分析请求头

[复制链接]
发表于 2023-3-3 15:45 | 显示全部楼层 |阅读模式
描述
Protobuf是Google protocol buffer的简称,是一种语言中立、平台无关、易于扩展的结构化数据序列化技术,可用于数据传输、存储等领域。
与Protoful类似的序列化技术还有XML、JSON、Thrift等,但Protoful更快、更小、更简单,且具备良好的兼容性。
配置请求头
方法一
只设置客户端请求时附带的header
类 io.grpc.stub.MetadataUtils,其中有个方法
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1789")
  public static <T extends AbstractStub<T>> T attachHeaders(
      T stub,
      final Metadata extraHeaders) {
    return stub.withInterceptors(newAttachHeadersInterceptor(extraHeaders));
  }自己封装后
private static <T extends AbstractStub<T>> T attachHeaders(T stub, final Map<String, String> headerMap) {
    Metadata extraHeaders = new Metadata();
    if (headerMap != null) {
        for (String key : headerMap.keySet()) {
            Metadata.Key<String> customHeadKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
            extraHeaders.put(customHeadKey, headerMap.get(key));
        }
    }
    return MetadataUtils.attachHeaders(stub, extraHeaders);
}
方法二
支持设置客户端请求的header以及获取服务端返回结果中的header
1. 设置拦截器
class HeaderClientInterceptor implements ClientInterceptor {
    private static final String TAG = "HeaderClientInterceptor";
    private Map<String, String> mHeaderMap;
    public HeaderClientInterceptor(Map<String, String> headerMap) {
        mHeaderMap = headerMap;
    }
    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
                                                               CallOptions callOptions, Channel next) {
        return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
            @Override
            public void start(Listener<RespT> responseListener, Metadata headers) {
                /* put custom header */
                if (mHeaderMap != null) {
                    for (String key : mHeaderMap.keySet()) {
                        Metadata.Key<String> customHeadKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
                        headers.put(customHeadKey, mHeaderMap.get(key));
                    }
                }
                Logger.i(TAG, "header send to server:" + headers);
                super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
                    @Override
                    public void onHeaders(Metadata headers) {
                        /**
                         * if you don't need receive header from server,
                         * you can use {@link io.grpc.stub.MetadataUtils attachHeaders}
                         * directly to send header
                         */
                        Logger.i(TAG, "header received from server:" + headers);
                        super.onHeaders(headers);
                    }
                }, headers);
            }
        };
    }
}

2.使用
Map<String, String> headerMap = new HashMap<>();
//...
ClientInterceptor interceptor = new HeaderClientInterceptor(headerMap);
//...
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build();
Channel channel = ClientInterceptors.intercept(managedChannel, interceptor);
// then create stub here by this channel所以

要实现添加header 那么必须实现 ClientInterceptor 接口类中的 interceptCall 的方法

    而且还要有一个 添加Header的具体方法
    不管 grpc 怎么混淆都离不开这种配置模式

例如这种


注意他们的类型,很明显这种情况可以快速推论

    z1.c.v.p.a.d.b.f.a.a() 大概率就是混淆后的 interceptCall
    z1.c.v.p.a.d.b.f.a.c() 是添加请求头的函数
了解更多

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-17 20:23 , Processed in 0.093461 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表