rpc: add PeerInfo (#24255)
This replaces the sketchy and undocumented string context keys for HTTP requests with a defined interface. Using string keys with context is discouraged because they may clash with keys created by other packages. We added these keys to make connection metadata available in the signer, so this change also updates signer/core to use the new PeerInfo API.
This commit is contained in:
@ -33,6 +33,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/ethereum/go-ethereum/signer/core/apitypes"
|
||||
"github.com/ethereum/go-ethereum/signer/storage"
|
||||
)
|
||||
@ -188,23 +189,24 @@ func StartClefAccountManager(ksLocation string, nousb, lightKDF bool, scpath str
|
||||
|
||||
// MetadataFromContext extracts Metadata from a given context.Context
|
||||
func MetadataFromContext(ctx context.Context) Metadata {
|
||||
info := rpc.PeerInfoFromContext(ctx)
|
||||
|
||||
m := Metadata{"NA", "NA", "NA", "", ""} // batman
|
||||
|
||||
if v := ctx.Value("remote"); v != nil {
|
||||
m.Remote = v.(string)
|
||||
if info.Transport != "" {
|
||||
if info.Transport == "http" {
|
||||
m.Scheme = info.HTTP.Version
|
||||
}
|
||||
m.Scheme = info.Transport
|
||||
}
|
||||
if v := ctx.Value("scheme"); v != nil {
|
||||
m.Scheme = v.(string)
|
||||
if info.RemoteAddr != "" {
|
||||
m.Remote = info.RemoteAddr
|
||||
}
|
||||
if v := ctx.Value("local"); v != nil {
|
||||
m.Local = v.(string)
|
||||
}
|
||||
if v := ctx.Value("Origin"); v != nil {
|
||||
m.Origin = v.(string)
|
||||
}
|
||||
if v := ctx.Value("User-Agent"); v != nil {
|
||||
m.UserAgent = v.(string)
|
||||
if info.HTTP.Host != "" {
|
||||
m.Local = info.HTTP.Host
|
||||
}
|
||||
m.Origin = info.HTTP.Origin
|
||||
m.UserAgent = info.HTTP.UserAgent
|
||||
return m
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user