doulan2827 2018-07-20 18:03
浏览 70
已采纳

可以使用Go net / rpc检查元数据吗?

Examples of an RPC server in Go often look something like this (from https://golang.org/pkg/net/rpc/):

arith := new(Arith)
rpc.Register(arith)
rpc.HandleHTTP()
l, e := net.Listen("tcp", ":1234")
if e != nil {
    log.Fatal("listen error:", e)
}
go http.Serve(l, nil)

Is it possible to inspect the metadata associated with the connection before it is passed off to the registered function? I would like to check the client IP, API key, and requested function against an ACL first.

Edit to add: I realize that can get the remote address from the listener with l.RemoteAddr() - it isn't clear how I would get the API key, and requested function.

Once inside the function, I can get the API key, but not the client IP. Doing it inside the function is less than desirable because ACL check boilerplate would have to go in every function.

  • 写回答

1条回答 默认 最新

  • douwu5009 2018-07-23 21:45
    关注

    Regrettably, I have determined that this is not possible without forking net/rpc and adding context functionality to it.

    Passing a context to the registered function appears to be a common feature request; however, per https://golang.org/pkg/net/rpc/, "The net/rpc package is frozen and is not accepting new features."

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分