Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse RPC method from request body in proxy server #15

Merged
merged 6 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.54.2
args: --timeout 3m0s --verbose --modules-download-mode readonly
args: --timeout 5m0s --verbose --modules-download-mode readonly

- name: Run staticcheck # see: staticcheck.io
uses: dominikh/staticcheck-action@v1.3.0
Expand Down
24 changes: 22 additions & 2 deletions paymentproxy/proxy.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package paymentproxy

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"math/big"
"net/http"
Expand Down Expand Up @@ -101,11 +104,28 @@ func (p *PaymentProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
requiresPayment := true

if p.enablePaidRpcMethods {
rpcMethod := queryParams.Get("method")

var ReqBody struct {
Method string `json:"method"`
}

bodyBytes, _ := io.ReadAll(r.Body)
// TODO: Check for content type
err := json.Unmarshal(bodyBytes, &ReqBody)
if err != nil {
p.handleError(w, r, createPaymentError(fmt.Errorf("could not unmarshall request body: %w", err)))
return
}

slog.Debug("Serving RPC request", "method", ReqBody.Method)

// Reassign request body as io.ReadAll consumes it
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))

rpcMethod := ReqBody.Method
requiresPayment = false

// Check if payment is required for RPC method
// TODO: Check RPC method in request body
for _, paidRPCMethod := range paidRPCMethods {
if paidRPCMethod == rpcMethod {
requiresPayment = true
Expand Down