Skip to content

Instantly share code, notes, and snippets.

@sh4dowb
Last active March 1, 2025 15:38
Show Gist options
  • Save sh4dowb/74e6c3a3114ea9e5f0cdd147277e2660 to your computer and use it in GitHub Desktop.
Save sh4dowb/74e6c3a3114ea9e5f0cdd147277e2660 to your computer and use it in GitHub Desktop.
ethereum rpc proxy, to be used as "cheap light node"
import http.server
import requests
import json
class Proxy(http.server.BaseHTTPRequestHandler):
def do_POST(self):
data = self.rfile.read(int(self.headers['Content-Length'])).decode('utf-8')
decodedData = json.loads(data)
newheaders = {}
for k, v in self.headers.items():
if k.lower() in ['host', 'content-length', 'content-encoding']:
continue
newheaders[k] = v
if decodedData['method'] in ['eth_getBalance','eth_gasPrice','eth_call','eth_getBlockByNumber','eth_getTransactionReceipt','eth_blockNumber','eth_getTransactionByHash']:
r = requests.post('https://cloudflare-eth.com/', data=data, headers=newheaders)
elif decodedData['method'] in ['personal_sendTransaction']:
data = json.dumps({"jsonrpc":"2.0","id":2,"method":"eth_getTransactionCount","params":[decodedData['params'][0]['from'], "pending"]})
print(data)
r = requests.post('https://cloudflare-eth.com/', data=data, headers=newheaders)
print(r.json())
nonce = r.json()['result']
data = json.dumps({"jsonrpc":"2.0","id":2,"method":"eth_gasPrice","params":[]})
print(data)
r = requests.post('https://cloudflare-eth.com/', data=data, headers=newheaders)
gasprice = r.json()['result']
print(r.json())
params1 = {"from":decodedData['params'][0]['from'],"to":decodedData['params'][0]['to'],"value":decodedData['params'][0]['value'],"gas":decodedData['params'][0]['gas'],"gasPrice":gasprice,"nonce":nonce}
if "data" in decodedData['params'][0]:
params1["data"] = decodedData['params'][0]['data']
data = json.dumps({"jsonrpc":"2.0","id":2,"method":"personal_signTransaction","params":[params1,decodedData['params'][1]]})
print(params1)
r = requests.post('http://127.0.0.1:8545', data=data, headers=newheaders)
print(r.json())
data = json.dumps({"jsonrpc":"2.0","id":decodedData['id'],"method":"eth_sendRawTransaction","params":[r.json()['result']['raw']]})
print(data)
r = requests.post('https://cloudflare-eth.com/', data=data, headers=newheaders)
print(r.json())
else:
r = requests.post('http://127.0.0.1:8545' + self.path, data=data, headers=newheaders)
self.send_response(r.status_code)
print("FINALRESP", r.headers, r.content)
self.send_header("Content-Type", "application/json")
self.send_header('Content-Length', len(r.content))
self.end_headers()
self.wfile.write(r.content)
httpd = http.server.HTTPServer(('127.0.0.1', 8546), Proxy)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment