Skip to content

Commit

Permalink
feat: PRT - adding user IP data to referrer request (#1426)
Browse files Browse the repository at this point in the history
* adding ip to referer request

* adding ip to json rpc

* adding ip to rest

* adding ip to tendermint

* fixing test

* adding ip to ReferrerRequest

* changing consumer ip to user ip
  • Loading branch information
ranlavanet authored May 9, 2024
1 parent ca2e106 commit 4737838
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions protocol/chainlib/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ type RefererData struct {
ReferrerClient *metrics.ConsumerReferrerClient
}

func (rd *RefererData) SendReferer(refererMatchString string, chainId string, msg string, headers map[string][]string, c *websocket.Conn) error {
func (rd *RefererData) SendReferer(refererMatchString string, chainId string, msg string, userIp string, headers map[string][]string, c *websocket.Conn) error {
if rd == nil || rd.Address == "" {
return nil
}
Expand All @@ -427,7 +427,7 @@ func (rd *RefererData) SendReferer(refererMatchString string, chainId string, ms
}

utils.LavaFormatDebug("referer detected", utils.LogAttr("referer", refererMatchString))
rd.ReferrerClient.AppendReferrer(metrics.NewReferrerRequest(refererMatchString, chainId, msg, referer, origin, userAgent))
rd.ReferrerClient.AppendReferrer(metrics.NewReferrerRequest(refererMatchString, chainId, msg, referer, origin, userAgent, userIp))
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions protocol/chainlib/jsonRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func (apil *JsonRPCChainListener) Serve(ctx context.Context, cmdFlags common.Con
metricsData := metrics.NewRelayAnalytics(dappID, chainID, apiInterface)
relayResult, err := apil.relaySender.SendRelay(ctx, "", string(msg), http.MethodPost, dappID, websockConn.RemoteAddr().String(), metricsData, nil)
if ok && refererMatch != "" && apil.refererData != nil && err == nil {
go apil.refererData.SendReferer(refererMatch, chainID, string(msg), nil, websockConn)
go apil.refererData.SendReferer(refererMatch, chainID, string(msg), websockConn.RemoteAddr().String(), nil, websockConn)
}
reply := relayResult.GetReply()
replyServer := relayResult.GetReplyServer()
Expand Down Expand Up @@ -439,7 +439,7 @@ func (apil *JsonRPCChainListener) Serve(ctx context.Context, cmdFlags common.Con
apil.logger.LogTestMode(fiberCtx)
}

consumerIp := fiberCtx.Get(common.IP_FORWARDING_HEADER_NAME, fiberCtx.IP())
userIp := fiberCtx.Get(common.IP_FORWARDING_HEADER_NAME, fiberCtx.IP())
metadataValues := fiberCtx.GetReqHeaders()
headers := convertToMetadataMap(metadataValues)

Expand All @@ -459,9 +459,9 @@ func (apil *JsonRPCChainListener) Serve(ctx context.Context, cmdFlags common.Con
utils.LogAttr("headers", headers),
)
refererMatch := fiberCtx.Params(refererMatchString, "")
relayResult, err := apil.relaySender.SendRelay(ctx, path, msg, http.MethodPost, dappID, consumerIp, metricsData, headers)
relayResult, err := apil.relaySender.SendRelay(ctx, path, msg, http.MethodPost, dappID, userIp, metricsData, headers)
if refererMatch != "" && apil.refererData != nil && err == nil {
go apil.refererData.SendReferer(refererMatch, chainID, msg, metadataValues, nil)
go apil.refererData.SendReferer(refererMatch, chainID, msg, userIp, metadataValues, nil)
}
reply := relayResult.GetReply()
go apil.logger.AddMetricForHttp(metricsData, err, fiberCtx.GetReqHeaders())
Expand Down
8 changes: 5 additions & 3 deletions protocol/chainlib/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,12 @@ func (apil *RestChainListener) Serve(ctx context.Context, cmdFlags common.Consum
utils.LogAttr("msgSeed", msgSeed),
utils.LogAttr("headers", restHeaders),
)
userIp := fiberCtx.Get(common.IP_FORWARDING_HEADER_NAME, fiberCtx.IP())
refererMatch := fiberCtx.Params(refererMatchString, "")
requestBody := string(fiberCtx.Body())
relayResult, err := apil.relaySender.SendRelay(ctx, path+query, requestBody, http.MethodPost, dappID, fiberCtx.Get(common.IP_FORWARDING_HEADER_NAME, fiberCtx.IP()), analytics, restHeaders)
relayResult, err := apil.relaySender.SendRelay(ctx, path+query, requestBody, http.MethodPost, dappID, userIp, analytics, restHeaders)
if refererMatch != "" && apil.refererData != nil && err == nil {
go apil.refererData.SendReferer(refererMatch, chainID, requestBody, metadataValues, nil)
go apil.refererData.SendReferer(refererMatch, chainID, requestBody, userIp, metadataValues, nil)
}
reply := relayResult.GetReply()
go apil.logger.AddMetricForHttp(analytics, err, fiberCtx.GetReqHeaders())
Expand Down Expand Up @@ -374,10 +375,11 @@ func (apil *RestChainListener) Serve(ctx context.Context, cmdFlags common.Consum
utils.LogAttr("msgSeed", msgSeed),
utils.LogAttr("headers", restHeaders),
)
userIp := fiberCtx.Get(common.IP_FORWARDING_HEADER_NAME, fiberCtx.IP())
refererMatch := fiberCtx.Params(refererMatchString, "")
relayResult, err := apil.relaySender.SendRelay(ctx, path+query, "", fiberCtx.Method(), dappID, fiberCtx.Get(common.IP_FORWARDING_HEADER_NAME, fiberCtx.IP()), analytics, restHeaders)
if refererMatch != "" && apil.refererData != nil && err == nil {
go apil.refererData.SendReferer(refererMatch, chainID, path, metadataValues, nil)
go apil.refererData.SendReferer(refererMatch, chainID, path, userIp, metadataValues, nil)
}
reply := relayResult.GetReply()
go apil.logger.AddMetricForHttp(analytics, err, fiberCtx.GetReqHeaders())
Expand Down
15 changes: 7 additions & 8 deletions protocol/chainlib/tendermintRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func (apil *TendermintRpcChainListener) Serve(ctx context.Context, cmdFlags comm
metricsData := metrics.NewRelayAnalytics(dappID, chainID, apiInterface)
relayResult, err := apil.relaySender.SendRelay(ctx, "", string(msg), "", dappID, websocketConn.RemoteAddr().String(), metricsData, nil)
if ok && refererMatch != "" && apil.refererData != nil && err == nil {
go apil.refererData.SendReferer(refererMatch, chainID, string(msg), nil, websocketConn)
go apil.refererData.SendReferer(refererMatch, chainID, string(msg), websocketConn.RemoteAddr().String(), nil, websocketConn)
}
reply := relayResult.GetReply()
replyServer := relayResult.GetReplyServer()
Expand Down Expand Up @@ -476,9 +476,10 @@ func (apil *TendermintRpcChainListener) Serve(ctx context.Context, cmdFlags comm
utils.LogAttr("headers", headers),
)
refererMatch := fiberCtx.Params(refererMatchString, "")
relayResult, err := apil.relaySender.SendRelay(ctx, "", msg, "", dappID, fiberCtx.Get(common.IP_FORWARDING_HEADER_NAME, fiberCtx.IP()), metricsData, headers)
userIp := fiberCtx.Get(common.IP_FORWARDING_HEADER_NAME, fiberCtx.IP())
relayResult, err := apil.relaySender.SendRelay(ctx, "", msg, "", dappID, userIp, metricsData, headers)
if refererMatch != "" && apil.refererData != nil && err == nil {
go apil.refererData.SendReferer(refererMatch, chainID, msg, metadataValues, nil)
go apil.refererData.SendReferer(refererMatch, chainID, msg, userIp, metadataValues, nil)
}
reply := relayResult.GetReply()
go apil.logger.AddMetricForHttp(metricsData, err, fiberCtx.GetReqHeaders())
Expand Down Expand Up @@ -539,13 +540,11 @@ func (apil *TendermintRpcChainListener) Serve(ctx context.Context, cmdFlags comm
utils.LogAttr("dappID", dappID),
utils.LogAttr("headers", headers),
)
userIp := fiberCtx.Get(common.IP_FORWARDING_HEADER_NAME, fiberCtx.IP())
refererMatch := fiberCtx.Params(refererMatchString, "")
if refererMatch != "" && apil.refererData != nil {
go apil.refererData.SendReferer(refererMatch, chainID, path, metadataValues, nil)
}
relayResult, err := apil.relaySender.SendRelay(ctx, path+query, "", "", dappID, fiberCtx.Get(common.IP_FORWARDING_HEADER_NAME, fiberCtx.IP()), metricsData, headers)
relayResult, err := apil.relaySender.SendRelay(ctx, path+query, "", "", dappID, userIp, metricsData, headers)
if refererMatch != "" && apil.refererData != nil && err == nil {
go apil.refererData.SendReferer(refererMatch, chainID, path, metadataValues, nil)
go apil.refererData.SendReferer(refererMatch, chainID, path, userIp, metadataValues, nil)
}
msgSeed := strconv.FormatUint(guid, 10)
reply := relayResult.GetReply()
Expand Down
4 changes: 3 additions & 1 deletion protocol/metrics/consumer_referrer_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ConsumerReferrerClient struct {
*QueueSender
}

func NewReferrerRequest(referrerId string, chainId string, msg string, referer string, origin string, userAgent string) ReferrerRequest {
func NewReferrerRequest(referrerId string, chainId string, msg string, referer string, origin string, userAgent string, userIp string) ReferrerRequest {
return ReferrerRequest{
Name: referrerName,
ReferrerId: referrerId,
Expand All @@ -30,6 +30,7 @@ func NewReferrerRequest(referrerId string, chainId string, msg string, referer s
Referer: referer,
Origin: origin,
UserAgent: userAgent,
UserIp: userIp,
}
}

Expand All @@ -42,6 +43,7 @@ type ReferrerRequest struct {
Referer string `json:"http-referer"`
Origin string `json:"origin"`
UserAgent string `json:"user-agent"`
UserIp string `json:"user-ip"`
}

func (rr ReferrerRequest) String() string {
Expand Down
10 changes: 5 additions & 5 deletions protocol/metrics/consumer_referrer_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func TestReferrerClientFlows(t *testing.T) {
defer mockServer.Close()
endpoint := mockServer.URL
serverClient := NewConsumerReferrerClient(endpoint, 100*time.Millisecond)
serverClient.AppendReferrer(NewReferrerRequest("banana", "ETH1", "Message-1", "https://referer.com", "https://origin.com", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"))
serverClient.AppendReferrer(NewReferrerRequest("banana", "COSMOSHUB", "Message-2", "https://referer.com", "https://origin.com", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"))
serverClient.AppendReferrer(NewReferrerRequest("papaya", "ETH1", "Message-3", "https://referer.com", "https://origin.com", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"))
serverClient.AppendReferrer(NewReferrerRequest("banana", "ETH1", "Message-1", "https://referer.com", "https://origin.com", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0", "127.0.0.1"))
serverClient.AppendReferrer(NewReferrerRequest("banana", "COSMOSHUB", "Message-2", "https://referer.com", "https://origin.com", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0", "127.0.0.1"))
serverClient.AppendReferrer(NewReferrerRequest("papaya", "ETH1", "Message-3", "https://referer.com", "https://origin.com", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0", "127.0.0.1"))
time.Sleep(110 * time.Millisecond)
require.Len(t, messages, 2)
bananas := 0
Expand All @@ -55,12 +55,12 @@ func TestReferrerClientNull(t *testing.T) {
t.Run("null", func(t *testing.T) {
serverClient := NewConsumerReferrerClient("")
require.Nil(t, serverClient)
serverClient.AppendReferrer(NewReferrerRequest("banana", "ETH1", "Message-1", "https://referer.com", "https://origin.com", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"))
serverClient.AppendReferrer(NewReferrerRequest("banana", "ETH1", "Message-1", "https://referer.com", "https://origin.com", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0", "127.0.0.1"))
time.Sleep(110 * time.Millisecond)
getSender := func() ReferrerSender {
return serverClient
}
reporter := getSender()
reporter.AppendReferrer(NewReferrerRequest("banana", "ETH1", "Message-2", "https://referer.com", "https://origin.com", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"))
reporter.AppendReferrer(NewReferrerRequest("banana", "ETH1", "Message-2", "https://referer.com", "https://origin.com", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0", "127.0.0.1"))
})
}

0 comments on commit 4737838

Please sign in to comment.