feat(rpc-user-agent)_: Added user agent to upstream client

This commit is contained in:
Samuel Hawksby-Robinson 2024-08-13 11:25:19 +01:00
parent bb01332af9
commit c97cedba9a
2 changed files with 24 additions and 8 deletions

View File

@ -37,6 +37,10 @@ const (
// rpcUserAgentFormat 'procurator': *an agent representing others*, aka a "proxy" // rpcUserAgentFormat 'procurator': *an agent representing others*, aka a "proxy"
// allows for the rpc client to have a dedicated user agent, which is useful for the proxy server logs. // allows for the rpc client to have a dedicated user agent, which is useful for the proxy server logs.
rpcUserAgentFormat = "procuratee-%s/1.0" rpcUserAgentFormat = "procuratee-%s/1.0"
// rpcUserAgentUpstreamFormat a separate user agent format for upstream, because we should not be using upstream
// if we see this user agent in the logs that means parts of the application are using a malconfigured http client
rpcUserAgentUpstreamFormat = "procuratee-%s-upstream/1.0"
) )
// List of RPC client errors. // List of RPC client errors.
@ -46,15 +50,20 @@ var (
var ( var (
// rpcUserAgentName the user agent // rpcUserAgentName the user agent
rpcUserAgentName = fmt.Sprintf(rpcUserAgentFormat, "no-GOOS") rpcUserAgentName = fmt.Sprintf(rpcUserAgentFormat, "no-GOOS")
rpcUserAgentUpstreamName = fmt.Sprintf(rpcUserAgentUpstreamFormat, "no-GOOS")
) )
func init() { func init() {
switch runtime.GOOS { switch runtime.GOOS {
case "android", "ios": case "android", "ios":
rpcUserAgentName = fmt.Sprintf(rpcUserAgentFormat, "mobile") mobile := "mobile"
rpcUserAgentName = fmt.Sprintf(rpcUserAgentFormat, mobile)
rpcUserAgentUpstreamName = fmt.Sprintf(rpcUserAgentUpstreamFormat, mobile)
default: default:
rpcUserAgentName = fmt.Sprintf(rpcUserAgentFormat, "desktop") desktop := "desktop"
rpcUserAgentName = fmt.Sprintf(rpcUserAgentFormat, desktop)
rpcUserAgentUpstreamName = fmt.Sprintf(rpcUserAgentUpstreamFormat, desktop)
} }
} }
@ -128,16 +137,18 @@ func NewClient(client *gethrpc.Client, upstreamChainID uint64, upstream params.U
providerConfigs: providerConfigs, providerConfigs: providerConfigs,
} }
// TODO update user agent here var opts []gethrpc.ClientOption
// Something like "status-go-rpc-client-upstream" opts = append(opts,
gethrpc.WithHeaders(http.Header{
gethrpc.DialOptions(context.Background(), c.upstreamURL) "User-Agent": {rpcUserAgentUpstreamName},
}),
)
if upstream.Enabled { if upstream.Enabled {
c.UpstreamChainID = upstreamChainID c.UpstreamChainID = upstreamChainID
c.upstreamEnabled = upstream.Enabled c.upstreamEnabled = upstream.Enabled
c.upstreamURL = upstream.URL c.upstreamURL = upstream.URL
upstreamClient, err := gethrpc.Dial(c.upstreamURL) upstreamClient, err := gethrpc.DialOptions(context.Background(), c.upstreamURL, opts...)
if err != nil { if err != nil {
return nil, fmt.Errorf("dial upstream server: %s", err) return nil, fmt.Errorf("dial upstream server: %s", err)
} }

View File

@ -196,3 +196,8 @@ func TestGetClientsUsingCache(t *testing.T) {
assert.Nil(t, balance) assert.Nil(t, balance)
wg.Wait() wg.Wait()
} }
func TestUserAgent(t *testing.T) {
require.Equal(t, "procuratee-desktop/1.0", rpcUserAgentName)
require.Equal(t, "procuratee-desktop-upstream/1.0", rpcUserAgentUpstreamName)
}