From fcef031629515cb273bf210906c12b59f75fb454 Mon Sep 17 00:00:00 2001 From: Sergey Date: Sat, 8 Jun 2024 17:48:28 +0300 Subject: [PATCH] feat: add custom user-agent --- lite/client.go | 26 ++++++++++++++++++++++++++ lite/query.go | 13 ++++++------- lite/tx.go | 5 ++--- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lite/client.go b/lite/client.go index 714ba60..487400a 100644 --- a/lite/client.go +++ b/lite/client.go @@ -2,7 +2,12 @@ package lite import ( "io" + "net/http" "sync" + "time" + + rpchttp "github.com/tendermint/tendermint/rpc/client/http" + jsonrpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" @@ -151,3 +156,24 @@ func (c *Client) FromAddress() sdk.AccAddress { return c.ctx.FromAddress } func (c *Client) FromName() string { return c.ctx.FromName } func (c *Client) SimulateAndExecute() bool { return c.txf.SimulateAndExecute() } func (c *Client) TxConfig() client.TxConfig { return c.ctx.TxConfig } + +type AddHeaderTransport struct { + T http.RoundTripper +} + +func (adt *AddHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) { + req.Header.Add("User-Agent", "sentinel-dvpn-node") + return adt.T.RoundTrip(req) +} + +func (c *Client) GetHttpClient(remote string, timeout uint) (*rpchttp.HTTP, error) { + httpClient, err := jsonrpcclient.DefaultHTTPClient(remote) + if err != nil { + return nil, err + } + + httpClient.Timeout = time.Duration(timeout) * time.Second + httpClient.Transport = &AddHeaderTransport{T: httpClient.Transport} + + return rpchttp.NewWithClient(remote, "/websocket", httpClient) +} diff --git a/lite/query.go b/lite/query.go index 3d051eb..fbaae5e 100644 --- a/lite/query.go +++ b/lite/query.go @@ -10,7 +10,6 @@ import ( sessiontypes "github.com/sentinel-official/hub/x/session/types" subscriptiontypes "github.com/sentinel-official/hub/x/subscription/types" vpntypes "github.com/sentinel-official/hub/x/vpn/types" - rpchttp "github.com/tendermint/tendermint/rpc/client/http" "github.com/sentinel-official/dvpn-node/types" ) @@ -18,7 +17,7 @@ import ( func (c *Client) queryAccount(remote string, accAddr sdk.AccAddress) (authtypes.AccountI, error) { c.log.Debug("Querying the account", "remote", remote, "address", accAddr) - client, err := rpchttp.NewWithTimeout(remote, "/websocket", c.queryTimeout) + client, err := c.GetHttpClient(remote, c.queryTimeout) if err != nil { return nil, err } @@ -64,7 +63,7 @@ func (c *Client) QueryAccount(accAddr sdk.AccAddress) (result authtypes.AccountI func (c *Client) queryNode(remote string, nodeAddr hubtypes.NodeAddress) (*nodetypes.Node, error) { c.log.Debug("Querying the node", "remote", remote, "address", nodeAddr) - client, err := rpchttp.NewWithTimeout(remote, "/websocket", c.queryTimeout) + client, err := c.GetHttpClient(remote, c.queryTimeout) if err != nil { return nil, err } @@ -103,7 +102,7 @@ func (c *Client) QueryNode(nodeAddr hubtypes.NodeAddress) (result *nodetypes.Nod func (c *Client) querySubscription(remote string, id uint64) (subscriptiontypes.Subscription, error) { c.log.Debug("Querying the subscription", "remote", remote, "id", id) - client, err := rpchttp.NewWithTimeout(remote, "/websocket", c.queryTimeout) + client, err := c.GetHttpClient(remote, c.queryTimeout) if err != nil { return nil, err } @@ -147,7 +146,7 @@ func (c *Client) QuerySubscription(id uint64) (result subscriptiontypes.Subscrip func (c *Client) queryAllocation(remote string, id uint64, accAddr sdk.AccAddress) (*subscriptiontypes.Allocation, error) { c.log.Debug("Querying the allocation", "remote", remote, "id", id, "address", accAddr) - client, err := rpchttp.NewWithTimeout(remote, "/websocket", c.queryTimeout) + client, err := c.GetHttpClient(remote, c.queryTimeout) if err != nil { return nil, err } @@ -186,7 +185,7 @@ func (c *Client) QueryAllocation(id uint64, accAddr sdk.AccAddress) (result *sub func (c *Client) querySession(remote string, id uint64) (*sessiontypes.Session, error) { c.log.Debug("Querying the session", "remote", remote, "id", id) - client, err := rpchttp.NewWithTimeout(remote, "/websocket", c.queryTimeout) + client, err := c.GetHttpClient(remote, c.queryTimeout) if err != nil { return nil, err } @@ -223,7 +222,7 @@ func (c *Client) QuerySession(id uint64) (result *sessiontypes.Session, err erro } func (c *Client) hasNodeForPlan(remote string, id uint64, nodeAddr hubtypes.NodeAddress) (bool, error) { - client, err := rpchttp.NewWithTimeout(remote, "/websocket", c.queryTimeout) + client, err := c.GetHttpClient(remote, c.queryTimeout) if err != nil { return false, err } diff --git a/lite/tx.go b/lite/tx.go index b16c647..a0204ef 100644 --- a/lite/tx.go +++ b/lite/tx.go @@ -7,13 +7,12 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/pkg/errors" abcitypes "github.com/tendermint/tendermint/abci/types" - rpchttp "github.com/tendermint/tendermint/rpc/client/http" ) func (c *Client) broadcastTx(remote string, txBytes []byte) (*sdk.TxResponse, error) { c.log.Debug("Broadcasting the transaction", "remote", remote, "size", len(txBytes)) - client, err := rpchttp.NewWithTimeout(remote, "/websocket", c.txTimeout) + client, err := c.GetHttpClient(remote, c.txTimeout) if err != nil { return nil, err } @@ -58,7 +57,7 @@ func (c *Client) BroadcastTx(txBytes []byte) (res *sdk.TxResponse, err error) { func (c *Client) calculateGas(remote string, txf tx.Factory, messages ...sdk.Msg) (uint64, error) { c.log.Debug("Calculating the gas", "remote", remote, "messages", len(messages)) - client, err := rpchttp.NewWithTimeout(remote, "/websocket", c.txTimeout) + client, err := c.GetHttpClient(remote, c.txTimeout) if err != nil { return 0, err }