commit 108a711d8af91229a8a821dae197da6840a9879c
parent d2dd7addd817f1c373e2fa8c0dc197fd70500fcf
Author: lash <dev@holbrook.no>
Date: Fri, 28 Jun 2024 21:23:14 +0100
Successful passthrough
Diffstat:
4 files changed, 91 insertions(+), 8 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,2 +1,5 @@
all:
go build -v -o eth-proxy ./cmd/
+
+run: all
+ ./eth-proxy
diff --git a/cmd/main.go b/cmd/main.go
@@ -8,13 +8,13 @@ import (
"defalsify.org/go-eth-proxy/proxy"
"defalsify.org/go-eth-proxy/store/lmdb"
- "github.com/ethereum/go-ethereum/rpc"
)
func main() {
dbpath := flag.String("cachepath", ".", "Path to lmdb data")
+ flag.Parse()
db, err := lmdb.NewStore(*dbpath)
if err != nil {
log.Printf("%s", err)
@@ -23,13 +23,11 @@ func main() {
defer db.Close()
svc := proxy.NewProxyService(db)
- h := rpc.NewServer()
- err = h.RegisterName("eth", svc)
+ h, err := proxy.NewProxyServer(svc, flag.Args()[0])
if err != nil {
log.Printf("%s", err)
os.Exit(1)
}
-
srv := &http.Server{
Handler: h,
Addr: "0.0.0.0:8080",
diff --git a/proxy/rpc.go b/proxy/rpc.go
@@ -1,3 +1,89 @@
package proxy
+import (
+ "bytes"
+ "encoding/json"
+ "io"
+ "log"
+ "net/http"
+ "net/url"
+ "github.com/ethereum/go-ethereum/rpc"
+)
+
+type jsonRpcMsg struct {
+ Jsonrpc string
+ Id string
+ Method string
+ Params []any
+
+}
+type ProxyServer struct {
+ *rpc.Server
+ uri *url.URL
+}
+
+func NewProxyServer(svc *ProxyService, remoteURI string) (*ProxyServer, error) {
+ uri, err := url.Parse(remoteURI)
+ if err != nil {
+ return nil, err
+ }
+ srv := &ProxyServer{
+ Server: rpc.NewServer(),
+ uri: uri,
+ }
+ err = srv.Server.RegisterName("eth", svc)
+ if err != nil {
+ return nil, err
+ }
+ return srv, nil
+}
+
+func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ var rr io.Reader
+ msg := jsonRpcMsg{}
+ b := make([]byte, r.ContentLength)
+ _, err := io.ReadFull(r.Body, b)
+ if (err != nil) {
+ log.Printf("%s", err)
+ r.Body.Close()
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ r.Body.Close()
+ err = json.Unmarshal(b, &msg)
+ if (err != nil) {
+ log.Printf("%s", err)
+ r.Body.Close()
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+
+ rr = bytes.NewReader(b)
+ r.Body = io.NopCloser(rr)
+
+ for _, k := range([]string{
+ "eth_getTransactionFromHash",
+ }) {
+ if msg.Method == k {
+ log.Printf("match %s", msg.Method)
+ s.Server.ServeHTTP(w, r)
+ return
+ }
+ }
+ client_req := r.Clone(r.Context())
+ client_req.RequestURI = ""
+ client_req.Method = "POST"
+ client_req.URL = s.uri
+ client := &http.Client{}
+ res, err := client.Do(client_req)
+ if err != nil {
+ log.Printf("%s", err)
+ r.Body.Close()
+ w.WriteHeader(http.StatusBadGateway)
+ return
+ }
+ w.WriteHeader(res.StatusCode)
+ rr = io.TeeReader(r.Body, w)
+ io.ReadAll(rr)
+}
diff --git a/proxy/service.go b/proxy/service.go
@@ -20,10 +20,6 @@ func NewProxyService(store store.Store) (*ProxyService) {
}
}
-func (p *ProxyService) GasPrice() (string, error) {
- return "0x0", nil
-}
-
func (p *ProxyService) GetTransactionByHash(ctx context.Context, hsh string) (*types.Transaction, error) {
log.Printf("get tx hash %s", hsh)
b := common.FromHex(hsh)