2019-09-19 05:46:39 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package rpcpeer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2019-11-11 19:32:08 +00:00
|
|
|
"net"
|
2019-09-19 05:46:39 +01:00
|
|
|
|
|
|
|
"storj.io/drpc/drpcctx"
|
|
|
|
)
|
|
|
|
|
2019-11-01 23:04:23 +00:00
|
|
|
// drpcInternalFromContext returns a peer from the context using drpc.
|
|
|
|
func drpcInternalFromContext(ctx context.Context) (*Peer, error) {
|
2019-09-19 05:46:39 +01:00
|
|
|
tr, ok := drpcctx.Transport(ctx)
|
|
|
|
if !ok {
|
|
|
|
return nil, Error.New("unable to get drpc peer from context")
|
|
|
|
}
|
|
|
|
|
2019-11-11 19:32:08 +00:00
|
|
|
conn, ok := tr.(interface {
|
|
|
|
RemoteAddr() net.Addr
|
|
|
|
ConnectionState() tls.ConnectionState
|
|
|
|
})
|
2019-09-19 05:46:39 +01:00
|
|
|
if !ok {
|
2019-11-11 19:32:08 +00:00
|
|
|
return nil, Error.New("drpc transport does not have required methods")
|
2019-09-19 05:46:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Peer{
|
|
|
|
Addr: conn.RemoteAddr(),
|
|
|
|
State: conn.ConnectionState(),
|
|
|
|
}, nil
|
|
|
|
}
|