-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: RPC API server listens on loopback by default (#142)
* feat: authenticate RPC server * fix: rpc api only listens on loopback by default * chore: fix rpc server host warning in readme
- Loading branch information
Showing
3 changed files
with
84 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { createServer } from 'node:http' | ||
import { writeHeapSnapshot } from 'node:v8' | ||
|
||
export interface RpcServerOptions { | ||
apiPort?: number | ||
apiHost?: string | ||
} | ||
|
||
export async function createRpcServer ({ apiPort, apiHost }: RpcServerOptions): Promise<void> { | ||
if (apiHost !== '127.0.0.1') { | ||
// eslint-disable-next-line no-console | ||
console.info('Warning: The RPC API host has been changed from 127.0.0.1. The RPC server is now running in insecure mode. This may expose critical control to external sources. Ensure that you implement proper authentication and authorization measures.') | ||
} | ||
|
||
const apiServer = createServer((req, res) => { | ||
if (req.method === 'GET') { | ||
if (req.url === '/api/v0/nodejs/gc') { | ||
if (globalThis.gc == null) { | ||
// maybe we're running in a non-v8 engine or `--expose-gc` wasn't passed | ||
res.writeHead(503, { 'Content-Type': 'text/plain' }) | ||
res.end('Service Unavailable') | ||
return | ||
} | ||
// force nodejs to run garbage collection | ||
globalThis.gc?.() | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
res.end('OK') | ||
} else if (req.url === '/api/v0/nodejs/heapdump') { | ||
// force nodejs to generate a heapdump | ||
// you can analyze the heapdump with https://github.com/facebook/memlab#heap-analysis-and-investigation to get some really useful insights | ||
const filename = writeHeapSnapshot(`./snapshot-dir/${(new Date()).toISOString()}.heapsnapshot`) | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
res.end(`OK ${filename}`) | ||
} | ||
} else { | ||
res.writeHead(404, { 'Content-Type': 'text/plain' }) | ||
res.end('Not Found') | ||
} | ||
}) | ||
|
||
await new Promise<void>((resolve) => apiServer.listen(apiPort, apiHost, resolve)) | ||
|
||
// eslint-disable-next-line no-console | ||
console.info(`RPC api listening on: ${apiHost}:${apiPort}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters