forked from archerdao/miner-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
83 lines (71 loc) · 1.98 KB
/
Copy pathmain.js
File metadata and controls
83 lines (71 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// A simple server that proxies only specific methods to an Ethereum JSON-RPC
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const _ = require('lodash')
const ALLOWED_METHODS = ['eth_sendBundle']
function validPort(port) {
if (isNaN(port) || port < 0 || port > 65535) {
return false
}
return true
}
const PUBLIC_PORT = parseInt(process.env.PUBLIC_PORT || '18545')
const GETH_PORT = parseInt(process.env.GETH_PORT || '8545')
const GETH_URL = process.env.GETH_URL || 'http://localhost'
const API_KEYS = []
if (process.env.API_KEYS) {
API_KEYS = _.split(process.env.API_KEYS, ',')
}
if (!validPort(PUBLIC_PORT)) {
console.error(`invalid port specified for PUBLIC_PORT: ${PUBLIC_PORT}`)
process.exit(1)
}
if (!validPort(GETH_PORT)) {
console.error(`invalid port specified for GETH_PORT: ${GETH_PORT}`)
process.exit(1)
}
const app = express()
app.use(bodyParser.json())
app.use(function (req, res) {
if (!req.body) {
res.writeHead(400)
res.end('invalid json body')
return
}
if (!req.body.method) {
res.writeHead(400)
res.end('missing method')
return
}
if (!_.includes(ALLOWED_METHODS, req.body.method)) {
res.writeHead(400)
res.end(`invalid method, only ${ALLOWED_METHODS} supported, you provided: ${req.body.method}`)
return
}
const apiKey = req.header('API-Key')
if (!apiKey) {
res.writeHead(400)
res.end('missing API-Key header')
return
}
if (!_.includes(API_KEYS, apiKey)) {
res.writeHead(400)
res.end(`invalid API-Key header`)
return
}
request
.post({
url: `${GETH_URL}:${GETH_PORT}`,
body: JSON.stringify(req.body),
headers: { 'Content-Type': 'application/json' }
})
.on('error', function (e) {
res.writeHead(500)
res.end(`error in proxy: ${e}`)
})
.pipe(res)
})
app.listen(PUBLIC_PORT, () => {
console.log(`proxy listening at ${PUBLIC_PORT} and forwarding to ${GETH_URL}:${GETH_PORT}`)
})