Skip to content

Commit

Permalink
feat: add custom user-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jun 8, 2024
1 parent 934b433 commit fcef031
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
26 changes: 26 additions & 0 deletions lite/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
13 changes: 6 additions & 7 deletions lite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ 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"
)

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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions lite/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit fcef031

Please sign in to comment.