Skip to content

Commit

Permalink
client - Add functionality to specify cors for rpc server (#1762)
Browse files Browse the repository at this point in the history
* Add functionality to specify cors for rpc server
  • Loading branch information
g11tech committed Mar 3, 2022
1 parent f96c9ec commit da0c698
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
12 changes: 4 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ const args = yargs(hideBin(process.argv))
describe: 'Additionally log complete RPC calls on log level debug (i.e. --loglevel=debug)',
boolean: true,
})
.option('rpcCors', {
describe: 'Configure the Access-Control-Allow-Origin CORS header for RPC server',
string: true,
default: '*',
})
.option('maxPerRequest', {
describe: 'Max items per block or header request',
number: true,
Expand Down
5 changes: 5 additions & 0 deletions packages/client/bin/startRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type RPCArgs = {
helprpc: boolean
'jwt-secret'?: string
rpcEngineAuth: boolean
rpcCors: string
}

/**
Expand Down Expand Up @@ -104,6 +105,7 @@ export function startRPCServers(client: EthereumClient, args: RPCArgs) {
rpcEnginePort,
'jwt-secret': jwtSecretPath,
rpcEngineAuth,
rpcCors,
} = args
const manager = new RPCManager(client, config)
const jwtSecret = parseJwtSecret(config, jwtSecretPath)
Expand All @@ -121,6 +123,7 @@ export function startRPCServers(client: EthereumClient, args: RPCArgs) {

if (rpc) {
rpcHttpServer = createRPCServerListener({
rpcCors,
server,
withEngineMiddleware:
withEngineMethods && rpcEngineAuth
Expand All @@ -140,6 +143,7 @@ export function startRPCServers(client: EthereumClient, args: RPCArgs) {
}
if (ws) {
const opts: any = {
rpcCors,
server,
withEngineMiddleware: withEngineMethods && rpcEngineAuth ? { jwtSecret } : undefined,
}
Expand All @@ -166,6 +170,7 @@ export function startRPCServers(client: EthereumClient, args: RPCArgs) {
server.on('response', onBatchResponse)

createRPCServerListener({
rpcCors,
server,
withEngineMiddleware: rpcEngineAuth
? {
Expand Down
19 changes: 16 additions & 3 deletions packages/client/lib/util/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { Server as RPCServer, HttpServer } from 'jayson/promise'
import { json as jsonParser } from 'body-parser'
import { decode, TAlgorithm } from 'jwt-simple'
import Connect, { IncomingMessage } from 'connect'
import cors from 'cors'

const algorithm: TAlgorithm = 'HS256'

type CreateRPCServerListenerOpts = {
rpcCors?: string
server: RPCServer
withEngineMiddleware?: WithEngineMiddleware
}
Expand All @@ -24,8 +26,10 @@ function checkHeaderAuth(req: any, jwtSecret: Buffer): void {
}

export function createRPCServerListener(opts: CreateRPCServerListenerOpts): HttpServer {
const { server, withEngineMiddleware } = opts
const { server, withEngineMiddleware, rpcCors } = opts

const app = Connect()
if (rpcCors) app.use(cors({ origin: rpcCors }))
app.use(jsonParser())

if (withEngineMiddleware) {
Expand Down Expand Up @@ -54,9 +58,18 @@ export function createRPCServerListener(opts: CreateRPCServerListenerOpts): Http
export function createWsRPCServerListener(
opts: CreateRPCServerListenerOpts & { httpServer?: HttpServer }
): HttpServer | undefined {
const { server, withEngineMiddleware } = opts
const { server, withEngineMiddleware, rpcCors } = opts

// Get the server to hookup upgrade request on
const httpServer = opts.httpServer ?? createServer()
let httpServer = opts.httpServer
if (!httpServer) {
const app = Connect()
// In case browser pre-flights the upgrade request with an options request
// more likely in case of wss connection
if (rpcCors) app.use(cors({ origin: rpcCors }))
httpServer = createServer(app)
}

const wss = server.websocket({ noServer: true })

httpServer.on('upgrade', (req, socket, head) => {
Expand Down
1 change: 1 addition & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"body-parser": "^1.19.2",
"chalk": "^4.1.2",
"connect": "^3.7.0",
"cors": "^2.8.5",
"debug": "^4.3.3",
"ethereumjs-util": "^7.1.4",
"fs-extra": "^10.0.0",
Expand Down

0 comments on commit da0c698

Please sign in to comment.